Advertisement
NB52053

LAB_5

Jul 23rd, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import static java.lang.System.exit;
  3.  
  4. public class Bank {
  5.     public static void main(String[] args) {
  6.         String openAccount = JOptionPane.showInputDialog(null, "Do you want to open a Bank Account?:\n(1) YES\n(0) NO");
  7.         int accountOpener = Integer.parseInt(openAccount);
  8.  
  9.         if (accountOpener == 1){
  10.             String typeofAccount = JOptionPane.showInputDialog(null, "Account Type:\n1- Current Account\n2-Savings Account\n3-Student Account");
  11.             int accountType = Integer.parseInt(typeofAccount);
  12.             MethodCalls insert = new MethodCalls();
  13.             insert.insertItem(accountType);
  14.             String options = JOptionPane.showInputDialog(null," 1- Diposit\n 2 - Withdraw\n 3 - Display\n0- Exit");
  15.             int option = Integer.parseInt(options);
  16.  
  17.             if(option==1) {
  18.                 insert.depositAmount(accountType);
  19.             }
  20.             else if(option ==2) {
  21.                insert.withdrawAmount(accountType);
  22.             }
  23.             else if(option==3){
  24.                 insert.checkBalance(accountType);
  25.             }
  26.             else{
  27.                 exit(0);
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33.  
  34. //import javax.swing.*;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37.  
  38. public class MethodCalls {
  39.     List<BankAccount> account = new ArrayList<BankAccount>();
  40.  
  41.     BankAccount currentaccount = new CurrentAccount("",0,0.0,0.0);
  42.     BankAccount savingsaccount = new SavingsAccount("",0.0,0.0);
  43.     BankAccount studentaccount = new StudentAccount("","",0.0,0.0);
  44.  
  45.     void insertItem(int type){
  46.         String accountHolder = JOptionPane.showInputDialog(null, "Name of the Account Holder:");
  47.         String initialBalance = JOptionPane.showInputDialog(null, "Initial Amount:");
  48.         double init = Double.parseDouble(initialBalance);
  49.  
  50.         if(type==1){
  51.             currentaccount = new CurrentAccount(accountHolder,2856378, init, 5000);
  52.             account.add(currentaccount);
  53.         }
  54.         else if(type==2) {
  55.             savingsaccount = new SavingsAccount(accountHolder, init, 2000);
  56.             account.add(savingsaccount);
  57.         }
  58.         else if(type==3){
  59.             studentaccount = new StudentAccount(accountHolder,"UIU", init, 100);
  60.             account.add(studentaccount);
  61.         }
  62.     }
  63.  
  64.     void depositAmount(int type){
  65.         String deposit = JOptionPane.showInputDialog( "Deposit Amount:");
  66.         double depositAmount = Double.parseDouble(deposit);
  67.  
  68.         if (type == 1) {
  69.             currentaccount.deposit(depositAmount);
  70.             currentaccount.getBalance();
  71.             JOptionPane.showMessageDialog(null, "Updated Balance:\n" + currentaccount.getBalance());
  72.         } else if (type == 2) {
  73.             savingsaccount.deposit(depositAmount);
  74.             savingsaccount.getBalance();
  75.             JOptionPane.showMessageDialog(null, "Updated Balance:\n" + savingsaccount.getBalance());
  76.         } else if (type == 3) {
  77.             studentaccount.deposit(depositAmount);
  78.             studentaccount.getBalance();
  79.             JOptionPane.showMessageDialog(null, "Updated Balance:\n" + studentaccount.getBalance());
  80.         }
  81.     }
  82.     void withdrawAmount(int type){
  83.         String withdraw = JOptionPane.showInputDialog(null, "Amount to Withdraw:");
  84.         double withdrawAmount = Double.parseDouble(withdraw);
  85.         String check = JOptionPane.showInputDialog(null, "Do you wish to check your Account Balance?\n(1) Yes\n(0) No");
  86.         int balanceCheck = Integer.parseInt(check);
  87.  
  88.         if (balanceCheck == 1) {
  89.             if (type == 1) {
  90.                 currentaccount.getBalance();
  91.                 JOptionPane.showMessageDialog(null, "Balance:\n" + currentaccount.getBalance());
  92.                 currentaccount.withdrawl(withdrawAmount);
  93.                 currentaccount.getBalance();
  94.                 JOptionPane.showMessageDialog(null, "Updated Balance:\n" + currentaccount.getBalance());
  95.  
  96.             } else if (type == 2) {
  97.                 savingsaccount.getBalance();
  98.                 JOptionPane.showMessageDialog(null, "Balance:\n" + savingsaccount.getBalance());
  99.                 savingsaccount.withdrawl(withdrawAmount);
  100.                 savingsaccount.getBalance();
  101.                 JOptionPane.showMessageDialog(null, "Updated Balance:\n" + savingsaccount.getBalance());
  102.             } else if (type == 3) {
  103.                 studentaccount.getBalance();
  104.                 JOptionPane.showMessageDialog(null, "Balance:\n" + studentaccount.getBalance());
  105.                 studentaccount.withdrawl(withdrawAmount);
  106.                 studentaccount.getBalance();
  107.                 JOptionPane.showMessageDialog(null, "Updated Balance:\n" + studentaccount.getBalance());
  108.             }
  109.  
  110.         }
  111.     }
  112.     void checkBalance(int type){
  113.         if (type == 1){
  114.             currentaccount.getBalance();
  115.             JOptionPane.showMessageDialog(null, "Balance:\n" + currentaccount.getBalance());
  116.         }
  117.         else if (type == 2) {
  118.             String note = JOptionPane.showInputDialog(null,"Do you want to check Balance with Interest?\n(1) Yes\n(0) No");
  119.             int op = Integer.parseInt(note);
  120.             if(op == 0){
  121.                 savingsaccount.getBalance();
  122.                 JOptionPane.showMessageDialog(null, "Balance:\n" + savingsaccount.getBalance());
  123.             }
  124.             else{
  125.                 String year = JOptionPane.showInputDialog(null,"How many years of interest: ");
  126.                 int y = Integer.parseInt(year);
  127.                 savingsaccount.getBalance(y);
  128.                 JOptionPane.showMessageDialog(null, "Balance:\n" + savingsaccount.getBalance(y));
  129.             }
  130.         }
  131.         else if (type == 3) {
  132.             studentaccount.getBalance();
  133.             JOptionPane.showMessageDialog(null, "Balance:\n" + studentaccount.getBalance());
  134.         }
  135.     }
  136. }
  137.  
  138. // BankAccount
  139.  
  140.  
  141. import java.util.Random;
  142.  
  143. public abstract class BankAccount {
  144.  
  145.     private String name;
  146.     private int accountNumber;
  147.     private double accountBalance;
  148.     private double minimumBalance;
  149.  
  150.     public int accountGenerator(){
  151.         Random rand = new Random();
  152.         return 10000+rand.nextInt(89999);
  153.     }
  154.     BankAccount(){
  155.         name =" ";
  156.         accountBalance = 0.0;
  157.     }
  158.     BankAccount (String name, double balance, double minBalance){
  159.         this.name = name;
  160.         accountBalance = balance;
  161.         minimumBalance = minBalance;
  162.         accountNumber = accountGenerator();
  163.     }
  164.     public void setMemberName(){this.name = name;}
  165.     public String getName(){return name;}
  166.     public void setAccountNumber(){this.accountNumber=accountGenerator();}
  167.     public int getAccountNumber(){return accountNumber;}
  168.     public void setAccountBalance(double accountBalance){this.accountBalance=accountBalance;}
  169.     public double getAccountBalance(){return accountBalance;}
  170.     public void setMinimumBalance(){this.minimumBalance=minimumBalance;}
  171.     public double getMinimumBalance(){return minimumBalance;}
  172.     public void deposit(double amount){
  173.         accountBalance+= amount;
  174.     }
  175.     public abstract void withdrawl(double amount);
  176.     public double getBalance(int year){
  177.         return accountBalance;
  178.     }
  179.     public double getBalance(){
  180.         return accountBalance;
  181.     }
  182. }
  183.  
  184. // SavingsAccount
  185.  
  186. import javax.swing.JOptionPane;
  187. public class SavingsAccount extends BankAccount{
  188.  
  189.     private double interest = 0.05;
  190.  
  191.     private double maxWithdrawlAmount = 1000000;
  192.     public SavingsAccount(String name, double balance, double minBalance){
  193.         super(name,balance,minBalance);
  194.     }
  195.     SavingsAccount(){
  196.         getName();
  197.         getAccountBalance();
  198.     }
  199.     public void setInterest(){this.interest=interest;}
  200.     public double getInterest(){return interest;}
  201.     public void setMaxWithdrawlAmount(){this.maxWithdrawlAmount=maxWithdrawlAmount;}
  202.     public double getMaxWithdrawlAmount(){return maxWithdrawlAmount;}
  203.     public double getBalance(int year){
  204.  
  205.         double amount = (getAccountBalance() + interest * getAccountBalance())*year;
  206.         return amount;
  207.     }
  208.     public void withdrawl(double amount){
  209.         if((getAccountBalance() - amount)>= getMinimumBalance()){
  210.             setAccountBalance(getAccountBalance() -amount);
  211.         }
  212.         else{
  213.             JOptionPane.showMessageDialog(null,"Balance cannot be withdrawn!");
  214.         }
  215.     }
  216.     public double getBalance(){
  217.         return getAccountBalance();
  218.     }
  219. }
  220.  
  221. //CurrentAccount
  222.  
  223. import javax.swing.JOptionPane;
  224. public class CurrentAccount extends BankAccount {
  225.     private int tradeLicenceNumber;
  226.     public CurrentAccount(String name,int tradeLicence, double balance, double minBalance){
  227.         super(name,balance,minBalance);
  228.         tradeLicenceNumber = tradeLicence;
  229.     }
  230.     CurrentAccount(){
  231.         getName();
  232.         getAccountBalance();
  233.     }
  234.  
  235.     public void setTradeLicenceNumber(){this.tradeLicenceNumber=tradeLicenceNumber;}
  236.     public int getTradeLicenceNumber(){return tradeLicenceNumber;}
  237.  
  238.     public void withdrawl(double amount){
  239.         if((getAccountBalance() - amount)>= getMinimumBalance()){;
  240.             setAccountBalance(getAccountBalance()-amount);
  241.         }
  242.         else{
  243.             JOptionPane.showMessageDialog(null,"Balance cannot be withdrawn!");
  244.         }
  245.     }
  246.     public double getBalance(){
  247.         return getAccountBalance();
  248.     }
  249.  
  250. }
  251.  
  252.  
  253. //StudentAccount
  254.  
  255. import javax.swing.JOptionPane;
  256. public class StudentAccount extends BankAccount{
  257.    
  258.     private String institutionName;
  259.     public StudentAccount(String name, String institute, double balance, double minBalance){
  260.         super(name,balance,minBalance);
  261.         institutionName = institute;
  262.     }
  263.     StudentAccount(){
  264.         getName();
  265.         getAccountBalance();
  266.     }
  267.     public void setInstitutionName(){this.institutionName=institutionName;}
  268.     public String getInstitutionName(){return institutionName;}
  269.     public void withdrawl(double amount){
  270.         if(amount<=20000){
  271.             if((getAccountBalance() - amount)>= getMinimumBalance()){
  272.                 setAccountBalance(getAccountBalance() - amount);
  273.             }
  274.             else{
  275.                 JOptionPane.showMessageDialog(null,"Balance cannot be withdrawn!");
  276.             }
  277.         }
  278.         else{
  279.             JOptionPane.showMessageDialog(null,"Balance exceeds limit of withdrawl!");
  280.         }
  281.     }
  282.     public double getBalance(){
  283.         return getAccountBalance();
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement