Continuing from Problem 2, we want to model the situation where a Person can hav
ID: 3671436 • Letter: C
Question
Continuing from Problem 2, we want to model the situation where a Person can have many accounts (any combination of RegularAccounts and PremiumAccounts). The Person class should have an arraylist of accounts and have these methods: a. addAccount( a : Account ) – Adds the account a to the Person
b. getAccount( i : int ) : Account – Return the ith account. No error checking required.
c. getNumAccounts() : int – Returns the number of accounts.
d. getTotalBalance() : double – Returns the total balance added over all the accounts the person has.
e. applyInterest() – applies interest to every account the person has.
f. getPremiumAccounts() : ArrayList - Returns all the PremiumAccounts in an ArrayList.
g. toString() : string – Returns a string like this: Num Accounts: 3 bal = 2234.34 bal = 4523.29 bal = 45.62 Total Balance = 6803.25 Do the following: 3
a. Continue your class diagram from Problem 2 by adding the Person class and any association with the Account class. b. Write the Person class as described above. c. Write a tester, PersonTester.java and test your classes thoroughly. Use this Main method: Person p1 = new Person(); p1.addAccount(new RegularAccount(2000)); p1.addAccount(new PremiumAccount(3000)); p1.addAccount(new RegularAccount(200)); p1.addAccount(new RegularAccount(1000)); p1.addAccount(new PremiumAccount(500)); System.out.println("***Call toString() on Person after 5 accounts are added:"); System.out.println(p1); System.out.println(); System.out.println("***Call getNumAccounts(): " + p1.getNumAccounts() + " " ); System.out.println("***Call getAccount(1): " + p1.getAccount(1) + " " ); System.out.println("***Call getTotalBalance(): " + p1.getTotalBalance() + " " ); System.out.println("***Call getPremiumAccounts()"); System.out.println(" Loop over each PremiumAccount and print:"); ArrayList pAccounts = p1.getPremiumAccounts(); for( PremiumAccount pa : pAccounts ) System.out.println(pa); System.out.println(); System.out.println("***Call applyInterest(), then print Person:"); p1.applyInterest(); System.out.println(p1); The above test code should output the following: ***Call toString() on Person after 5 accounts are added: Num Accounts: 5 bal = 2000.0 bal = 3000.0 bal = 200.0 bal = 1000.0 bal = 500.0 Total Balance = 6700.0 ***Call getNumAccounts(): 5 ***Call getAccount(1): Premium Account: The balance is $3000.00 ***Call getTotalBalance(): 6700.0 ***Call getPremiumAccounts() Loop over each PremiumAccount and print: Premium Account: The balance is $3000.00 Premium Account: The balance is $500.00 4 ***Call applyInterest(), then print Person: Num Accounts: 5 bal = 2010.0 bal = 3045.0 bal = 200.0 bal = 1000.0 bal = 507.5 Total Balance = 6762.5
Explanation / Answer
Below is the Account Class:
package prob2;
public class Account {
double balance;
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public void addInterest () {
balance += (balance - 1000)*.01;
}
public double getBalance() {
return balance;
}
}
Below is the Person class:
package prob2;
import java.util.ArrayList;
public class Person {
ArrayList<Account> accounts = new ArrayList<Account>();
int accounts.size();
double sum;
double balance;
public void addAccount(Account a){
accounts.size();
accounts.add(a);
}
public Account getAccount (int i) {
return accounts.get(i);
}
public int getNumAccounts() {
return accounts.size();
}
public double getTotalBalance() {
double sum = 0;
for (Account a: accounts)
sum = sum + a.getBalance();
return sum;
}
public void applyInterest() {
for (Account a: accounts)
a.addInterest();
}
public void removeAccount(int i) {
accounts.remove(i);
accounts.size()--;
}
public void depositFunds(int i, double amount) {
getAccount(i).deposit(amount);
}
public void withdrawFunds(int i, double amount) {
getAccount(i).withdraw(amount);
}
public void clearAccounts() {
accounts.clear();
}
int count=0;
public String[] toString() {
double balance = 0;
String s[]=new String[accounts.size()];
for (Account a: accounts) {
balance = a.getBalance();
String result;
result = ""+balance;
s[count]=result; count++; }
return s;
}
public void showDetails(String[] input) {
System.out.println("Num Accounts: "+accounts.size());
String result=toString():
for(int i=0;i<result.length;i++) System.out.println("Balance :- "+result[i]);
System.out.println(" Total Balance: " + getTotalBalance());
}
}
The output should look something like this:
Num Accounts: 3
bal = 2234.34
bal = 4523.29
bal = 45.62
Total Balance = 6803.25
PersonTester.java program is given below:
package personapp;
import java.util.Scanner;
public class Validator {
int stringLength; // Variable for testing length of input strings
int loopCont2 = 0; // Variable for custEmpChoice Loop
int loopCont3 = 0; // Variable for First Name Loop
int loopCont4 = 0; // Variable for Last Name Loop
int loopCont5 = 0; // Variable for Email Loop
int loopCont6 = 0; // Variable for Customer Number Loop
int loopCont7 = 0; // Variable for Social Security Loop
public void keepGoing(){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
int loopCont = 0;
String choice;
while(loopCont == 0){
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
if (choice.equalsIgnoreCase("y"))
loopCont = 1;
else if (choice.equalsIgnoreCase("n")){
loopCont = 1;
System.exit(0);
}
else
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
}
public void custEmpChoice(){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
String choice;
while(loopCont2 == 0){
System.out.print("Create customer or employee? (c/e): ");
choice = sc.nextLine();
if (choice.equalsIgnoreCase("c"))
loopCont2 = 1;
else if (choice.equalsIgnoreCase("e")){
loopCont2 = 2;
}
else
System.out.println("Error! Entry must be 'c' or 'e'. Try again.");
}
}
public void name(Person name){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
String firstName;
String lastName;
// Loop for obtaining and setting First Name
while(loopCont3 == 0){
System.out.print("Enter first name: ");
firstName = sc.nextLine();
if (firstName.length() > 3){
loopCont3 = 1;
name.setFirstName(firstName);
}
else
System.out.println("Error! Name must be longer "
+ "than 3 characters. Try again.");
}
// Loop for obtaining and setting Last Name
// Loop for obtaining and setting First Name
while(loopCont4 == 0){
System.out.print("Enter last name: ");
lastName = sc.nextLine();
if (lastName.length() > 3){
loopCont4 = 1;
name.setLastName(lastName);
}
else
System.out.println("Error! Name must be longer "
+ "than 3 characters. Try again.");
}
}
public void email(Person name){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
String email;
// Loop for obtaining and setting First Name
while(loopCont5 == 0){
System.out.print("Enter email address: ");
email = sc.nextLine();
if (email.length() > 10){
loopCont5 = 1;
name.setEmail(email);
}
else
System.out.println("Error! Email must be longer "
+ "than 10 characters. Try again.");
}
}
public void customerNumber(Customer name){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
String customerNumber;
// Loop for obtaining and setting First Name
while(loopCont6 == 0){
System.out.print("Customer number: ");
customerNumber = sc.nextLine();
if (customerNumber.length() > 5){
loopCont6 = 1;
name.setCustomerNumber(customerNumber);
}
else
System.out.println("Error! Customer number must be "
+ "longer than 5 characters. Try again.");
}
}
public void socialSecurity(Employee name){
// Create Scanner object for accepting input
Scanner sc = new Scanner(System.in);
String socialSecurity;
// Loop for obtaining and setting First Name
while(loopCont7 == 0){
System.out.print("Social security number: ");
socialSecurity = sc.nextLine();
if (socialSecurity.length() > 8){
loopCont7 = 1;
name.setSocialSecurity(socialSecurity);
}
else
System.out.println("Error! Social security number must be "
+ "longer than 5 characters. Try again.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.