eranseg

Bank Accounts

Sep 1st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. //---------- Account -------------------
  2. package BankAccounts;
  3.  
  4. public class Account {
  5.  
  6.     // Instance variables
  7.     private int id;
  8.     private String name;
  9.     private double fee;
  10.     private double balance;
  11.     private double lineOfCredit;
  12.     private String accountType;
  13.  
  14.     // Constructor
  15.  
  16.     public Account(int id, String name, double fee, double lineOfCredit, String accountType) {
  17.         this.id = id;
  18.         this.name = name;
  19.         this.fee = fee;
  20.         this.balance = 0;
  21.         this.lineOfCredit = lineOfCredit;
  22.         this.accountType = accountType;
  23.     }
  24.  
  25.     // Class methods
  26.     public int getId() {
  27.         return id;
  28.     }
  29.  
  30.     public String getName() {
  31.         return name;
  32.     }
  33.  
  34.     public double getFee() {
  35.         return fee;
  36.     }
  37.  
  38.     public boolean setFee(double fee) {
  39.         if(this instanceof RegularAccount || this instanceof BusinessAccount) {
  40.             if(fee > 0) {
  41.                 this.fee = fee;
  42.                 return true;
  43.             }
  44.         }
  45.         return false;
  46.     }
  47.  
  48.     public double getBalance() {
  49.         return balance;
  50.     }
  51.  
  52.     public double getLineOfCredit() {
  53.         return lineOfCredit;
  54.     }
  55.  
  56.     public void setLineOfCredit(double lineOfCredit) {
  57.         this.lineOfCredit = lineOfCredit;
  58.     }
  59.  
  60.     public void deposit(double money) {
  61.         if(money < 0) {
  62.             System.out.println("Cannot deposit negative value!");
  63.         } else if(money == 0) {
  64.             System.out.println("You tried to deposit 0 ILS! Balance will stay the same!");
  65.         } else {
  66.             this.balance += (getBalance()*getFee()*-1 + money);
  67.         }
  68.     }
  69.  
  70.     public void withdraw(double money) {
  71.         if(money <= 0) {
  72.             System.out.println("Bad Input! Cannot withdraw 0 or negative value!");
  73.         } else {
  74.             if(getBalance() - money*(getFee()+1) < lineOfCredit * -1) {
  75.                 System.out.println("Impossible to withdraw");
  76.             } else {
  77.                 this.balance -= money + Math.abs(getBalance())*getFee();
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void getLoan(double loan) {
  83.         int loanAmount = 0;
  84.         if(this instanceof RegularAccount || this instanceof BusinessAccount) {
  85.             loanAmount = this instanceof RegularAccount ? 10000 : 100000;
  86.             if(loan <= loanAmount && loan > 0) {
  87.                 deposit(loan);
  88.             } else {
  89.                 System.out.println("Bad Input!!!");
  90.             }
  91.         }
  92.     }
  93.  
  94.     public void printDetails() {
  95.         System.out.println("Account details:\nAccount type: " + this.accountType + "\nName of account owner: " + getName() + "\nId of account owner: " + getId() +
  96.         "\nFee is: " + getFee() + "\nBalance is: " + getBalance() + "\n");
  97.     }
  98. }
  99.  
  100. //----------------- Business Account ------------------------
  101. package BankAccounts;
  102.  
  103. public class BusinessAccount extends Account{
  104.  
  105.     public BusinessAccount(int id, String name, double lineOfCredit) {
  106.         super(id, name, 0.03, lineOfCredit, "Business Account");
  107.     }
  108. }
  109.  
  110. //----------------- Regular Account -----------------------------
  111. package BankAccounts;
  112.  
  113. public class RegularAccount extends Account{
  114.  
  115.     public RegularAccount(int id, String name, double lineOfCredit) {
  116.         super(id, name, 0.05, lineOfCredit, "Regular Account");
  117.     }
  118. }
  119.  
  120. //-------------------- Student Account ------------------------
  121. package BankAccounts;
  122.  
  123. public class StudentAccount extends Account{
  124.  
  125.     public StudentAccount(int id, String name, double lineOfCredit) {
  126.         super(id, name, 0.01, lineOfCredit, "Student Account");
  127.     }
  128. }
  129.  
  130. //-------------------- Young Account ----------------------------
  131. package BankAccounts;
  132.  
  133. public class YoungAccount extends Account{
  134.  
  135.     public YoungAccount(int id, String name, double lineOfCredit) {
  136.         super(id, name, 0, lineOfCredit, "Young Account");
  137.     }
  138. }
  139.  
  140. //------------------------ Main Class --------------------------
  141. package Tester;
  142. import BankAccounts.*;
  143.  
  144. public class BankAccounts {
  145.  
  146.     public static void main(String[] args) {
  147.         Account[] accounts = new Account[4];
  148.         accounts[0] = new BusinessAccount(111111111, "Eran", 40000);
  149.         accounts[1] = new StudentAccount(222222222, "Moshe", 30000);
  150.         accounts[2] = new RegularAccount(333333333, "Yossi", 20000);
  151.         accounts[3] = new YoungAccount(111111111, "Yaron", 15000);
  152.         for (int i = 0; i < accounts.length; i++) {
  153.             accounts[i].deposit(20000 + 1000*i);
  154.             accounts[i].printDetails();
  155.             accounts[i].withdraw(50000 + 500*i);
  156.             accounts[i].printDetails();
  157.             if(accounts[i] instanceof RegularAccount || accounts[i] instanceof BusinessAccount) {
  158.                 accounts[i].getLoan(50000);
  159.             }
  160.             accounts[i].printDetails();
  161.         }
  162.     }
  163. }
Add Comment
Please, Sign In to add comment