From 08593347cadf999f728b84e7a7db9aab8f1f7f70 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Jan 2016 14:14:03 -0600 Subject: [PATCH 1/4] My attempt.. --- Refactoring.sln | 11 +- Refactoring/Tusc.cs | 441 ++++++++++++++++++++--------------- Refactoring/User.cs | 2 + UnitTestProject/UnitTests.cs | 4 +- 4 files changed, 260 insertions(+), 198 deletions(-) diff --git a/Refactoring.sln b/Refactoring.sln index e744380..c6e0645 100644 --- a/Refactoring.sln +++ b/Refactoring.sln @@ -1,12 +1,17 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.40629.0 -MinimumVisualStudioVersion = 10.0.40219.1 +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Refactoring", "Refactoring\Refactoring.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{1FAF99A1-CCBF-435D-B599-4DAE1DAD5105}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{76B6DD1C-3907-4232-A969-A9D0969AE912}" + ProjectSection(SolutionItems) = preProject + .nuget\NuGet.Config = .nuget\NuGet.Config + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..fbe4137 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -10,221 +10,276 @@ namespace Refactoring { public class Tusc { + private static List users; + private static List products; + private const int EXIT_CODE = 7; + public static void Start(List usrs, List prods) { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); + users = usrs; + products = prods; - // Login - Login: - bool loggedIn = false; // Is logged in? + displaySplash(); - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + User user = loginProcess(); - // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + if (user != null) { - for (int i = 0; i < 5; i++) - { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } - } + // Show welcome message + displayLoginSuccess(user); + displayBalance(user); - // if valid user - if (valUsr) + // Show product list + int selection; + bool changesMade = false; + do { - // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + displayProducts(); + selection = readSelection(); - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + if (selection != EXIT_CODE) { - User user = usrs[i]; + displayOrderConfirmation(selection, user); + int quantity = readQty(); - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) - { - valPwd = true; + if(validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)){ + changesMade = true; + user.Bal -= products[selection].Price * quantity; + products[selection].Qty = products[selection].Qty - quantity; + + displayPurchaseConfirmation(quantity, user, selection); } } - // 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")); - } - } + } while (selection != EXIT_CODE); - // 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 - 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 + if (changesMade) { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); - - goto Login; + updateBalance(user); + saveChanges(); } } - // Prevent console from closing Console.WriteLine(); Console.WriteLine("Press Enter key to exit"); Console.ReadLine(); } + + private static User loginProcess() + { + User user = null; + + bool sentinel = false; + do + { + user = attemptLogin(); + if (user == null || user.isValid) + { + sentinel = true; + } + } + while (!sentinel); + + return user; + } + + private static void saveChanges() + { + 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); + } + + private static void updateBalance(User user) + { + foreach (var usr in users) + { + // Check that name and password match + if (usr.Name == user.Name && usr.Pwd == user.Pwd) + { + usr.Bal = user.Bal; + } + } + } + + private static void displayPurchaseConfirmation(int quantity, User user, int selection) + { + prepConsole(ConsoleColor.Green); + Console.WriteLine("You bought " + quantity + " " + products[selection].Name); + Console.WriteLine("Your new balance is " + user.Bal.ToString("C")); + Console.ResetColor(); + } + + private static bool validateQuantity(int quantity) + { + bool validQuantity = true; + if (quantity <= 0) + { + prepConsole(ConsoleColor.Yellow); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + validQuantity = false; + } + return validQuantity; + } + + private static bool checkQuantity(int quantity, int selection) + { + bool validQuantity = true; + if (products[selection].Qty <= quantity) + { + prepConsole(ConsoleColor.Red); + Console.WriteLine("Sorry, " + products[selection].Name + " is out of stock"); + Console.ResetColor(); + validQuantity = false; + } + return validQuantity; + } + + private static bool checkBalance(int quantity, User user, int selection) + { + bool balanceAvailable = true; + if (user.Bal - products[selection].Price * quantity < 0) + { + prepConsole(ConsoleColor.Red); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + balanceAvailable = false; + } + return balanceAvailable; + } + + private static int readQty() + { + Console.WriteLine("Enter amount to purchase:"); + int qty = readInt(); + return qty; + } + + private static void displayOrderConfirmation(int selection, User user) + { + Console.WriteLine(); + Console.WriteLine("You want to buy: " + products[selection].Name); + Console.WriteLine("Your balance is " + user.Bal.ToString("C")); + } + + private static int readSelection() + { + Console.WriteLine("Enter a number:"); + int num = readInt(); + return num - 1; + + } + + private static int readInt() + { + string answer = Console.ReadLine(); + int num; + bool isNum = int.TryParse(answer, out num); + if (!isNum) + { + throw new FormatException("Could not parse input"); + } + return num; + } + + private static void displayProducts() + { + // Prompt for user input + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + for (int i = 0; i < 7; i++) + { + Product prod = products[i]; + Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")"); + } + Console.WriteLine(products.Count + 1 + ": Exit"); + + } + + private static void displayBalance(User user) + { + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.Bal.ToString("C")); + } + + private static void displayLoginSuccess(User user) + { + prepConsole(ConsoleColor.Green); + Console.WriteLine("Login successful! Welcome " + user.Name + "!"); + Console.ResetColor(); + } + + private static User attemptLogin() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + String name = Console.ReadLine(); + Console.WriteLine("Enter Password:"); + String pwd = Console.ReadLine(); + + User attemptedUser = null; + if (!string.IsNullOrEmpty(name)) + { + attemptedUser = getUser(name, pwd); + } + + if (attemptedUser != null && !attemptedUser.isValid) + { + displayAuthenticationError(); + } + + return attemptedUser; + } + + + private static void displayAuthenticationError() + { + + Console.WriteLine("You entered an invalid username/password."); + Console.ResetColor(); + } + + private static void displaySplash() + { + // Write welcome message + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + } + + private static User getUser(String name, String pwd) + { + bool validUser = false; + + User attemptedUser = new User(); + attemptedUser.isValid = false; + + int userCount = 0; + while (userCount < users.Count && !validUser) + { + User user = users[userCount++]; + if (user.Name == name && user.Pwd == pwd) + { + attemptedUser = new User(); + attemptedUser.Name = name; + attemptedUser.Pwd = pwd; + attemptedUser.Bal = user.Bal; + attemptedUser.isValid = true; + validUser = true; + } + } + + return attemptedUser; + } + + private static void prepConsole(ConsoleColor color) + { + Console.Clear(); + Console.ForegroundColor = color; + Console.WriteLine(); + } } } diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..1700826 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -16,5 +16,7 @@ public class User public string Pwd; [JsonProperty("Balance")] public double Bal; + + public bool isValid; } } diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..f5ed3d2 100644 --- a/UnitTestProject/UnitTests.cs +++ b/UnitTestProject/UnitTests.cs @@ -89,7 +89,7 @@ public void Test_InvalidUserIsNotAccepted() Tusc.Start(users, products); } - Assert.IsTrue(writer.ToString().Contains("You entered an invalid user")); + Assert.IsTrue(writer.ToString().Contains("You entered an invalid username/password.")); } } @@ -123,7 +123,7 @@ public void Test_InvalidPasswordIsNotAccepted() Tusc.Start(users, products); } - Assert.IsTrue(writer.ToString().Contains("You entered an invalid password")); + Assert.IsTrue(writer.ToString().Contains("You entered an invalid username/password.")); } } From 16f71d865bccb6dc360772a31cda66aacb58d1cc Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Jan 2016 14:17:11 -0600 Subject: [PATCH 2/4] One more thing --- Refactoring/Tusc.cs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index fbe4137..9999b04 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -36,21 +36,7 @@ public static void Start(List usrs, List prods) { displayProducts(); selection = readSelection(); - - if (selection != EXIT_CODE) - { - displayOrderConfirmation(selection, user); - int quantity = readQty(); - - if(validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)){ - changesMade = true; - user.Bal -= products[selection].Price * quantity; - products[selection].Qty = products[selection].Qty - quantity; - - displayPurchaseConfirmation(quantity, user, selection); - } - } - + changesMade = processSelection(selection, user); } while (selection != EXIT_CODE); if (changesMade) @@ -65,6 +51,26 @@ public static void Start(List usrs, List prods) Console.ReadLine(); } + private static bool processSelection(int selection, User user) + { + bool changesMade = false; + if (selection != EXIT_CODE) + { + displayOrderConfirmation(selection, user); + int quantity = readQty(); + + if (validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)) + { + changesMade = true; + user.Bal -= products[selection].Price * quantity; + products[selection].Qty = products[selection].Qty - quantity; + + displayPurchaseConfirmation(quantity, user, selection); + } + } + return changesMade; + } + private static User loginProcess() { User user = null; From 555af60ec079a84e19fc5e990a4b65f72e1fbdd5 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Jan 2016 14:21:06 -0600 Subject: [PATCH 3/4] Last thing --- Refactoring/Tusc.cs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index 9999b04..13c5a0c 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -29,16 +29,7 @@ public static void Start(List usrs, List prods) displayLoginSuccess(user); displayBalance(user); - // Show product list - int selection; - bool changesMade = false; - do - { - displayProducts(); - selection = readSelection(); - changesMade = processSelection(selection, user); - } while (selection != EXIT_CODE); - + bool changesMade = processSession(user); if (changesMade) { updateBalance(user); @@ -51,23 +42,30 @@ public static void Start(List usrs, List prods) Console.ReadLine(); } - private static bool processSelection(int selection, User user) + private static bool processSession(User user) { + int selection; bool changesMade = false; - if (selection != EXIT_CODE) + do { - displayOrderConfirmation(selection, user); - int quantity = readQty(); - - if (validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)) + displayProducts(); + selection = readSelection(); + if (selection != EXIT_CODE) { - changesMade = true; - user.Bal -= products[selection].Price * quantity; - products[selection].Qty = products[selection].Qty - quantity; + displayOrderConfirmation(selection, user); + int quantity = readQty(); - displayPurchaseConfirmation(quantity, user, selection); + if (validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)) + { + changesMade = true; + user.Bal -= products[selection].Price * quantity; + products[selection].Qty = products[selection].Qty - quantity; + + displayPurchaseConfirmation(quantity, user, selection); + } } - } + } while (selection != EXIT_CODE); + return changesMade; } From c86f4dd2b67378fbbdc75736849936503a662316 Mon Sep 17 00:00:00 2001 From: Chris Jeffers Date: Fri, 29 Jan 2016 14:23:34 -0600 Subject: [PATCH 4/4] eugh --- Refactoring/Tusc.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index 13c5a0c..7f4c247 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -57,11 +57,8 @@ private static bool processSession(User user) if (validateQuantity(quantity) && checkBalance(quantity, user, selection) && checkQuantity(quantity, selection)) { + processTransaction(quantity, selection, user); changesMade = true; - user.Bal -= products[selection].Price * quantity; - products[selection].Qty = products[selection].Qty - quantity; - - displayPurchaseConfirmation(quantity, user, selection); } } } while (selection != EXIT_CODE); @@ -69,6 +66,13 @@ private static bool processSession(User user) return changesMade; } + private static void processTransaction(int quantity, int selection, User user) + { + user.Bal -= products[selection].Price * quantity; + products[selection].Qty = products[selection].Qty - quantity; + displayPurchaseConfirmation(quantity, user, selection); + } + private static User loginProcess() { User user = null;