Advertisement
Ahmed_Negm

Untitled

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