From e22db82e9dd85e8684da8a31198202e7508d9e52 Mon Sep 17 00:00:00 2001 From: lyumul Date: Fri, 29 Jan 2016 14:16:07 -0600 Subject: [PATCH] Refactored version 1 --- Refactoring/Tusc.cs | 139 +++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 67 deletions(-) diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..856bac4 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -15,76 +15,45 @@ public static void Start(List usrs, List prods) // Write welcome message Console.WriteLine("Welcome to TUSC"); Console.WriteLine("---------------"); + Console.WriteLine(); - // Login + // Return to Login when user or password is invalid Login: - bool loggedIn = false; // Is logged in? - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + string userName = PromptForUserInput("Enter Username:"); // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + if (!string.IsNullOrEmpty(userName)) { - for (int i = 0; i < 5; i++) - { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } - } + int totalUsers = usrs.Count(); - // if valid user - if (valUsr) + if (IsUserValid(usrs, userName, totalUsers)) { - // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + string userPassword = PromptForUserInput("Enter Password:"); - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + if (IsPasswordValid(usrs, userName, totalUsers, userPassword)) { - 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.WriteLine("Login successful! Welcome " + userName + "!"); Console.ResetColor(); // Show remaining balance - double bal = 0; - for (int i = 0; i < 5; i++) + double userBalance = 0; + for (int i = 0; i < totalUsers; i++) { - User usr = usrs[i]; + User user = usrs[i]; // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) + if (user.Name == userName && user.Pwd == userPassword) { - bal = usr.Bal; + userBalance = user.Bal; // Show balance Console.WriteLine(); - Console.WriteLine("Your balance is " + usr.Bal.ToString("C")); + Console.WriteLine("Your balance is " + user.Bal.ToString("C")); } } @@ -101,23 +70,21 @@ 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(); - int num = Convert.ToInt32(answer); - num = num - 1; /* Subtract 1 from number + string answer = PromptForUserInput("Enter a number:"); + int productNumber = Convert.ToInt32(answer); + productNumber = productNumber - 1; /* Subtract 1 from number num = num + 1 // Add 1 to number */ // Check if user entered number that equals product count - if (num == 7) + if (productNumber == 7) { // Update balance foreach (var usr in usrs) { // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) + if (usr.Name == userName && usr.Pwd == userPassword) { - usr.Bal = bal; + usr.Bal = userBalance; } } @@ -139,16 +106,15 @@ public static void Start(List usrs, List prods) else { Console.WriteLine(); - Console.WriteLine("You want to buy: " + prods[num].Name); - Console.WriteLine("Your balance is " + bal.ToString("C")); + Console.WriteLine("You want to buy: " + prods[productNumber].Name); + Console.WriteLine("Your balance is " + userBalance.ToString("C")); // Prompt for user input - Console.WriteLine("Enter amount to purchase:"); - answer = Console.ReadLine(); - int qty = Convert.ToInt32(answer); + answer = PromptForUserInput("Enter amount to purchase:"); + int productQuantity = Convert.ToInt32(answer); // Check if balance - quantity * price is less than 0 - if (bal - prods[num].Price * qty < 0) + if (userBalance - prods[productNumber].Price * productQuantity < 0) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; @@ -159,29 +125,29 @@ public static void Start(List usrs, List prods) } // Check if quantity is less than quantity - if (prods[num].Qty <= qty) + if (prods[productNumber].Qty <= productQuantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); - Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock"); + Console.WriteLine("Sorry, " + prods[productNumber].Name + " is out of stock"); Console.ResetColor(); continue; } // Check if quantity is greater than zero - if (qty > 0) + if (productQuantity > 0) { // Balance = Balance - Price * Quantity - bal = bal - prods[num].Price * qty; + userBalance = userBalance - prods[productNumber].Price * productQuantity; // Quanity = Quantity - Quantity - prods[num].Qty = prods[num].Qty - qty; + prods[productNumber].Qty = prods[productNumber].Qty - productQuantity; Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("You bought " + qty + " " + prods[num].Name); - Console.WriteLine("Your new balance is " + bal.ToString("C")); + Console.WriteLine("You bought " + productQuantity + " " + prods[productNumber].Name); + Console.WriteLine("Your new balance is " + userBalance.ToString("C")); Console.ResetColor(); } else @@ -226,5 +192,44 @@ public static void Start(List usrs, List prods) Console.WriteLine("Press Enter key to exit"); Console.ReadLine(); } + + private static bool IsPasswordValid(List usrs, string userName, int totalUsers, string userPassword) + { + bool isPasswordValid = false; + for (int i = 0; i < totalUsers; i++) + { + User user = usrs[i]; + + // Check that name and password match + if (user.Name == userName && user.Pwd == userPassword) + { + isPasswordValid = true; + } + } + return isPasswordValid; + } + + private static bool IsUserValid(List usrs, string userName, int totalUsers) + { + bool isUserValid = false; + + for (int i = 0; i < totalUsers; i++) + { + User user = usrs[i]; + if (user.Name == userName) + { + isUserValid = true; + } + } + + return isUserValid; + } + + private static string PromptForUserInput(string message) + { + Console.WriteLine(message); + string userInput = Console.ReadLine(); + return userInput; + } } }