Skip to content
Open
Show file tree
Hide file tree
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
84 changes: 49 additions & 35 deletions Refactoring/Data/Products.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
[
{
"Name": "Chips",
"Price": 1.49,
"Quantity": 50
},
{
"Name": "Cookies",
"Price": 1.0,
"Quantity": 100
},
{
"Name": "Gum",
"Price": 0.85,
"Quantity": 50
},
{
"Name": "Pop",
"Price": 0.75,
"Quantity": 75
},
{
"Name": "Candy",
"Price": 0.85,
"Quantity": 30
},
{
"Name": "Chocolate Bars",
"Price": 1.25,
"Quantity": 25
},
{
"Name": "Nuts",
"Price": 1.0,
"Quantity": 1
}
{
"Name": "Soup",
"Price": 1.25,
"Quantity": 50,
"Id": 1
},
{
"Name": "Chips",
"Price": 1.49,
"Quantity": 50,
"Id": 2
},
{
"Name": "Cookies",
"Price": 1.0,
"Quantity": 100,
"Id": 3
},
{
"Name": "Gum",
"Price": 0.85,
"Quantity": 50,
"Id": 4
},
{
"Name": "Pop",
"Price": 0.75,
"Quantity": 75,
"Id": 5
},
{
"Name": "Candy",
"Price": 0.85,
"Quantity": 30,
"Id": 6
},
{
"Name": "Chocolate Bars",
"Price": 1.25,
"Quantity": 25,
"Id": 7
},
{
"Name": "Nuts",
"Price": 1.0,
"Quantity": 1,
"Id": 8
}

]
2 changes: 2 additions & 0 deletions Refactoring/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class Product
public double Price;
[JsonProperty("Quantity")]
public int Qty;
[JsonProperty("Id")]
public int Id;
}
}
71 changes: 46 additions & 25 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Tusc
private static User LoggedInUser;
private static int ProductCount;

public static string QuitCode { get { return "quit"; } }
public static string QuitMenuMessage { get { return "Type quit to exit the application"; } }

