Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Comparator;
  3.  
  4. public class Bank {
  5.     AcctSorter acctSorter;
  6.  
  7.     private static List<BankCustomer> customers = new ArrayList<>();
  8.  
  9.     // Runs the menu, where the user is allowed to input a choice.
  10.     public static void main(String[] args) {
  11.         Scanner input = new Scanner(System.in);
  12.  
  13.             loop: while (true) {
  14.  
  15.                 System.out.println("Menu: ");
  16.                 System.out.println("1. Add Bank Customer");
  17.                 System.out.println("2. Remove a Bank Customer");
  18.                 System.out.println("3. Print all Bank Customers");
  19.                 System.out.println("0. Exit\n");
  20.                 System.out.print("Choice: ");
  21.  
  22.                 boolean keepAsking = true;
  23.  
  24.                     int choice = input.nextInt();
  25.                     input.nextLine();
  26.                 while (keepAsking) {
  27.                     try {
  28.                         switch (choice) {
  29.                             case 0:
  30.                                 break loop;
  31.  
  32.                             case 1:
  33.                                 System.out.print("Enter account number: ");
  34.                                 int accountNumber = input.nextInt();
  35.                                 input.nextLine();
  36.                                 System.out.print("Enter name: ");
  37.                                 String name = input.nextLine();
  38.                                 System.out.print("Enter balance: ");
  39.                                 double balance = input.nextDouble();
  40.                                 input.nextLine();
  41.                                 keepAsking = false;
  42.  
  43.                                 try {
  44.                                     add(new BankCustomer(accountNumber, balance, name));
  45.                                 } catch (BCException e) {
  46.                                     System.out.println(e.getMessage());
  47.                                     System.out.println("Exiting back to menu\n");
  48.                                 }
  49.                                 break;
  50.  
  51.                             case 2:
  52.                                 System.out.print("Enter an account number to remove: ");
  53.                                 remove(input.nextInt());
  54.                                 break;
  55.  
  56.                             case 3:
  57.                                 printCustomers();
  58.                                 break;
  59.  
  60.                             default:
  61.                                 System.out.println("Invalid option, try again\n");
  62.                         }
  63.  
  64.  
  65.                     }
  66.  
  67.                     // Throws an exception if the user inputs in the incorrect data type.
  68.                     catch (InputMismatchException ime) {
  69.                         input = new Scanner(System.in);
  70.                         System.out.println("Please enter a number.\n");
  71.                     }
  72.  
  73.                 }
  74.             }
  75.  
  76.     }
  77.  
  78.     public static void add(BankCustomer customer) {
  79.         int accountNumber = customer.getAcctNumber();
  80.         int index = 0;
  81.         for (int i = 0; i < customers.size(); i++) {
  82.             if (accountNumber < customers.get(i).getAcctNumber()) {
  83.                 index = i;
  84.             }
  85.         }
  86.         customers.add(index, customer);
  87.     }
  88.  
  89.     public static void remove(int accountNumber) {
  90.         for (int i = 0; i < customers.size(); i++) {
  91.             if (customers.get(i).getAcctNumber() == accountNumber) {
  92.                 customers.remove(i);
  93.                 return;
  94.             }
  95.         }
  96.         System.out.println("Customer could not be found");
  97.     }
  98.  
  99.     public static void printCustomers() {
  100.         for (BankCustomer customer : customers) {
  101.             System.out.println("Account number: " + customer.getAcctNumber());
  102.             System.out.println("Name: " + customer.getName());
  103.             System.out.println("Balance: " + customer.getBalance() + "\n");
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement