Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Cls;
- import MyInterfaces.Giftable;
- import MyInterfaces.Loanable;
- import MyInterfaces.Mortgageable;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.sql.SQLOutput;
- import java.util.Scanner;
- import java.util.Vector;
- public abstract class Account {
- Scanner s = new Scanner(System.in);
- int check = 8;
- int check2 = 0;
- char a, c, b;
- static int accountNumber = 0;
- int account;
- String name;
- double balance;
- int com;
- boolean loan = false;
- boolean mortgage = false;
- double savings, loanSum;
- boolean gift = false;
- boolean credit = false;
- double mortgageSum;
- public Account() { //default constructor;
- }
- public Account(String name, double balance, int com) { // main constructor, creating a new account;
- this.name = name;
- this.balance = balance;
- this.com = com;
- this.account = accountNumber;
- accountNumber += 1;
- }
- public Account(Scanner s) throws FileNotFoundException { //
- String rawData = s.nextLine();
- String[] splitStr = rawData.split(",");
- this.accountNumber = Integer.parseInt(splitStr[0]);
- this.name = splitStr[1];
- this.balance = Double.parseDouble(splitStr[2]);
- }
- public Account(double accountNumber, String name, double balance) {
- }
- public Account(Account account) { //creating an account using the singleton;
- this(account.accountNumber, account.name, account.balance);
- }
- public Account(String fileName) throws FileNotFoundException { // creating a new file;
- this(new Scanner(new File(fileName)));
- }
- public void withDrawal(int sum) { //withdrawal method;
- double wantWithdraw = sum + sum * (com / 100);
- if (balance - wantWithdraw >= 0)
- this.balance -= wantWithdraw;
- else if (balance - wantWithdraw < 0 && this instanceof BusinessAccount)
- this.balance -= wantWithdraw;
- else System.out.println("you can only draw: " + this.balance + "₪");
- }
- public void addFunds(double sum) { //adding funds method;
- balance += sum - sum * com / 100;
- }
- public double getBalance() { //getting current balance;
- return balance;
- }
- public void updateCom(int newCom) { // method that allows the bank to change the current commission;
- this.com = newCom;
- }
- @Override
- public String toString() { // printing method;
- return
- "accountNumber: " + account +
- ", name: " + name + '\'' +
- ", balance:" + balance +
- ", commission rate is: " + com;
- }
- public static Vector<Account> getLoanList(Vector<Account> allAccounts, boolean hasLoan) { // loaners list;
- Vector<Account> returnClient = new Vector<>();
- for (Account item : allAccounts) {
- if (item instanceof Loanable && item.loan == hasLoan) {
- System.out.println(item);
- returnClient.add(item);
- }
- }
- return returnClient;
- }
- public static Vector<Account> getMortgageList(Vector<Account> allAccounts, boolean hasMortgage) { // mortgage list;
- Vector<Account> returnClient = new Vector<>();
- for (Account item : allAccounts) {
- if (item instanceof Mortgageable && item.mortgage == hasMortgage) {
- System.out.println(item);
- returnClient.add(item);
- }
- }
- return returnClient;
- }
- public static Vector<Account> getGiftList(Vector<Account> allAccounts, boolean hasGift) { // gift list;
- Vector<Account> returnClient = new Vector<>();
- for (Account item : allAccounts) {
- if (item instanceof Giftable && item.gift == hasGift) {
- System.out.println(item);
- returnClient.add(item);
- }
- }
- return returnClient;
- }
- public void setLoan(int sum) { // method that checks if eligible for loan and gives it.
- if (this instanceof Loanable) {
- this.loan = true;
- this.balance = balance + sum;
- this.loanSum = sum;
- } else {
- System.out.println("you cant get loans with this kind of account");
- }
- }
- public void setMortgage(int sum) { // method that checks if client can get mortgage, if so, gives him.
- if (this instanceof Mortgageable && mortgage==false){
- this.mortgage = true;
- //this.balance = balance + sum;
- this.mortgageSum = sum;
- } else {
- System.out.println("you cant get mortgage with this kind of account");
- }
- }
- public boolean getMortgage() {
- return (this instanceof Mortgageable);
- }
- public void addMortgage(double sum) { // adding the mortgage sum to the account.
- this.mortgageSum = sum;
- }
- public double getMortgagesum() {
- return this.mortgageSum;
- }
- public boolean getGift() {
- return (this instanceof StudentAccount || this instanceof YoungAccount);
- }
- public void setgift() {
- if (this instanceof Giftable)
- this.gift = true;
- else System.out.println("this account doesn't deserve a gift");
- }
- public boolean savings() {
- return (this instanceof RegularAccount);
- }
- public boolean getLoan() {
- return (this instanceof Loanable);
- }
- public void save(PrintWriter pw) throws FileNotFoundException { //saving method;
- String data = this.accountNumber + "," + this.name + "," + this.balance;
- pw.println(data);
- }
- public void save(String file) throws FileNotFoundException { // saving method.
- File f1 = new File(file);
- PrintWriter pw = new PrintWriter(f1);
- save(pw);
- pw.close();
- }
- public void setSavings(double sum) {
- savings = savings + sum;
- }
- public void changeCommission(Vector<Account> allaccounts) { // changing the commission of each account;
- System.out.println("which Account type would you like to change");
- System.out.println("1. Regular Account, 2. Student Account. 3.Young Account. 4.Business Account");
- check = s.nextInt();
- System.out.println("the new commission is ?");
- check2 = s.nextInt();
- switch (check) {
- case 1:
- for (Account item : allaccounts)
- if (item instanceof RegularAccount) {
- item.updateCom(check2);
- }
- break;
- case 2:
- for (Account item : allaccounts)
- if (item instanceof StudentAccount)
- item.updateCom(check2);
- break;
- case 3:
- for (Account item : allaccounts)
- if (item instanceof YoungAccount)
- item.updateCom(check2);
- break;
- case 4:
- for (Account item : allaccounts)
- if (item instanceof BusinessAccount)
- item.updateCom(check2);
- break;
- }
- }
- public static Vector<Account> getAccountType(Vector<Account> allAccounts, char type) { // method that allows to get account by type;
- Vector<Account> returnClient = new Vector<>();
- switch (type) {
- case 'y':
- case 'Y':
- for (Account item : allAccounts)
- if (item instanceof YoungAccount) {
- System.out.println(item);
- }
- break;
- case 'B':
- case 'b':
- for (Account item : allAccounts) {
- if (item instanceof BusinessAccount) {
- System.out.println(item);
- }
- }
- break;
- case 's':
- case 'S':
- for (Account item : allAccounts) {
- if (item instanceof StudentAccount) {
- System.out.println(item);
- }
- }
- break;
- case 'r':
- case 'R':
- for (Account item : allAccounts) {
- if (item instanceof RegularAccount) {
- System.out.println(item);
- }
- }
- break;
- }
- return returnClient;
- }
- public void bankerOptions(Vector<Account> allAccounts) { // gives the banker all the options he need;
- System.out.println("what kind of action would you like to do?");
- while (check != 0) {
- System.out.println("0. Exit. 1.client list by type. 2.loaners list. 3. clients with/out a mortgage list. 4.gift takers list. 5.Change commission ");
- check = s.nextInt();
- switch (check) {
- case 1:
- System.out.println("which type would you like to print?");
- System.out.println("B - Business, R-Regular, Y-young, S-Student");
- a = s.next().charAt(0);
- allAccounts.get(0).getAccountType(allAccounts, a);
- break;
- case 2:
- System.out.println("loaners list:");
- allAccounts.get(0).getLoanList(allAccounts, true);
- break;
- case 3:
- System.out.println(" accounts mortgage list");
- allAccounts.get(0).getMortgageList(allAccounts, true);
- System.out.println("accounts without mortgage");
- allAccounts.get(0).getMortgageList(allAccounts, false);
- break;
- case 4:
- System.out.println("gift takers list:");
- allAccounts.get(0).getGiftList(allAccounts, true);
- break;
- case 5:
- System.out.println("ok lets do this: ");
- allAccounts.get(0).changeCommission(allAccounts);
- }
- }
- }
- public void clientOptions(Vector<Account> allAccounts) { // gives the client all the options he needs;
- System.out.println("Hi,are you an Existing client or New client? N/E");
- c = s.next().charAt(0);
- if (c == 'n' || c == 'N') {
- System.out.println("please enter your name");
- String name = s.next();
- System.out.println("please enter amount of money you would like to deposit");
- int amount = s.nextInt();
- System.out.println("what kind of account would you like : R-Regular, B-Business,R-Regular,Y-young ");
- a = s.next().charAt(0);
- switch (a) {
- case 'b':
- case 'B':
- allAccounts.add(new BusinessAccount(name, amount));
- break;
- case 'y':
- case 'Y':
- allAccounts.add(new YoungAccount(name, amount));
- break;
- case 's':
- case 'S':
- allAccounts.add(new StudentAccount(name, amount));
- break;
- default:
- allAccounts.add(new RegularAccount(name, amount));
- }
- System.out.println("thanks for opening a new account at our bank");
- System.out.println("your account info is:" + allAccounts.get(allAccounts.size() - 1).toString());
- } else if (c == 'E' || c == 'e') {
- System.out.println("please enter your account number");
- int i = s.nextInt();
- if (i < allAccounts.size()) {
- System.out.println("what kind of action would like to do? ");
- System.out.print(" PRESS: 0.to exit, 1.To check balance?, 2.To Add funds, 3.To Withdrawal money, 4. TO get a loan, 5.get a mortgage");
- check = s.nextInt();
- while (check != 0) {
- switch (check) {
- case 1:
- System.out.println(allAccounts.get(i).getBalance());
- break;
- case 2:
- System.out.println("how much would you like to add?");
- allAccounts.get(i).addFunds(s.nextInt());
- System.out.println("your updated balance is: " + allAccounts.get(i).getBalance());
- break;
- case 3:
- System.out.print("how much would you like to withdrawal?");
- allAccounts.get(i).withDrawal(s.nextInt());
- System.out.println("your current balance is " + allAccounts.get(i).getBalance());
- break;
- case 4:
- System.out.println("lets see if you can make a loan");
- if (allAccounts.get(i).getLoan()) {
- System.out.println("how much would you like to loan?");
- allAccounts.get(i).setLoan(s.nextInt());
- System.out.println("your current balance after loan is: " + allAccounts.get(i).getBalance());
- } else System.out.println("sorry cant give you loan");
- break;
- case 5:
- System.out.println("lets see if you can get a mortgage");
- if (allAccounts.get(i).getMortgage()) {
- System.out.println("you can get a mortgage, how much would you like");
- allAccounts.get(i).addMortgage(s.nextDouble());
- System.out.println("your account details are: " + allAccounts.get(i).toString() + " mortgage: " + allAccounts.get(i).getMortgagesum());
- } else
- System.out.println("sorry cant give you mortgage");
- break;
- }
- System.out.print(" PRESS: 0.to exit, 1.To check balance?, 2.To Add funds, 3.To Withdrawal money, 4. TO get a loan, 5.get mortgage?");
- check = s.nextInt();
- }
- System.out.println("see you");
- } else System.out.println("sorry no such account number in our systems");
- }
- }
- }
- ******************************************************************************************************
- package Cls;
- import MyInterfaces.Giftable;
- import MyInterfaces.Loanable;
- public class StudentAccount extends Account implements Loanable, Giftable {
- private final String name;
- private double balance;
- private static int com;
- public StudentAccount(String name, double balance) {
- super(name, balance, 1);
- this.name = name;
- this.balance = balance;
- this.com = 1;
- }
- public void getLoan(double sum) {
- balance = balance + sum;
- this.loan = true;
- }
- public String toString() {
- return super.toString() + " ,loan sum : " + loanSum;
- }
- }
- ******************************************************************************************************
- package Cls;
- import MyInterfaces.Loanable;
- public class BusinessAccount extends Account implements Loanable {
- private final String name;
- private double balance;
- private static int commission;
- public BusinessAccount(String name, double balance) {
- super(name, balance, 3);
- this.name = name;
- this.balance = balance;
- this.com = 3;
- }
- public String toString() {
- return super.toString() + " Loans : " + loanSum;
- }
- }
- ******************************************************************************************************
- package Cls;
- import MyInterfaces.Giftable;
- public class YoungAccount extends Account implements Giftable {
- private final String name;
- private final double balance;
- private static int com;
- public YoungAccount(String name, double balance) {
- super(name, balance, 0);
- this.name = name;
- this.balance = balance;
- this.com = 0;
- }
- }
- ******************************************************************************************************
- package Cls;
- import MyInterfaces.Loanable;
- import MyInterfaces.Mortgageable;
- import MyInterfaces.Saveable;
- public class RegularAccount extends Account implements Saveable, Mortgageable, Loanable {
- private final String name;
- private final double balance;
- private static int com = 5;
- private double savings = 0;
- public RegularAccount(String name, double balance) {
- super(name, balance, 5);
- this.name = name;
- this.balance = balance;
- }
- public String toString() {
- return super.toString() + " loan sum: " + loanSum + " mortgage sum: " + mortgageSum;
- }
- public void savings(double sum) {
- savings = savings + sum;
- }
- }
- ******************************************************************************************************
- package Cls;
- import java.util.Vector;
- public class Banker extends Account {
- private static Banker theBanker;
- Vector<Account> allAccounts = new Vector<Account>();
- private Banker() {
- //System.out.println("the banker was made");
- }
- public Vector<Account> getList() {
- return allAccounts;
- }
- public static Banker getInstance() {
- //System.out.println("getting instance of Banker");
- if (theBanker == null) {
- theBanker = new Banker();
- // System.out.println("the new banker was made");
- }
- return theBanker;
- }
- public void addAccount(Account account) {
- allAccounts.add(account);
- }
- public Account getList(int num) {
- return allAccounts.get(num);
- }
- }
- ******************************************************************************************************
- package MyInterfaces;
- public interface Giftable {
- }
- *****************************************************
- package MyInterfaces;
- public interface Loanable {
- }
- *****************************************************
- package MyInterfaces;
- public interface Mortgageable {
- }
- *****************************************************
- package MyInterfaces;
- public interface Saveable {
- }
- *****************************************************
- package Tester;
- //import Cls.Business;
- import Cls.*;
- import java.security.AllPermission;
- import java.util.*;
- public class Tester {
- public static void main(String[] args) {
- int i = 0;
- int check = 0;
- char b, c, a;
- Scanner s = new Scanner(System.in);
- Banker b1 = Banker.getInstance();
- b1.addAccount(new RegularAccount("testAccount", 0)); // creating a new account using singelton;
- b1.addAccount(new RegularAccount("moshe", 20000));
- b1.addAccount(new RegularAccount("walla", 3000));
- b1.addAccount(new StudentAccount("avi", 15000));
- b1.addAccount(new YoungAccount("daniel", 4500));
- b1.addAccount(new BusinessAccount("avi", 3500));
- b1.getList(1).setLoan(5000);
- b1.getList(2).setLoan(4000);
- //b1.getList(3).setLoan(3500); //young account cant get a loan.
- b1.getList(1).setMortgage(500000);
- b1.getList(0).setMortgage(40000);
- b1.getList(3).setgift();
- //b1.getList(2).setgift();
- System.out.println("hi are you a Banker or a client? B/C");
- b = s.next().charAt(0);
- if (b == 'b' || b == 'B') { // checking who is using the software, banker or client;
- b1.bankerOptions(b1.getList());
- } else if (b == 'c' || b == 'C')
- b1.clientOptions(b1.getList());
- else System.out.println("no such option ...");
- }
- }
Add Comment
Please, Sign In to add comment