From 21edc211cc595778942a46b3511497256d57edca Mon Sep 17 00:00:00 2001 From: VarianLuke Date: Fri, 29 Jan 2016 14:31:17 -0600 Subject: [PATCH 1/2] Luke's changes. (It technically runs.) --- Refactoring/Product.cs | 12 +- Refactoring/Program.cs | 10 +- Refactoring/Refactoring.csproj | 2 + Refactoring/Tusc.cs | 333 ++++++++++++++------------------- Refactoring/User.cs | 9 +- UnitTestProject/UnitTests.cs | 32 +++- 6 files changed, 191 insertions(+), 207 deletions(-) diff --git a/Refactoring/Product.cs b/Refactoring/Product.cs index c9ceee5..edd10eb 100644 --- a/Refactoring/Product.cs +++ b/Refactoring/Product.cs @@ -15,6 +15,16 @@ public class Product [JsonProperty("Price")] public double Price; [JsonProperty("Quantity")] - public int Qty; + public int Quantity; + + internal double GetPrice(int selectedQuantity) + { + return (Price * selectedQuantity); + } + + internal bool HasEnoughInventory(int selectedQuantity) + { + return (Quantity >= selectedQuantity); + } } } diff --git a/Refactoring/Program.cs b/Refactoring/Program.cs index f0509aa..c3b4d70 100644 --- a/Refactoring/Program.cs +++ b/Refactoring/Program.cs @@ -1,9 +1,6 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; namespace Refactoring { @@ -12,12 +9,13 @@ public class Program public static void Main(string[] args) { // Load users from data file - List users = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Users.json")); + List users = FileManager.LoadJsonFile(@"Data/Users.json"); // Load products from data file - List products = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); + List products = FileManager.LoadJsonFile(@"Data/Products.json"); - Tusc.Start(users, products); + var tusc = new Tusc(users, products); + tusc.Start(); } } } diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj index 6696ba9..0fb5a7b 100644 --- a/Refactoring/Refactoring.csproj +++ b/Refactoring/Refactoring.csproj @@ -45,11 +45,13 @@ + + diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs index bd07dce..b8350b5 100644 --- a/Refactoring/Tusc.cs +++ b/Refactoring/Tusc.cs @@ -10,221 +10,176 @@ namespace Refactoring { public class Tusc { - public static void Start(List usrs, List prods) + private List _userList; + private List _productList; + private User _currentUser; + private bool _isDisplayingProductList = false; + + private const int MENU_CHOICE_EXIT = 7; + + public Tusc(List userList, List productList) + { + _userList = userList; + _productList = productList; + } + + public void Start() { - // Write welcome message - Console.WriteLine("Welcome to TUSC"); - Console.WriteLine("---------------"); + if (_userList == null || _productList == null) + { + throw new Exception("Application not started correctly."); + } + + UserInterface.DisplayTuscWelcomeMessage(); + + var loggedIn = BeginUserLogin(); + + + UserInterface.DisplayUserWelcomeMessage(_currentUser); + + UserInterface.DisplayUserBalance(_currentUser); + + ShowProductList(); + + UserInterface.WaitForUserInputBeforeClosing(); + } + + private bool BeginUserLogin() + { // Login Login: - bool loggedIn = false; // Is logged in? - // Prompt for user input - Console.WriteLine(); - Console.WriteLine("Enter Username:"); - string name = Console.ReadLine(); + string enteredUserName = UserInterface.PromptForUserName(); + + if (string.IsNullOrEmpty(enteredUserName)) + { + goto Login; + } // Validate Username - bool valUsr = false; // Is valid user? - if (!string.IsNullOrEmpty(name)) + var isUserNameValid = IsUserInList(enteredUserName); + + if (!isUserNameValid) { - for (int i = 0; i < 5; i++) + UserInterface.DisplayInvalidUserMessage(); + + goto Login; + } + + _currentUser = GetUserFromList(enteredUserName); + + string password = UserInterface.PromptForPassword(); + + var isPasswordValid = ValidatePassword(password); + + if (!isPasswordValid) + { + UserInterface.DisplayInvalidPasswordMessage(); + + goto Login; + } + + return true; + } + + private void ShowProductList() + { + _isDisplayingProductList = true; + + // Show product list + while (_isDisplayingProductList) + { + // 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 = _productList[i]; + Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")"); } + Console.WriteLine(_productList.Count + 1 + ": Exit"); - // if valid user - if (valUsr) + // Prompt for user input + var menuChoice = UserInterface.PromptForNumber("Enter a number:"); + menuChoice = menuChoice - 1; + + // Check if user entered number that equals product count + if (menuChoice == MENU_CHOICE_EXIT) { + SaveModifications(); + _isDisplayingProductList = false; + } + else + { + var product = _productList[menuChoice]; + + UserInterface.DisplayPurchaseChoiceInfo(_currentUser, product); + // Prompt for user input - Console.WriteLine("Enter Password:"); - string pwd = Console.ReadLine(); + Console.WriteLine(); + int selectedQuantity = UserInterface.PromptForNumber("Enter amount to purchase:"); + + var totalPrice = product.GetPrice(selectedQuantity); - // Validate Password - bool valPwd = false; // Is valid password? - for (int i = 0; i < 5; i++) + if (!_currentUser.HasEnoughMoney(totalPrice)) { - User user = usrs[i]; + UserInterface.DisplayUserInsufficientFundsMessage(); + continue; + } - // Check that name and password match - if (user.Name == name && user.Pwd == pwd) - { - valPwd = true; - } + // Check if quantity is less than quantity + if (!product.HasEnoughInventory(selectedQuantity)) + { + UserInterface.DisplayUserInsufficientQuantityMessage(product); + continue; } - // if valid password - if (valPwd == true) + // Check if quantity is greater than zero + if (selectedQuantity > 0) { - 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(); - } - } - } + _currentUser.Balance -= totalPrice; + + product.Quantity -= selectedQuantity; + + UserInterface.DisplayUserPurchase(_currentUser, product, selectedQuantity); } else { - // Invalid Password - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid password."); - Console.ResetColor(); - - goto Login; + UserInterface.DisplayQuantityLessThanZeroMessage(); } } - else - { - // Invalid User - Console.Clear(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.WriteLine("You entered an invalid user."); - Console.ResetColor(); + } + } - goto Login; - } + + + private void SaveModifications() + { + FileManager.SaveJsonFile(@"Data/Users.json", _userList); + FileManager.SaveJsonFile(@"Data/Products.json", _productList); + } + + private bool IsUserInList(string enteredUserName) + { + var userFound = _userList.FirstOrDefault(u => u.Name == enteredUserName); + + return (userFound != null); + } + + private User GetUserFromList(string enteredUserName) + { + return _userList.FirstOrDefault(u => u.Name == enteredUserName); + } + + private bool ValidatePassword(string password) + { + if (_currentUser == null) + { + return false; } - // Prevent console from closing - Console.WriteLine(); - Console.WriteLine("Press Enter key to exit"); - Console.ReadLine(); + return (password == _currentUser.Password); } } -} +} \ No newline at end of file diff --git a/Refactoring/User.cs b/Refactoring/User.cs index fdc34e8..7b761d4 100644 --- a/Refactoring/User.cs +++ b/Refactoring/User.cs @@ -13,8 +13,13 @@ public class User [JsonProperty("Username")] public string Name; [JsonProperty("Password")] - public string Pwd; + public string Password; [JsonProperty("Balance")] - public double Bal; + public double Balance; + + internal bool HasEnoughMoney(double price) + { + return (Balance >= price); + } } } diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs index 51a30ad..13bbfb1 100644 --- a/UnitTestProject/UnitTests.cs +++ b/UnitTestProject/UnitTests.cs @@ -70,7 +70,9 @@ public void Test_TuscDoesNotThrowAnException() { Console.SetIn(reader); - Tusc.Start(users, products); + var tusc = new Tusc(users, products); + + tusc.Start(); } } } @@ -86,7 +88,9 @@ public void Test_InvalidUserIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + var tusc = new Tusc(users, products); + + tusc.Start(); } 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); + var tusc = new Tusc(users, products); + + tusc.Start(); } } } @@ -120,7 +126,9 @@ public void Test_InvalidPasswordIsNotAccepted() { Console.SetIn(reader); - Tusc.Start(users, products); + var tusc = new Tusc(users, products); + + tusc.Start(); } 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); + var tusc = new Tusc(users, products); + + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("Purchase cancelled")); @@ -151,7 +161,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.Name == "Jason").Single().Balance = 0.0; using (var writer = new StringWriter()) { @@ -161,7 +171,9 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice() { Console.SetIn(reader); - Tusc.Start(tempUsers, products); + var tusc = new Tusc(users, products); + + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("You do not have enough money to buy that")); @@ -173,7 +185,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 +195,9 @@ public void Test_ErrorOccursWhenProductOutOfStock() { Console.SetIn(reader); - Tusc.Start(users, tempProducts); + var tusc = new Tusc(users, products); + + tusc.Start(); } Assert.IsTrue(writer.ToString().Contains("is out of stock")); From c61b5595dcdfbb9e17046b70d4a3cb9412638b33 Mon Sep 17 00:00:00 2001 From: VarianLuke Date: Fri, 29 Jan 2016 16:03:53 -0600 Subject: [PATCH 2/2] Luke - Adding missing files --- Refactoring/FileManager.cs | 26 ++++++++ Refactoring/UserInterface.cs | 122 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 Refactoring/FileManager.cs create mode 100644 Refactoring/UserInterface.cs diff --git a/Refactoring/FileManager.cs b/Refactoring/FileManager.cs new file mode 100644 index 0000000..997ab82 --- /dev/null +++ b/Refactoring/FileManager.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.IO; + +namespace Refactoring +{ + class FileManager + { + public static List LoadJsonFile(string fileName) + { + var fileText = File.ReadAllText(fileName); + + return JsonConvert.DeserializeObject>(fileText); + } + + public static void SaveJsonFile(string fileName, List list) + { + string json = JsonConvert.SerializeObject(list, Formatting.Indented); + File.WriteAllText(fileName, json); + } + } +} diff --git a/Refactoring/UserInterface.cs b/Refactoring/UserInterface.cs new file mode 100644 index 0000000..87d635c --- /dev/null +++ b/Refactoring/UserInterface.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Refactoring +{ + internal static class UserInterface + { + internal static void DisplayTuscWelcomeMessage() + { + Console.WriteLine("Welcome to TUSC"); + Console.WriteLine("---------------"); + } + + internal static void DisplayUserWelcomeMessage(User user) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(); + Console.WriteLine("Login successful! Welcome " + user.Name + "!"); + Console.ResetColor(); + } + + internal static void DisplayInvalidPasswordMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid password."); + Console.ResetColor(); + } + + internal static void DisplayInvalidUserMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You entered an invalid user."); + Console.ResetColor(); + } + + internal static void DisplayUserBalance(User user) + { + // Show balance + Console.WriteLine(); + Console.WriteLine("Your balance is " + user.Balance.ToString("C")); + } + + internal static void DisplayUserPurchase(User user, Product product, int quantity) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("You bought " + quantity + " " + product.Name); + Console.WriteLine("Your new balance is " + user.Balance.ToString("C")); + Console.ResetColor(); + } + + internal static void DisplayQuantityLessThanZeroMessage() + { + // Quantity is less than zero + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(); + Console.WriteLine("Purchase cancelled"); + Console.ResetColor(); + } + + internal static void DisplayPurchaseChoiceInfo(User user, Product product) + { + Console.WriteLine(); + Console.WriteLine("You want to buy: " + product.Name); + Console.WriteLine("Your balance is " + user.Balance.ToString("C")); + } + + internal static void DisplayUserInsufficientQuantityMessage(Product product) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("Sorry, " + product.Name + " is out of stock"); + Console.ResetColor(); + } + + internal static void DisplayUserInsufficientFundsMessage() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(); + Console.WriteLine("You do not have enough money to buy that."); + Console.ResetColor(); + } + + internal static string PromptForUserName() + { + Console.WriteLine(); + Console.WriteLine("Enter Username:"); + return Console.ReadLine(); + } + + internal static string PromptForPassword() + { + Console.WriteLine("Enter Password:"); + return Console.ReadLine(); + } + + internal static int PromptForNumber(string message) + { + Console.WriteLine(message); + var enteredString = Console.ReadLine(); + return Convert.ToInt32(enteredString); + } + + internal static void WaitForUserInputBeforeClosing() + { + Console.WriteLine(); + Console.WriteLine("Press Enter key to exit"); + Console.ReadLine(); + } + } +}