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
27 changes: 20 additions & 7 deletions Refactoring/Data/Products.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,49 @@
{
"Name": "Chips",
"Price": 1.49,
"Quantity": 50
"Quantity": 50,
"Id": 1
},
{
"Name": "Cookies",
"Price": 1.0,
"Quantity": 100
"Quantity": 100,
"Id": 2
},
{
"Name": "Gum",
"Price": 0.85,
"Quantity": 50
"Quantity": 50,
"Id": 3
},
{
"Name": "Pop",
"Price": 0.75,
"Quantity": 75
"Quantity": 75,
"Id": 4
},
{
"Name": "Candy",
"Price": 0.85,
"Quantity": 30
"Quantity": 30,
"Id": 5
},
{
"Name": "Chocolate Bars",
"Price": 1.25,
"Quantity": 25
"Quantity": 25,
"Id": 6
},
{
"Name": "Nuts",
"Price": 1.0,
"Quantity": 1
"Quantity": 1,
"Id": 7
},
{
"Name": "Soup",
"Price": 1.25,
"Quantity": 50,
"Id": 8
}
]
7 changes: 7 additions & 0 deletions Refactoring/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ public class Product
public double Price;
[JsonProperty("Quantity")]
public int Qty;
[JsonProperty("Id")]
public int Id;

public bool HasStock()
{
return Qty > 0;
}
}
}
64 changes: 40 additions & 24 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Tusc
private static List<Product> ProductList;
private static User LoggedInUser;
private static int ProductCount;
private const string QUIT_APPLICATION_INPUT = "quit";
private const int QUIT_INDEX = -1;

public static void Start(List<User> users, List<Product> products)
{
Expand Down Expand Up @@ -50,15 +52,15 @@ private static void OrderProducts()
{
ShowProductList();
SelectedProductNumber = GetValidUserProductSelection();
if (SelectedProductNumber == ProductList.Count + 1)
if (SelectedProductNumber == QUIT_INDEX)
{
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 +93,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 (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 +128,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,22 +196,27 @@ 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))
if (ProductNumberEntered.Equals(QUIT_APPLICATION_INPUT, StringComparison.InvariantCultureIgnoreCase))
{
productNumber = QUIT_INDEX;
break;
}
else 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 (Int32.TryParse(ProductNumberEntered, out productNumber) && (ProductNumberMatchesProductId(ProductNumberEntered)))
{
validProductSelected = true;
}
Expand All @@ -220,11 +227,20 @@ private static bool validateProduct(string ProductNumberEntered, out int product
return validProductSelected;
}

private static bool ProductNumberMatchesProductId(string productNumber)
{
foreach (Product product in ProductList)
{
if (product.Id.ToString() == productNumber) return true;
}
return false;
}

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("Product numbers must be numeric in the range of 1 - " + (ProductCount).ToString());
Console.WriteLine("");
Console.ResetColor();
}
Expand All @@ -236,9 +252,9 @@ private static void ShowProductList()
for (int i = 0; i < ProductCount; i++)
{
Product prod = ProductList[i];
Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
if (prod.HasStock()) Console.WriteLine(prod.Id + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
}
Console.WriteLine(ProductList.Count + 1 + ": Exit");
Console.WriteLine("Type quit to exit the application.");
}

private static void ShowRemainingBalance()
Expand All @@ -265,7 +281,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 +292,7 @@ private static bool LoginUser()
ShowFailedCredentialsMessage();
}
return validatedUser;
}
}

private static void GetUserCredentials(ref string userName, ref string userPassword)
{
Expand All @@ -298,7 +314,7 @@ private static bool ValidateUserPassword(User userName, string password)
{
bool passwordValid = false;
if (userName.Password == password)
{
{
passwordValid = true;
}
return passwordValid;
Expand All @@ -321,7 +337,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,7 +362,7 @@ private static bool FindUserInUserList(string name, ref User foundUser)
return UserIsFound;
}



private static void ShowWelcomeMessage()
{
Expand Down
Loading