From 211756998f276e00af242091d847b85a308b7941 Mon Sep 17 00:00:00 2001 From: mmichiels Date: Fri, 29 Jan 2016 14:25:00 -0600 Subject: [PATCH] Finished as much as I could in the 2.5 hrs --- Refactoring/Product.cs | 2 +- Refactoring/Tusc.cs | 403 ++++++++++++++++++++--------------- Refactoring/User.cs | 6 +- UnitTestProject/UnitTests.cs | 4 +- 4 files changed, 232 insertions(+), 183 deletions(-) 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/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..3922573 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -10,221 +10,270 @@ namespace Refactoring { public class Tusc { - public static void Start(List usrs, List prods) + public static void Start(List users, List products) { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); + + int totalNumberAppUsers = users.Count; + int totalNumberProducts = products.Count; + + WriteWelcomeMessage(); // Login Login: - bool loggedIn = false; // Is logged in? + bool userIsLoggedIn = false; - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + var userName = PromptForUserName(); + var userPassword = string.Empty; - // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + if (string.IsNullOrEmpty(userName)) + { + CloseConsoleIfEnterKeyPressed(); + } + else { - for (int i = 0; i < 5; i++) + bool usernameIsValid = UsernameIsValid(users, userName, totalNumberAppUsers); + if (usernameIsValid) { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) + userPassword = PromptForPassword(); + + bool userPasswordIsValid = UserPasswordIsValid(users, totalNumberAppUsers, userName, userPassword); + + if (userPasswordIsValid) { - valUsr = true; + userIsLoggedIn = true; + } + else + { + DisplayErrorToUser("You entered an invalid password."); + + goto Login; } } + else + { + DisplayErrorToUser("You entered an invalid user."); + + goto Login; + + } + } - // if valid user - if (valUsr) + if (userIsLoggedIn) + { + ShowUserWelcomeMessage(userName); + + var userRemainingBalance = ShowUserRemainingBalance(users, totalNumberAppUsers, userName, userPassword); + + // Show product list + while (true) { + PromptUserForPurchases(products, totalNumberProducts); + // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + Console.WriteLine("Enter a number:"); + string productNumberAsString = Console.ReadLine(); + int productNumber = Convert.ToInt32(productNumberAsString); + productNumber = productNumber - 1; // Subtract 1 from number - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + // Check if user entered number that equals product count + if (productNumber == totalNumberProducts) { - User user = usrs[i]; - - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) + var user = GetUserByUsernameAndPassword(userName, userPassword, users); + if (user != null) { - valPwd = true; + user.Balance = userRemainingBalance; } - } - // if valid password - if (valPwd == true) - { - loggedIn = true; + WriteUserBalance(users); + WriteProductQuantities(products); + CloseConsoleIfEnterKeyPressed(); - // Show welcome message - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Green; + return; + } + else + { 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]; + Console.WriteLine("You want to buy: " + products[productNumber].Name); + Console.WriteLine("Your balance is " + userRemainingBalance.ToString("C")); - // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) - { - bal = usr.Bal; + // Prompt for user input + Console.WriteLine("Enter amount to purchase:"); + string amountToPurchaseAsString = Console.ReadLine(); + int amountToPurchase = Convert.ToInt32(amountToPurchaseAsString); - // Show balance - Console.WriteLine(); - Console.WriteLine("Your balance is " + usr.Bal.ToString("C")); - } + // Check if balance - quantity * price is less than 0 + if (userRemainingBalance - products[productNumber].Price * amountToPurchase < 0) + { + DisplayErrorToUser("You do not have enough money to buy that."); + continue; + } + + // Check if quantity is less than quantity + if (products[productNumber].Quantity <= amountToPurchase) + { + DisplayErrorToUser("Sorry, " + products[productNumber].Name + " is out of stock"); + continue; } - // Show product list - while (true) + // Check if quantity is greater than zero + if (amountToPurchase > 0) { - // Prompt for user input + // Balance = Balance - Price * Quantity + userRemainingBalance = userRemainingBalance - products[productNumber].Price*amountToPurchase; + + // Quanity = Quantity - Quantity + products[productNumber].Quantity = products[productNumber].Quantity - amountToPurchase; + + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("You bought " + amountToPurchase + " " + products[productNumber].Name); + Console.WriteLine("Your new balance is " + userRemainingBalance.ToString("C")); + Console.ResetColor(); + } + else + { + // Quantity is less than zero + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; 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(); - } - } + 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; + private static void WriteWelcomeMessage() + { + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + } + + private static string PromptForUserName() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + string userName = Console.ReadLine(); + return userName; + } + + private static bool UsernameIsValid(List users, string userName, int totalNumberAppUsers) + { + for (int userIndex = 0; userIndex < totalNumberAppUsers; userIndex++) + { + User user = users[userIndex]; + if (user.Username == userName) + { + return true; } } - // Prevent console from closing + return false; + } + + private static string PromptForPassword() + { + Console.WriteLine("Enter Password:"); + string pwd = Console.ReadLine(); + return pwd; + } + + private static bool UserPasswordIsValid(List users, int totalNumberAppUsers, string userName, string userPassword) + { + var user = GetUserByUsernameAndPassword(userName, userPassword, users); + if (user != null) + { + return true; + } + return false; + } + + private static void ShowUserWelcomeMessage(string userName) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + userName + "!"); + Console.ResetColor(); + } + + private static double ShowUserRemainingBalance(List users, int totalNumberAppUsers, string userName, string userPassword) + { + double userRemainingBalance = 0; + var user = GetUserByUsernameAndPassword(userName, userPassword, users); + + if (user != null) + { + userRemainingBalance = user.Balance; + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.Balance.ToString("C")); + } + + return userRemainingBalance; + } + + private static void WriteProductQuantities(List products) + { + string json2 = JsonConvert.SerializeObject(products, Formatting.Indented); + File.WriteAllText(@"Data/Products.json", json2); + } + + private static void WriteUserBalance(List users) + { + string json = JsonConvert.SerializeObject(users, Formatting.Indented); + File.WriteAllText(@"Data/Users.json", json); + } + + private static void PromptUserForPurchases(List products, int totalNumberProducts) + { + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + WriteProductsAndPrice(products, totalNumberProducts); + Console.WriteLine(products.Count + 1 + ": Exit"); + } + + private static void WriteProductsAndPrice(List products, int totalNumberProducts) + { + for (int i = 0; i < totalNumberProducts; i++) + { + Product prod = products[i]; + Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")"); + } + } + + private static void DisplayErrorToUser(string error) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine(error); + Console.ResetColor(); + } + + private static void CloseConsoleIfEnterKeyPressed() + { Console.WriteLine(); Console.WriteLine("Press Enter key to exit"); Console.ReadLine(); } + + public static User GetUserByUsernameAndPassword(string userName, string userPassword, List users) + { + int totalNumberAppUsers = users.Count; + + for (int userIndex = 0; userIndex < totalNumberAppUsers; userIndex++) + { + User user = users[userIndex]; + if (user.Username == userName && user.Password == userPassword) + { + return user; + } + } + + return null; + } } } + diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..f6b177d 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -11,10 +11,10 @@ namespace Refactoring public class User { [JsonProperty("Username")] - public string Name; + public string Username; [JsonProperty("Password")] - public string Pwd; + public string Password; [JsonProperty("Balance")] - public double Bal; + public double Balance; } } diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..9c7b328 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.Username == "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()) {