Advertisement
Ahmed_Negm

Untitled

May 24th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class Menu {
  7.     File fi = new File("/home/ahmednegm/Desktop/Practical2/input.txt");
  8.     BufferedWriter fw = new BufferedWriter(new FileWriter("/home/ahmednegm/Desktop/Practical2/output.txt"));
  9. Scanner in = new Scanner(fi);
  10. Bank bank = new Bank();
  11. boolean exit;
  12.  
  13.     public Menu() throws IOException {
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         Menu menu = new Menu();
  18.         menu.runMenu();
  19.     }
  20.     public void runMenu() throws IOException {
  21.         String welcome = " ********************************\n *                              *\n *     Welcome to our Bank      *\n *                              *\n ********************************\n";
  22.         fw.write(welcome);
  23.         int startAccounts ;
  24.         while(true) {
  25. //            System.out.print("How many Accounts you want to create?: ");
  26.             startAccounts = in.nextInt();
  27.             if(startAccounts >0){
  28.                 break;
  29.             }else{
  30.                 fw.write("Error, Invalid input.");
  31.                 fw.close();
  32.                 System.exit(0);
  33. //                System.out.println("You should enter a positive number.");
  34.             }
  35.  
  36.         }
  37.         for (int i=0; i<startAccounts;i++){
  38. //            System.out.println("Enter the information for account no."+(i+1)+".");
  39.             CreateAnAccount();
  40.         }
  41.         while(!exit){
  42.  
  43. //            System.out.println("Please make a selection:");
  44. //            System.out.println("1) Create a new Account.");
  45. //            System.out.println("2) Make a Deposit.");
  46. //            System.out.println("3) Make a Withdrawal.");
  47. //            System.out.println("4) List account balance.");
  48. //            System.out.println("0) Exit.");
  49.             int choice = -1;
  50.             do {
  51. //                System.out.print("Enter your choice:");
  52.                 try {
  53.                     choice = in.nextInt();
  54.                 }catch (InputMismatchException e){
  55.                     fw.write("Error, Invalid input.");
  56.                     fw.close();
  57.                     System.exit(0);
  58. //                    System.out.println("Invalid selection, Numbers only.");
  59.                     in.next();
  60.                     continue;
  61.                 }
  62. //                if(choice <0 || choice >4) System.out.println("Choice out of range, Choose again.");
  63.             }while (choice <0 || choice >4);
  64.  
  65.             switch (choice){
  66.                 case 0:
  67.                     fw.write("*Thank you for using our Bank.*");
  68.                     fw.close();
  69.                     System.exit(0);
  70.                     break;
  71.                 case 1:
  72.                     CreateAnAccount();
  73.                     break;
  74.                 case 2:
  75.                     MakeADeposit();
  76.                     break;
  77.                 case 3:
  78.                     MakeAWithdrawal();
  79.                     break;
  80.                 case 4:
  81.                     ListBalances();
  82.                     break;
  83.                 default:
  84. //                    System.out.println("Unknown Error.");
  85.             }
  86.         }
  87.     }
  88.  
  89.  
  90.  
  91.  
  92.     private void CreateAnAccount() throws IOException {
  93.         String firstname,lastname,pass,accountType = null;
  94.         double initialDeposit = 0;
  95.         boolean valid = false;
  96.         while (!valid){
  97. //            System.out.print("Please enter an account type (checking / savings):");
  98.             accountType = in.next();
  99.             in.nextLine();
  100.             if(accountType.equalsIgnoreCase("checking") || accountType.equalsIgnoreCase("savings")){
  101.                 valid = true;
  102.             }else{
  103.                 fw.write("Error, Invalid account type selected, enter checking or savings.");
  104.                 fw.close();
  105.                 System.exit(0);
  106. //                System.out.println("Invalid account type selected, enter checking or savings.");
  107.             }
  108.         }
  109. //        System.out.print("Enter your first name : ");
  110.         firstname = in.next();
  111.         in.nextLine();
  112. //        System.out.print("Enter your last name : ");
  113.         lastname = in.next();
  114.         in.nextLine();
  115. //        System.out.print("Enter your password : ");
  116.         pass = in.next();
  117.         in.nextLine();
  118.         valid = false;
  119.         while(!valid){
  120. //            System.out.print("Please enter an initial deposit : ");
  121.             try {
  122.                 initialDeposit = in.nextDouble();
  123.             }catch (InputMismatchException e){
  124.                 fw.write("Error, Deposit must be a number.");
  125.                 fw.close();
  126.                 System.exit(0);
  127. //                System.out.println("Deposit must be a number.");
  128.                 in.next();
  129.             }
  130.             if (accountType.equalsIgnoreCase("checking")){
  131.                 if(initialDeposit < 100){
  132.                     fw.write("Error, checking account require a minimum of of $100 to open.");
  133.                     fw.close();
  134.                     System.exit(0);
  135. //                    System.out.println("checking account require a minimum of of $100 to open.");
  136.                 }else{
  137.                     valid = true;
  138.                 }
  139.             }else if (accountType.equalsIgnoreCase("savings")){
  140.                 if(initialDeposit < 50){
  141.                     fw.write("Error, checking account require a minimum of of $50 to open.");
  142.                     fw.close();
  143.                     System.exit(0);
  144. //                    System.out.println("checking account require a minimum of of $50 to open.");
  145.                 }else{
  146.                     valid = true;
  147.                 }
  148.             }
  149.     }
  150.         Account account;
  151.         if(accountType.equalsIgnoreCase("checking")){
  152.             account = new Checking(initialDeposit);
  153.         }else{
  154.             account = new Savings(initialDeposit);
  155.         }
  156.         Customer customer = new Customer(firstname,lastname,pass,account);
  157.         bank.addCustomer(customer);
  158.  
  159.  
  160.     }
  161.     private void MakeADeposit() throws IOException {
  162.         int account = selectAccount();
  163.         if(account >=0) {
  164. //            System.out.print("How much would you like to deposit? : ");
  165.             double amount = 0;
  166.             amount = in.nextDouble();
  167.             bank.getCustomer(account).getAccount().deposit(amount);
  168.         }
  169.  
  170.     }
  171.     private void MakeAWithdrawal() throws IOException {
  172.         int account = selectAccount();
  173.         if(account >=0) {
  174. //            System.out.print("How much would you like to withdraw? : ");
  175.             double amount = 0;
  176.             amount = in.nextDouble();
  177.             bank.getCustomer(account).getAccount().withdraw(amount);
  178.         }
  179.     }
  180.     private void ListBalances() throws IOException {
  181.         int account = selectAccount();
  182.         if(account >=0) {
  183.             String info= null;
  184.             fw.write("Name: "+bank.getCustomer(account).getFullname()+"\n");
  185.             info= String.valueOf((bank.getCustomer(account).getAccount()));
  186.             fw.write(info);
  187. //            fw.close();
  188.         }
  189.     }
  190.     private int selectAccount() throws IOException {
  191.         ArrayList<Customer> customers = bank.getCustomers();
  192.         if(customers.size() <= 0){
  193.             fw.write("No customers at the bank.");
  194.             fw.close();
  195.             System.exit(0);
  196. //            System.out.println("No customers at the bank.");
  197.             return -1;
  198.         }
  199. //        System.out.println("Select an account: ");
  200.         for (int i=0; i<customers.size(); i++){
  201. //            System.out.println((i+1) +") " +customers.get(i).basicInfo());
  202.         }
  203.         int account =0;
  204. //        System.out.print("Please enter your selection: ");
  205.         account = in.nextInt()-1;
  206.         if(account >= customers.size() || account < 0){
  207.             fw.write("Invalid Customer number.");
  208.             fw.close();
  209.             System.exit(0);
  210. //            System.out.println("Invalid Customer.");
  211.             return -1;
  212.         }
  213.         return account;
  214.     }
  215.  
  216.  
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement