Store product list under product
This commit is contained in:
parent
e777427de8
commit
93f71317df
4 changed files with 36 additions and 33 deletions
10
Product.cs
10
Product.cs
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace productlist
|
||||
|
@ -9,11 +10,20 @@ namespace productlist
|
|||
public int version;
|
||||
public string description;
|
||||
|
||||
public static List<Product> allProducts = new List<Product>();
|
||||
|
||||
public Product(string name, int version, string description) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
this.description = description;
|
||||
|
||||
allProducts.Add (this);
|
||||
}
|
||||
|
||||
~Product() {
|
||||
allProducts.Remove (this);
|
||||
}
|
||||
|
||||
public static Product fromPrompt() {
|
||||
string pName = UI.getAnswer ("Product Name", UI.validateString);
|
||||
int pVersion = Int32.Parse(UI.getAnswer ("Version", UI.validateInt));
|
||||
|
|
17
Program.cs
17
Program.cs
|
@ -7,7 +7,22 @@ namespace productlist
|
|||
public static void Main (string[] args)
|
||||
{
|
||||
Console.WriteLine ("Welcome to the Product List");
|
||||
UI.promptForCommand ();
|
||||
UI.inputType type = UI.promptForCommand ();
|
||||
|
||||
switch (type) {
|
||||
case UI.inputType.create:
|
||||
Product.fromPrompt ();
|
||||
break;
|
||||
case UI.inputType.list:
|
||||
Storage.List ();
|
||||
break;
|
||||
case UI.inputType.dump:
|
||||
Storage.dump ();
|
||||
break;
|
||||
case UI.inputType.load:
|
||||
Storage.load ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
15
Storage.cs
15
Storage.cs
|
@ -6,19 +6,14 @@ namespace productlist
|
|||
{
|
||||
public static class Storage
|
||||
{
|
||||
private static List<Product> productStorageList = new List<Product>();
|
||||
private static readonly string filePath = @"data.json";
|
||||
|
||||
public static void Add(Product p) {
|
||||
productStorageList.Add (p);
|
||||
}
|
||||
|
||||
public static void dump() {
|
||||
if (productStorageList.Count < 1) {
|
||||
if (Product.allProducts.Count < 1) {
|
||||
Console.WriteLine ("Nothing to dump");
|
||||
return;
|
||||
}
|
||||
string json = JsonConvert.SerializeObject (productStorageList);
|
||||
string json = JsonConvert.SerializeObject (Product.allProducts);
|
||||
System.IO.File.WriteAllText (filePath, json);
|
||||
|
||||
}
|
||||
|
@ -29,15 +24,15 @@ namespace productlist
|
|||
return;
|
||||
}
|
||||
string text = System.IO.File.ReadAllText (filePath);
|
||||
productStorageList = JsonConvert.DeserializeObject<List<Product>> (text);
|
||||
Product.allProducts = JsonConvert.DeserializeObject<List<Product>> (text);
|
||||
}
|
||||
|
||||
private static void listProducts() {
|
||||
if (productStorageList.Count < 1) {
|
||||
if (Product.allProducts.Count < 1) {
|
||||
Console.WriteLine ("No Products found!");
|
||||
return;
|
||||
}
|
||||
foreach (Product p in productStorageList) {
|
||||
foreach (Product p in Product.allProducts) {
|
||||
p.display ();
|
||||
}
|
||||
}
|
||||
|
|
27
UI.cs
27
UI.cs
|
@ -4,7 +4,7 @@ namespace productlist
|
|||
{
|
||||
public static class UI
|
||||
{
|
||||
private enum inputType {
|
||||
public enum inputType {
|
||||
create,
|
||||
delete,
|
||||
list,
|
||||
|
@ -49,27 +49,10 @@ namespace productlist
|
|||
return input;
|
||||
}
|
||||
|
||||
public static void promptForCommand() {
|
||||
while (true) {
|
||||
Console.Write ("Enter command: ");
|
||||
string command = Console.ReadLine ();
|
||||
inputType type = decodeInputType (command);
|
||||
switch (type) {
|
||||
case inputType.create:
|
||||
Product p = Product.fromPrompt ();
|
||||
Storage.Add (p);
|
||||
break;
|
||||
case inputType.list:
|
||||
Storage.List ();
|
||||
break;
|
||||
case inputType.dump:
|
||||
Storage.dump ();
|
||||
break;
|
||||
case inputType.load:
|
||||
Storage.load ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static inputType promptForCommand() {
|
||||
Console.Write ("Enter command: ");
|
||||
string command = Console.ReadLine ();
|
||||
return decodeInputType (command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue