diff --git a/Refactoring/Data/Users.json b/Refactoring/Data/Users.json index b25cd80..83dd48e 100644 --- a/Refactoring/Data/Users.json +++ b/Refactoring/Data/Users.json @@ -23,5 +23,10 @@ "Username": "Admin", "Password": "admin", "Balance": 1000000.0 + }, + { + "Username": "Jay", + "Password": "password", + "Balance": 1000000.0 } ] \ No newline at end of file diff --git a/Refactoring/Program.cs b/Refactoring/Program.cs index f0509aa..9847731 100644 --- a/Refactoring/Program.cs +++ b/Refactoring/Program.cs @@ -17,7 +17,8 @@ public static void Main(string[] args) // Load products from data file List products = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + tusc.Start(users, products); } } } diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..6c73826 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -10,221 +10,271 @@ namespace Refactoring { public class Tusc { - public static void Start(List usrs, List prods) + private const int EXIT = 7; + User user; + + public void Start(List users, List prods) { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); + WelcomeMessage(); - // Login - Login: - bool loggedIn = false; // Is logged in? + var authenticated = false; - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + do + { + authenticated = AuthenticateUser(users); + }while(!authenticated); + + ShowLoginSuccessful(user.Name); + ShowBalance(user); - // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + // Show product list + while (true) { - for (int i = 0; i < 5; i++) + ShowProducts(prods); + + // Prompt for user input + Console.WriteLine("Enter a number:"); + string answer = Console.ReadLine(); + int num = Convert.ToInt32(answer); + num = num - 1; + + // Check if user entered number that equals product count + if (num == EXIT) { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } - } + UpdateBalance(users, user); + SaveBalance(users); + SaveQuantities(prods); - // if valid user - if (valUsr) + // 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); + ShowBalance(user); + // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + Console.WriteLine("Enter amount to purchase:"); + answer = Console.ReadLine(); + int qty = Convert.ToInt32(answer); - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + // Check if balance - quantity * price is less than 0 + if (user.Bal - prods[num].Price * qty < 0) { - User user = usrs[i]; + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + continue; + } - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) - { - valPwd = true; - } + // 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; } - // if valid password - if (valPwd == true) + // Check if quantity is greater than zero + if (qty > 0) { - loggedIn = true; + // Balance = Balance - Price * Quantity + user.Bal = user.Bal - prods[num].Price * qty; + + // Quanity = Quantity - Quantity + prods[num].Qty = prods[num].Qty - qty; - // Show welcome message Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(); - Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.WriteLine("You bought " + qty + " " + prods[num].Name); + Console.WriteLine("Your new balance is " + user.Bal.ToString("C")); 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")); - } - } - - // 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; + // Quantity is less than zero + ShowPurchaseCancelled(); } } + + } + } + + public bool AuthenticateUser(List users) + { + bool authenticated = false; + + user = null; + + string name = PromptForUsername(); + + user = GetUser(users, name); + + if (user != null) + { + string pwd = PromptForPassword(); + + // if valid password + if (PasswordIsValid(user, name, pwd)) + { + authenticated = true; + } else { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); - - goto Login; + ShowInvalidPassword(); } } + else + { + ShowInvalidUser(); + } + + return authenticated; + } - // Prevent console from closing + private static void ShowPurchaseCancelled() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + + private static void ShowInvalidPassword() + { + // Invalid Password + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + } + + private static void ShowInvalidUser() + { + // Invalid User + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + } + + private static void ShowProducts(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 void ShowBalance(User user) + { + // Show balance + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.Bal.ToString("C")); + } + + private static void ShowLoginSuccessful(string name) + { + // Show welcome message + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.ResetColor(); + } + + private static bool PasswordIsValid(User user, string name, string pwd) + { + if (user.Name == name && user.Pwd == pwd) + { + return true; + } + else + { + return false; + } + } + + private static string PromptForPassword() + { + // Prompt for user input + Console.WriteLine("Enter Password:"); + string pwd = Console.ReadLine(); + return pwd; + } + + private static string PromptForUsername() + { + // Prompt for user input + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + string name = Console.ReadLine(); + return name; + } + + private User GetUser(List users, string name) + { + User foundUser = null; + + foreach (User user in users) + { + if (user.Name == name) + { + foundUser = user; + break; + } + } + return foundUser; + } + + private static void SaveQuantities(List prods) + { + // Write out new quantities + string json2 = JsonConvert.SerializeObject(prods, Formatting.Indented); + File.WriteAllText(@"Data/Products.json", json2); + } + + private static void SaveBalance(List usrs) + { + // Write out new balance + string json = JsonConvert.SerializeObject(usrs, Formatting.Indented); + File.WriteAllText(@"Data/Users.json", json); + } + + private static void UpdateBalance(List users, User user) + { + // Update balance + foreach (var foundUser in users) + { + // Check that name and password match + if (foundUser.Name == user.Name && foundUser.Pwd == user.Pwd) + { + foundUser.Bal = user.Bal; + } + } + } + + private static void WelcomeMessage() + { + // Write welcome message + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); } } } diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..22007c7 100644 --- a/UnitTestProject/UnitTests.cs +++ b/UnitTestProject/UnitTests.cs @@ -70,7 +70,9 @@ public void Test_TuscDoesNotThrowAnException() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + + tusc.Start(users, products); } } } @@ -86,7 +88,9 @@ public void Test_InvalidUserIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + + tusc.AuthenticateUser(users); } Assert.IsTrue(writer.ToString().Contains("You entered an invalid user")); @@ -104,7 +108,9 @@ public void Test_EmptyUserDoesNotThrowAnException() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + + tusc.AuthenticateUser(users); } } } @@ -120,7 +126,9 @@ public void Test_InvalidPasswordIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + + tusc.AuthenticateUser(users); } Assert.IsTrue(writer.ToString().Contains("You entered an invalid password")); @@ -138,7 +146,9 @@ public void Test_UserCanCancelPurchase() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(); + + tusc.Start(users, products); } Assert.IsTrue(writer.ToString().Contains("Purchase cancelled")); @@ -161,7 +171,9 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice() { Console.SetIn(reader); - Tusc.Start(tempUsers, products); + Tusc tusc = new Tusc(); + + tusc.Start(tempUsers, products); } Assert.IsTrue(writer.ToString().Contains("You do not have enough money to buy that")); @@ -183,7 +195,9 @@ public void Test_ErrorOccursWhenProductOutOfStock() { Console.SetIn(reader); - Tusc.Start(users, tempProducts); + Tusc tusc = new Tusc(); + + tusc.Start(users, tempProducts); } Assert.IsTrue(writer.ToString().Contains("is out of stock"));