Store product list under product

This commit is contained in:
Jake Howard 2016-04-15 15:27:33 +01:00
parent e777427de8
commit 93f71317df
4 changed files with 36 additions and 33 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace productlist namespace productlist
@ -9,11 +10,20 @@ namespace productlist
public int version; public int version;
public string description; public string description;
public static List<Product> allProducts = new List<Product>();
public Product(string name, int version, string description) { public Product(string name, int version, string description) {
this.name = name; this.name = name;
this.version = version; this.version = version;
this.description = description; this.description = description;
allProducts.Add (this);
} }
~Product() {
allProducts.Remove (this);
}
public static Product fromPrompt() { public static Product fromPrompt() {
string pName = UI.getAnswer ("Product Name", UI.validateString); string pName = UI.getAnswer ("Product Name", UI.validateString);
int pVersion = Int32.Parse(UI.getAnswer ("Version", UI.validateInt)); int pVersion = Int32.Parse(UI.getAnswer ("Version", UI.validateInt));

View file

@ -7,7 +7,22 @@ namespace productlist
public static void Main (string[] args) public static void Main (string[] args)
{ {
Console.WriteLine ("Welcome to the Product List"); 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;
}
} }
} }

View file

@ -6,19 +6,14 @@ namespace productlist
{ {
public static class Storage public static class Storage
{ {
private static List<Product> productStorageList = new List<Product>();
private static readonly string filePath = @"data.json"; private static readonly string filePath = @"data.json";
public static void Add(Product p) {
productStorageList.Add (p);
}
public static void dump() { public static void dump() {
if (productStorageList.Count < 1) { if (Product.allProducts.Count < 1) {
Console.WriteLine ("Nothing to dump"); Console.WriteLine ("Nothing to dump");
return; return;
} }
string json = JsonConvert.SerializeObject (productStorageList); string json = JsonConvert.SerializeObject (Product.allProducts);
System.IO.File.WriteAllText (filePath, json); System.IO.File.WriteAllText (filePath, json);
} }
@ -29,15 +24,15 @@ namespace productlist
return; return;
} }
string text = System.IO.File.ReadAllText (filePath); string text = System.IO.File.ReadAllText (filePath);
productStorageList = JsonConvert.DeserializeObject<List<Product>> (text); Product.allProducts = JsonConvert.DeserializeObject<List<Product>> (text);
} }
private static void listProducts() { private static void listProducts() {
if (productStorageList.Count < 1) { if (Product.allProducts.Count < 1) {
Console.WriteLine ("No Products found!"); Console.WriteLine ("No Products found!");
return; return;
} }
foreach (Product p in productStorageList) { foreach (Product p in Product.allProducts) {
p.display (); p.display ();
} }
} }

27
UI.cs
View file

@ -4,7 +4,7 @@ namespace productlist
{ {
public static class UI public static class UI
{ {
private enum inputType { public enum inputType {
create, create,
delete, delete,
list, list,
@ -49,27 +49,10 @@ namespace productlist
return input; return input;
} }
public static void promptForCommand() { public static inputType promptForCommand() {
while (true) { Console.Write ("Enter command: ");
Console.Write ("Enter command: "); string command = Console.ReadLine ();
string command = Console.ReadLine (); return decodeInputType (command);
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;
}
}
} }
} }
} }