Advertisement
anivasileva

Main

Feb 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import jdk.internal.org.objectweb.asm.tree.analysis.Value;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Created by avi on 21.2.2017 г..
  8.  */
  9. public class Main {
  10.     public static void main(String[] args) {
  11.         BankAccount acc = new BankAccount();
  12.  
  13.         HashMap<Integer, BankAccount> accounts = new HashMap<>();
  14.         Scanner scanner = new Scanner(System.in);
  15.         String command = scanner.nextLine();
  16.         while (!command.equals("End")) {
  17.             String[] cmdArgs = command.split("\\s+");
  18.             String cmdType = cmdArgs[0];
  19.             switch (cmdType) {
  20.                 case "Create":
  21.                     execCreate(cmdArgs, accounts);
  22.                     break;
  23.                 case "Deposit":
  24.                     execDeposit(cmdArgs, accounts);
  25.                     break;
  26.                 case "Withdraw":
  27.                     execWithdraw(cmdArgs, accounts);
  28.                     break;
  29.                 case "Prirnt":
  30.                     execPrint(cmdArgs, accounts);
  31.                     break;
  32.             }
  33.  
  34.  
  35.             command = scanner.nextLine();
  36.         }
  37.  
  38.     }
  39.  
  40.     private static void execPrint(String[] cmdArgs, HashMap<Integer, BankAccount> accounts) {
  41.         int id = Integer.valueOf(cmdArgs[1]);
  42.         if (!accounts.containsKey(id)) {
  43.             System.out.println("Account does not exist");
  44.         } else {
  45.             System.out.printf("Account %s, balance %.2f\n", accounts.get(id), accounts.get(id).getBalance());
  46.         }
  47.     }
  48.  
  49.     private static void execWithdraw(String[] cmdArgs, HashMap<Integer, BankAccount> accounts) {
  50.         int id = Integer.valueOf(cmdArgs[1]);
  51.         double amount = Double.valueOf(cmdArgs[2]);
  52.         if (!accounts.containsKey(id)) {
  53.             System.out.println("Account does not exist");
  54.         } else {
  55.             if (amount > accounts.get(id).getBalance()) {
  56.                 System.out.println("Insufficient balance");
  57.             } else {
  58.                 BankAccount account = accounts.get(id);
  59.                 account.withdraw(amount);
  60.             }
  61.         }
  62.     }
  63.  
  64.     private static void execDeposit(String[] cmdArgs, HashMap<Integer, BankAccount> accounts) {
  65.         int id = Integer.valueOf(cmdArgs[1]);
  66.         double amount = Double.valueOf(cmdArgs[2]);
  67.         if (!accounts.containsKey(id)) {
  68.             System.out.println("Account does not exist");
  69.         } else {
  70.             BankAccount account = accounts.get(id);
  71.             account.deposit(amount);
  72.         }
  73.  
  74.     }
  75.  
  76.     private static void execCreate(String[] cmdArgs, HashMap<Integer, BankAccount> accounts) {
  77.         int id = Integer.valueOf(cmdArgs[1]);
  78.         if (accounts.containsKey(id)) {
  79.             System.out.println("Account already exists");
  80.         }
  81.         else {
  82.             BankAccount account = new BankAccount();
  83.             account.setId(id);
  84.             accounts.put(id, account);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement