-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
194 lines (184 loc) · 8.29 KB
/
Program.cs
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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text.Json;
namespace LibraryManager
{
class Program
{
private const string Menu = @"
Welcome to the Osborn Family Library Management App.
, ,
|\\\\ ////|
| \\\V/// |
| |~~~| |
| |===| |
| |O | |
| | S | |
\ | B| /
\|===|/
'---'
1 - To Add a Book to the Library, type add or 1 and hit Return.
2 - To Checkout a book, type checkout or 2 and hit Return.
3 - To Return a book, type return or 3 and hit Return.
4 - To Remove a book, type remove or 4 and hit Return.
5 - To Exit, type exit or 5 and hit Return.";
static void Main(string[] args)
{
bool keepGoing = true;
//read from JSON file
string jsonFilePath = "data.json";
byte[] json = File.ReadAllBytes(jsonFilePath);
//cast bytes as List<Item>
List<Item> itemsList = JsonSerializer.Deserialize<List<Item>>(json);
//writes itemsList to data.json
void WriteJSON()
{
//serialize
string jsonString = JsonSerializer.Serialize(itemsList);
//write jsonString to JSON file
System.IO.File.WriteAllText("data.json", jsonString);
}
//returns specific entry from a list of matching entries or null if none.
Item GetItem(List<Item> searchResult)
{
if (searchResult.Any())
{
Console.WriteLine("Please enter the ID of the book you would like to select.");
for(int i = 0; i < searchResult.Count; i++)
{
Console.WriteLine($"ID: {i}, Title: {searchResult[i].Title}, Author: {searchResult[i].Author}");
}
int ID = Int32.Parse(Console.ReadLine());
return itemsList.Find(x => x.Title.Equals(searchResult[ID].Title));
} else
{
return null;
}
}
void DisplayDate()
{
DateTime date = DateTime.Now;
Console.WriteLine(date);
}
//Master Loop
while(keepGoing)
{
DisplayDate();
Console.WriteLine(Menu);
var menuInput = Console.ReadLine().ToLower();
switch (menuInput)
{
case "1":
case "add":
{
Console.Clear();
Console.WriteLine("Please Enter the Title of the book you wish to add.");
var newBookTitle = Console.ReadLine();
Console.WriteLine("Please Enter the Author of the book you wish to add.");
var newBookAuthor = Console.ReadLine();
//Cast user input as new Item
Item newItem = new Item
{
Title = newBookTitle,
Author = newBookAuthor,
OnLoan = false,
Loanee = ""
};
//add new item to previous list read from JSON file
itemsList.Add(newItem);
WriteJSON();
Console.Clear();
//success message
Console.WriteLine($"Item Added {newItem.Title}");
break;
}
case "2":
case "checkout":
{
Console.Clear();
Console.WriteLine("Please Enter the Title of the Book you would like to Check out.");
string searchTitle = Console.ReadLine();
List<Item> searchResult = itemsList.FindAll(x => x.Title.ToLower().Contains(searchTitle.ToLower()) && x.OnLoan.Equals(false));
Item itemToLoan = null;
itemToLoan = GetItem(searchResult);
if (itemToLoan != null && itemToLoan.OnLoan == false)
{
Console.WriteLine("Please enter your Full Name");
var name = Console.ReadLine();
itemToLoan.OnLoan = true;
itemToLoan.Loanee = name;
WriteJSON();
Console.Clear();
//success message
Console.WriteLine($"Item Loaned: {itemToLoan.Title} to {itemToLoan.Loanee}");
} else
{
Console.WriteLine("**** That Title is not found or is already Loaned out. Please try another title or come back later. ****");
break;
}
break;
}
case "3":
case "return":
{
Console.Clear();
Console.WriteLine("Please Enter your full name");
string name = Console.ReadLine();
Item itemToReturn = null;
List<Item> itemsOnLoan = itemsList.FindAll(x => x.Loanee.ToLower() == name.ToLower());
if (itemsOnLoan.Any())
{
itemToReturn = GetItem(itemsOnLoan);
if(itemToReturn != null)
{
itemToReturn.Loanee = "";
itemToReturn.OnLoan = false;
WriteJSON();
Console.Clear();
Console.WriteLine($"Book Returned: {itemToReturn.Title}.");
} else
{
Console.WriteLine("Please enter a valid selection.");
}
} else
{
Console.WriteLine($"**** No books found for Loanee: {name}. Please Try another name. ****");
}
break;
}
case "4":
case "remove":
{
Console.Clear();
Console.WriteLine("Please enter the title of the book you would like to remove permanently from the library.");
string searchTitle = Console.ReadLine();
List<Item> searchResult = itemsList.FindAll(x => x.Title.ToLower().Contains(searchTitle.ToLower()));
Item itemToDelete;
itemToDelete = GetItem(searchResult);
itemsList.Remove(itemToDelete);
WriteJSON();
Console.Clear();
Console.WriteLine($"Item Deleted: {itemToDelete.Title}");
break;
}
case "5":
case "exit":
{
Console.Clear();
Console.WriteLine("Goodbye!");
keepGoing = false;
break;
}
default:
{
Console.Clear();
Console.WriteLine("Please enter a valid choice");
break;
}
}
}
}
}
}