SIRAKOV4444

Untitled

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