-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventorySystem.java
More file actions
137 lines (106 loc) · 3.42 KB
/
InventorySystem.java
File metadata and controls
137 lines (106 loc) · 3.42 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.*;
class Item {
private String name;
private int quantity;
private double price;
public Item(String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String toString() {
return "Item{name='" + name + "', quantity=" + quantity + ", price=" + price + "}";
}
}
class Inventory {
private List<Item> items;
public Inventory() {
items = new ArrayList<>();
}
public void addItem(Item item) {
items.add(item);
System.out.println("Item added successfully: " + item);
}
public void removeItem(String name) {
Item itemToRemove = null;
for (Item item : items) {
if (item.getName().equalsIgnoreCase(name)) {
itemToRemove = item;
break;
}
}
if (itemToRemove != null) {
items.remove(itemToRemove);
System.out.println("Item removed successfully.");
} else {
System.out.println("Item not found in inventory.");
}
}
public void viewItems() {
if (items.isEmpty()) {
System.out.println("Inventory is empty.");
} else {
System.out.println("\nInventory items:");
for (Item item : items) {
System.out.println(item);
}
}
}
}
public class InventorySystem {
public static void main(String[] args) {
Inventory inventory = new Inventory();
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("\nInventory System");
System.out.println("1. Add Item");
System.out.println("2. Remove Item");
System.out.println("3. View Items");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter item name: ");
String name = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
System.out.print("Enter price: ");
double price = scanner.nextDouble();
scanner.nextLine();
Item item = new Item(name, quantity, price);
inventory.addItem(item);
break;
case 2:
System.out.print("Enter item name to remove: ");
String removeName = scanner.nextLine();
inventory.removeItem(removeName);
break;
case 3:
inventory.viewItems();
break;
case 4:
exit = true;
System.out.println("Exiting Inventory System.");
break;
default:
System.out.println("Invalid choice.");
}
}
scanner.close();
}
}