-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (64 loc) · 2.36 KB
/
Program.cs
File metadata and controls
70 lines (64 loc) · 2.36 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
using Mission3;
List<FoodItem> foodItems = new List<FoodItem>();
int choice = 0;
while (choice != 4)
{
Console.WriteLine("Choose an option: 1. Add, 2. Delete Food Item, 3. Print food items. 4. Exit");
// Add reprompt for not adding int.
string input = Console.ReadLine();
bool isValid = int.TryParse(input, out choice);
if (!isValid)
{
Console.WriteLine("Please enter a valid number. ");
continue;
}
if (choice == 1)
{
Console.WriteLine("Enter food item name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter food item category: ");
string category = Console.ReadLine();
Console.WriteLine("Enter quantity: ");
int quantity = int.Parse(Console.ReadLine());
if (quantity < 0) // Make sure that quantity is not negative.
{
Console.WriteLine("Quantity cannot be negative.");
continue;
}
Console.WriteLine("Enter expiration date: ");
DateTime expirationDate = DateTime.Parse(Console.ReadLine());
FoodItem newItem = new FoodItem(name, category, quantity, expirationDate);
foodItems.Add(newItem);
Console.WriteLine();
}
if (choice == 2)
{
if (foodItems.Count == 0) // Error handling for no food items.
{
Console.WriteLine("No food items found.");
continue;
}
for (int i = 0; i < foodItems.Count; i++)
{
Console.WriteLine((i + 1) + ": " + foodItems[i].Name); // Add one to make show from 1 instead of from 0.
}
Console.WriteLine("Enter the number of the item to delete:");
int index = int.Parse(Console.ReadLine()) - 1; // Subtract one to remove actual list entry for that number instead of number shown that they selected.
if (index < 0 || index >= foodItems.Count) // Make sure that the number selected is actually a number of item.
{
Console.WriteLine("Invalid selection.");
continue;
}
foodItems.RemoveAt(index);
Console.WriteLine();
}
if (choice == 3)
{
Console.WriteLine();
foreach (FoodItem item in foodItems)
{
Console.WriteLine(item.Name + " - " + item.Category + " - " + item.Quantity + " - " + item.ExpirationDate);
}
Console.WriteLine();
}
}