This needs to be in Java: Bank Class- addAccount() Bank Class data members: -->p
ID: 3726721 • Letter: T
Question
This needs to be in Java:
Bank Class- addAccount()
Bank Class data members:
-->private BankAccount[ ] accounts;
-->private int numAccounts = 0;
--> private int sizeBank =100;
Method addAccount( ):
IF numAccount >= sizeBank
PUT error message- no room
RETURN
PUT message to user to enter type of account - c or s (where c = chequing and s = savings)
GET type
IF accType == 's'
account[numAccounts] = new SavingsAccount( );
ELSE accounts[numAccounts] = new ChequingAccount( );
IF(accounts[numAcccounts].addBankAccount())
//account added successfully
numAccounts++
ELSE PUT account not added
Class Bank . This class will contain the array of BankAccount objects (which are instantiated with either SavingAccount or ChequingAccount objects); You will need to keep two ints as well -a maxSize and numAccounts Methods o constructor )-allocates default size of 1000 o constructor (int) parameter is size of array to be allocated o addAccount:boolean success add or not; prompts user to enter data for an account which is added to arrayeither chequing or savings account is added if there is room displayAccount() - String prompts user to enter an account number to display, then returns data formatted to display or an error message should use toString() from BankAccount class This o printAccountDetails()- prints details of all accounts o updateAccount) prompts user to enter which account number to update, and by how much and then updates the balance appropriately returns success message or error message o findAccount) intprompts user to enter which account number they wish to find and re turns array index of where it is found otherwise returns -1 monthlyUpdate() process through each current account in the array and updates the balance appropriatelyExplanation / Answer
import java.util.Arrays; import java.util.Scanner; public class BankDemo { public static void menu() { System.out.println(); System.out.println("1->AddAccount"); System.out.println("2->DisplayAccount"); System.out.println("3->PrintAccountDetails"); System.out.println("4->UpdateAccount"); System.out.println("5->FindAccount"); System.out.println("6->MonthlyUpdate"); System.out.println("0->break"); System.out.printf("Enter your option : "); } public static void main(String[] args) { Bank bank = new Bank(); int option = 0; Scanner scanner = new Scanner(System.in); do { menu(); option = scanner.nextInt(); if (option == 1) { bank.addAccount(); } if (option == 2) { bank.displayAccounts(); } if (option == 3) { bank.printAccountDetails(); } if (option == 4) { bank.updateAccount(); } if (option == 5) { int account = bank.findAccount(); System.out.println("Fount at : " + account); } if (option == 6) { bank.monthlyUpdate(); } if (option == 0) { break; } } while (true); } } class Bank { private BankAccount[] accounts; private int numAccounts = 0; private int sizeBank = 100; private Scanner scanner = new Scanner(System.in); public Bank() { this.accounts = new BankAccount[100]; this.sizeBank = 100; } public Bank(int sizeBank) { this.sizeBank = sizeBank; this.accounts = new BankAccount[this.sizeBank]; } public void addAccount() { if (numAccounts >= sizeBank) { System.out.println("No room"); return; } System.out.printf("Enter bank account type : "); char type = scanner.next().charAt(0); if (type == 'S' || type == 's' || type == 'C' || type == 'c') { System.out.printf("Id : "); String id = scanner.next(); System.out.printf("Balance : "); double balance = scanner.nextDouble(); if (type == 'S' || type == 's') { this.accounts[numAccounts++] = new SavingAccount(id, balance); } else { this.accounts[numAccounts++] = new ChequingAccount(id, balance); } System.out.println("Account Successfully added"); } System.out.println("Account not added"); } public void displayAccounts() { System.out.printf("Enter account id: "); String id = scanner.next(); BankAccount findAccount = null; for (BankAccount account : accounts) { if (account != null && account.getId().equalsIgnoreCase(id)) { findAccount = account; } } if (findAccount != null) { System.out.println(findAccount); } else { System.out.println("Not found"); } } public void printAccountDetails() { System.out.println("Account Details : "); Arrays.stream(this.accounts).forEach(x -> { if (x != null) { System.out.println(x); } }); } public void updateAccount() { System.out.printf("Enter account id: "); String id = scanner.next(); BankAccount findAccount = null; for (BankAccount account : accounts) { if (account != null && account.getId().equalsIgnoreCase(id)) { findAccount = account; } } if (findAccount != null) { System.out.printf("Enter balance to update : "); double money = scanner.nextDouble(); findAccount.updateBalance(money); System.out.println(findAccount); } else { System.out.println("Not found"); } } public int findAccount() { System.out.printf("Enter account id: "); String id = scanner.next(); int count = 0; for (BankAccount account : accounts) { if (account != null && account.getId().equalsIgnoreCase(id)) { return count; } count++; } return -1; } public void monthlyUpdate() { System.out.printf("Enter monthly update"); double bal = scanner.nextDouble(); for (BankAccount account : accounts) { account.updateBalance(bal); } } } abstract class BankAccount { private String id; private double balance; public BankAccount(String id, double balance) { this.id = id; this.balance = balance; } @Override public String toString() { return "id = " + id + " , balance : " + balance; } public String getId() { return id; } public void updateBalance(double balance) { this.balance = this.balance + balance; } } class SavingAccount extends BankAccount { public SavingAccount(String id, double balance) { super(id, balance); } @Override public String toString() { return "Savings account " + super.toString(); } } class ChequingAccount extends BankAccount { public ChequingAccount(String id, double balance) { super(id, balance); } @Override public String toString() { return "Chequing Account " + super.toString(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.