Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 134 additions & 118 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,112 +10,69 @@ namespace Refactoring
{
public class Tusc
{
private static List<User> users;
private static List<Product> products;
private static int numberOfUsers;
private static int numberOfProducts;

public static void Start(List<User> usrs, List<Product> prods)
{
// This feels a bit like a global variable declaration/use, even though it is within the class.
// But, I can live with it for the first attempt. :)
users = usrs;
products = prods;
numberOfUsers = users.Count();
numberOfProducts = products.Count();

// Write welcome message
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");

// Login
Login:
bool loggedIn = false; // Is logged in?


// Prompt for user input
Console.WriteLine();
Console.WriteLine("Enter Username:");
string name = Console.ReadLine();
WriteBlankLineAndMessage("Enter Username:");
string enteredUserName = Console.ReadLine();

// Validate Username
bool valUsr = false; // Is valid user?
if (!string.IsNullOrEmpty(name))
if (!string.IsNullOrEmpty(enteredUserName))
{
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)
if (IsValidUser (enteredUserName))
{
// Prompt for user input
Console.WriteLine("Enter Password:");
string pwd = Console.ReadLine();
WriteMessage("Enter Password:");
string enteredPassword = Console.ReadLine();

// Validate Password
bool valPwd = false; // Is valid password?
for (int i = 0; i < 5; i++)
if (IsValidPassword(enteredUserName, enteredPassword))
{
User user = usrs[i];

// Check that name and password match
if (user.Name == name && user.Pwd == pwd)
{
valPwd = true;
}
}

// 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();
WriteSuccessMessage("Login successful! Welcome " + enteredUserName + "!");

double bal = GetUserAccountBalance(enteredUserName, enteredPassword);
WriteBlankLineAndMessage("Your balance is " + bal.ToString("C"));

// 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");
WriteBlankLineAndMessage("What would you like to buy?");
WriteProductList();

// Prompt for user input
Console.WriteLine("Enter a number:");
WriteBlankLineAndMessage("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 */
num = num - 1; // Subtract 1 from number, because array is zero based and user list is one based.

// Check if user entered number that equals product count
if (num == 7)
// The last item is the 'exit' choice.
if (num == numberOfProducts)
{
// Update balance
foreach (var usr in usrs)
{
// Check that name and password match
if (usr.Name == name && usr.Pwd == pwd)
if (usr.Name == enteredUserName && usr.Pwd == enteredPassword)
{
usr.Bal = bal;
}
Expand All @@ -131,67 +88,52 @@ public static void Start(List<User> usrs, List<Product> prods)


// Prevent console from closing
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
WriteBlankLineAndMessage("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:");
WriteBlankLineAndMessage("You want to buy: " + prods[num].Name);
WriteMessage("Your balance is " + bal.ToString("C"));
WriteMessage("Enter amount to purchase:");
answer = Console.ReadLine();
int qty = Convert.ToInt32(answer);
int userEnteredQuantityToBuy = Convert.ToInt32(answer);

// Check if balance - quantity * price is less than 0
if (bal - prods[num].Price * qty < 0)
if (bal - prods[num].Price * userEnteredQuantityToBuy < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You do not have enough money to buy that.");
Console.ResetColor();
WriteErrorMessage("You do not have enough money to buy that.");
continue;
}

// Check if quantity is less than quantity
if (prods[num].Qty <= qty)
// This is the one defect I fixed. I left other defects/improvements as is.
if (prods[num].Qty < userEnteredQuantityToBuy)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock");
Console.ResetColor();
WriteErrorMessage("Sorry, " + prods[num].Name + " is out of stock");
continue;
}

// Check if quantity is greater than zero
if (qty > 0)
if (userEnteredQuantityToBuy > 0)
{
// Balance = Balance - Price * Quantity
bal = bal - prods[num].Price * qty;
bal = bal - prods[num].Price * userEnteredQuantityToBuy;

// Quanity = Quantity - Quantity
prods[num].Qty = prods[num].Qty - qty;
prods[num].Qty = prods[num].Qty - userEnteredQuantityToBuy;

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();
WriteSuccessMessage("You bought " + userEnteredQuantityToBuy + " " + prods[num].Name);
WriteMessage("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();
WriteBlankLineAndMessage("Purchase cancelled");
}
}
}
Expand All @@ -200,31 +142,105 @@ public static void Start(List<User> usrs, List<Product> prods)
{
// Invalid Password
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You entered an invalid password.");
Console.ResetColor();

WriteErrorMessage("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();

WriteBlankLineAndMessage("You entered an invalid user.");

goto Login;
}
}

// Prevent console from closing
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
WriteBlankLineAndMessage("Press Enter key to exit");
Console.ReadLine();
}

private static void WriteProductList()
{
for (int i = 0; i < numberOfProducts; i++)
{
WriteMessage(i + 1 + ": " + products[i].Name + " (" + products[i].Price.ToString("C") + ")");
}
WriteMessage(numberOfProducts + 1 + ": Exit");
}

private static bool IsValidUser(string userName)
{
bool isValidUser = false;

for (int i = 0; i < numberOfUsers; i++)
{
User user = users[i];
if (user.Name == userName)
{
isValidUser = true;
}
}
return isValidUser;
}

private static bool IsValidPassword(string userName, string password)
{
bool isValidPassword = false;

for (int i = 0; i < numberOfUsers; i++)
{
User user = users[i];
if (user.Name == userName && user.Pwd == password)
{
isValidPassword = true;
}
}
return isValidPassword;
}

private static double GetUserAccountBalance(string userName, string password)
{
double userAccountBalance = 0.0;

for (int i = 0; i < numberOfUsers; i++)
{
User user = users[i];
if (user.Name == userName && user.Pwd == password)
{
userAccountBalance = user.Bal;
}
}
return userAccountBalance;
}

private static void WriteBlankLineAndMessage(string message)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
Console.WriteLine(message);
}

private static void WriteMessage(string message)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(message);
}

private static void WriteErrorMessage(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(message);
}

private static void WriteSuccessMessage(string message)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(message);
}
}
}