-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.java
More file actions
166 lines (135 loc) · 5.65 KB
/
track.java
File metadata and controls
166 lines (135 loc) · 5.65 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
import java.io.*;
public class track {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String itemName = "";
int itemQuantity = 0;
System.out.println("Offline Inventory tracker");
System.out.println("Open new store or no (y/n)");
String openStore = sc.nextLine();
ArrayList<String> items = new ArrayList<>();
// LOAD FILE INTO ARRAYLIST
File file = new File("store.txt");
if (file.exists()) {
Scanner readFile = new Scanner(file);
while (readFile.hasNextLine()) {
items.add(readFile.nextLine());
}
readFile.close();
}
// 🔹 SHOW ITEMS ALWAYS
for (String item : items) {
System.out.println(item);
String[] parts = item.split(",");
int qty = Integer.parseInt(parts[1].replace("Item quantity:", "").trim());
if (qty <= 1) {
System.out.println("⚠ " + parts[0].replace("Item name:", "").trim() + " is low in stock");
}
}
if (openStore.equals("y")) {
System.out.println("Enter how much items you have");
int itemNums = sc.nextInt();
sc.nextLine();
for (int i = 0; i < itemNums; i++) {
System.out.println("Enter " + (i + 1) + " item name");
itemName = sc.nextLine();
System.out.println("Enter " + (i + 1) + " item quantity");
itemQuantity = sc.nextInt();
sc.nextLine();
addItems(items, itemName, itemQuantity);
}
// 🔹 Show items after adding
for (String item : items) {
System.out.println(item);
String[] parts = item.split(",");
int qty = Integer.parseInt(parts[1].replace("Item quantity:", "").trim());
if (qty <= 1) {
System.out.println("⚠ " + parts[0].replace("Item name:", "").trim() + " is low in stock");
}
}
FileWriter writer = new FileWriter("store.txt", false);
for (String item : items) {
writer.write(item + "\n");
}
writer.close();
}
System.out.println("Do you wanna remove an item (y/n)");
String r = sc.nextLine();
if (r.equals("y")) {
System.out.println("Item name:");
itemName = sc.nextLine();
System.out.println("Item Quantity:");
itemQuantity = sc.nextInt();
sc.nextLine();
removeItem(items, itemName, itemQuantity);
// 🔹 Show items after removing
for (String item : items) {
System.out.println(item);
String[] parts = item.split(",");
int qty = Integer.parseInt(parts[1].replace("Item quantity:", "").trim());
if (qty <= 1) {
System.out.println("⚠ " + parts[0].replace("Item name:", "").trim() + " is low in stock");
}
}
FileWriter writer = new FileWriter("store.txt", false);
for (String item : items) {
writer.write(item + "\n");
}
writer.close();
} else {
System.out.println("Do you wanna edit (y/n): ");
String e = sc.nextLine();
if (e.equals("y")) {
System.out.println("Whats the item you want to edit: ");
itemName = sc.nextLine();
System.out.println("Enter new quantity: ");
int newQuantity = sc.nextInt();
sc.nextLine();
updateQuantity(items, itemName, newQuantity);
// 🔹 Show items after editing
for (String item : items) {
System.out.println(item);
String[] parts = item.split(",");
int qty = Integer.parseInt(parts[1].replace("Item quantity:", "").trim());
if (qty <= 1) {
System.out.println("⚠ " + parts[0].replace("Item name:", "").trim() + " is low in stock");
}
}
FileWriter writer = new FileWriter("store.txt", false);
for (String item : items) {
writer.write(item + "\n");
}
writer.close();
}
}
sc.close();
}
public static void addItems(ArrayList<String> items, String itemName, int itemQuantity) {
items.add("Item name: " + itemName + ", Item quantity: " + itemQuantity);
}
public static void removeItem(ArrayList<String> items, String itemName, int itemQuantity) {
Iterator<String> it = items.iterator();
while (it.hasNext()) {
String item = it.next();
if (item.contains("Item name: " + itemName)
&& item.contains("Item quantity: " + itemQuantity)) {
it.remove();
System.out.println("Item removed");
return;
}
}
System.out.println("Item not found");
}
public static void updateQuantity(ArrayList<String> items, String itemName, int newQuantity) {
for (int i = 0; i < items.size(); i++) {
String item = items.get(i);
if (item.contains("Item name: " + itemName)) {
items.set(i,
"Item name: " + itemName + ", Item quantity: " + newQuantity);
return;
}
}
System.out.println("Item not found");
}
}