Advertisement
coffeebeforecode

Java class 4 Feb DA5 Q3

Feb 6th, 2022
1,096
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 KB | None | 1 0
  1. package lab_class_04_Feb_ex5_q3;
  2.  
  3. import java.util.*;
  4.  
  5. public class Devansh20BCE0410 {
  6.  
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         Scanner ReadInput = new Scanner(System.in);
  10.         ArrayList<customer> Customers = new ArrayList<customer>();
  11.         int choice;
  12.         boolean flag = true;
  13.         while(flag) {
  14.             // Menu
  15.             System.out.println("\n\t  Welcome to Example Bank!\n\tBy Devansh Sehgal 20BCE0410\n");
  16.             System.out.println("1. Open an account");
  17.             System.out.println("2. Deposit money");
  18.             System.out.println("3. See account details");
  19.             System.out.println("4. Change address");
  20.             System.out.println("5. Change phone number");
  21.             System.out.println("6. Change email ID");
  22.             System.out.println("7. View all costumers (requires admin accesss)");
  23.             System.out.println("8. Generate list (requires admin accesss)");
  24.             System.out.println("9. Exit");
  25.             System.out.print(">> ");
  26.            
  27.             choice = ReadInput.nextInt();
  28.             int index;
  29.             // switch
  30.             switch(choice) {
  31.             case 1:
  32.                 // Create account
  33.                 Customers.add(new customer());
  34.                 break;
  35.             case 2:
  36.                 // Deposit
  37.                 index = login(Customers);
  38.                 if (index >= 0) {
  39.                     Customers.get(index).deposit();
  40.                 }
  41.                 break;
  42.             case 3:
  43.                 // Show Account details
  44.                 index = login(Customers);
  45.                 if (index >= 0) {
  46.                     Customers.get(index).showCustomer();
  47.                 }
  48.                 break;
  49.             case 4:
  50.                 // Change Address
  51.                 index = login(Customers);
  52.                 if (index >= 0) {
  53.                     Customers.get(index).changeAddress();
  54.                 }
  55.                 break;
  56.             case 5:
  57.                 // Change phone number
  58.                 index = login(Customers);
  59.                 if (index >= 0) {
  60.                     Customers.get(index).changePhone();
  61.                 }
  62.                 break;
  63.             case 6:
  64.                 // Change email
  65.                 index = login(Customers);
  66.                 if (index >= 0) {
  67.                     Customers.get(index).changeEmail();
  68.                 }
  69.                 break;
  70.             case 7:
  71.                 // view all customers
  72.                 if (customer.adminLogin()) {
  73.                     for (customer c : Customers) {
  74.                         System.out.println();
  75.                         c.showCustomer();
  76.                     }
  77.                 }
  78.                 break;
  79.             case 8:
  80.                 // Generate list
  81.                 if (customer.adminLogin()) {
  82.                     System.out.println("Input max balance:");
  83.                     float maxBalance = ReadInput.nextFloat();
  84.                     for (customer c : Customers) {
  85.                         if (c.getBalance() <= maxBalance) {
  86.                             System.out.println();
  87.                             c.showCustomer();
  88.                         }
  89.                     }
  90.                 }
  91.                 break;
  92.             case 9:
  93.                 flag = false;
  94.                 break;
  95.             default:
  96.                 System.out.println("Invalid choice, try again!");
  97.                 break;
  98.             }
  99.            
  100.         }
  101.        
  102.         System.out.println();
  103.         System.out.println("\tThank you for using our service!");
  104.        
  105.         ReadInput.close();
  106.     }
  107.  
  108.     static int login(ArrayList<customer> Customers) {
  109.         Scanner login_sc = new Scanner(System.in);
  110.         System.out.println("\nInput ID:");
  111.         String id = login_sc.nextLine();
  112.         System.out.println("\nInput PIN:");
  113.         String pin = login_sc.nextLine();
  114.        
  115.         for(customer c : Customers) {
  116.             if (id.equals(c.getID())) {
  117.                 if (c.checkPIN(pin)) {
  118.                     return Customers.indexOf(c);
  119.                 }
  120.                 else {
  121.                     System.out.println("Wrong PIN!");
  122.                     return -1;
  123.                 }
  124.             }
  125.         }
  126.        
  127.         System.out.println("Customer not found!");
  128.         return -1;
  129.     }
  130. }
  131.  
  132. class customer{
  133.     private static String adminID = "Admin";
  134.     private static String adminPass = "1234";
  135.    
  136.     protected String ID;
  137.     protected String email;
  138.     private String pin;
  139.     protected float balance;
  140.     protected String address;
  141.     protected String phone;
  142.    
  143.     public customer() {
  144.         Scanner openacc_sc = new Scanner(System.in);
  145.         System.out.println();
  146.         System.out.println("Input ID:");
  147.         this.ID = openacc_sc.nextLine();
  148.         System.out.println("Input PIN:");
  149.         this.pin = openacc_sc.nextLine();
  150.         System.out.println("Input email:");
  151.         this.email = openacc_sc.nextLine();
  152.         System.out.println("Input Address:");
  153.         this.address = openacc_sc.nextLine();
  154.         System.out.println("Input phone number:");
  155.         this.phone = openacc_sc.nextLine();
  156.         System.out.println("Input initial balance:");
  157.         this.balance = openacc_sc.nextFloat();
  158.         System.out.println("\nAccount Created!");
  159.     }
  160.    
  161.     public String getID() {
  162.         return this.ID;
  163.     }
  164.    
  165.     public float getBalance() {
  166.         return this.balance;
  167.     }
  168.    
  169.     public boolean checkPIN(String pin) {
  170.         return this.pin.equals(pin);
  171.     }
  172.    
  173.     public void deposit() {
  174.         Scanner deposit_sc = new Scanner(System.in);
  175.         System.out.println();
  176.         System.out.println("Current Balance: " + this.balance);
  177.         System.out.println("\nInput deposit amount: ");
  178.         this.balance += deposit_sc.nextFloat();
  179.         System.out.println("\nAmount Deposited!\nNew Balance: " + this.balance);
  180.     }
  181.    
  182.     public void changeAddress() {
  183.         Scanner chAdd_sc = new Scanner(System.in);
  184.         System.out.println();
  185.         System.out.println("Old Address:");
  186.         System.out.println(this.address);
  187.         System.out.println("\nInput new address:");
  188.         this.address = chAdd_sc.nextLine();
  189.         System.out.println("\nAddress changed!");
  190.     }
  191.    
  192.     public void changePhone() {
  193.         Scanner chPh_sc = new Scanner(System.in);
  194.         System.out.println();
  195.         System.out.println("Old phone number:");
  196.         System.out.println(this.phone);
  197.         System.out.println("\nInput new phone number:");
  198.         this.phone = chPh_sc.nextLine();
  199.         System.out.println("\nPhone number changed!");
  200.     }
  201.    
  202.     public void changeEmail() {
  203.         Scanner chEm_sc = new Scanner(System.in);
  204.         System.out.println();
  205.         System.out.println("Old email:");
  206.         System.out.println(this.email);
  207.         System.out.println("\nInput new email:");
  208.         this.email = chEm_sc.nextLine();
  209.         System.out.println("\nEmail changed!");
  210.     }
  211.    
  212.     public void showCustomer() {
  213.         System.out.println();
  214.         System.out.println("ID: " + this.ID);
  215.         System.out.println("Email: " + this.email);
  216.         System.out.println("Phone: " + this.phone);
  217.         System.out.println("Address: " + this.address);
  218.         System.out.println("Balance: " + this.balance);
  219.     }
  220.    
  221.     public static boolean adminLogin() {
  222.         Scanner adminLogin_sc = new Scanner(System.in);
  223.         System.out.println();
  224.         System.out.println("Input Admin ID:");
  225.         String inp_id = adminLogin_sc.nextLine();
  226.         System.out.println("Input Admin password:");
  227.         String inp_pin = adminLogin_sc.nextLine();
  228.        
  229.         if (adminID.equals(inp_id) && adminPass.equals(inp_pin)) {
  230.             return true;
  231.         }
  232.         else {
  233.             System.out.println("Admin Login Failed!");
  234.             return false;
  235.         }
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement