diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj
index 6696ba9..4ec237a 100644
--- a/Refactoring/Refactoring.csproj
+++ b/Refactoring/Refactoring.csproj
@@ -36,6 +36,25 @@
..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll
True
+
+ ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.dll
+ False
+
+
+ ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.interfaces.dll
+ False
+
+
+ ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.framework.dll
+
+
+ ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.util.dll
+ False
+
+
+ ..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll
+ False
+
diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs
index bd07dce..947ae48 100644
--- a/Refactoring/Tusc.cs
+++ b/Refactoring/Tusc.cs
@@ -10,7 +10,7 @@ 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");
@@ -18,128 +18,70 @@ public static void Start(List usrs, List prods)
// Login
Login:
- bool loggedIn = false; // Is logged in?
// Prompt for user input
- Console.WriteLine();
- Console.WriteLine("Enter Username:");
+ Console.WriteLine("\nEnter 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)
+ User user = validUserName(users, name);
+ if (user != null)
{
// 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;
- }
- }
+ string password = Console.ReadLine();
- // if valid password
- if (valPwd == true)
+ user = validPassword(users, name, password);
+ if (user != null)
{
- loggedIn = true;
-
- // Show welcome message
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine();
- Console.WriteLine("Login successful! Welcome " + name + "!");
- Console.ResetColor();
+ // Welcome
+ printMessage(ConsoleColor.Green, "", "Login successful! Welcome " + name + "!");
// 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"));
- }
- }
+ double bal = user.Bal;
+ Console.WriteLine("\nYour balance is " + 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");
+ printProductsList(products);
// 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 */
+ int num = Convert.ToInt32(answer) - 1;
// Check if user entered number that equals product count
if (num == 7)
{
// Update balance
- foreach (var usr in usrs)
+ foreach (var usr in users)
{
// Check that name and password match
- if (usr.Name == name && usr.Pwd == pwd)
+ if (usr.Name == name && usr.Pwd == password)
{
usr.Bal = bal;
}
}
// 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.WriteLine("\nPress Enter key to exit");
Console.ReadLine();
return;
}
else
{
- Console.WriteLine();
- Console.WriteLine("You want to buy: " + prods[num].Name);
+ Console.WriteLine("\nYou want to buy: " + products[num].Name);
Console.WriteLine("Your balance is " + bal.ToString("C"));
// Prompt for user input
@@ -148,24 +90,16 @@ public static void Start(List usrs, List prods)
int qty = Convert.ToInt32(answer);
// Check if balance - quantity * price is less than 0
- if (bal - prods[num].Price * qty < 0)
+ if (bal - products[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();
+ printMessage(ConsoleColor.Red, "", "You do not have enough money to buy that.");
continue;
}
// Check if quantity is less than quantity
- if (prods[num].Qty <= qty)
+ if (products[num].Qty <= qty)
{
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock");
- Console.ResetColor();
+ printMessage(ConsoleColor.Red, "", "Sorry, " + products[num].Name + " is out of stock");
continue;
}
@@ -173,25 +107,16 @@ public static void Start(List usrs, List prods)
if (qty > 0)
{
// Balance = Balance - Price * Quantity
- bal = bal - prods[num].Price * qty;
+ bal = bal - products[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();
+ products[num].Qty = products[num].Qty - qty;
+ printMessage(ConsoleColor.Green, "You bought " + qty + " " + products[num].Name, "Your new balance is " + bal.ToString("C"));
}
else
{
// Quantity is less than zero
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine();
- Console.WriteLine("Purchase cancelled");
- Console.ResetColor();
+ printMessage(ConsoleColor.Yellow, "", "Purchase cancelled");
}
}
}
@@ -199,32 +124,67 @@ public static void Start(List usrs, List prods)
else
{
// Invalid Password
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid password.");
- Console.ResetColor();
-
+ printMessage(ConsoleColor.Red, "", "You entered an invalid password.");
goto Login;
}
}
else
{
// Invalid User
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid user.");
- Console.ResetColor();
-
+ printMessage(ConsoleColor.Red, "", "You entered an invalid user.");
goto Login;
}
}
// Prevent console from closing
- Console.WriteLine();
- Console.WriteLine("Press Enter key to exit");
+ Console.WriteLine("\nPress Enter key to exit");
Console.ReadLine();
}
+
+ public static User validUserName(List users, String input)
+ {
+ foreach (User user in users)
+ {
+ if (user.Name == input)
+ {
+ return user;
+ }
+ }
+
+ return null;
+ }
+
+ public static User validPassword(List users, String name, String password)
+ {
+ foreach (User user in users)
+ {
+ if (user.Name == name && user.Pwd == password)
+ {
+ return user;
+ }
+ }
+
+ return null;
+ }
+
+ public static void printProductsList(List products)
+ {
+ Console.WriteLine("\nWhat would you like to buy?");
+ foreach (Product product in products)
+ {
+ Console.WriteLine(products.IndexOf(product) + 1 + ": " + product.Name + " (" + product.Price.ToString("C") + ")");
+ }
+ Console.WriteLine(products.Count + 1 + ": Exit");
+ }
+
+ public static void printMessage(ConsoleColor color, String messageOne, String messageTwo)
+ {
+ Console.Clear();
+ Console.ForegroundColor = color;
+ Console.WriteLine(messageOne);
+ Console.WriteLine(messageTwo);
+ Console.ResetColor();
+ }
}
+
}
diff --git a/Refactoring/packages.config b/Refactoring/packages.config
index 2abc396..21b1784 100644
--- a/Refactoring/packages.config
+++ b/Refactoring/packages.config
@@ -1,4 +1,5 @@
+
\ No newline at end of file