shlomibergic

Fucking Bank

Jul 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.18 KB | None | 0 0
  1. package Cls;
  2.  
  3. import MyInterfaces.Giftable;
  4. import MyInterfaces.Loanable;
  5. import MyInterfaces.Mortgageable;
  6.  
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.PrintWriter;
  10. import java.sql.SQLOutput;
  11. import java.util.Scanner;
  12. import java.util.Vector;
  13.  
  14. public abstract class Account {
  15. Scanner s = new Scanner(System.in);
  16. int check = 8;
  17. int check2 = 0;
  18. char a, c, b;
  19. static int accountNumber = 0;
  20. int account;
  21. String name;
  22. double balance;
  23. int com;
  24. boolean loan = false;
  25. boolean mortgage = false;
  26. double savings, loanSum;
  27. boolean gift = false;
  28. boolean credit = false;
  29. double mortgageSum;
  30.  
  31. public Account() { //default constructor;
  32. }
  33.  
  34. public Account(String name, double balance, int com) { // main constructor, creating a new account;
  35. this.name = name;
  36. this.balance = balance;
  37. this.com = com;
  38. this.account = accountNumber;
  39. accountNumber += 1;
  40. }
  41.  
  42. public Account(Scanner s) throws FileNotFoundException { //
  43. String rawData = s.nextLine();
  44. String[] splitStr = rawData.split(",");
  45. this.accountNumber = Integer.parseInt(splitStr[0]);
  46. this.name = splitStr[1];
  47. this.balance = Double.parseDouble(splitStr[2]);
  48. }
  49.  
  50. public Account(double accountNumber, String name, double balance) {
  51. }
  52.  
  53. public Account(Account account) { //creating an account using the singleton;
  54. this(account.accountNumber, account.name, account.balance);
  55. }
  56.  
  57. public Account(String fileName) throws FileNotFoundException { // creating a new file;
  58. this(new Scanner(new File(fileName)));
  59. }
  60.  
  61.  
  62. public void withDrawal(int sum) { //withdrawal method;
  63. double wantWithdraw = sum + sum * (com / 100);
  64. if (balance - wantWithdraw >= 0)
  65. this.balance -= wantWithdraw;
  66. else if (balance - wantWithdraw < 0 && this instanceof BusinessAccount)
  67. this.balance -= wantWithdraw;
  68. else System.out.println("you can only draw: " + this.balance + "₪");
  69. }
  70.  
  71. public void addFunds(double sum) { //adding funds method;
  72. balance += sum - sum * com / 100;
  73. }
  74.  
  75.  
  76. public double getBalance() { //getting current balance;
  77. return balance;
  78. }
  79.  
  80. public void updateCom(int newCom) { // method that allows the bank to change the current commission;
  81. this.com = newCom;
  82.  
  83. }
  84.  
  85. @Override
  86. public String toString() { // printing method;
  87. return
  88. "accountNumber: " + account +
  89. ", name: " + name + '\'' +
  90. ", balance:" + balance +
  91. ", commission rate is: " + com;
  92. }
  93.  
  94. public static Vector<Account> getLoanList(Vector<Account> allAccounts, boolean hasLoan) { // loaners list;
  95. Vector<Account> returnClient = new Vector<>();
  96. for (Account item : allAccounts) {
  97. if (item instanceof Loanable && item.loan == hasLoan) {
  98. System.out.println(item);
  99. returnClient.add(item);
  100. }
  101. }
  102. return returnClient;
  103. }
  104.  
  105. public static Vector<Account> getMortgageList(Vector<Account> allAccounts, boolean hasMortgage) { // mortgage list;
  106. Vector<Account> returnClient = new Vector<>();
  107. for (Account item : allAccounts) {
  108. if (item instanceof Mortgageable && item.mortgage == hasMortgage) {
  109. System.out.println(item);
  110. returnClient.add(item);
  111. }
  112. }
  113. return returnClient;
  114. }
  115.  
  116. public static Vector<Account> getGiftList(Vector<Account> allAccounts, boolean hasGift) { // gift list;
  117. Vector<Account> returnClient = new Vector<>();
  118. for (Account item : allAccounts) {
  119. if (item instanceof Giftable && item.gift == hasGift) {
  120. System.out.println(item);
  121. returnClient.add(item);
  122. }
  123. }
  124. return returnClient;
  125. }
  126.  
  127. public void setLoan(int sum) { // method that checks if eligible for loan and gives it.
  128. if (this instanceof Loanable) {
  129. this.loan = true;
  130. this.balance = balance + sum;
  131. this.loanSum = sum;
  132.  
  133. } else {
  134. System.out.println("you cant get loans with this kind of account");
  135. }
  136. }
  137.  
  138. public void setMortgage(int sum) { // method that checks if client can get mortgage, if so, gives him.
  139. if (this instanceof Mortgageable && mortgage==false){
  140. this.mortgage = true;
  141. //this.balance = balance + sum;
  142. this.mortgageSum = sum;
  143.  
  144. } else {
  145. System.out.println("you cant get mortgage with this kind of account");
  146. }
  147. }
  148.  
  149. public boolean getMortgage() {
  150. return (this instanceof Mortgageable);
  151. }
  152.  
  153. public void addMortgage(double sum) { // adding the mortgage sum to the account.
  154. this.mortgageSum = sum;
  155. }
  156.  
  157. public double getMortgagesum() {
  158. return this.mortgageSum;
  159. }
  160.  
  161. public boolean getGift() {
  162. return (this instanceof StudentAccount || this instanceof YoungAccount);
  163. }
  164.  
  165. public void setgift() {
  166. if (this instanceof Giftable)
  167. this.gift = true;
  168. else System.out.println("this account doesn't deserve a gift");
  169. }
  170.  
  171. public boolean savings() {
  172. return (this instanceof RegularAccount);
  173. }
  174.  
  175. public boolean getLoan() {
  176. return (this instanceof Loanable);
  177. }
  178.  
  179.  
  180. public void save(PrintWriter pw) throws FileNotFoundException { //saving method;
  181. String data = this.accountNumber + "," + this.name + "," + this.balance;
  182. pw.println(data);
  183. }
  184.  
  185. public void save(String file) throws FileNotFoundException { // saving method.
  186. File f1 = new File(file);
  187. PrintWriter pw = new PrintWriter(f1);
  188. save(pw);
  189. pw.close();
  190. }
  191.  
  192. public void setSavings(double sum) {
  193. savings = savings + sum;
  194. }
  195.  
  196. public void changeCommission(Vector<Account> allaccounts) { // changing the commission of each account;
  197. System.out.println("which Account type would you like to change");
  198. System.out.println("1. Regular Account, 2. Student Account. 3.Young Account. 4.Business Account");
  199. check = s.nextInt();
  200. System.out.println("the new commission is ?");
  201. check2 = s.nextInt();
  202. switch (check) {
  203. case 1:
  204. for (Account item : allaccounts)
  205. if (item instanceof RegularAccount) {
  206. item.updateCom(check2);
  207. }
  208. break;
  209. case 2:
  210. for (Account item : allaccounts)
  211. if (item instanceof StudentAccount)
  212. item.updateCom(check2);
  213.  
  214. break;
  215. case 3:
  216. for (Account item : allaccounts)
  217. if (item instanceof YoungAccount)
  218. item.updateCom(check2);
  219. break;
  220. case 4:
  221. for (Account item : allaccounts)
  222. if (item instanceof BusinessAccount)
  223. item.updateCom(check2);
  224. break;
  225.  
  226.  
  227. }
  228.  
  229. }
  230.  
  231. public static Vector<Account> getAccountType(Vector<Account> allAccounts, char type) { // method that allows to get account by type;
  232. Vector<Account> returnClient = new Vector<>();
  233. switch (type) {
  234. case 'y':
  235. case 'Y':
  236. for (Account item : allAccounts)
  237. if (item instanceof YoungAccount) {
  238. System.out.println(item);
  239. }
  240.  
  241. break;
  242. case 'B':
  243. case 'b':
  244. for (Account item : allAccounts) {
  245. if (item instanceof BusinessAccount) {
  246. System.out.println(item);
  247.  
  248. }
  249. }
  250. break;
  251. case 's':
  252. case 'S':
  253. for (Account item : allAccounts) {
  254. if (item instanceof StudentAccount) {
  255. System.out.println(item);
  256. }
  257. }
  258. break;
  259. case 'r':
  260. case 'R':
  261. for (Account item : allAccounts) {
  262. if (item instanceof RegularAccount) {
  263. System.out.println(item);
  264.  
  265. }
  266. }
  267. break;
  268.  
  269. }
  270. return returnClient;
  271. }
  272.  
  273. public void bankerOptions(Vector<Account> allAccounts) { // gives the banker all the options he need;
  274. System.out.println("what kind of action would you like to do?");
  275. while (check != 0) {
  276. 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 ");
  277. check = s.nextInt();
  278. switch (check) {
  279. case 1:
  280. System.out.println("which type would you like to print?");
  281. System.out.println("B - Business, R-Regular, Y-young, S-Student");
  282. a = s.next().charAt(0);
  283. allAccounts.get(0).getAccountType(allAccounts, a);
  284. break;
  285. case 2:
  286. System.out.println("loaners list:");
  287. allAccounts.get(0).getLoanList(allAccounts, true);
  288. break;
  289. case 3:
  290. System.out.println(" accounts mortgage list");
  291. allAccounts.get(0).getMortgageList(allAccounts, true);
  292. System.out.println("accounts without mortgage");
  293. allAccounts.get(0).getMortgageList(allAccounts, false);
  294.  
  295. break;
  296. case 4:
  297. System.out.println("gift takers list:");
  298. allAccounts.get(0).getGiftList(allAccounts, true);
  299. break;
  300. case 5:
  301. System.out.println("ok lets do this: ");
  302. allAccounts.get(0).changeCommission(allAccounts);
  303. }
  304.  
  305. }
  306. }
  307.  
  308. public void clientOptions(Vector<Account> allAccounts) { // gives the client all the options he needs;
  309. System.out.println("Hi,are you an Existing client or New client? N/E");
  310. c = s.next().charAt(0);
  311. if (c == 'n' || c == 'N') {
  312. System.out.println("please enter your name");
  313. String name = s.next();
  314. System.out.println("please enter amount of money you would like to deposit");
  315. int amount = s.nextInt();
  316. System.out.println("what kind of account would you like : R-Regular, B-Business,R-Regular,Y-young ");
  317. a = s.next().charAt(0);
  318. switch (a) {
  319. case 'b':
  320. case 'B':
  321. allAccounts.add(new BusinessAccount(name, amount));
  322. break;
  323. case 'y':
  324. case 'Y':
  325. allAccounts.add(new YoungAccount(name, amount));
  326. break;
  327. case 's':
  328. case 'S':
  329. allAccounts.add(new StudentAccount(name, amount));
  330. break;
  331. default:
  332. allAccounts.add(new RegularAccount(name, amount));
  333. }
  334. System.out.println("thanks for opening a new account at our bank");
  335. System.out.println("your account info is:" + allAccounts.get(allAccounts.size() - 1).toString());
  336. } else if (c == 'E' || c == 'e') {
  337. System.out.println("please enter your account number");
  338. int i = s.nextInt();
  339. if (i < allAccounts.size()) {
  340. System.out.println("what kind of action would like to do? ");
  341. 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");
  342. check = s.nextInt();
  343. while (check != 0) {
  344. switch (check) {
  345.  
  346. case 1:
  347. System.out.println(allAccounts.get(i).getBalance());
  348. break;
  349.  
  350. case 2:
  351. System.out.println("how much would you like to add?");
  352. allAccounts.get(i).addFunds(s.nextInt());
  353. System.out.println("your updated balance is: " + allAccounts.get(i).getBalance());
  354. break;
  355. case 3:
  356. System.out.print("how much would you like to withdrawal?");
  357. allAccounts.get(i).withDrawal(s.nextInt());
  358. System.out.println("your current balance is " + allAccounts.get(i).getBalance());
  359. break;
  360. case 4:
  361. System.out.println("lets see if you can make a loan");
  362. if (allAccounts.get(i).getLoan()) {
  363. System.out.println("how much would you like to loan?");
  364. allAccounts.get(i).setLoan(s.nextInt());
  365. System.out.println("your current balance after loan is: " + allAccounts.get(i).getBalance());
  366. } else System.out.println("sorry cant give you loan");
  367. break;
  368. case 5:
  369. System.out.println("lets see if you can get a mortgage");
  370. if (allAccounts.get(i).getMortgage()) {
  371. System.out.println("you can get a mortgage, how much would you like");
  372. allAccounts.get(i).addMortgage(s.nextDouble());
  373. System.out.println("your account details are: " + allAccounts.get(i).toString() + " mortgage: " + allAccounts.get(i).getMortgagesum());
  374. } else
  375. System.out.println("sorry cant give you mortgage");
  376. break;
  377. }
  378. 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?");
  379. check = s.nextInt();
  380. }
  381. System.out.println("see you");
  382. } else System.out.println("sorry no such account number in our systems");
  383.  
  384. }
  385. }
  386. }
  387. ******************************************************************************************************
  388.  
  389. package Cls;
  390.  
  391. import MyInterfaces.Giftable;
  392. import MyInterfaces.Loanable;
  393.  
  394. public class StudentAccount extends Account implements Loanable, Giftable {
  395. private final String name;
  396. private double balance;
  397. private static int com;
  398.  
  399. public StudentAccount(String name, double balance) {
  400. super(name, balance, 1);
  401. this.name = name;
  402. this.balance = balance;
  403. this.com = 1;
  404. }
  405.  
  406. public void getLoan(double sum) {
  407. balance = balance + sum;
  408. this.loan = true;
  409. }
  410.  
  411. public String toString() {
  412. return super.toString() + " ,loan sum : " + loanSum;
  413. }
  414.  
  415. }
  416.  
  417. ******************************************************************************************************
  418.  
  419. package Cls;
  420.  
  421. import MyInterfaces.Loanable;
  422.  
  423. public class BusinessAccount extends Account implements Loanable {
  424. private final String name;
  425. private double balance;
  426. private static int commission;
  427.  
  428. public BusinessAccount(String name, double balance) {
  429. super(name, balance, 3);
  430. this.name = name;
  431. this.balance = balance;
  432. this.com = 3;
  433. }
  434.  
  435.  
  436. public String toString() {
  437. return super.toString() + " Loans : " + loanSum;
  438. }
  439.  
  440. }
  441. ******************************************************************************************************
  442.  
  443. package Cls;
  444.  
  445. import MyInterfaces.Giftable;
  446.  
  447. public class YoungAccount extends Account implements Giftable {
  448. private final String name;
  449. private final double balance;
  450. private static int com;
  451.  
  452. public YoungAccount(String name, double balance) {
  453. super(name, balance, 0);
  454. this.name = name;
  455. this.balance = balance;
  456. this.com = 0;
  457.  
  458. }
  459.  
  460. }
  461. ******************************************************************************************************
  462.  
  463. package Cls;
  464.  
  465. import MyInterfaces.Loanable;
  466. import MyInterfaces.Mortgageable;
  467. import MyInterfaces.Saveable;
  468.  
  469. public class RegularAccount extends Account implements Saveable, Mortgageable, Loanable {
  470. private final String name;
  471. private final double balance;
  472. private static int com = 5;
  473. private double savings = 0;
  474.  
  475. public RegularAccount(String name, double balance) {
  476. super(name, balance, 5);
  477. this.name = name;
  478. this.balance = balance;
  479. }
  480.  
  481. public String toString() {
  482. return super.toString() + " loan sum: " + loanSum + " mortgage sum: " + mortgageSum;
  483. }
  484.  
  485. public void savings(double sum) {
  486. savings = savings + sum;
  487. }
  488.  
  489. }
  490. ******************************************************************************************************
  491.  
  492. package Cls;
  493.  
  494. import java.util.Vector;
  495.  
  496. public class Banker extends Account {
  497. private static Banker theBanker;
  498. Vector<Account> allAccounts = new Vector<Account>();
  499.  
  500. private Banker() {
  501. //System.out.println("the banker was made");
  502. }
  503.  
  504. public Vector<Account> getList() {
  505. return allAccounts;
  506. }
  507.  
  508. public static Banker getInstance() {
  509. //System.out.println("getting instance of Banker");
  510. if (theBanker == null) {
  511. theBanker = new Banker();
  512. // System.out.println("the new banker was made");
  513. }
  514. return theBanker;
  515.  
  516. }
  517.  
  518. public void addAccount(Account account) {
  519. allAccounts.add(account);
  520. }
  521.  
  522. public Account getList(int num) {
  523. return allAccounts.get(num);
  524. }
  525.  
  526. }
  527. ******************************************************************************************************
  528. package MyInterfaces;
  529.  
  530. public interface Giftable {
  531. }
  532.  
  533. *****************************************************
  534. package MyInterfaces;
  535.  
  536. public interface Loanable {
  537.  
  538. }
  539. *****************************************************
  540.  
  541. package MyInterfaces;
  542.  
  543. public interface Mortgageable {
  544. }
  545. *****************************************************
  546. package MyInterfaces;
  547.  
  548. public interface Saveable {
  549. }
  550. *****************************************************
  551. package Tester;
  552.  
  553. //import Cls.Business;
  554.  
  555. import Cls.*;
  556.  
  557. import java.security.AllPermission;
  558. import java.util.*;
  559.  
  560. public class Tester {
  561. public static void main(String[] args) {
  562. int i = 0;
  563. int check = 0;
  564. char b, c, a;
  565. Scanner s = new Scanner(System.in);
  566. Banker b1 = Banker.getInstance();
  567. b1.addAccount(new RegularAccount("testAccount", 0)); // creating a new account using singelton;
  568. b1.addAccount(new RegularAccount("moshe", 20000));
  569. b1.addAccount(new RegularAccount("walla", 3000));
  570. b1.addAccount(new StudentAccount("avi", 15000));
  571. b1.addAccount(new YoungAccount("daniel", 4500));
  572. b1.addAccount(new BusinessAccount("avi", 3500));
  573. b1.getList(1).setLoan(5000);
  574. b1.getList(2).setLoan(4000);
  575. //b1.getList(3).setLoan(3500); //young account cant get a loan.
  576. b1.getList(1).setMortgage(500000);
  577. b1.getList(0).setMortgage(40000);
  578. b1.getList(3).setgift();
  579. //b1.getList(2).setgift();
  580. System.out.println("hi are you a Banker or a client? B/C");
  581. b = s.next().charAt(0);
  582. if (b == 'b' || b == 'B') { // checking who is using the software, banker or client;
  583. b1.bankerOptions(b1.getList());
  584.  
  585. } else if (b == 'c' || b == 'C')
  586. b1.clientOptions(b1.getList());
  587.  
  588. else System.out.println("no such option ...");
  589. }
  590.  
  591. }
Add Comment
Please, Sign In to add comment