diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..18fa58b 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -8,200 +8,60 @@ namespace Refactoring { + public class Tusc { - public static void Start(List usrs, List prods) + + static string name; + static string password; + static List userList; + static List productListing; + + public static void Start(List usersList, List productList) { - // Write welcome message + + userList = usersList; + productListing = productList; + Console.WriteLine("Welcome to TUSC"); Console.WriteLine("---------------"); + bool loggedIn = false; // Is logged in? // Login Login: - bool loggedIn = false; // Is logged in? - - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + + name = promptForUserName(); - // Validate Username - bool valUsr = false; // Is valid user? + bool validUser = false; 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; - } - } + validUser = isUserInList(); - // if valid user - if (valUsr) + if (validUser) { - // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + password = promptForUserPassword(); - // 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; - } - } + bool validPassword = isPasswordValid(); - // if valid password - if (valPwd == true) + if (validPassword == true) { loggedIn = true; // Show welcome message - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(); + initializeConsole(ConsoleColor.Green); 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]; - - // 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(); - } - } - } + double balance = showRemainingBalance(); + + processRequests(ref balance); + + return; } else { // Invalid Password - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); + initializeConsole(ConsoleColor.Red); Console.WriteLine("You entered an invalid password."); Console.ResetColor(); @@ -211,9 +71,7 @@ public static void Start(List usrs, List prods) else { // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); + initializeConsole(ConsoleColor.Red); Console.WriteLine("You entered an invalid user."); Console.ResetColor(); @@ -226,5 +84,223 @@ public static void Start(List usrs, List prods) Console.WriteLine("Press Enter key to exit"); Console.ReadLine(); } + + private static void processRequests(ref double balance) + { + int numberOfProductRequested; + string answer; + // Show product list + while (true) + { + // Prompt for user input + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + displayProductList(); + numberOfProductRequested = requestNumberOfProduct(); + + // Check if user entered number that equals product count + if (numberOfProductRequested == productListing.Count) + { + // Update balance + updateBalance(balance); + + // Write out new balance + updateJsonFiles(); + + // 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: " + productListing[numberOfProductRequested].Name); + Console.WriteLine("Your balance is " + balance.ToString("C")); + + // Prompt for user input + Console.WriteLine("Enter amount to purchase:"); + answer = Console.ReadLine(); + int quantity = Convert.ToInt32(answer); + bool continueProcessing; + // Check if balance - quantity * price is less than 0 + continueProcessing = validateSale(balance, numberOfProductRequested, quantity); + + if (continueProcessing) + { + continue; + } + + // Check if quantity is greater than zero + if (quantity > 0) + { + // Balance = Balance - Price * Quantity + balance = finalizeSale(balance, numberOfProductRequested, quantity); + } + else + { + // Quantity is less than zero + initializeConsole(ConsoleColor.Yellow); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + } + } + } + + private static double finalizeSale(double balance, int numberOfProductRequested, int quantity) + { + balance = balance - productListing[numberOfProductRequested].Price * quantity; + + // Quanity = Quantity - Quantity + productListing[numberOfProductRequested].Qty = productListing[numberOfProductRequested].Qty - quantity; + + initializeConsole(ConsoleColor.Green); + Console.WriteLine("You bought " + quantity + " " + productListing[numberOfProductRequested].Name); + Console.WriteLine("Your new balance is " + balance.ToString("C")); + Console.ResetColor(); + return balance; + } + + private static bool validateSale(double balance, int numberOfProductRequested, int quantity) + { + if (balance - productListing[numberOfProductRequested].Price * quantity < 0) + { + initializeConsole(ConsoleColor.Red); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + return true; + } + + + // Check if quantity is less than quantity + if (productListing[numberOfProductRequested].Qty < quantity) + { + initializeConsole(ConsoleColor.Red); + Console.WriteLine("Sorry, " + productListing[numberOfProductRequested].Name + " is out of stock"); + Console.ResetColor(); + return true; + } + + return false; + } + + private static void updateJsonFiles() + { + string json = JsonConvert.SerializeObject(userList, Formatting.Indented); + File.WriteAllText(@"Data/Users.json", json); + + // Write out new quantities + string json2 = JsonConvert.SerializeObject(productListing, Formatting.Indented); + File.WriteAllText(@"Data/Products.json", json2); + } + + private static int requestNumberOfProduct() + { + string answer; + int numberOfProductRequested; + + Console.WriteLine("Enter a number:"); + answer = Console.ReadLine(); + numberOfProductRequested = Convert.ToInt32(answer); + numberOfProductRequested = numberOfProductRequested - 1; + + return numberOfProductRequested; + } + + private static void updateBalance(double balance) + { + foreach (var user in userList) + { + // Check that name and password match + if (user.Name == name && user.Pwd == password) + { + user.Bal = balance; + } + } + } + + private static void initializeConsole(ConsoleColor color) + { + Console.Clear(); + Console.ForegroundColor = color; + Console.WriteLine(); + } + + private static double showRemainingBalance() + { + double balance = 0; + for (int userRow = 0; userRow < 5; userRow++) + { + User user = userList[userRow]; + + // Check that name and password match + if (user.Name == name && user.Pwd == password) + { + balance = user.Bal; + + // Show balance + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.Bal.ToString("C")); + } + } + return balance; + } + + private static string promptForUserPassword() + { + Console.WriteLine("Enter Password:"); + string pwd = Console.ReadLine(); + return pwd; + } + + private static bool isPasswordValid() + { + bool valPwd = false; + for (int userRow = 0; userRow < userList.Count; userRow++) + { + User user = userList[userRow]; + + if (user.Name == name && user.Pwd == password) + { + valPwd = true; + } + } + return valPwd; + } + + private static string promptForUserName() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + string name = Console.ReadLine(); + return name; + } + + private static bool isUserInList() + { + bool valUsr = false; + for (int userRow = 0; userRow < userList.Count; userRow++) + { + User user = userList[userRow]; + // Check that name matches + if (user.Name == name) + { + valUsr = true; + } + } + return valUsr; + } + + private static void displayProductList() + { + for (int productRow = 0; productRow < productListing.Count; productRow++) + { + Product product = productListing[productRow]; + Console.WriteLine(productRow + 1 + ": " + product.Name + " (" + product.Price.ToString("C") + ")"); + } + Console.WriteLine(productListing.Count + 1 + ": Exit"); + } } }