mmayoub

Bank, Tester class (main)

Aug 10th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package Tester;
  2.  
  3. import java.time.LocalDate;
  4. import java.util.Random;
  5.  
  6. import BankAccounts.Account;
  7. import BankAccounts.LoanableAccount;
  8. import BankPkg.AccountType;
  9. import BankPkg.Bank;
  10.  
  11. public class Tester {
  12.     static Random rnd = new Random();
  13.     static AccountType[] typesArray = AccountType.values();
  14.     static Bank myBank = new Bank();
  15.  
  16.     public static void CreateAccounts(Bank aBank, int n) {
  17.         for (int i = 1; i <= n; i += 1) {
  18.             int personId;
  19.  
  20.             boolean clientCreated = false;
  21.             do {
  22.                 System.out.println("trying to create a new account!");
  23.                 String personName = "Name" + (char) (60 + rnd.nextInt(35));
  24.                 personId = 10000000 + rnd.nextInt(900000000);
  25.                 LocalDate birthDate = LocalDate.of(1988 + rnd.nextInt(18),
  26.                         1 + rnd.nextInt(12), 1 + rnd.nextInt(28));
  27.  
  28.                 clientCreated = aBank
  29.                         .addClient(personName, personId, birthDate);
  30.             } while (!clientCreated);
  31.  
  32.             int accountNo;
  33.             AccountType type;
  34.             do {
  35.                 type = typesArray[rnd.nextInt(typesArray.length)];
  36.                 accountNo = aBank.addAccount(personId, type);
  37.             } while (accountNo == -1);
  38.  
  39.             System.out.printf("new account created (%d of %d)\n", i, n);
  40.         }
  41.     }
  42.  
  43.     public static void printStat() {
  44.         System.out.println("Number of accounts from each type:");
  45.         for (AccountType item : typesArray) {
  46.             System.out.printf("%3d of type %s\n",
  47.                     Account.getTypeAccountsCounter(item), item.name());
  48.         }
  49.     }
  50.  
  51.     public static void printAccountsList(String title, Account[] accountList) {
  52.         System.out.println(title);
  53.  
  54.         for (Account item : accountList) {
  55.             System.out.println(item);
  56.         }
  57.  
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.  
  62.         CreateAccounts(myBank, 10);
  63.  
  64.         printStat();
  65.  
  66.         Account[] studentsAccounts = myBank
  67.                 .getAccountsByType(AccountType.student);
  68.         printAccountsList("accounts of type student", studentsAccounts);
  69.  
  70.         for (Account item : myBank.getAccounts()) {
  71.             myBank.withdraw(item.getAccountId(), 4000);
  72.             if (item instanceof LoanableAccount) {
  73.                 myBank.giveLoan(item.getAccountId(), 18000, 36);
  74.             }
  75.         }
  76.         printAccountsList("all Bank account", myBank.getAccounts());
  77.  
  78.         Account[] invalid2020 = myBank.getInvalidAgeAccounts(LocalDate.of(2018,
  79.                 12, 31));
  80.         printAccountsList("Invalid Accounts at 1/1/2020", invalid2020);
  81.  
  82.         System.out.println(myBank.getClients("fsdfs"));
  83.  
  84.         for (Account item : myBank.getAccounts()) {
  85.             if (item instanceof LoanableAccount) {
  86.                 myBank.loanPaymentReturn(item.getAccountId());
  87.             }
  88.         }
  89.         printAccountsList("all Bank account", myBank.getAccounts());
  90.  
  91.     }
  92. }
Add Comment
Please, Sign In to add comment