|
| 1 | +package Day7.Task; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.Scanner; |
| 4 | +public class MainApp1 { |
| 5 | + static HashMap<String, String> userMap = new HashMap<>(); |
| 6 | + static Scanner sc = new Scanner(System.in); |
| 7 | + static BLClass bl = new BLClass(); |
| 8 | + |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + // Add some users to the userMap |
| 12 | + userMap.put("user1", "password1"); |
| 13 | + userMap.put("user2", "password2"); |
| 14 | + |
| 15 | + |
| 16 | + boolean status = false; |
| 17 | + while (!status) { |
| 18 | + displayMenu(); |
| 19 | + int choice = sc.nextInt(); |
| 20 | + switch (choice) { |
| 21 | + case 1: |
| 22 | + login(); |
| 23 | + break; |
| 24 | + case 2: |
| 25 | + buyProduct(); |
| 26 | + break; |
| 27 | + case 3: |
| 28 | + cancelProduct(); |
| 29 | + break; |
| 30 | + case 4: |
| 31 | + bl.displayAllProducts(); |
| 32 | + break; |
| 33 | + case 5: |
| 34 | + logout(); |
| 35 | + break; |
| 36 | + case 6: |
| 37 | + status = true; |
| 38 | + break; |
| 39 | + default: |
| 40 | + System.out.println("Invalid choice"); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void displayMenu() { |
| 46 | + System.out.println("================================="); |
| 47 | + System.out.println("1. Log in"); |
| 48 | + System.out.println("2. Buy product"); |
| 49 | + System.out.println("3. Cancel product"); |
| 50 | + System.out.println("4. Display all products"); |
| 51 | + System.out.println("5. Sign out"); |
| 52 | + System.out.println("6. Exit"); |
| 53 | + System.out.println("=================================="); |
| 54 | + System.out.print("Enter your choice: "); |
| 55 | + } |
| 56 | + |
| 57 | + private static void login() { |
| 58 | + System.out.print("Enter username: "); |
| 59 | + String username = sc.next(); |
| 60 | + System.out.print("Enter password: "); |
| 61 | + int password=sc.nextInt(); |
| 62 | + if (userMap.containsKey(username)) { |
| 63 | + userMap.get(username); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + System.out.println("Login successful"); |
| 68 | + } { |
| 69 | + System.out.println("Invalid username or password"); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private static void buyProduct() { |
| 74 | + int loggedInUser=0; |
| 75 | + if (loggedInUser == 0) { |
| 76 | + System.out.print("Enter product name: "); |
| 77 | + String productName = sc.next(); |
| 78 | + System.out.print("Enter product price: "); |
| 79 | + double productPrice = sc.nextDouble(); |
| 80 | + bl.buyProduct(productName, productPrice); |
| 81 | + } else { |
| 82 | + System.out.println("You need to log in first"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static void cancelProduct() { |
| 87 | + int loggedInUser=0; |
| 88 | + if (loggedInUser == 0) { |
| 89 | + System.out.print("Enter product name: "); |
| 90 | + String productName = sc.next(); |
| 91 | + bl.cancelProduct(productName); |
| 92 | + } else { |
| 93 | + System.out.println("You need to log in first"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void logout() { |
| 98 | + System.out.println("Logged out"); |
| 99 | + } |
| 100 | + } |
0 commit comments