diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..21a1394 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -10,221 +10,247 @@ namespace Refactoring { public class Tusc { + + + public static List usrs = new List(); + + public static string name { get; set;} + public static string pwd { get; set; } + public static double bal = 0; + public static void Start(List usrs, List prods) { + Tusc tusc = new Tusc(); + tusc.Login(usrs, pwd); // 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(); - - // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + // Show product list + while (true) { - for (int i = 0; i < 5; i++) + // Prompt for user input + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + for (int i = 0; i < 7; i++) { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } + Product prod = prods[i]; + Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")"); } + Console.WriteLine(prods.Count + 1 + ": Exit"); - // if valid user - if (valUsr) - { - // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + // 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 */ - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + // Check if user entered number that equals product count + if (num == 7) + { + // Update balance + foreach (var usr in usrs) { - User user = usrs[i]; - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) + if (usr.Name == name && usr.Pwd == pwd) { - valPwd = true; + usr.Bal = bal; } } - // if valid password - if (valPwd == true) - { - loggedIn = true; + // 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); - // Show welcome message + // Check if balance - quantity * price is less than 0 + if (bal - prods[num].Price * qty < 0) + { Console.Clear(); - Console.ForegroundColor = ConsoleColor.Green; + Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); - Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.WriteLine("You do not have enough money to buy that."); Console.ResetColor(); - - // Show remaining balance - double bal = 0; - for (int i = 0; i < 5; i++) - { - User usr = usrs[i]; + continue; + } - // Check that name and password match - if (usr.Name == name && usr.Pwd == pwd) - { - bal = usr.Bal; + // 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; + } - // Show balance - Console.WriteLine(); - Console.WriteLine("Your balance is " + usr.Bal.ToString("C")); - } - } + // Check if quantity is greater than zero + if (qty > 0) + { + // Balance = Balance - Price * Quantity + bal = bal - prods[num].Price * qty; - // 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 */ + // Quanity = Quantity - Quantity + prods[num].Qty = prods[num].Qty - qty; - // 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(); - } - } - } + 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 { - // Invalid Password + // Quantity is less than zero Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; + Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(); - Console.WriteLine("You entered an invalid password."); + Console.WriteLine("Purchase cancelled"); Console.ResetColor(); + } + } + } + } + + + // Login + public bool Login(List usrs, string pwd) + { + // if username and password valid + bool LoggedIn = false; + + if (ValidateUserName(usrs) == true) + { + + if (ValidatePwd(pwd) == true) + { + LoggedIn = true; + // Show welcome message + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + name + "!"); + Console.ResetColor(); + + // Show remaining balance + + for (int i = 0; i < 5; i++) + { + User usr = usrs[i]; - goto Login; + // 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")); + } } } else { - // Invalid User + // Invalid Password Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); + Console.WriteLine("You entered an invalid password."); Console.ResetColor(); - goto Login; - } + Login(usrs, pwd); + } + } + else + { + // Invalid User + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + + Login(usrs, pwd); } + return LoggedIn; - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + //// Prevent console from closing + //Console.WriteLine(); + //Console.WriteLine("Press Enter key to exit"); + //Console.ReadLine(); + } + + //Validate User Name + public bool ValidateUserName(List usrs) + { + // Prompt for user input + Console.WriteLine("Enter Username:"); + string name = Console.ReadLine(); + + // 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; + } + } + } + return valUsr; + } + + //Validate Password + public bool ValidatePwd(string password) + { + // Prompt for password 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; + } + } + return valPwd; } } } + + + +