From 253afa36198e2bcb3798f028781a2a3c8e6ed9f6 Mon Sep 17 00:00:00 2001 From: smarr00 Date: Fri, 29 Jan 2016 14:40:22 -0600 Subject: [PATCH] refactoring --- Refactoring/Output.cs | 119 ++++++++++ Refactoring/Product.cs | 2 +- Refactoring/Refactoring.csproj | 21 ++ Refactoring/Tusc.cs | 384 ++++++++++++++------------------- Refactoring/User.cs | 4 +- Refactoring/Validation.cs | 13 ++ Refactoring/packages.config | 1 + UnitTestProject/UnitTests.cs | 4 +- 8 files changed, 326 insertions(+), 222 deletions(-) create mode 100644 Refactoring/Output.cs create mode 100644 Refactoring/Validation.cs diff --git a/Refactoring/Output.cs b/Refactoring/Output.cs new file mode 100644 index 0000000..8af18f2 --- /dev/null +++ b/Refactoring/Output.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Refactoring +{ + public static class Output + { + internal static void WelcomeMessage() + { + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + } + + internal static string PromptForUserName() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + return Console.ReadLine(); + } + + internal static void InvalidPasswordMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + } + + internal static void InvalidUserMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + } + + internal static void PromptForExitKey() + { + Console.WriteLine(); + Console.WriteLine("Press Enter key to exit"); + Console.ReadLine(); + } + + internal static string PromptForPassword() + { + Console.WriteLine("Enter Password:"); + return Console.ReadLine(); + } + + internal static void SuccessfulLoginMessage(string name) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.ResetColor(); + } + + internal static void RemainingBalanceMessage(double balance) + { + Console.WriteLine(); + Console.WriteLine("Your balance is " + balance.ToString("C")); + } + + internal static string PromptForPurchaseQuantity() + { + Console.WriteLine("Enter amount to purchase:"); + return Console.ReadLine(); + } + + internal static void LowBalanceMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + } + + internal static void OutOfStockMessage(string name) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("Sorry, " + name + " is out of stock"); + Console.ResetColor(); + } + + internal static void PurchaseCancelledMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + + internal static void PurchaseCompleteMessage(int quantity, string name, double balance) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("You bought " + quantity + " " + name); + Console.WriteLine("Your new balance is " + balance.ToString("C")); + Console.ResetColor(); + } + + internal static void PuchaseIntentMessage(string name, double balance) + { + Console.WriteLine(); + Console.WriteLine("You want to buy: " + name); + Console.WriteLine("Your balance is " + balance.ToString("C")); + } + } +} diff --git a/Refactoring/Product.cs b/Refactoring/Product.cs index c9ceee5..2e42aa5 100644 --- a/Refactoring/Product.cs +++ b/Refactoring/Product.cs @@ -15,6 +15,6 @@ public class Product [JsonProperty("Price")] public double Price; [JsonProperty("Quantity")] - public int Qty; + public int Quantity; } } diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj index 6696ba9..618af69 100644 --- a/Refactoring/Refactoring.csproj +++ b/Refactoring/Refactoring.csproj @@ -36,6 +36,25 @@ ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True + + ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.dll + False + + + ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.interfaces.dll + False + + + ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.framework.dll + + + ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.util.dll + False + + + ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll + False + @@ -45,11 +64,13 @@ + + diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..7493d4a 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -8,223 +8,173 @@ namespace Refactoring { - public class Tusc - { - public static void Start(List usrs, List prods) - { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); - - // Login - Login: - bool loggedIn = false; // Is logged in? - - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); - - // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) - { - for (int i = 0; i < 5; i++) - { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } - } - - // if valid user - if (valUsr) - { - // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); - - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) - { - User user = usrs[i]; - - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) - { - valPwd = true; - } - } - - // if valid password - if (valPwd == true) - { - loggedIn = true; - - // Show welcome message - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(); - Console.WriteLine("Login successful! Welcome " + name + "!"); - Console.ResetColor(); - - // Show remaining balance - double bal = 0; - for (int i = 0; i < 5; i++) - { - User usr = usrs[i]; - - // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) - { - bal = usr.Bal; - - // Show balance - Console.WriteLine(); - Console.WriteLine("Your balance is " + usr.Bal.ToString("C")); - } - } - - // Show product list - while (true) - { - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("What would you like to buy?"); - for (int i = 0; i < 7; i++) - { - Product prod = prods[i]; - Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")"); - } - Console.WriteLine(prods.Count + 1 + ": Exit"); - - // Prompt for user input - Console.WriteLine("Enter a number:"); - string answer = Console.ReadLine(); - int num = Convert.ToInt32(answer); - num = num - 1; /* Subtract 1 from number + public class Tusc + { + public static void Start(List users, List products) + { + Output.WelcomeMessage(); + + // Login + Login: + bool isLoggedIn = false; + + string name = Output.PromptForUserName(); + + // Validate Username + bool validUser = false; // Is valid user? + if (!string.IsNullOrEmpty(name)) + { + for (int i = 0; i < 5; i++) + { + User user = users[i]; + // Check that name matches + if (user.Name == name) + { + validUser = true; + } + } + + // if valid user + if (validUser) + { + string password = Output.PromptForPassword(); + + // Validate Password + bool validPassword = false; // Is valid password? + for (int i = 0; i < users.Count; i++) + { + User user = users[i]; + + // Check that name and password match + if (user.Name == name && user.Password == password) + { + validPassword = true; + } + } + + // if valid password + if (validPassword == true) + { + isLoggedIn = true; + Output.SuccessfulLoginMessage(name); + + // Show remaining balance + double balance = 0; + for (int i = 0; i < users.Count; i++) + { + User user = users[i]; + + // Check that name and password match + if (user.Name == name && user.Password == password) + { + balance = user.Balance; + + // Show balance + Output.RemainingBalanceMessage(balance); + } + } + + // Show product list + while (true) + { + // Prompt for user input + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + for (int i = 0; i < products.Count; i++) + { + Product product = products[i]; + Console.WriteLine(i + 1 + ": " + product.Name + " (" + product.Price.ToString("C") + ")"); + } + Console.WriteLine(products.Count + 1 + ": Exit"); + + // Prompt for user input + Console.WriteLine("Enter a number:"); + string answer = Console.ReadLine(); + int number = Convert.ToInt32(answer); + number = number - 1; /* Subtract 1 from number num = num + 1 // Add 1 to number */ - // Check if user entered number that equals product count - if (num == 7) - { - // Update balance - foreach (var usr in usrs) - { - // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) - { - usr.Bal = bal; - } - } - - // Write out new balance - string json = JsonConvert.SerializeObject(usrs, Formatting.Indented); - File.WriteAllText(@"Data/Users.json", json); - - // Write out new quantities - string json2 = JsonConvert.SerializeObject(prods, Formatting.Indented); - File.WriteAllText(@"Data/Products.json", json2); - - - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); - return; - } - else - { - Console.WriteLine(); - Console.WriteLine("You want to buy: " + prods[num].Name); - Console.WriteLine("Your balance is " + bal.ToString("C")); - - // Prompt for user input - Console.WriteLine("Enter amount to purchase:"); - answer = Console.ReadLine(); - int qty = Convert.ToInt32(answer); - - // Check if balance - quantity * price is less than 0 - if (bal - prods[num].Price * qty < 0) - { - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You do not have enough money to buy that."); - Console.ResetColor(); - continue; - } - - // Check if quantity is less than quantity - if (prods[num].Qty <= qty) - { - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock"); - Console.ResetColor(); - continue; - } - - // Check if quantity is greater than zero - if (qty > 0) - { - // Balance = Balance - Price * Quantity - bal = bal - prods[num].Price * qty; - - // Quanity = Quantity - Quantity - prods[num].Qty = prods[num].Qty - qty; - - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("You bought " + qty + " " + prods[num].Name); - Console.WriteLine("Your new balance is " + bal.ToString("C")); - Console.ResetColor(); - } - else - { - // Quantity is less than zero - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine(); - Console.WriteLine("Purchase cancelled"); - Console.ResetColor(); - } - } - } - } - else - { - // Invalid Password - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid password."); - Console.ResetColor(); - - goto Login; - } - } - else - { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); - - goto Login; - } - } - - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); - } - } + // Check if user entered number that equals product count + if (number == 7) + { + // Update balance + foreach (var user in users) + { + // Check that name and password match + if (user.Name == name && user.Password == password) + { + user.Balance = balance; + } + } + + // Write out new balance + string json = JsonConvert.SerializeObject(users, Formatting.Indented); + File.WriteAllText(@"Data/Users.json", json); + + // Write out new quantities + string json2 = JsonConvert.SerializeObject(products, Formatting.Indented); + File.WriteAllText(@"Data/Products.json", json2); + + + // Prevent console from closing + Output.PromptForExitKey(); + return; + } + else + { + Output.PuchaseIntentMessage(products[number].Name, balance); + + // Prompt for user input + answer = Output.PromptForPurchaseQuantity(); + int quantity = Convert.ToInt32(answer); + + // Check if balance - quantity * price is less than 0 + if (balance - products[number].Price * quantity < 0) + { + Output.LowBalanceMessage(); + continue; + } + + // Check if quantity is less than quantity + if (products[number].Quantity <= quantity) + { + Output.OutOfStockMessage(products[number].Name); + continue; + } + + // Check if quantity is greater than zero + if (quantity > 0) + { + // Balance = Balance - Price * Quantity + balance = balance - products[number].Price * quantity; + + // Quanity = Quantity - Quantity + products[number].Quantity = products[number].Quantity - quantity; + + Output.PurchaseCompleteMessage(quantity, products[number].Name, balance); + } + else + { + // Quantity is less than zero + Output.PurchaseCancelledMessage(); + } + } + } + } + else + { + Output.InvalidPasswordMessage(); + goto Login; + } + } + else + { + Output.InvalidUserMessage(); + goto Login; + } + } + + Output.PromptForExitKey(); + } + } } diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..0192ed1 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -13,8 +13,8 @@ public class User [JsonProperty("Username")] public string Name; [JsonProperty("Password")] - public string Pwd; + public string Password; [JsonProperty("Balance")] - public double Bal; + public double Balance; } } diff --git a/Refactoring/Validation.cs b/Refactoring/Validation.cs new file mode 100644 index 0000000..f1be834 --- /dev/null +++ b/Refactoring/Validation.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Refactoring +{ + public static class Validation + { + + } +} diff --git a/Refactoring/packages.config b/Refactoring/packages.config index 2abc396..21b1784 100644 --- a/Refactoring/packages.config +++ b/Refactoring/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..18292c2 100644 --- a/UnitTestProject/UnitTests.cs +++ b/UnitTestProject/UnitTests.cs @@ -151,7 +151,7 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice() { // Update data file List tempUsers = DeepCopy>(originalUsers); - tempUsers.Where(u => u.Name == "Jason").Single().Bal = 0.0; + tempUsers.Where(u => u.Name == "Jason").Single().Balance = 0.0; using (var writer = new StringWriter()) { @@ -173,7 +173,7 @@ public void Test_ErrorOccursWhenProductOutOfStock() { // Update data file List tempProducts = DeepCopy>(originalProducts); - tempProducts.Where(u => u.Name == "Chips").Single().Qty = 0; + tempProducts.Where(u => u.Name == "Chips").Single().Quantity = 0; using (var writer = new StringWriter()) {