Advertisement
NB52053

LAB4

Jul 23rd, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.10 KB | None | 0 0
  1.                                                                                                                
  2. import java.util.Scanner;
  3.  
  4. public class Bank {
  5.  
  6.     public static void main(String[] args) {
  7.         BankAccount account  = null;
  8.         Scanner scan = new Scanner(System.in);
  9.  
  10.         // Step 1: Create account
  11.         String msg = "Please Enter \n 1 to create Savings account\n 2 for Current Account \n 3 or Student Account.";
  12.         String type = null;
  13.         boolean isValidtype = false;
  14.  
  15.         do{
  16.             System.out.println(msg);
  17.             type = scan.nextLine();
  18.             isValidtype = type.equals("1") || type.equals("2") || type.equals("3");
  19.             msg = "Not a valid Option. /n Please enter 1 to create Savings account, 2 for Current Account\n 3 or Student Account.";
  20.         }while(!isValidtype);
  21.  
  22.         System.out.println("Enter your name:");
  23.         String name = scan.nextLine();
  24.         System.out.println("Enter Initial Balance:");
  25.         String initBalance = scan.nextLine();
  26.         double balance = Double.parseDouble(initBalance);
  27.  
  28.  
  29.         switch(type){
  30.             case "1":
  31.                 while(balance < 2000){
  32.                     System.out.println("Enter your balance (minimum 2000 tk):");
  33.                     initBalance = scan.nextLine();
  34.                     balance = Double.parseDouble(initBalance);
  35.                 }
  36.  
  37.                 System.out.println("Enter your maximum withdraw limit:");
  38.                 double maxWithLimit = Double.parseDouble(scan.nextLine());
  39.                 account = new SavingAccount(name, balance, maxWithLimit);
  40.                 break;
  41.  
  42.             case "2":
  43.                 while(balance < 5000){
  44.                     System.out.println("Enter your balance (minimum 5000 tk):");
  45.                     initBalance = scan.nextLine();
  46.                     balance = Double.parseDouble(initBalance);
  47.                 }
  48.                 System.out.println("Enter your trade License Number:");
  49.                 String trLncNum = scan.nextLine();
  50.                 account = new CurrentAccount(name, balance, trLncNum);
  51.                 break;
  52.  
  53.             case "3":
  54.                 while(balance < 100){
  55.                     System.out.println("Enter your balance (minimum 100 tk):");
  56.                     initBalance = scan.nextLine();
  57.                     balance = Double.parseDouble(initBalance);
  58.                 }
  59.                 System.out.println("Enter your educational institution's name:");
  60.                 String univName = scan.nextLine();
  61.                 account = new StudentAccount(name, balance, univName);
  62.                 break;
  63.         }
  64.  
  65.     // Transaction process here
  66.  
  67.         msg = "Please enter what type of transaction you want to perform: \ndeposit(d), withdraw(w), check balance(c) or exit.";
  68.         System.out.println(msg);
  69.         String selection = scan.nextLine().trim().toLowerCase();
  70.         isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
  71.         boolean Exit = selection.equals("exit");
  72.         Outer:
  73.         while(!Exit){
  74.             while(!isValidtype)
  75.             {
  76.                 String updMsg = "Not a valid Option. \nPlease enter what type of transaction you want to perform: \ndeposit(d), withdraw(w), check balance(c) or exit.";
  77.                 System.out.println(updMsg);
  78.                 selection = scan.nextLine().toLowerCase();
  79.                 isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
  80.                 if (selection.equals("exit"))
  81.                     break Outer;
  82.             }
  83.  
  84.             double amt;
  85.             String temp;
  86.             double initialbalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
  87.             double newBalance;
  88.             switch(selection){
  89.                 case "d":
  90.                     System.out.println("Enter the amount you want to deposit:");
  91.                     temp = scan.nextLine();
  92.                     amt = Double.parseDouble(temp);
  93.                     account.deposit(amt);
  94.                     newBalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
  95.                     if(initialbalance != newBalance)
  96.                         System.out.printf("Balance before deposit:%.2f, after deposit:%.2f\n", initialbalance, newBalance);
  97.                     break;
  98.                 case "w":
  99.                     System.out.println("Enter the amount you want ot withdraw:");
  100.                     temp = scan.nextLine();
  101.                     amt = Double.parseDouble(temp);
  102.                     account.withdraw(amt);
  103.                     newBalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
  104.                     if(initialbalance != newBalance)
  105.                         System.out.printf("Balance before withdraw:%.2f, after withdraw:%.2f\n", initialbalance, newBalance);
  106.                     break;
  107.                 case "c":
  108.                     if (type.equals("2"))
  109.                         System.out.println("Your balance:" + account.getBalance());
  110.                     else
  111.                         System.out.printf("Your Balance:%.2f and Balance with interest:%.2f\n",((SavingAccount)account).getOriginalBalance(), account.getBalance());
  112.                     break;
  113.             }
  114.             System.out.println(msg);
  115.             selection = scan.nextLine().toLowerCase();
  116.             isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
  117.             Exit = selection.equals("exit");
  118.         }
  119.     }
  120. }
  121.  
  122. // BankAccount
  123.  
  124. import java.util.Random;
  125.  
  126. public class BankAccount {
  127.     private String memberName, accountNumber;
  128.     private double accountBalance, minimumBalance;
  129.  
  130.     public BankAccount(String n, double b, double minB){
  131.         memberName = n;
  132.         accountBalance = b;
  133.         minimumBalance = minB;
  134.         Random rand = new Random();
  135.  
  136.         accountNumber ="" + rand.nextInt(10) + rand.nextInt(10)+ rand.nextInt(10)+ rand.nextInt(10)+ rand.nextInt(10);
  137.     }
  138.  
  139.     public void deposit(double amount)  {
  140.         accountBalance += amount;
  141.     }
  142.  
  143.     public void withdraw(double amount){
  144.         if(accountBalance-amount>minimumBalance)
  145.             accountBalance -= amount;
  146.         else
  147.             System.out.println("You do not have enough to withdraw");
  148.  
  149.     }
  150.  
  151.     public double getBalance() {
  152.         return accountBalance;
  153.     }
  154.  
  155.     public void display()
  156.     {
  157.         System.out.printf("Name:%s, Account Number:%s, Balance:%.2f\n", memberName, accountNumber, accountBalance);
  158.     }
  159.  
  160. }
  161.  
  162. //StudentAccount
  163.  
  164. public class StudentAccount extends SavingAccount {
  165.     private String uniName;
  166.  
  167.     public StudentAccount(String name, double balance, String university) {
  168.         super(name, balance, 100, 20000);
  169.         uniName = university;
  170.     }
  171. }
  172.  
  173. //CurrentAccount
  174. public class CurrentAccount extends BankAccount{
  175.     private String tradeLicenseNumber;
  176.  
  177.     public CurrentAccount(String name, double balance, String tradeLicense) {
  178.         super(name, balance, 5000);
  179.         tradeLicenseNumber = tradeLicense;
  180.     }
  181.  
  182.     public void getLicenseNumber() {
  183.         System.out.println("Trade License Number: " + tradeLicenseNumber);
  184.     }
  185. }
  186.  
  187. //SavingAccount
  188. public class SavingAccount extends BankAccount {
  189.     private int interest;
  190.     private double withdrawLimit;
  191.  
  192.     public SavingAccount(String name, double balance, double wl) {
  193.         super(name, balance, 2000);
  194.         interest = 5;
  195.         withdrawLimit = wl;
  196.     }
  197.  
  198.     public SavingAccount(String name, double balance, double minBalance, double wl) {
  199.         super(name, balance, minBalance);
  200.         interest = 5;
  201.         withdrawLimit = wl;
  202.     }
  203.  
  204.  
  205.     public double getBalance() {
  206.         return super.getBalance() * ( (100.00 + interest) / 100 );
  207.     }
  208.  
  209.  
  210.     public void withdraw(double amount) {
  211.         if (amount < withdrawLimit) {
  212.             super.withdraw(amount);
  213.         }
  214.         else {
  215.             System.out.println("THe amount that you want to withdraw exceeds your 'Withdraw Limit'");
  216.         }
  217.     }
  218.  
  219.     public double getOriginalBalance() {
  220.         return super.getBalance();
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement