Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // CW
- import java.util.Scanner;
- public class BankManagment {
- public static void main(String[] args)
- {
- final int MAX_SIZE = 10;
- Scanner in = new Scanner(System.in);
- BankAccount[] accounts = new BankAccount[MAX_SIZE];
- String function;
- String firstName;
- String secondName;
- double amount;
- int nextIni = 0;
- int i = 0, j = 0;
- System.out.printf("What would you like to do? ");
- function = in.next();
- while(!(function.equals("end")))
- {
- switch(function)
- {
- case "new":
- firstName = in.next();
- amount = in.nextDouble();
- if(nextIni < MAX_SIZE)
- {
- accounts[nextIni] = new BankAccount(firstName, amount);
- nextIni++;
- System.out.printf("Success\n");
- }
- else
- System.out.printf("Invalid operation!\n");
- break;
- case "deposit":
- firstName = in.next();
- amount = in.nextDouble();
- for(i = 0; i < MAX_SIZE; i++)
- if(accounts[i].getName().equals(firstName))
- {
- accounts[i].deposit(amount);
- System.out.printf("Success\n");
- break;
- }
- if(i == MAX_SIZE)
- System.out.printf("Invalid operation!\n");
- break;
- case "withdraw":
- firstName = in.next();
- amount = in.nextDouble();
- for(i = 0; i < MAX_SIZE; i++)
- {
- if(accounts[i].getName().equals(firstName))
- {
- if(accounts[i].getBalance() >= amount)
- {
- accounts[i].withdraw(amount);
- System.out.printf("Success\n");
- break;
- }
- else
- System.out.printf("Invalid operation!\n");
- }
- }
- if(i == MAX_SIZE)
- System.out.printf("Invalid operation!\n");
- break;
- case "trans":
- firstName = in.next();
- secondName = in.next();
- amount = in.nextDouble();
- for(i = 0; i < MAX_SIZE; i++)
- {
- if(accounts[i].getName().equals(firstName))
- {
- if(accounts[i].getBalance() < amount)
- {
- System.out.printf("Invalid operation!\n");
- break;
- }
- else
- {
- for(j = 0; j < MAX_SIZE; j++)
- {
- if(i == j)
- continue;
- else
- if(accounts[j].getName().equals(secondName))
- {
- accounts[i].transfer(accounts[j], amount);
- break;
- }
- }
- }
- break;
- }
- }
- if((i == MAX_SIZE) || (j == MAX_SIZE))
- System.out.printf("Invalid operation!\n");
- break;
- default:
- System.out.printf("Invalid operatoin!\n");
- System.out.printf("What would you like to do next? ");
- function = in.next();
- }
- }
- System.out.printf("-----");
- for(i = 0; i <= nextIni; i++)
- System.out.printf("\nName: %s\nBalance: %f\n-----", accounts[i].getName(), accounts[i].getBalance());
- in.close();
- }
- }
- // Next class
- public class BankAccount
- {
- private double balance;
- private String name;
- public BankAccount(String name, double balance)
- {
- this.name = name;
- this.balance = balance;
- }
- public void deposit(double amount)
- {
- balance = balance + amount;
- }
- public void withdraw(double amount)
- {
- balance = balance - amount;
- }
- public void transfer(BankAccount otherAccount, double amount)
- {
- withdraw(amount);
- otherAccount.deposit(amount);
- }
- public double getBalance()
- {
- return balance;
- }
- public String getName()
- {
- return name;
- }
- }
Add Comment
Please, Sign In to add comment