From e9296cf639b0e68145cb7b042e13ffd58522afd7 Mon Sep 17 00:00:00 2001 From: clamcode Date: Fri, 29 Jan 2016 14:43:33 -0600 Subject: [PATCH 1/2] Christine Lam refractoring training --- Refactoring/Tusc.cs | 82 +++++++++++++++++++++++++++++---------------- Refactoring/User.cs | 82 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 130 insertions(+), 34 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..dd493be 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -6,45 +6,37 @@ using System.Text; using System.Threading.Tasks; + namespace Refactoring { public class Tusc { public static void Start(List usrs, List prods) { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); + DisplayWelcomeMsg(); + // Login Login: bool loggedIn = false; // Is logged in? - // Prompt for user input Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + string name = PromptUserInputFor("Username"); // Validate Username - bool valUsr = false; // Is valid user? + bool validUsr = 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; - } - } + + validUsr = IsUserNameFound(usrs, name); // if valid user - if (valUsr) + if (validUsr) { // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + string pwd = PromptUserInputFor("Password"); // Validate Password bool valPwd = false; // Is valid password? @@ -101,9 +93,9 @@ public static void Start(List usrs, List prods) } Console.WriteLine(prods.Count + 1 + ": Exit"); - // Prompt for user input - Console.WriteLine("Enter a number:"); - string answer = Console.ReadLine(); + + string answer = PromptUserInputFor("a number"); + int num = Convert.ToInt32(answer); num = num - 1; /* Subtract 1 from number num = num + 1 // Add 1 to number */ @@ -130,10 +122,7 @@ public static void Start(List usrs, List prods) File.WriteAllText(@"Data/Products.json", json2); - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + PromptForClose(); return; } else @@ -143,8 +132,7 @@ public static void Start(List usrs, List prods) Console.WriteLine("Your balance is " + bal.ToString("C")); // Prompt for user input - Console.WriteLine("Enter amount to purchase:"); - answer = Console.ReadLine(); + answer = PromptUserInputFor("amount to purchase"); int qty = Convert.ToInt32(answer); // Check if balance - quantity * price is less than 0 @@ -221,6 +209,44 @@ public static void Start(List usrs, List prods) } } + PromptForClose(); + } + + private static void DisplayWelcomeMsg() + { + // Write welcome message + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + } + + public static bool IsUserNameFound(List usrs, string name) + { + + const int totalUsrs = 5; + bool matched = false; + + for (int usr = 0; usr < totalUsrs; usr++) + { + User user = usrs[usr]; + // Check that name matches + if (user.Name == name) + { + matched = true; + } + } + return matched; + } + + public static string PromptUserInputFor(string inputFor) + { + //Set the input for and return the input + Console.WriteLine("Enter " + inputFor + ":"); + string name = Console.ReadLine(); + return name; + } + + public static void PromptForClose() + { // Prevent console from closing Console.WriteLine(); Console.WriteLine("Press Enter key to exit"); diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..b25523c 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -10,11 +10,81 @@ namespace Refactoring [Serializable] public class User { - [JsonProperty("Username")] - public string Name; - [JsonProperty("Password")] - public string Pwd; - [JsonProperty("Balance")] - public double Bal; + [JsonProperty("Username")] public string Name; + [JsonProperty("Password")] public string Pwd; + [JsonProperty("Balance")] public double Bal; + + const Int16 TotalUsers = 5; + + public bool ValidateUser(List usrs) + { + + // Login + Login: + bool loggedIn = false; // Is logged in? + + Console.WriteLine(); + string name = PromptUserInputFor("Username"); + bool matchedNamePassword = false; + + if (!string.IsNullOrEmpty(name)) + { + bool validUsr = false; // Is valid user? + + validUsr = IsUserNameFound(usrs, name); + + // if valid user + if (validUsr) + { + // Prompt for user input + string pwd = PromptUserInputFor("Password"); + + // Validate Password + matchedNamePassword = IsUserNamePasswordMatched(usrs, name, pwd); + } + + } + return matchedNamePassword; + } + + public bool IsUserNamePasswordMatched(List usrs, string name, string pwd) + { + bool valPwd = false; + for (int usr = 0; usr < TotalUsers; usr++) + { + User user = usrs[usr]; + + // Check that name and password match + if (user.Name == name && user.Pwd == pwd) + { + valPwd = true; + } + } + return valPwd; + } + + public bool IsUserNameFound(List usrs, string name) + { + bool matched = false; + + for (int usr = 0; usr < TotalUsers; usr++) + { + User user = usrs[usr]; + // Check that name matches + if (user.Name == name) + { + matched = true; + } + } + return matched; + } + + public string PromptUserInputFor(string inputFor) + { + //Set the input for and return the input + Console.WriteLine("Enter " + inputFor + ":"); + string name = Console.ReadLine(); + return name; + } } } From 122fd6e598aff28ce6710e5927a277deed830cd9 Mon Sep 17 00:00:00 2001 From: clamcode Date: Fri, 29 Jan 2016 16:13:32 -0600 Subject: [PATCH 2/2] Christine Lam's Refractoring last changes --- Refactoring/Tusc.cs | 336 ++++++++++++++++++++++++-------------------- 1 file changed, 181 insertions(+), 155 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index dd493be..606b79e 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -15,7 +15,7 @@ public static void Start(List usrs, List prods) { DisplayWelcomeMsg(); - + // Login Login: bool loggedIn = false; // Is logged in? @@ -23,193 +23,219 @@ public static void Start(List usrs, List prods) Console.WriteLine(); string name = PromptUserInputFor("Username"); - // Validate Username - bool validUsr = false; // Is valid user? - - + if (!string.IsNullOrEmpty(name)) { - validUsr = IsUserNameFound(usrs, name); + bool validUsrName = IsUserNameValid(usrs, name); - // if valid user - if (validUsr) + if (validUsrName) { - // Prompt for user input - string pwd = PromptUserInputFor("Password"); + + string pwd = PromptUserInputFor("Password"); + loggedIn = IsUserPasswordValid(usrs, name, pwd); + + if (loggedIn == true) + { + + ShowWelcomeMessage(name); + + double bal = GetAndShowRemainingBalance(usrs, name, pwd); - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + ShowProductList(usrs, prods, name, pwd, bal); + } + else { - User user = usrs[i]; + BackToLoginForInvalidPassword(); - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) - { - valPwd = true; - } + goto Login; } + } + else + { + BackToLoginForInvalidUser(); - // if valid password - if (valPwd == true) - { - loggedIn = true; + goto Login; + } + } - // 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]; + PromptForClose(); + } - // 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")); - } - } + public static bool IsUserPasswordValid(List usrs, string name, string pwd) + { + // Validate Password + bool valPwd = false; // Is valid password? + for (int i = 0; i < 5; i++) + { + User user = usrs[i]; - // 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"); + // Check that name and password match + if (user.Name == name && user.Pwd == pwd) + { + valPwd = true; + } + } + return valPwd; + } + public static void BackToLoginForInvalidUser() + { + // Invalid User + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + } + + public static void BackToLoginForInvalidPassword() + { + // Invalid Password + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + } + + public static void ShowProductList(List usrs, List prods, string name, string pwd, double 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"); - string answer = PromptUserInputFor("a number"); - int num = Convert.ToInt32(answer); - num = num - 1; /* Subtract 1 from number + string answer = PromptUserInputFor("a number"); + + 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); - - - PromptForClose(); - 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 - answer = PromptUserInputFor("amount to purchase"); - 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(); - } - } + // 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; } } - else + + // 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); + + + PromptForClose(); + 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 + answer = PromptUserInputFor("amount to purchase"); + int qty = Convert.ToInt32(answer); + + // Check if balance - quantity * price is less than 0 + if (bal - prods[num].Price*qty < 0) { - // Invalid Password Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); - Console.WriteLine("You entered an invalid password."); + Console.WriteLine("You do not have enough money to buy that."); Console.ResetColor(); + continue; + } - goto Login; + // 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 + } + } + + public static double GetAndShowRemainingBalance(List usrs, string name, string pwd) + { + double bal = 0; + for (int usrLoop = 0; usrLoop < 5; usrLoop++) + { + User usr = usrs[usrLoop]; + + // Check that name and password match + if (usr.Name == name && usr.Pwd == pwd) { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); + bal = usr.Bal; - goto Login; + // Show balance + Console.WriteLine(); + Console.WriteLine("Your balance is " + usr.Bal.ToString("C")); } } + return bal; + } - PromptForClose(); + public static void ShowWelcomeMessage(string name) + { + // Show welcome message + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.ResetColor(); } private static void DisplayWelcomeMsg() @@ -219,7 +245,7 @@ private static void DisplayWelcomeMsg() Console.WriteLine("---------------"); } - public static bool IsUserNameFound(List usrs, string name) + public static bool IsUserNameValid(List usrs, string name) { const int totalUsrs = 5;