NB52053

BANK

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