Advertisement
Ahmed_Negm

Untitled

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