Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Anyone could explain this code step by step? thanks in advance! importjava.util.Scanner; public class BankApplication { Scanner scan; Bank bank; private boolean shouldRunAgain = true;

Anyone could explain this code step by step? thanks in advance!

importjava.util.Scanner;

public class BankApplication {
Scanner scan;
Bank bank;
private boolean shouldRunAgain = true;
public static void main(String[] args) {
BankApplication application = new BankApplication();
application.runApplication();
}
public BankApplication() {
// CONFIG - START
scan = new Scanner(System.in);
// Should be using this according to FAQ
scan.useDelimiter(System.lineSeparator());
// CONFIG - END
bank = new Bank();
}
/**
* Wrapper for running application(menu and what comes after it)
*/
private void runApplication() {
int menuChoice;
int accountNr;
String input;
double amount;
do {
printMenu();
printAction("val");
menuChoice = scan.nextInt();
switch (menuChoice) {
case 1:
printAction("id");
long id = scan.nextLong();
System.out.println(handleFindAccountFromOwner(id));
break;
case 2:
printAction("namn");
input = scan.next();
System.out.println(handleSearchAccountOwnerFromPartOfName(input));
break;
case 3:
printAction("konto");
accountNr = scan.nextInt();
printAction("belopp");
amount = scan.nextDouble();
try {
System.out.println(handleDepositMoney(accountNr, amount));
} catch (IllegalArgumentException e) {
System.out.println("Beloppet mste vara positivt. ");
} catch (NullPointerException e) {
System.out.println("Var snll ange ett konto som finns");
}
break;
case 4:
printAction("frn konto");
accountNr = scan.nextInt();
printAction("belopp");
amount = scan.nextDouble();
try {
System.out.println(handleWithdrawMoney(accountNr, amount));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case 5:
printAction("frn konto");
int fromAccountId = scan.nextInt();
printAction("till konto");
int toAccountId = scan.nextInt();
printAction("belopp");
amount = scan.nextDouble();
try {
System.out.println(handleTransfer(fromAccountId, toAccountId, amount));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case 6:
printAction("namn");
String name = scan.next();
printAction("id");
id = scan.nextLong();
System.out.println(handleCreateAccount(name, id));
break;
case 7:
printAction("konto");
accountNr = scan.nextInt();
handleRemoveAccount(accountNr);
break;
case 8:
System.out.println(handlePrintAllAccounts());
break;
case 9:
handleCloseApplication();
break;
default:
System.out.println("Skriv ett tal mellan 0 till 9");
break;
}
}
// This should run again and again until user closes it.
while (shouldRunAgain);
}
private String handleFindAccountFromOwner(long id) {
StringBuilder returnString = new StringBuilder();
for (BankAccount account : bank.findAccountsForHolder(id)) {
returnString.append(account + " ");
}
return returnString.toString();
}
private String handleSearchAccountOwnerFromPartOfName(String name) {
StringBuilder returnString = new StringBuilder();
for (Customer customer : bank.findByPartofName(name)) {
returnString.append(customer + " ");
}
return returnString.toString();
}
private double handleDepositMoney(int accountNr, double amount) {
// Search for the account and then deposit the money
BankAccount account = bank.findByNumber(accountNr);
account.deposit(amount);
return amount;
}
private BankAccount handleWithdrawMoney(int accountNr, double amount) {
// Search for the account and then deposit the money
BankAccount account = bank.findByNumber(accountNr);
// If the account is null, then we cannot withdraw
if (account == null) {
throw new IllegalArgumentException("Accountnr not valid: " + accountNr);
}
account.withdraw(amount);
return account;
}
private String handleTransfer(int fromAccountId, int toAccountId, double amount) {
StringBuilder returnString = new StringBuilder();
// Here we transfer the money, it is removed from sender and added to
// reciever.
BankAccount fromAccount = bank.findByNumber(fromAccountId);
if (fromAccount == null) {
throw new IllegalArgumentException("Sending account is not valid: " + fromAccountId);
}
BankAccount toAccount = bank.findByNumber(toAccountId);
if (toAccount == null) {
throw new IllegalArgumentException("Recieveing account is not valid: " + toAccountId);
}
fromAccount.withdraw(amount);
toAccount.deposit(amount);
returnString.append(fromAccount.toString() + " ");
returnString.append(toAccount.toString() + " ");
return returnString.toString();
}
private String handleCreateAccount(String name, long id) {
int accountId = bank.addAccount(name, id);
return "konto skapat: " + accountId;
}
private void handleRemoveAccount(int accountNr) {
bank.removeAccount(accountNr);
}
private String handlePrintAllAccounts() {
StringBuilder returnString = new StringBuilder();
for (BankAccount account : bank.getAllAccounts()) {
returnString.append(account.toString() + " ");
}
return returnString.toString();
}
private void handleCloseApplication() {
// When setting this to false, the while loop run runApplication() will
// not run again.
shouldRunAgain = false;
}
private void printMenu() {
System.out.println("------------------ ");
System.out.println("1. Hitta konto utifrn innehavare" + " 2. Sk kontoinnehavare utifrn (del av) namn"
+ " 3. Stt in" + " 4. Ta ut" + " 5. verfring" + " 6. Skapa konto" + " 7. Ta bort konto"
+ " 8. Skriv ut konton" + " 9. Avsluta");
}
/**
* Prints the action that the user should type in. E.g. if name needs to be
* sent in, run printAction("name") The colon will automatically be
* inserted.
*
* @param action
*/
private void printAction(String action) {
System.out.println(action + ": ");
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions