-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathMedicalStore.java
111 lines (93 loc) · 3.23 KB
/
MedicalStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MedicalStore {
public static class Product {
private int id;
private String name;
private double price;
private int quantity;
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
private List<Product> products;
public MedicalStore() {
products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
}
public void listProducts() {
System.out.println("Products available:");
for (Product product : products) {
System.out.println(product.getId() + ". " + product.getName() + " - $" + product.getPrice());
}
}
public Product findProductById(int id) {
for (Product product : products) {
if (product.getId() == id) {
return product;
}
}
return null;
}
public void sellProduct(int productId, int quantity) {
Product product = findProductById(productId);
if (product != null && product.getQuantity() >= quantity) {
product.setQuantity(product.getQuantity() - quantity);
System.out.println(quantity + " " + product.getName() + " sold for $" + (product.getPrice() * quantity));
} else {
System.out.println("Product not available or insufficient quantity.");
}
}
public static void main(String[] args) {
MedicalStore store = new MedicalStore();
store.addProduct(new Product(1, "Painkiller", 5.99, 50));
store.addProduct(new Product(2, "Bandages", 3.49, 100));
store.addProduct(new Product(3, "Vitamins", 8.99, 30);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n1. List Products");
System.out.println("2. Sell Product");
System.out.println("3. Exit");
System.out.print("Select an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
store.listProducts();
break;
case 2:
System.out.print("Enter product ID: ");
int productId = scanner.nextInt();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
store.sellProduct(productId, quantity);
break;
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}