-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (189 loc) · 7.6 KB
/
Program.cs
File metadata and controls
206 lines (189 loc) · 7.6 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.IO;
namespace InventoryManagementSystem
{
//Store amount and value
public struct Stats
{
public int Count;
public double Value;
public Stats(int c, double v) { Count = c; Value = v; }
}
//Store product details
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
public Product(string n, double p, int q) { Name = n; Price = p; Quantity = q; }
//Calculates value
public double GetValue() => Price * Quantity;
public void Show()
{
Console.WriteLine($"{Name} - ${Price:F2} x {Quantity} = ${GetValue():F2}");
}
}
public class Inventory
{
private List<Product> items = new List<Product>();
private const string FILE = "inventory.txt";
//Adds product to inventory
public void Add(Product p) { items.Add(p); Console.WriteLine($" Added {p.Name}"); }
public bool Remove(string name)
{
//For loop until product is found
for (int i = 0; i < items.Count; i++)
{
if (items[i].Name.ToLower() == name.ToLower())
{
Console.WriteLine($" Removed {items[i].Name}");
items.RemoveAt(i);
return true;
}
}
return false;
}
// Edits current product details
public bool Edit(string name)
{
foreach (var p in items)
{
if (p.Name.ToLower() == name.ToLower())
{
Console.WriteLine($"\nCurrent: {p.Name} - ${p.Price:F2} x {p.Quantity}");
Console.Write("New Price (press Enter to keep current): $");
string priceInput = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(priceInput))
p.Price = double.Parse(priceInput);
Console.Write("New Quantity (press Enter to keep current): ");
string qtyInput = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(qtyInput))
p.Quantity = int.Parse(qtyInput);
Console.WriteLine($" Updated {p.Name}");
return true;
}
}
return false;
}
//Checks for products, then displays all inventory
public void ShowAll()
{
Console.WriteLine("\n---- INVENTORY ----");
if (items.Count == 0) { Console.WriteLine("Empty"); return; }
foreach (var p in items) p.Show();
}
public Product Find(string name)
{
foreach (var p in items)
if (p.Name.ToLower() == name.ToLower()) return p;
return null;
}
public Stats GetStats()
{
double total = 0;
foreach (var p in items) total += p.GetValue();
return new Stats(items.Count, total);
}
//Save feature
public void Save()
{
try
{
using (var w = new StreamWriter(FILE))
foreach (var p in items)
w.WriteLine($"{p.Name}|{p.Price}|{p.Quantity}");
Console.WriteLine($" Saved {items.Count} items");
}
catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); }
}
//Loads inventory for user
public void Load()
{
if (!File.Exists(FILE)) return;
try
{
items.Clear();
foreach (var line in File.ReadAllLines(FILE))
{
var parts = line.Split('|');
if (parts.Length == 3)
items.Add(new Product(parts[0], double.Parse(parts[1]), int.Parse(parts[2])));
}
Console.WriteLine($" Loaded {items.Count} items...");
}
catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); }
}
}
//Main UI
class Program
{
static void Main()
{
Console.Clear();
Console.WriteLine("\n----- INVENTORY MANAGEMENT SYSTEM -----\n");
var inv = new Inventory();
inv.Load();
bool run = true;
while (run)
{
Console.WriteLine("\n---- MAIN MENU ----\n");
Console.WriteLine(" 1 | Add Product ");
Console.WriteLine(" 2 | Remove Product ");
Console.WriteLine(" 3 | Edit Product ");
Console.WriteLine(" 4 | Find Product ");
Console.WriteLine(" 5 | Show All ");
Console.WriteLine(" 6 | Statistics ");
Console.WriteLine(" 7 | Save & Exit ");
Console.Write("\n Choice: ");
switch (Console.ReadLine())
{
//Define each option
case "1":
Console.WriteLine("\n--- ADD PRODUCT ---");
Console.Write("Name: "); var n = Console.ReadLine();
Console.Write("Price: $"); var p = double.Parse(Console.ReadLine());
Console.Write("Quantity: "); var q = int.Parse(Console.ReadLine());
inv.Add(new Product(n, p, q));
break;
case "2":
Console.WriteLine("\n--- REMOVE PRODUCT ---");
Console.Write("Product Name: ");
if (!inv.Remove(Console.ReadLine()))
Console.WriteLine("Product not found");
break;
case "3":
Console.WriteLine("\n--- EDIT PRODUCT ---");
Console.Write("Product Name: ");
if (!inv.Edit(Console.ReadLine()))
Console.WriteLine("Product not found");
break;
case "4":
Console.WriteLine("\n--- FIND PRODUCT ---");
Console.Write("Name: ");
var found = inv.Find(Console.ReadLine());
if (found != null) { Console.WriteLine(); found.Show(); }
else Console.WriteLine("Not found, try again");
break;
case "5":
inv.ShowAll();
break;
case "6":
var s = inv.GetStats();
Console.WriteLine("\n ----STATISTICS---- \n");
Console.WriteLine($" Total Items: {s.Count,-14}");
Console.WriteLine($" Total Value: ${s.Value,-13:F2}");
break;
case "7":
inv.Save();
Console.WriteLine("\nThank you! Goodbye!");
run = false;
break;
default:
Console.WriteLine("Invalid choice. Try again.");
break;
}
}
}
}
}