diff --git a/Refactoring/Product.cs b/Refactoring/Product.cs index c9ceee5..7e8c5e0 100644 --- a/Refactoring/Product.cs +++ b/Refactoring/Product.cs @@ -7,14 +7,16 @@ namespace Refactoring { - [Serializable] - public class Product - { - [JsonProperty("Name")] - public string Name; - [JsonProperty("Price")] - public double Price; - [JsonProperty("Quantity")] - public int Qty; - } + [Serializable] + public class Product + { + [JsonProperty("Name")] + public string Name; + + [JsonProperty("Price")] + public double Price; + + [JsonProperty("Quantity")] + public int Quantity; + } } diff --git a/Refactoring/Program.cs b/Refactoring/Program.cs index f0509aa..e1532c9 100644 --- a/Refactoring/Program.cs +++ b/Refactoring/Program.cs @@ -7,17 +7,25 @@ namespace Refactoring { - public class Program + public class Program + { + public static void Main(string[] args) { - public static void Main(string[] args) - { - // Load users from data file - List users = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Users.json")); + List users = getUsers(); + List products = getProducts(); - // Load products from data file - List products = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); + Tusc tusc = new Tusc(users, products); + tusc.Start(); + } - Tusc.Start(users, products); - } + private static List getProducts() + { + return JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); + } + + private static List getUsers() + { + return JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Users.json")); } + } } diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj index 6696ba9..a18268a 100644 --- a/Refactoring/Refactoring.csproj +++ b/Refactoring/Refactoring.csproj @@ -49,6 +49,7 @@ + diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..e9a85dc 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -8,223 +8,235 @@ namespace Refactoring { - public class Tusc + public class Tusc + { + private List users; + private List products; + + public Tusc(List users, List products) + { + this.users = users; + this.products = products; + } + + public void Start() + { + showWelcome(); + + User currentUser = performUserLogin(users); + if (currentUser != null) + { + showLoginSuccess(currentUser); + showBalance(currentUser); + processUserSelections(currentUser); + } + + showGoodbye(); + } + + private void processUserSelections(User currentUser) { - public static void Start(List usrs, List prods) + while (true) + { + showProductList(); + + int selectedProduct = getProductSelection(); + if (userSelectedExit(selectedProduct)) { - // 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)) - { - for (int i = 0; i < 5; i++) - { - User user = usrs[i]; - // Check that name matches - if (user.Name == name) - { - valUsr = true; - } - } - - // if valid user - if (valUsr) - { - // Prompt for user 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; - } - } - - // 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.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; - } - } - else - { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); - - goto Login; - } - } - - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + onExit(); + return; } + else + { + if (!processProductSelection(currentUser, selectedProduct)) + { + continue; + } + } + } + } + + private bool processProductSelection(User currentUser, int selectedProduct) + { + showProductSelectionSuccess(currentUser, selectedProduct); + + int selectedQuantity = getQuantitySelection(); + double costToPurchase = products[selectedProduct].Price * selectedQuantity; + if (currentUser.balance < costToPurchase) + { + showInsufficientBalanceMessage(); + return false; + } + + int remainingQuantity = products[selectedProduct].Quantity; + if (selectedQuantity > remainingQuantity) + { + showInsufficientQuantityMessage(selectedProduct); + return false; + } + + if (selectedQuantity > 0) + { + processPurchase(currentUser, selectedProduct, selectedQuantity); + } + else + { + showSelectedQuantityNegativeMessage(); + } + + return true; + } + + private void onExit() + { + updateBalances(); + updateQuantities(); + showGoodbye(); + } + + private void processPurchase(User currentUser, int selectedProduct, int selectedQuantity) + { + currentUser.balance -= products[selectedProduct].Price * selectedQuantity; + products[selectedProduct].Quantity -= selectedQuantity; + + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("You bought " + selectedQuantity + " " + products[selectedProduct].Name); + Console.WriteLine("Your new balance is " + currentUser.balance.ToString("C")); + Console.ResetColor(); + } + + private static void showSelectedQuantityNegativeMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + + private void showInsufficientQuantityMessage(int selection) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("Sorry, " + products[selection].Name + " is out of stock"); + Console.ResetColor(); + } + + private static void showInsufficientBalanceMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + } + + private void showProductSelectionSuccess(User currentUser, int selection) + { + Console.WriteLine(); + Console.WriteLine("You want to buy: " + products[selection].Name); + Console.WriteLine("Your balance is " + currentUser.balance.ToString("C")); + } + + private static int getQuantitySelection() + { + Console.WriteLine("Enter amount to purchase:"); + string answer = Console.ReadLine(); + int qty = Convert.ToInt32(answer); + return qty; + } + + private void updateQuantities() + { + string json2 = JsonConvert.SerializeObject(products, Formatting.Indented); + File.WriteAllText(@"Data/Products.json", json2); + } + + private void updateBalances() + { + string json = JsonConvert.SerializeObject(users, Formatting.Indented); + File.WriteAllText(@"Data/Users.json", json); + } + + private static int getProductSelection() + { + Console.WriteLine("Enter a number:"); + string answer = Console.ReadLine(); + return Convert.ToInt32(answer) - 1; + } + + private void showProductList() + { + Console.WriteLine(); + Console.WriteLine("What would you like to buy?"); + + int productIndex = 0; + foreach (Product product in products) + { + productIndex++; + Console.WriteLine(productIndex + ": " + product.Name + " (" + product.Price.ToString("C") + ")"); + } + Console.WriteLine(products.Count + 1 + ": Exit"); + } + + private static void showBalance(User user) + { + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.balance.ToString("C")); + } + + private static void showLoginSuccess(User user) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + user.username + "!"); + Console.ResetColor(); + } + + private static User performUserLogin(List users) + { + User user = UserAuthentication.getUserByUsername(users, getUsername()); + + if (user != null) + { + user = UserAuthentication.validatePassword(user, getPassword()); + } + + return user; + } + + private static string getPassword() + { + Console.WriteLine("Enter Password:"); + return Console.ReadLine(); + } + + private static string getUsername() + { + Console.WriteLine("Enter Username:"); + return Console.ReadLine(); + } + + private static void showGoodbye() + { + Console.WriteLine(); + Console.WriteLine("Press Enter key to exit"); + Console.ReadLine(); + } + + private static void showWelcome() + { + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + Console.WriteLine(); + } + + private bool userSelectedExit(int selection) + { + return (selection == products.Count); } + } } diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..0004a9d 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -7,14 +7,16 @@ namespace Refactoring { - [Serializable] - public class User - { - [JsonProperty("Username")] - public string Name; - [JsonProperty("Password")] - public string Pwd; - [JsonProperty("Balance")] - public double Bal; - } + [Serializable] + public class User + { + [JsonProperty("Username")] + public string username; + + [JsonProperty("Password")] + public string password; + + [JsonProperty("Balance")] + public double balance; + } } diff --git a/Refactoring/UserAuthentication.cs b/Refactoring/UserAuthentication.cs new file mode 100644 index 0000000..b2743be --- /dev/null +++ b/Refactoring/UserAuthentication.cs @@ -0,0 +1,69 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Refactoring +{ + public class UserAuthentication + { + public static User getUserByUsername(List users, string username) + { + User foundUser = null; + + if (!string.IsNullOrEmpty(username)) + { + foreach (User user in users) + { + if (user.username == username) + { + foundUser = user; + } + } + } + + if (foundUser == null) + { + showInvalidUsernameMessage(); + } + + return foundUser; + } + + private static void showInvalidUsernameMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + } + + public static User validatePassword(User user, string password) + { + User validUser = null; + + if (user.password == password) + { + validUser = user; + } + else + { + showInvalidPasswordMessage(); + } + + return validUser; + } + + private static void showInvalidPasswordMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + } + } +} diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..2f5c61b 100644 --- a/UnitTestProject/UnitTests.cs +++ b/UnitTestProject/UnitTests.cs @@ -70,7 +70,8 @@ public void Test_TuscDoesNotThrowAnException() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(users, products); + tusc.Start(); } } } @@ -86,7 +87,8 @@ public void Test_InvalidUserIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(users, products); + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("You entered an invalid user")); @@ -104,7 +106,8 @@ public void Test_EmptyUserDoesNotThrowAnException() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(users, products); + tusc.Start(); } } } @@ -120,7 +123,8 @@ public void Test_InvalidPasswordIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(users, products); + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("You entered an invalid password")); @@ -138,7 +142,8 @@ public void Test_UserCanCancelPurchase() { Console.SetIn(reader); - Tusc.Start(users, products); + Tusc tusc = new Tusc(users, products); + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("Purchase cancelled")); @@ -151,7 +156,7 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice() { // Update data file List tempUsers = DeepCopy>(originalUsers); - tempUsers.Where(u => u.Name == "Jason").Single().Bal = 0.0; + tempUsers.Where(u => u.username == "Jason").Single().balance = 0.0; using (var writer = new StringWriter()) { @@ -161,7 +166,8 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice() { Console.SetIn(reader); - Tusc.Start(tempUsers, products); + Tusc tusc = new Tusc(tempUsers, products); + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("You do not have enough money to buy that")); @@ -173,7 +179,7 @@ public void Test_ErrorOccursWhenProductOutOfStock() { // Update data file List tempProducts = DeepCopy>(originalProducts); - tempProducts.Where(u => u.Name == "Chips").Single().Qty = 0; + tempProducts.Where(u => u.Name == "Chips").Single().Quantity = 0; using (var writer = new StringWriter()) { @@ -183,7 +189,8 @@ public void Test_ErrorOccursWhenProductOutOfStock() { Console.SetIn(reader); - Tusc.Start(users, tempProducts); + Tusc tusc = new Tusc(users, tempProducts); + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("is out of stock"));