Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Comparator;
- public class Bank {
- AcctSorter acctSorter;
- private static List<BankCustomer> customers = new ArrayList<>();
- // Runs the menu, where the user is allowed to input a choice.
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- loop: while (true) {
- System.out.println("Menu: ");
- System.out.println("1. Add Bank Customer");
- System.out.println("2. Remove a Bank Customer");
- System.out.println("3. Print all Bank Customers");
- System.out.println("0. Exit\n");
- System.out.print("Choice: ");
- boolean keepAsking = true;
- int choice = input.nextInt();
- input.nextLine();
- while (keepAsking) {
- try {
- switch (choice) {
- case 0:
- break loop;
- case 1:
- System.out.print("Enter account number: ");
- int accountNumber = input.nextInt();
- input.nextLine();
- System.out.print("Enter name: ");
- String name = input.nextLine();
- System.out.print("Enter balance: ");
- double balance = input.nextDouble();
- input.nextLine();
- keepAsking = false;
- try {
- add(new BankCustomer(accountNumber, balance, name));
- } catch (BCException e) {
- System.out.println(e.getMessage());
- System.out.println("Exiting back to menu\n");
- }
- break;
- case 2:
- System.out.print("Enter an account number to remove: ");
- remove(input.nextInt());
- break;
- case 3:
- printCustomers();
- break;
- default:
- System.out.println("Invalid option, try again\n");
- }
- }
- // Throws an exception if the user inputs in the incorrect data type.
- catch (InputMismatchException ime) {
- input = new Scanner(System.in);
- System.out.println("Please enter a number.\n");
- }
- }
- }
- }
- public static void add(BankCustomer customer) {
- int accountNumber = customer.getAcctNumber();
- int index = 0;
- for (int i = 0; i < customers.size(); i++) {
- if (accountNumber < customers.get(i).getAcctNumber()) {
- index = i;
- }
- }
- customers.add(index, customer);
- }
- public static void remove(int accountNumber) {
- for (int i = 0; i < customers.size(); i++) {
- if (customers.get(i).getAcctNumber() == accountNumber) {
- customers.remove(i);
- return;
- }
- }
- System.out.println("Customer could not be found");
- }
- public static void printCustomers() {
- for (BankCustomer customer : customers) {
- System.out.println("Account number: " + customer.getAcctNumber());
- System.out.println("Name: " + customer.getName());
- System.out.println("Balance: " + customer.getBalance() + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement