Order Now  +1 678 648 4277 

Client Server Assignment Solution Using Java


The program will allow up to 5 clients and each client will be able to have 2 accounts. The program will start showing the following options:

- Add a client

If the number of clients is already 5, the program will say that it is not possible to add an extra client. If the number of clients is less than 5, then the program will ask the following inputs:

  • Client name (if a client with that name already exists, the program prints an error message)
  • Client age (need to be a positive number. If not, the program will ask the input again)
  • Client income per year (need to be a positive number. If not, the program will ask the input again)

-Delete a client

The program will ask the name of the client to be deleted. If the name does not exist, show a message. If the name exists, then the client should be deleted.

- Show a client

The program will ask the name of the client. If the name does not exist, show a message. If the name exists, then the program will display the name, age, income, and the number of accounts of this client.

- Add an account

The program will ask the name of the client. If the name does not exist, show a message. If the name exists, then the program will check if the number of accounts that the client has is 2. If it is 2, the program will show a message. If it is less than 2, then the program will ask

  • Amount of money the client would like to borrow (need to be a positive number. If not, the program will ask the input again)
  • Number of months the client would like to pay (need to be a positive number. If not, the program will ask the input again)
  • type of account

There are two types of accounts the client can choose from:

(1) No fees. If the client chooses this option, the interest rate will be a. 6.5% if n < 50

b.7.5% if 50 <= n < =100

c.8.5% if n > 100 where n is the number of months

(2) Fees. The interest rate will be 6%, but the client needs to pay an additional fee of $10 every month.

- Delete an account

The program will ask the name of the client. If the name does not exist, show a message. If the name exists, then the program will ask which account to delete (1 or 2). If the account does not exist, show a message. If the account exists, the account will be deleted.

- Show account details

The program will ask the name of the client. If the name does not exist, show a message. If the name exists, then the program will ask which account the client would like to see. If the account does not exist, the program will show a message. If it exists, the program will display all the details of the account, which will be:

  • Amount
  • Number of months
  • Type of account
  • Total payment
  • Total interest paid

Amortizations table, which will have the following columns:

  • month
  • initial balance
  • payment
  • interest paid
  • principal paid
  • final balance

- Save in a file

The program will save in a file (txt format) the information of each client (the same information described in “show a client” above) and each account (the same information described in “show account details” above). If there are no clients, the files should have “no client”. For each client, if the client does not have an account, the file should have “no account”.

- Finish the program

Exit the program.

Solution:

Account:

// You can add extra methods if you think it is necessary public class Account { // declare instance variables private double interestRate; private String accountType; private double amount; private int numberOfMonths; public Account(double interestRate, String accountType, double amount, int numberOfMonths) { this.interestRate = interestRate; this.accountType = accountType; this.amount = amount; this.numberOfMonths = numberOfMonths; } // constructor public Account() { this.interestRate = 0; } // get / set methods public double getInterestRate() { return this.interestRate; } public void setNumberOfMonths(int newMonths) { this.numberOfMonths = newMonths; } public int getNumberOfMonths() { return this.numberOfMonths; } public void setAmount(double newAmount) { this.amount = newAmount; } public double getAmount() { return this.amount; } public void setAccountType(String newAccountType) { this.accountType = newAccountType; } public String getAccountType() { return this.accountType; } //Other methods public void setInterestRate() { if (accountType.equalsIgnoreCase("no fees")) { if (numberOfMonths < 50) { interestRate = 0.065; } else if (numberOfMonths > 100) { interestRate = 0.085; } else { interestRate = 0.075; } } else { interestRate = 0.06; } } public double calculateMonthlyPayment() { double a = 1 + (interestRate / 12); return (amount * interestRate * Math.pow(a, numberOfMonths)) / (12 * (Math.pow(a, numberOfMonths) - 1)); } public String getAmortizationTable() { StringBuilder buffer = new StringBuilder(); buffer.append(String.format("%-32s%-32s%-32s%-32s%-32s%-32s%n", "Month", "Initbalance", "MonthPayment", "InterestPaid", "PrincipalPaid", "FinalBalance")); double monthlyPayment = calculateMonthlyPayment(); double initbalance = amount; double finalBalance; for (int i = 1; i <= numberOfMonths; i++) { double interestPaid = (initbalance * interestRate / 12); finalBalance = initbalance - monthlyPayment + interestPaid; buffer.append(String.format("%-32d%-32s%-32s%-32s%-32s%-32s%n", i, "$" + String.format("%.2f", initbalance), "$" + String.format("%.2f", monthlyPayment), "$" + String.format("%.2f", interestPaid), "$" + String.format("%.2f", (monthlyPayment - interestPaid)), "$" + String.format("%.2f", finalBalance))); initbalance = finalBalance; } return buffer.toString(); } @Override public String toString() { return "Amount: " + amount + "\nNumber of months: " + numberOfMonths + "\nType of account: " + accountType + "\nTotal payment: " + String.format("%.2f", (calculateMonthlyPayment() * numberOfMonths)) + "\nTotal Interest paid: " + String.format("%.2f", ((calculateMonthlyPayment() * numberOfMonths) - amount)) + "\n" + getAmortizationTable(); } }

Client:

