Skip to content

Commit 54e13e3

Browse files
authoredApr 25, 2023
Add files via upload
0 parents  commit 54e13e3

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
 

‎BLClass.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Day7.Task;
2+
3+
import java.util.HashMap;
4+
5+
public class BLClass {
6+
7+
private HashMap<String, Double> productMap = new HashMap<String, Double>();
8+
9+
public void buyProduct(String productName, double productPrice) {
10+
productMap.put(productName, productPrice);
11+
System.out.println("Product bought: " + productName);
12+
}
13+
14+
public void cancelProduct(String productName) {
15+
if (productMap.containsKey(productName)) {
16+
productMap.remove(productName);
17+
System.out.println("Product canceled: " + productName);
18+
} else {
19+
System.out.println("Product not found");
20+
}
21+
}
22+
23+
public void displayAllProducts() {
24+
for (String productName : productMap.keySet()) {
25+
System.out.println(productName + ": " + productMap.get(productName));
26+
}
27+
}
28+
}
29+

‎Customer.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Day7.Task;
2+
3+
public class Customer {
4+
public String getName() {
5+
return name;
6+
}
7+
8+
public void setName(String name) {
9+
this.name = name;
10+
}
11+
12+
public int getPassword() {
13+
return password;
14+
}
15+
16+
public void setPassword(int password) {
17+
this.password = password;
18+
}
19+
20+
public Customer(String name, int password) {
21+
this.name = name;
22+
this.password = password;
23+
}
24+
25+
private String name;
26+
private int password;
27+
28+
@Override
29+
public String toString() {
30+
return "Customer{" +
31+
"name='" + name + '\'' +
32+
", password=" + password +
33+
'}';
34+
}
35+
}

‎MainApp1.java

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)
Please sign in to comment.