Shavit

Ex. Bank #32

Mar 1st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class BankManagment {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         final int MAX_SIZE = 10;
  11.        
  12.         Scanner in = new Scanner(System.in);
  13.         BankAccount[] accounts = new BankAccount[MAX_SIZE];
  14.        
  15.         String function;
  16.         String firstName;
  17.         String secondName;
  18.         double amount;
  19.         int nextIni = 0;
  20.         int i = 0, j = 0;
  21.        
  22.         System.out.printf("What would you like to do? ");
  23.         function = in.next();
  24.        
  25.         while(!(function.equals("end")))
  26.         {
  27.             switch(function)
  28.             {
  29.                 case "new":
  30.                     firstName = in.next();
  31.                     amount = in.nextDouble();
  32.                     if(nextIni < MAX_SIZE)
  33.                     {
  34.                         accounts[nextIni] = new BankAccount(firstName, amount);
  35.                         nextIni++;
  36.                         System.out.printf("Success\n");
  37.                     }
  38.                     else
  39.                         System.out.printf("Invalid operation!\n");
  40.                     break;
  41.                 case "deposit":
  42.                     firstName = in.next();
  43.                     amount = in.nextDouble();
  44.                     for(i = 0; i < MAX_SIZE; i++)
  45.                         if(accounts[i].getName().equals(firstName))
  46.                         {
  47.                             accounts[i].deposit(amount);
  48.                             System.out.printf("Success\n");
  49.                             break;
  50.                         }
  51.                     if(i == MAX_SIZE)
  52.                         System.out.printf("Invalid operation!\n");
  53.                     break;
  54.                 case "withdraw":
  55.                     firstName = in.next();
  56.                     amount = in.nextDouble();
  57.                     for(i = 0; i < MAX_SIZE; i++)
  58.                     {
  59.                         if(accounts[i].getName().equals(firstName))
  60.                         {
  61.                             if(accounts[i].getBalance() >= amount)
  62.                             {
  63.                                 accounts[i].withdraw(amount);
  64.                                 System.out.printf("Success\n");
  65.                                 break;
  66.                             }
  67.                             else
  68.                                 System.out.printf("Invalid operation!\n");
  69.                         }
  70.                     }
  71.                     if(i == MAX_SIZE)
  72.                         System.out.printf("Invalid operation!\n");
  73.                     break;
  74.                 case "trans":
  75.                     firstName = in.next();
  76.                     secondName = in.next();
  77.                     amount = in.nextDouble();
  78.                     for(i = 0; i < MAX_SIZE; i++)
  79.                     {
  80.                         if(accounts[i].getName().equals(firstName))
  81.                         {
  82.                             if(accounts[i].getBalance() < amount)
  83.                             {
  84.                                 System.out.printf("Invalid operation!\n");
  85.                                 break;
  86.                             }
  87.                             else
  88.                             {
  89.                                 for(j = 0; j < MAX_SIZE; j++)
  90.                                 {
  91.                                     if(i == j)
  92.                                         continue;
  93.                                     else
  94.                                         if(accounts[j].getName().equals(secondName))
  95.                                         {
  96.                                             accounts[i].transfer(accounts[j], amount);
  97.                                             break;
  98.                                         }
  99.                                 }
  100.                             }
  101.                            
  102.                         break;
  103.                         }
  104.                     }
  105.                    
  106.                     if((i == MAX_SIZE) || (j == MAX_SIZE))
  107.                         System.out.printf("Invalid operation!\n");
  108.                     break;
  109.                 default:
  110.                     System.out.printf("Invalid operatoin!\n");
  111.                    
  112.                 System.out.printf("What would you like to do next? ");
  113.                 function = in.next();
  114.             }
  115.         }
  116.         System.out.printf("-----");
  117.         for(i = 0; i <= nextIni; i++)
  118.             System.out.printf("\nName: %s\nBalance: %f\n-----", accounts[i].getName(), accounts[i].getBalance());
  119.         in.close();
  120.     }
  121. }
  122.  
  123. // Next class
  124.  
  125. public class BankAccount
  126. {
  127.     private double balance;
  128.     private String name;
  129.    
  130.     public BankAccount(String name, double balance)
  131.     {
  132.         this.name = name;
  133.         this.balance = balance;
  134.     }
  135.    
  136.     public void deposit(double amount)
  137.     {
  138.         balance = balance + amount;
  139.     }
  140.    
  141.     public void withdraw(double amount)
  142.     {
  143.         balance = balance - amount;
  144.     }
  145.    
  146.     public void transfer(BankAccount otherAccount, double amount)
  147.     {
  148.         withdraw(amount);
  149.         otherAccount.deposit(amount);
  150.     }
  151.    
  152.     public double getBalance()
  153.     {
  154.         return balance;
  155.     }
  156.    
  157.     public String getName()
  158.     {
  159.         return name;
  160.     }
  161. }
Add Comment
Please, Sign In to add comment