// You can add extra methods if you think it is necessary public class Client { private String name; private int age; private double income; private Account[] loans; public Client(String name, int age, double income) { this.name = name; this.age = age; this.income = income; this.loans = new Account[2]; } // Constructor public Client() { this.name = null; this.age = 0; this.income = 0; this.loans = new Account[2]; } // Get / Set methods public String getName() { return this.name; } public void setName(String newName) { this.name = newName; } public int getAge() { return this.age; } public void setAge(int newAge) { this.age = newAge; } public double getIncome() { return this.income; } public void setIncome(double newIncome) { this.income = newIncome; } public void addLoan(Account loan) { if(this.loans[0] == null) { this.loans[0] = loan; } else if(this.loans[1] == null) { this.loans[1] = loan; } } public int getNumberOfAccounts() { int num = 0; for (Account account : loans) { if (account != null) { num++; } } return num; } public Account getLoan(int pos) { if (pos >= loans.length) { return null; } return loans[pos]; } public Account[] getLoans() { return loans; } public void setLoan(int pos, Account loan) { if (pos >= loans.length) { return; } loans[pos] = loan; } //Other methods public double getAccountMonthlyPayment(int pos) { return loans[pos].calculateMonthlyPayment(); } @Override public String toString() { return name + ", age " + age + ", income " + income + ", number of accounts: " + getNumberOfAccounts(); } }

Loan Calculator:

import java.io.FileWriter; import java.io.IOException; import java.util.*; public class LoanCalculator { private final Client[] clients = new Client[5]; private final Scanner console = new Scanner(System.in); private int newClientPos() { for (int i = 0; i < clients.length; i++) { if(clients[i] == null) { return i; } } return -1; } private void addClient() { int pos = newClientPos(); if(pos != -1) { Client client = new Client(); String name; System.out.print("Name = "); name = console.nextLine(); client.setName(name); System.out.print("Age = "); int age = Integer.parseInt(console.nextLine()); client.setAge(age); System.out.print("Income per year = "); double income = Double.parseDouble(console.nextLine()); client.setIncome(income); this.clients[pos] = new Client(name, age, income); } else { System.out.println("Cannot add more than 5 clients."); } } private void deleteClient() { String name; System.out.print("Name = "); name = console.nextLine(); for (int i = 0; i < clients.length; i++) { if(clients[i] != null && clients[i].getName().equals(name)) { clients[i] = null; return; } } System.out.println("Client does not exist."); } private void showClient() { String name; System.out.print("Name = "); name = console.nextLine(); for (Client client : clients) { if (client != null && client.getName().equals(name)) { System.out.println(client); return; } } System.out.println("Client does not exist."); } private void addAccount() { String name; System.out.print("Name = "); name = console.nextLine(); for (Client client : clients) { if (client != null && client.getName().equals(name)) { if(client.getNumberOfAccounts() == 2) { System.out.println("Client already has 2 accounts. Cannot add more."); } else { Account loan = new Account(); System.out.print("Amount to borrow = "); double amount = Double.parseDouble(console.nextLine()); loan.setAmount(amount); System.out.print("Months to pay = "); int months = Integer.parseInt(console.nextLine()); loan.setNumberOfMonths(months); System.out.print("Account type = "); String type = console.nextLine(); loan.setAccountType(type); loan.setInterestRate(); client.addLoan(loan); } return; } } System.out.println("Client does not exist."); } private void deleteAccount() { String name; System.out.print("Name = "); name = console.nextLine(); for (Client client : clients) { if (client != null && client.getName().equals(name)) { System.out.print("Which account to delete (1 or 2): "); int num = Integer.parseInt(console.nextLine()); if(client.getLoan(num) == null) { System.out.println("Account does not exist"); } else { client.setLoan(num-1, null); } return; } } System.out.println("Client does not exist."); } private void showAccount() { String name; System.out.print("Name = "); name = console.nextLine(); for (Client client : clients) { if (client != null && client.getName().equals(name)) { System.out.print("Which account to show (1 or 2): "); int num = Integer.parseInt(console.nextLine()); if(client.getLoan(num-1) == null) { System.out.println("Account does not exist"); } else { System.out.println(client.getLoan(num)); } return; } } System.out.println("Client does not exist."); } private void displayMenu() { System.out.println("\nPlease select an option: "); System.out.println("1. Add a client"); System.out.println("2. Delete a client"); System.out.println("3. Show a client"); System.out.println("4. Add an account"); System.out.println("5. Delete an account"); System.out.println("6. Show account details"); System.out.println("7. Save in a file"); System.out.println("8. Finish the program"); System.out.print("Enter the selection: "); } private void save() throws IOException { FileWriter writer = new FileWriter("output.txt"); boolean noClient = true; for (Client client : clients) { if(client != null) { noClient = false; writer.write(client.toString() + "\n"); if(client.getNumberOfAccounts() == 0) { writer.write("no account" + "\n"); } else { for (Account account : client.getLoans()) { if (account != null) { writer.write(account + "\n"); } } } } } if(noClient) { writer.write("no client"); } writer.close(); } public void run() throws IOException { String selection = ""; while(!"8".equals(selection)) { displayMenu(); selection = console.nextLine(); if("1".equals(selection)) { addClient(); } else if ("2".equals(selection)) { deleteClient(); } else if ("3".equals(selection)) { showClient(); } else if ("4".equals(selection)) { addAccount(); } else if ("5".equals(selection)) { deleteAccount(); } else if ("6".equals(selection)) { showAccount(); } else if ("7".equals(selection)) { save(); } else if (!"8".equals(selection)) { System.out.println("Invalid option selected."); } } } public static void main(String[] args) throws IOException { LoanCalculator calc = new LoanCalculator(); calc.run(); } }