public static void Start(List<User> users, List<Product> products)
{
InitializeMemberVariables(users, products);
Expand Down Expand Up @@ -50,15 +53,15 @@ private static void OrderProducts()
{
ShowProductList();
SelectedProductNumber = GetValidUserProductSelection();
if (SelectedProductNumber == ProductList.Count + 1)
if (SelectedProductNumber == -1)
{
UpdateCurrentUsersBalance();
break;
}
else
{
Console.WriteLine();
Console.WriteLine("You want to buy: " + ProductList[SelectedProductNumber-1].Name);
Console.WriteLine("You want to buy: " + ProductList[SelectedProductNumber - 1].Name);
Console.WriteLine("Your balance is " + LoggedInUser.Balance.ToString("C"));

QuantityOrdered = GetValidUserProductQuantity();
Expand Down Expand Up @@ -91,32 +94,32 @@ private static void OrderProduct(int SelectedProductNumber, int QuantityOrdered)

private static void UpdateBalance(int SelectedProductNumber, int QuantityOrdered)
{
LoggedInUser.Balance = LoggedInUser.Balance - (ProductList[SelectedProductNumber-1].Price * QuantityOrdered);
LoggedInUser.Balance = LoggedInUser.Balance - (ProductList[SelectedProductNumber - 1].Price * QuantityOrdered);
}

private static void RemoveItemsFromInventory(int SelectedProductNumber, int QuantityOrdered)
{
ProductList[SelectedProductNumber-1].Qty = ProductList[SelectedProductNumber-1].Qty - QuantityOrdered;
ProductList[SelectedProductNumber - 1].Qty = ProductList[SelectedProductNumber - 1].Qty - QuantityOrdered;
}

private static void ShowOrderConfirmationMessage(int SelectedProductNumber, int QuantityOrdered)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You bought " + QuantityOrdered + " " + ProductList[SelectedProductNumber-1].Name);
Console.WriteLine("You bought " + QuantityOrdered + " " + ProductList[SelectedProductNumber - 1].Name);
Console.WriteLine("Your new balance is " + LoggedInUser.Balance.ToString("C"));
Console.ResetColor();
}

private static bool VerifyStockOnHand(int SelectedProductNumber, int QuantityOrdered)
{
bool stockOnHand = true;
if (ProductList[SelectedProductNumber-1].Qty <= QuantityOrdered)
if (QuantityOrdered > 0 && ProductList[SelectedProductNumber - 1].Qty < QuantityOrdered)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("Sorry, " + ProductList[SelectedProductNumber-1].Name + " is out of stock");
Console.WriteLine("Sorry, " + ProductList[SelectedProductNumber - 1].Name + " is out of stock");
Console.ResetColor();
stockOnHand = false;
}
Expand All @@ -126,7 +129,7 @@ private static bool VerifyStockOnHand(int SelectedProductNumber, int QuantityOrd
private static bool VerifyUserFundsForSelectedPurchase(int SelectedProductNumber, int QuantityOrdered)
{
bool fundsAvailable = true;
if ((LoggedInUser.Balance - (ProductList[SelectedProductNumber-1].Price * QuantityOrdered)) < 0)
if ((LoggedInUser.Balance - (ProductList[SelectedProductNumber - 1].Price * QuantityOrdered)) < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Expand Down Expand Up @@ -194,37 +197,49 @@ private static int GetValidUserProductSelection()
{
int productNumber;
while (true)
{
Console.WriteLine("Enter the product number:");
{
Console.WriteLine("Enter the product number:");
string ProductNumberEntered = Console.ReadLine();
if (validateProduct(ProductNumberEntered, out productNumber))
{
break;
break;
}
}
}
return productNumber;
}

private static bool validateProduct(string ProductNumberEntered, out int productNumber )
private static bool validateProduct(string ProductNumberEntered, out int productNumber)
{
bool validProductSelected = false;
if (Int32.TryParse(ProductNumberEntered, out productNumber) && (productNumber <= ProductCount + 1))

if (ProductNumberEntered.Equals(QuitCode))
{
productNumber = -1;
validProductSelected = true;
}

else
{
ShowProductNumberInvalidMessage();
var parseResult = Int32.TryParse(ProductNumberEntered, out productNumber);
var selectedId = productNumber;
if (parseResult && ProductList.Any(p => p.Id == selectedId && p.Qty > 0))
{
validProductSelected = true;
}
else
{
ShowProductNumberInvalidMessage();
}
}

return validProductSelected;
}

private static void ShowProductNumberInvalidMessage()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("");
Console.WriteLine("Product numbers must be numeric in the range of 1 - " + (ProductCount + 1).ToString());
Console.WriteLine("You must select only products that appear in the menu!");
Console.WriteLine("");
Console.ResetColor();
}
Expand All @@ -234,11 +249,16 @@ private static void ShowProductList()
Console.WriteLine();
Console.WriteLine("What would you like to buy?");
for (int i = 0; i < ProductCount; i++)
{
{
Product prod = ProductList[i];
Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
if (prod.Qty > 0)
{
Console.WriteLine(prod.Id + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
}

}
Console.WriteLine(ProductList.Count + 1 + ": Exit");

Console.WriteLine(QuitMenuMessage);
}

private static void ShowRemainingBalance()
Expand All @@ -265,7 +285,7 @@ private static bool LoginUser()
User user = new User();

GetUserCredentials(ref userName, ref userPassword);
if (ValidateUserCredentials(userName, userPassword, ref user))
if (ValidateUserCredentials(userName, userPassword, ref user))
{
LoggedInUser = user;
ShowSuccessfulLoginMessage();
Expand All @@ -276,7 +296,7 @@ private static bool LoginUser()
ShowFailedCredentialsMessage();
}
return validatedUser;
}
}

private static void GetUserCredentials(ref string userName, ref string userPassword)
{
Expand All @@ -298,7 +318,7 @@ private static bool ValidateUserPassword(User userName, string password)
{
bool passwordValid = false;
if (userName.Password == password)
{
{
passwordValid = true;
}
return passwordValid;
Expand All @@ -321,7 +341,7 @@ private static bool ValidateUserCredentials(string userName, string userPassword
{
bool validCredentials = false;

if(FindUserInUserList(userName, ref user) && ValidateUserPassword(user, userPassword))
if (FindUserInUserList(userName, ref user) && ValidateUserPassword(user, userPassword))
{
validCredentials = true;
}
Expand All @@ -346,12 +366,13 @@ private static bool FindUserInUserList(string name, ref User foundUser)
return UserIsFound;
}



private static void ShowWelcomeMessage()
{
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");
}

}
}
Loading