Advertisement
Guest User

Untitled

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