diff --git a/Refactoring/CurrentUser.cs b/Refactoring/CurrentUser.cs
new file mode 100644
index 0000000..2b05b4b
--- /dev/null
+++ b/Refactoring/CurrentUser.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Refactoring
+{
+ public class CurrentUser
+ {
+ public string Name { get; set; }
+ public string Pwd { get; set; }
+
+ public bool IsSameUser(string name, string pwd)
+ {
+ return name.Equals(Name) && pwd.Equals(Pwd);
+ }
+ }
+}
diff --git a/Refactoring/Refactoring.csproj b/Refactoring/Refactoring.csproj
index 6696ba9..b832f1a 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..fbb0bef 100644
--- a/Refactoring/Tusc.cs
+++ b/Refactoring/Tusc.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -10,221 +11,223 @@ namespace Refactoring
{
public class Tusc
{
+ private const int TotalUsers = 4;
+ private const int TotalItems = 7;
+ private const int MaxErrorLogins = 5;
+
+ private static readonly CurrentUser CurrentUser = new CurrentUser();
+
public static void Start(List usrs, List prods)
{
- // Write welcome message
- Console.WriteLine("Welcome to TUSC");
- Console.WriteLine("---------------");
+ PrintMessage("Welcome to TUSC\n---------------");
- // Login
- Login:
- bool loggedIn = false; // Is logged in?
- // Prompt for user input
- Console.WriteLine();
- Console.WriteLine("Enter Username:");
- string name = Console.ReadLine();
+ var errorLoginCounter = 0;
- // Validate Username
- bool valUsr = false; // Is valid user?
- if (!string.IsNullOrEmpty(name))
+ while (errorLoginCounter < MaxErrorLogins)
{
- for (int i = 0; i < 5; i++)
+ if (!AuthenticateUser(usrs))
+ {
+ errorLoginCounter++;
+ continue;
+ }
+
+
+ // Show remaining balance
+ double bal = 0;
+ for (int i = 0; i <= TotalUsers; i++)
{
- User user = usrs[i];
- // Check that name matches
- if (user.Name == name)
+ User usr = usrs[i];
+
+ // Check that name and password match
+ if (CurrentUser.IsSameUser(usr.Name, usr.Pwd))
{
- valUsr = true;
+ bal = usr.Bal;
+
+ // Show balance
+ PrintMessage("\nYour balance is " + usr.Bal.ToString("C"));
}
}
- // if valid user
- if (valUsr)
+ // Show product list
+ while (true)
{
// 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++)
+ PrintMessage("\nWhat would you like to buy?");
+ for (var i = 0; i < TotalItems; i++)
{
- User user = usrs[i];
-
- // Check that name and password match
- if (user.Name == name && user.Pwd == pwd)
- {
- valPwd = true;
- }
+ Product prod = prods[i];
+ Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
}
+ Console.WriteLine(prods.Count + 1 + ": Exit");
+
+ // Prompt for user input
+ PrintMessage("Enter a number:");
+ string answer = Console.ReadLine();
+
+ if (!IsValidAnswer(answer)) continue;
+
+ var num = Convert.ToInt32(answer);
+ num = num - 1;
- // if valid password
- if (valPwd == true)
+ // Check if user entered number that equals product count
+ if (num == TotalItems)
{
- 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++)
+ // Update balance
+ foreach (var usr in usrs)
{
- User usr = usrs[i];
-
// Check that name and password match
- if (usr.Name == name && usr.Pwd == pwd)
+ if (CurrentUser.IsSameUser(usr.Name, usr.Pwd))
{
- bal = usr.Bal;
-
- // Show balance
- Console.WriteLine();
- Console.WriteLine("Your balance is " + usr.Bal.ToString("C"));
+ usr.Bal = bal;
}
}
- // 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");
+ // Write out new balance
+ string json = JsonConvert.SerializeObject(usrs, Formatting.Indented);
+ File.WriteAllText(@"Data/Users.json", json);
- // 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 */
+ // Write out new quantities
+ string json2 = JsonConvert.SerializeObject(prods, Formatting.Indented);
+ File.WriteAllText(@"Data/Products.json", json2);
- // 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();
- }
- }
- }
+
+ // Prevent console from closing
+ PrintMessage("\nPress Enter key to exit\n");
+ return;
+ }
+
+ PrintMessage("\nYou want to buy: " + prods[num].Name + "\nYour balance is " + bal.ToString("C"));
+
+ // Prompt for user input
+ PrintMessage("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();
+ PrintMessage("\nYou do not have enough money to buy that.", true, ConsoleColor.Red, true);
+ continue;
+ }
+
+ // Check if quantity is less than quantity
+ if (prods[num].Qty <= qty)
+ {
+ //Console.Clear();
+ PrintMessage("\nSorry, " + prods[num].Name + " is out of stock", true, ConsoleColor.Red, true);
+ 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();
+ PrintMessage("You bought " + qty + " " + prods[num].Name + "\nYour new balance is " + bal.ToString("C"), true, ConsoleColor.Green, true);
}
else
{
- // Invalid Password
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid password.");
- Console.ResetColor();
-
- goto Login;
+ // Quantity is less than zero
+ //Console.Clear();
+ PrintMessage("\nPurchase cancelled", true, ConsoleColor.Yellow, true);
}
}
- else
+ }
+
+
+ if(errorLoginCounter >= MaxErrorLogins) PrintMessage("\nMaximum number of trials was reached.");
+
+ // Prevent console from closing
+ PrintMessage("\nPress Enter key to exit");
+ Console.ReadLine();
+ }
+
+ private static bool IsValidAnswer(string answer)
+ {
+ return !string.IsNullOrEmpty(answer);
+ }
+
+ private static string ReadUserInput()
+ {
+ return Console.ReadLine();
+ }
+
+ private static bool IsValidUserPwd(string userName, string pwd, IList usrs)
+ {
+ for (var i = 0; i <= TotalUsers; i++)
+ if (usrs[i].Name.Equals(userName) && usrs[i].Pwd.Equals(pwd)) return true;
+
+ return false;
+ }
+
+ private static void PrintMessage(string msg, bool isUpdateConcoleColor = false, ConsoleColor consoleColor = ConsoleColor.Black, bool isResetConsoleColor = false)
+ {
+ if (isUpdateConcoleColor) Console.ForegroundColor = consoleColor;
+ Console.WriteLine(msg);
+ if (isResetConsoleColor) Console.ResetColor();
+ }
+
+ private static bool IsValidUserName(string userName, IList usrs)
+ {
+ if (string.IsNullOrEmpty(userName)) return true;
+
+ for (var i = 0; i <= TotalUsers; i++)
+ if (usrs[i].Name.Equals(userName)) return true;
+
+ //user name was not found
+ return false;
+ }
+
+ private static bool AuthenticateUser(IList usrs)
+ {
+ // Prompt for user input
+ PrintMessage("\nEnter Username:\n");
+ string userName = ReadUserInput();
+
+ // Validate Username
+ if (!string.IsNullOrEmpty(userName) && IsValidUserName(userName, usrs))
+ {
+ // Prompt for user input
+ PrintMessage("Enter Password:\n");
+ string pwd = ReadUserInput();
+
+ if (!string.IsNullOrEmpty(pwd) && IsValidUserPwd(userName, pwd, usrs))
{
- // Invalid User
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine();
- Console.WriteLine("You entered an invalid user.");
+ // Show welcome message
+ //Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Green;
+ PrintMessage("\nLogin successful! Welcome " + userName + "!");
Console.ResetColor();
- goto Login;
+ CurrentUser.Name = userName;
+ CurrentUser.Pwd = pwd;
+
+ return true;
}
+
+ // Invalid Password
+ //Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Red;
+ PrintMessage("\nYou entered an invalid password.");
+ Console.ResetColor();
+ }
+ else
+ {
+ // Invalid User
+ //Console.Clear();
+ Console.ForegroundColor = ConsoleColor.Red;
+ PrintMessage("\nYou entered an invalid user.");
+ Console.ResetColor();
}
- // Prevent console from closing
- Console.WriteLine();
- Console.WriteLine("Press Enter key to exit");
- Console.ReadLine();
+ return false;
}
}
}