diff --git a/Refactoring/Product.cs b/Refactoring/Product.cs
index c9ceee5..b3a01e4 100644
--- a/Refactoring/Product.cs
+++ b/Refactoring/Product.cs
@@ -16,5 +16,10 @@ public class Product
public double Price;
[JsonProperty("Quantity")]
public int Qty;
+
+ public string Description(int index)
+ {
+ return (index+1) + ": " + this.Name + " (" + this.Price.ToString("C") + ")";
+ }
}
}
diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj
index 6696ba9..c2e99c6 100644
--- a/Refactoring/Refactoring.csproj
+++ b/Refactoring/Refactoring.csproj
@@ -32,9 +32,9 @@
4
-
+
+ False
..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll
- True
diff --git a/Refactoring/Tusc.cs b/Refactoring/Tusc.cs
index bd07dce..ec6f934 100644
--- a/Refactoring/Tusc.cs
+++ b/Refactoring/Tusc.cs
@@ -16,27 +16,113 @@ public static void Start(List usrs, List prods)
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");
- // Login
- Login:
- bool loggedIn = false; // Is logged in?
+ User currUser;
+ Users users = new Users(usrs);
+ currUser = UserLogin(users);
- // Prompt for user input
+ // if valid password
+ if (currUser != null)
+ {
+
+ Shop(prods, currUser);
+ SaveData((List)users, prods);
+ }
+
+ ShowExitMsg();
+ }
+
+ private static void Shop(List prods, User currUser)
+ {
+ ShowWelcomeMsg(currUser);
+
+ // Show remaining balance
+ double bal = 0;
+
+ if (currUser != null)
+ {
+ bal = currUser.Bal;
+
+ // Show balance
+ Console.WriteLine();
+ Console.WriteLine("Your balance is " + currUser.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 < prods.Count; i++)
+ {
+ Product prod = prods[i];
+ Console.WriteLine(prod.Description(i));
+ }
+ 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;
+
+ // Check if user entered number that equals product count
+ if (num == prods.Count)
+ {
+ currUser.Bal = bal;
+ return;
+ }
+ else if (num > prods.Count)
+ {
+ String errText = "Invalid Selection.";
+ ShowErrMsg(errText);
+ }
+ else
+ {
+ BuyItem(prods, ref bal, num);
+ }
+ }
+ }
+
+ private static void ShowWelcomeMsg(User currUser)
+ {
+ // Show welcome message
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
- Console.WriteLine("Enter Username:");
- string name = Console.ReadLine();
+ Console.WriteLine("Login successful! Welcome " + currUser.Name + "!");
+ Console.ResetColor();
+ }
- // Validate Username
- bool valUsr = false; // Is valid user?
- if (!string.IsNullOrEmpty(name))
+ private static User UserLogin(Users usrs)
+ {
+ User currUser = null;
+
+ do
{
- for (int i = 0; i < 5; i++)
+ // 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))
{
- User user = usrs[i];
- // Check that name matches
- if (user.Name == name)
- {
- valUsr = true;
- }
+ return null;
+ }
+
+
+ if (usrs.FindUserByName(name) != null)
+ {
+ valUsr = true;
+ }
+ else
+ {
+ valUsr = false;
+ String errText = "You entered an invalid user.";
+ // Invalid User
+ ShowErrMsg(errText);
}
// if valid user
@@ -46,185 +132,99 @@ public static void Start(List usrs, List prods)
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;
- }
- }
+ currUser = usrs.FindUser(name, pwd);
- // 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
+ if (currUser == null)
{
+ String errText = "You entered an invalid password.";
// Invalid Password
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid password.");
- Console.ResetColor();
-
- goto Login;
+ ShowErrMsg(errText);
}
}
- else
- {
- // Invalid User
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid user.");
- Console.ResetColor();
- goto Login;
- }
+ } while(currUser == null);
+
+ return currUser;
+ }
+
+ private static void BuyItem(List prods, ref double bal, int num)
+ {
+ 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:");
+ string answer = Console.ReadLine();
+ int qty = Convert.ToInt32(answer);
+
+ // Check if balance - quantity * price is less than 0
+ if (bal - prods[num].Price * qty < 0)
+ {
+ String errText = "You do not have enough money to buy that.";
+ ShowErrMsg(errText);
+ return;
}
+ // Check if quantity is less than quantity
+ if (prods[num].Qty <= qty)
+ {
+ String errText = "Sorry, " + prods[num].Name + " is out of stock";
+ ShowErrMsg(errText);
+ return;
+ }
+
+ // 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
+ {
+ String errText = "Purchase cancelled";
+ // Quantity is less than zero
+ ShowErrMsg(errText);
+ }
+
+ return;
+ }
+
+ private static void SaveData(List usrs, List prods)
+ {
+ // 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);
+ }
+
+ private static void ShowExitMsg()
+ {
// Prevent console from closing
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
Console.ReadLine();
}
+
+ private static void ShowErrMsg(String errText)
+ {
+ Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Red;
+ Console.WriteLine();
+ Console.WriteLine(errText);
+ Console.ResetColor();
+ }
}
}
diff --git a/Refactoring/User.cs b/Refactoring/User.cs
index fdc34e8..d17c4cb 100644
--- a/Refactoring/User.cs
+++ b/Refactoring/User.cs
@@ -16,5 +16,55 @@ public class User
public string Pwd;
[JsonProperty("Balance")]
public double Bal;
+
+ public bool IsUser(string name, string pwd)
+ {
+ return this.Name == name && this.Pwd == pwd;
+ }
+ }
+
+ public class Users : List
+ {
+ private Users(){} // Reserved constructor
+
+ public Users(List users)
+ {
+ this.Clear();
+ this.AddRange(users);
+ }
+
+ public User FindUserByName(string name)
+ {
+
+ for (int i = 0; i < this.Count; i++)
+ {
+ User usr = this[i];
+
+ // Check that name and password match
+ if (usr.Name == name)
+ {
+ return usr;
+
+ }
+ }
+ return null;
+ }
+
+ public User FindUser(string name, string pwd)
+ {
+
+ for (int i = 0; i < this.Count; i++)
+ {
+ User usr = this[i];
+
+ // Check that name and password match
+ if (usr.IsUser(name, pwd))
+ {
+ return usr;
+
+ }
+ }
+ return null;
+ }
}
}