From 09d4e485ae727712d8d1eb638cc6f820e8ed31f6 Mon Sep 17 00:00:00 2001 From: VarianErin Date: Mon, 1 Feb 2016 09:11:10 -0600 Subject: [PATCH] Refactoring changes --- Refactoring/Tusc.cs | 372 ++++++++++++++++++++++++++------------------ 1 file changed, 222 insertions(+), 150 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index fb93c58..6f65bc9 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -12,101 +12,38 @@ 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(); + + string name = PromptUserName(); + string pwd = PromptPassword(); - // 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) + if (ValidateUser(usrs, name) && ValidatePassword(usrs, name, pwd)) { - // 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(); + DisplayWelcomeMessage(name); // 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")); - } - } + bal = ShowRemainingBalance(usrs, name, pwd, bal); // 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"); + ShowProductList(prods); // 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 */ + // string answer; + int num; + num = GetUserSelection(); // Check if user entered number that equals product count if (num == 7) @@ -120,14 +57,7 @@ public static void Start(List usrs, List prods) 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); + UpdateUserDetails(usrs, prods); // Prevent console from closing @@ -142,89 +72,231 @@ public static void Start(List usrs, List prods) 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); + int qty; + qty = PromptUserPurchase(); - // 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(); - } + bal = ProcessOrder(prods, bal, num, qty); } } } - else + else { - // Invalid Password - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid password."); - Console.ResetColor(); + goto Login; } } - else + + // Prevent console from closing + Console.WriteLine(); + Console.WriteLine("Press Enter key to exit"); + Console.ReadLine(); + } + + private static double ProcessOrder(List prods, double bal, int num, int qty) + { + + + // Check if quantity is greater than zero + if (qty > 0) + { + // Check if balance - quantity * price is less than 0 + if (CheckBalance(prods, bal, num, qty) && ValidateQuantity(prods, num, qty)) { - // Invalid User + // Check if quantity is less than quantity + // ValidateQuantity(prods, num, qty); + + // 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.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("You bought " + qty + " " + prods[num].Name); + Console.WriteLine("Your new balance is " + bal.ToString("C")); Console.ResetColor(); + } + } + else + { + CancelOrder(); + } + return bal; + } + + private static void CancelOrder() + { + + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + + private static int PromptUserPurchase() + { + string answer; + int qty; + + Console.WriteLine("Enter amount to purchase:"); + answer = Console.ReadLine(); + qty = Convert.ToInt32(answer); - goto Login; + return qty; + } + + private static bool ValidateQuantity(List prods, int num, int qty) + { + 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(); + return false; + } + return true; + } + + private static bool CheckBalance(List prods, double bal, int num, int qty) + { + 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(); + return false; + } + return true; + } + + private static void UpdateUserDetails(List usrs, List prods) + { + + // 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); + } + + private static int GetUserSelection() + { + + string answer; + int num; + + Console.WriteLine("Enter a number:"); + answer = Console.ReadLine(); + num = Convert.ToInt32(answer); + return num - 1; /* Subtract 1 from number + num = num + 1 // Add 1 to number */ + } + + private static void ShowProductList(List prods) + { + 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"); + } + + private static double ShowRemainingBalance(List usrs, string name, string pwd, double bal) + { + 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")); } } + return bal; + } - // Prevent console from closing + private static void DisplayWelcomeMessage(string name) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.ResetColor(); + } + + private static bool ValidatePassword(List usrs, string name, string pwd) + { + for (int i = 0; i < 5; i++) + { + User user = usrs[i]; + + // Check that name and password match + if (user.Name == name && user.Pwd == pwd) + { + return true; + + } + } + + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + return false; + } + + private static string PromptPassword() + { + + Console.WriteLine("Enter Password:"); + string pwd = Console.ReadLine(); + return pwd; + } + + private static bool ValidateUser(List usrs, string name) + { + for (int i = 0; i < 5; i++) + { + User user = usrs[i]; + if (user.Name == name) + { + return true; + } + } + + // Invalid User + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + return false; + } + + private static string PromptUserName() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + string name = Console.ReadLine(); + return name; } } }