From 93f71317df55393b98327d22b03d11e52101e40e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 15 Apr 2016 15:27:33 +0100 Subject: [PATCH] Store product list under product --- Product.cs | 10 ++++++++++ Program.cs | 17 ++++++++++++++++- Storage.cs | 15 +++++---------- UI.cs | 27 +++++---------------------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Product.cs b/Product.cs index 686be34..28e850c 100644 --- a/Product.cs +++ b/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 allProducts = new List(); + 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)); diff --git a/Program.cs b/Program.cs index 9dfce96..2428cdf 100644 --- a/Program.cs +++ b/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; + } } } diff --git a/Storage.cs b/Storage.cs index 97be652..4559364 100644 --- a/Storage.cs +++ b/Storage.cs @@ -6,19 +6,14 @@ namespace productlist { public static class Storage { - private static List productStorageList = new List(); 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> (text); + Product.allProducts = JsonConvert.DeserializeObject> (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 (); } } diff --git a/UI.cs b/UI.cs index c1cd5a2..c5eb7b9 100644 --- a/UI.cs +++ b/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); } } }