diff --git a/Refactoring/PrintingCommands.cs b/Refactoring/PrintingCommands.cs
new file mode 100644
index 0000000..89928b0
--- /dev/null
+++ b/Refactoring/PrintingCommands.cs
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Refactoring
+{
+ class PrintingCommands
+ {
+ public static void PrintWelcomeMessage()
+ {
+ // Write welcome message test
+ Console.WriteLine("Welcome to TUSC");
+ Console.WriteLine("---------------");
+ }
+
+ public static void PrintUserNameInput()
+ {
+ // Prompt for user input
+ Console.WriteLine();
+ Console.WriteLine("Enter Username:");
+
+ }
+
+ public static void PrintInvalidUser()
+ {
+ // Invalid User
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Red;
+ Console.WriteLine();
+ Console.WriteLine("You entered an invalid user.");
+ Console.ResetColor();
+ }
+
+ public static void PrintInvalidPassword()
+ {
+ // Invalid Password
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Red;
+ Console.WriteLine();
+ Console.WriteLine("You entered an invalid password.");
+ Console.ResetColor();
+ }
+
+ public static void PrintQuanityIsLessThanZero()
+ {
+ // Quantity is less than zero
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Yellow;
+ Console.WriteLine();
+ Console.WriteLine("Purchase cancelled");
+ Console.ResetColor();
+ }
+
+ public static void PrintExitMessage()
+ {
+ // Prevent console from closing
+ Console.WriteLine();
+ Console.WriteLine("Press Enter key to exit");
+ Console.ReadLine();
+ }
+
+ public static void PrintWelcomeMessage(string inputedUsername)
+ {
+ // Show welcome message
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Green;
+ Console.WriteLine();
+ Console.WriteLine("Login successful! Welcome " + inputedUsername + "!");
+ Console.ResetColor();
+ }
+ }
+}
diff --git a/Refactoring/Product.cs b/Refactoring/Product.cs
index c9ceee5..64b7604 100644
--- a/Refactoring/Product.cs
+++ b/Refactoring/Product.cs
@@ -11,10 +11,10 @@ namespace Refactoring
public class Product
{
[JsonProperty("Name")]
- public string Name;
+ public string ProductName;
[JsonProperty("Price")]
- public double Price;
+ public double RegularPrice;
[JsonProperty("Quantity")]
- public int Qty;
+ public int RemainingQuantity;
}
}
diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj
index 6696ba9..20f2fcb 100644
--- a/Refactoring/Refactoring.csproj
+++ b/Refactoring/Refactoring.csproj
@@ -45,6 +45,7 @@
+
diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs
index bd07dce..f6c908f 100644
--- a/Refactoring/Tusc.cs
+++ b/Refactoring/Tusc.cs
@@ -10,145 +10,118 @@ namespace Refactoring
{
public class Tusc
{
- public static void Start(List usrs, List prods)
+ public static void Start(List users, List products)
{
- // Write welcome message
- Console.WriteLine("Welcome to TUSC");
- Console.WriteLine("---------------");
-
+ PrintingCommands.PrintWelcomeMessage();
+
// Login
Login:
- bool loggedIn = false; // Is logged in?
+ bool loggedIn = false;
- // Prompt for user input
- Console.WriteLine();
- Console.WriteLine("Enter Username:");
- string name = Console.ReadLine();
+ string inputedUsername = GetInputUserName();
// Validate Username
- bool valUsr = false; // Is valid user?
- if (!string.IsNullOrEmpty(name))
+ bool validatedUser = false; // Is valid user?
+ if (!string.IsNullOrEmpty(inputedUsername))
{
for (int i = 0; i < 5; i++)
{
- User user = usrs[i];
+ User user = users[i];
// Check that name matches
- if (user.Name == name)
+ if (user.UserName == inputedUsername)
{
- valUsr = true;
+ validatedUser = true;
}
}
// if valid user
- if (valUsr)
+ if (validatedUser)
{
// Prompt for user input
Console.WriteLine("Enter Password:");
- string pwd = Console.ReadLine();
+ string inputPassword = Console.ReadLine();
// Validate Password
- bool valPwd = false; // Is valid password?
+ bool validatedPassword = false; // Is valid password?
for (int i = 0; i < 5; i++)
{
- User user = usrs[i];
+ User user = users[i];
// Check that name and password match
- if (user.Name == name && user.Pwd == pwd)
+ if (user.UserName == inputedUsername && user.Password == inputPassword)
{
- valPwd = true;
+ validatedPassword = true;
}
}
// if valid password
- if (valPwd == true)
+ if (validatedPassword == true)
{
loggedIn = true;
- // Show welcome message
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine();
- Console.WriteLine("Login successful! Welcome " + name + "!");
- Console.ResetColor();
+ PrintingCommands.PrintWelcomeMessage(inputedUsername);
// Show remaining balance
- double bal = 0;
+ double outstandingBalance = 0;
for (int i = 0; i < 5; i++)
{
- User usr = usrs[i];
+ User user = users[i];
// Check that name and password match
- if (usr.Name == name && usr.Pwd == pwd)
+ if (user.UserName == inputedUsername && user.Password == inputPassword)
{
- bal = usr.Bal;
+ outstandingBalance = user.Balance;
// Show balance
Console.WriteLine();
- Console.WriteLine("Your balance is " + usr.Bal.ToString("C"));
+ Console.WriteLine("Your balance is " + user.Balance.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
+ GetInputItemToPurchase(products);
+ string productSelectionResponse = Console.ReadLine();
+ int productSelctionResponseNumber = Convert.ToInt32(productSelectionResponse);
+ productSelctionResponseNumber = productSelctionResponseNumber - 1; /* Subtract 1 from number
num = num + 1 // Add 1 to number */
// Check if user entered number that equals product count
- if (num == 7)
+ if (productSelctionResponseNumber == 7)
{
// Update balance
- foreach (var usr in usrs)
+ foreach (var user in users)
{
// Check that name and password match
- if (usr.Name == name && usr.Pwd == pwd)
+ if (user.UserName == inputedUsername && user.Password == inputPassword)
{
- usr.Bal = bal;
+ user.Balance = outstandingBalance;
}
}
// Write out new balance
- string json = JsonConvert.SerializeObject(usrs, Formatting.Indented);
+ string json = JsonConvert.SerializeObject(users, Formatting.Indented);
File.WriteAllText(@"Data/Users.json", json);
// Write out new quantities
- string json2 = JsonConvert.SerializeObject(prods, Formatting.Indented);
+ string json2 = JsonConvert.SerializeObject(products, Formatting.Indented);
File.WriteAllText(@"Data/Products.json", json2);
- // Prevent console from closing
- Console.WriteLine();
- Console.WriteLine("Press Enter key to exit");
- Console.ReadLine();
+ PrintingCommands.PrintExitMessage();
return;
}
else
{
Console.WriteLine();
- Console.WriteLine("You want to buy: " + prods[num].Name);
- Console.WriteLine("Your balance is " + bal.ToString("C"));
+ Console.WriteLine("You want to buy: " + products[productSelctionResponseNumber].ProductName);
+ Console.WriteLine("Your balance is " + outstandingBalance.ToString("C"));
- // Prompt for user input
- Console.WriteLine("Enter amount to purchase:");
- answer = Console.ReadLine();
- int qty = Convert.ToInt32(answer);
+ int requestedQuantity = GetInputRequestedQuantity(ref productSelectionResponse);
// Check if balance - quantity * price is less than 0
- if (bal - prods[num].Price * qty < 0)
+ if (outstandingBalance - products[productSelctionResponseNumber].RegularPrice * requestedQuantity < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
@@ -159,72 +132,94 @@ public static void Start(List usrs, List prods)
}
// Check if quantity is less than quantity
- if (prods[num].Qty <= qty)
+ if (products[productSelctionResponseNumber].RemainingQuantity <= requestedQuantity)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
- Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock");
+ Console.WriteLine("Sorry, " + products[productSelctionResponseNumber].ProductName + " is out of stock");
Console.ResetColor();
continue;
}
// Check if quantity is greater than zero
- if (qty > 0)
+ if (requestedQuantity > 0)
{
// Balance = Balance - Price * Quantity
- bal = bal - prods[num].Price * qty;
+ outstandingBalance = outstandingBalance - products[productSelctionResponseNumber].RegularPrice * requestedQuantity;
// Quanity = Quantity - Quantity
- prods[num].Qty = prods[num].Qty - qty;
+ products[productSelctionResponseNumber].RemainingQuantity = products[productSelctionResponseNumber].RemainingQuantity - requestedQuantity;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("You bought " + qty + " " + prods[num].Name);
- Console.WriteLine("Your new balance is " + bal.ToString("C"));
+ Console.WriteLine("You bought " + requestedQuantity + " " + products[productSelctionResponseNumber].ProductName);
+ Console.WriteLine("Your new balance is " + outstandingBalance.ToString("C"));
Console.ResetColor();
}
else
{
- // Quantity is less than zero
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine();
- Console.WriteLine("Purchase cancelled");
- Console.ResetColor();
+ PrintingCommands.PrintQuanityIsLessThanZero();
}
}
}
}
else
{
- // Invalid Password
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid password.");
- Console.ResetColor();
+ PrintingCommands.PrintInvalidPassword();
goto Login;
}
}
else
{
- // Invalid User
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid user.");
- Console.ResetColor();
+ PrintingCommands.PrintInvalidUser();
goto Login;
}
}
// Prevent console from closing
+ PrintingCommands.PrintExitMessage();
+ }
+
+ private static void GetInputItemToPurchase(List products)
+ {
+ // Prompt for user input
Console.WriteLine();
- Console.WriteLine("Press Enter key to exit");
- Console.ReadLine();
+ Console.WriteLine("What would you like to buy?");
+ for (int i = 0; i < 7; i++)
+ {
+ Product product = products[i];
+ Console.WriteLine(i + 1 + ": " + product.ProductName + " (" + product.RegularPrice.ToString("C") + ")");
+ }
+ Console.WriteLine(products.Count + 1 + ": Exit");
+
+ // Prompt for user input
+ Console.WriteLine("Enter a number:");
+
+ }
+
+
+
+ private static int GetInputRequestedQuantity(ref string productSelectionResponse)
+ {
+ // Prompt for user input
+ Console.WriteLine("Enter amount to purchase:");
+ productSelectionResponse = Console.ReadLine();
+ int qty = Convert.ToInt32(productSelectionResponse);
+ return qty;
}
+
+ private static string GetInputUserName()
+ {
+ PrintingCommands.PrintUserNameInput();
+ string inputedUsername = Console.ReadLine();
+ return inputedUsername;
+ }
+
+
+
+
}
}
diff --git a/Refactoring/User.cs b/Refactoring/User.cs
index fdc34e8..2e2faa3 100644
--- a/Refactoring/User.cs
+++ b/Refactoring/User.cs
@@ -11,10 +11,10 @@ namespace Refactoring
public class User
{
[JsonProperty("Username")]
- public string Name;
+ public string UserName;
[JsonProperty("Password")]
- public string Pwd;
+ public string Password;
[JsonProperty("Balance")]
- public double Bal;
+ public double Balance;
}
}
diff --git a/UnitTestProject/UnitTests.cs b/UnitTestProject/UnitTests.cs
index 51a30ad..66758c4 100644
--- a/UnitTestProject/UnitTests.cs
+++ b/UnitTestProject/UnitTests.cs
@@ -151,7 +151,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())
{
@@ -173,7 +173,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.ProductName == "Chips").Single().RemainingQuantity = 0;
using (var writer = new StringWriter())
{