Advertisement
Tsuki11

Untitled

Jun 15th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class BankAccount {
  2.  
  3. private static int idCounter = 1;
  4. private static double interestRate = 0.02;
  5.  
  6. private int id;
  7. private double balance;
  8.  
  9.  
  10. public BankAccount() {
  11. this.id = BankAccount.idCounter;
  12. BankAccount.idCounter++;
  13. System.out.println("Account ID" + this.id + " created");
  14. }
  15.  
  16. public int getId() {
  17. return this.id;
  18. }
  19.  
  20. public void setBalance(double depositToBalance) {
  21. this.balance += depositToBalance;
  22. }
  23.  
  24. public static void setInterestRate(double interest) {
  25. BankAccount.interestRate = interest;
  26. }
  27.  
  28.  
  29. public double getInterest(int years) {
  30. return BankAccount.interestRate * years * this.balance;
  31. }
  32.  
  33. public void deposit(double amount) {
  34.  
  35. }
  36.  
  37. }
  38. package L3_BankAccount;
  39.  
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. import java.util.Scanner;
  43.  
  44. public class Main {
  45. public static void main(String[] args) {
  46. Scanner scan = new Scanner(System.in);
  47.  
  48. Map<Integer, BankAccount> bankAccounts = new HashMap<>();
  49.  
  50. String input = "";
  51. while (!(input = scan.nextLine()).equals("End")) {
  52.  
  53. String[] tokens = input.split("\\s+");
  54. String command = tokens[0];
  55. int id;
  56. switch (command) {
  57. case "Create":
  58. BankAccount bankAccount = new BankAccount();
  59. bankAccounts.put(bankAccount.getId(), bankAccount);
  60. break;
  61. case "Deposit":
  62. id = Integer.parseInt(tokens[1]);
  63. if (hasAccount(bankAccounts, id)) break;
  64.  
  65. double depositToBalance = Double.parseDouble(tokens[2]);
  66. BankAccount account = bankAccounts.get(id);
  67. account.setBalance(depositToBalance);
  68. System.out.printf("Deposited %.0f to ID%s%n", depositToBalance, account.getId());
  69. break;
  70. case "SetInterest":
  71. double interest = Double.parseDouble(tokens[1]);
  72. BankAccount.setInterestRate(interest);
  73. break;
  74. case "GetInterest":
  75. id = Integer.parseInt(tokens[1]);
  76. if (hasAccount(bankAccounts, id)) break;
  77. BankAccount account1 = bankAccounts.get(id);
  78. int period = Integer.parseInt(tokens[2]);
  79. System.out.printf("%.2f%n",
  80. account1.getInterest(period));
  81. }
  82.  
  83. }
  84. }
  85.  
  86. private static boolean hasAccount(Map<Integer, BankAccount> bankAccounts, int id) {
  87. if (!bankAccounts.containsKey(id)) {
  88. System.out.println("Account does not exist");
  89. return true;
  90. }
  91. return false;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement