Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------- Account -------------------
- package BankAccounts;
- public class Account {
- // Instance variables
- private int id;
- private String name;
- private double fee;
- private double balance;
- private double lineOfCredit;
- private String accountType;
- // Constructor
- public Account(int id, String name, double fee, double lineOfCredit, String accountType) {
- this.id = id;
- this.name = name;
- this.fee = fee;
- this.balance = 0;
- this.lineOfCredit = lineOfCredit;
- this.accountType = accountType;
- }
- // Class methods
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public double getFee() {
- return fee;
- }
- public boolean setFee(double fee) {
- if(this instanceof RegularAccount || this instanceof BusinessAccount) {
- if(fee > 0) {
- this.fee = fee;
- return true;
- }
- }
- return false;
- }
- public double getBalance() {
- return balance;
- }
- public double getLineOfCredit() {
- return lineOfCredit;
- }
- public void setLineOfCredit(double lineOfCredit) {
- this.lineOfCredit = lineOfCredit;
- }
- public void deposit(double money) {
- if(money < 0) {
- System.out.println("Cannot deposit negative value!");
- } else if(money == 0) {
- System.out.println("You tried to deposit 0 ILS! Balance will stay the same!");
- } else {
- this.balance += (getBalance()*getFee()*-1 + money);
- }
- }
- public void withdraw(double money) {
- if(money <= 0) {
- System.out.println("Bad Input! Cannot withdraw 0 or negative value!");
- } else {
- if(getBalance() - money*(getFee()+1) < lineOfCredit * -1) {
- System.out.println("Impossible to withdraw");
- } else {
- this.balance -= money + Math.abs(getBalance())*getFee();
- }
- }
- }
- public void getLoan(double loan) {
- int loanAmount = 0;
- if(this instanceof RegularAccount || this instanceof BusinessAccount) {
- loanAmount = this instanceof RegularAccount ? 10000 : 100000;
- if(loan <= loanAmount && loan > 0) {
- deposit(loan);
- } else {
- System.out.println("Bad Input!!!");
- }
- }
- }
- public void printDetails() {
- System.out.println("Account details:\nAccount type: " + this.accountType + "\nName of account owner: " + getName() + "\nId of account owner: " + getId() +
- "\nFee is: " + getFee() + "\nBalance is: " + getBalance() + "\n");
- }
- }
- //----------------- Business Account ------------------------
- package BankAccounts;
- public class BusinessAccount extends Account{
- public BusinessAccount(int id, String name, double lineOfCredit) {
- super(id, name, 0.03, lineOfCredit, "Business Account");
- }
- }
- //----------------- Regular Account -----------------------------
- package BankAccounts;
- public class RegularAccount extends Account{
- public RegularAccount(int id, String name, double lineOfCredit) {
- super(id, name, 0.05, lineOfCredit, "Regular Account");
- }
- }
- //-------------------- Student Account ------------------------
- package BankAccounts;
- public class StudentAccount extends Account{
- public StudentAccount(int id, String name, double lineOfCredit) {
- super(id, name, 0.01, lineOfCredit, "Student Account");
- }
- }
- //-------------------- Young Account ----------------------------
- package BankAccounts;
- public class YoungAccount extends Account{
- public YoungAccount(int id, String name, double lineOfCredit) {
- super(id, name, 0, lineOfCredit, "Young Account");
- }
- }
- //------------------------ Main Class --------------------------
- package Tester;
- import BankAccounts.*;
- public class BankAccounts {
- public static void main(String[] args) {
- Account[] accounts = new Account[4];
- accounts[0] = new BusinessAccount(111111111, "Eran", 40000);
- accounts[1] = new StudentAccount(222222222, "Moshe", 30000);
- accounts[2] = new RegularAccount(333333333, "Yossi", 20000);
- accounts[3] = new YoungAccount(111111111, "Yaron", 15000);
- for (int i = 0; i < accounts.length; i++) {
- accounts[i].deposit(20000 + 1000*i);
- accounts[i].printDetails();
- accounts[i].withdraw(50000 + 500*i);
- accounts[i].printDetails();
- if(accounts[i] instanceof RegularAccount || accounts[i] instanceof BusinessAccount) {
- accounts[i].getLoan(50000);
- }
- accounts[i].printDetails();
- }
- }
- }
Add Comment
Please, Sign In to add comment