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
83 changes: 48 additions & 35 deletions Refactoring/Data/Products.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
[
{
"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
}
{
"ProductId": "pChips",
"Name": "Chips",
"Price": 1.49,
"Quantity": 50
},
{
"ProductId": "pCookies",
"Name": "Cookies",
"Price": 1.0,
"Quantity": 100
},
{
"ProductId": "pGum",
"Name": "Gum",
"Price": 0.85,
"Quantity": 50
},
{
"ProductId": "pPop",
"Name": "Pop",
"Price": 0.75,
"Quantity": 75
},
{
"ProductId": "pCandy",
"Name": "Candy",
"Price": 0.85,
"Quantity": 30
},
{
"ProductId": "pCholocateBars",
"Name": "Chocolate Bars",
"Price": 1.25,
"Quantity": 25
},
{
"ProductId": "pNuts",
"Name": "Nuts",
"Price": 1.0,
"Quantity": 1
},
{
"ProductId": "pSoup",
"Name": "Soup",
"Price": 1.60,
"Quantity": 50
}
]
2 changes: 2 additions & 0 deletions Refactoring/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Refactoring
[Serializable]
public class Product
{
[JsonProperty("ProductId")]
public string Id;
[JsonProperty("Name")]
public string Name;
[JsonProperty("Price")]
Expand Down
117 changes: 80 additions & 37 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Refactoring
Expand Down Expand Up @@ -43,28 +44,29 @@ private static void InitializeMemberVariables(List<User> usrs, List<Product> pro

private static void OrderProducts()
{
int SelectedProductNumber;
string SelectedProductId;
int QuantityOrdered;

while (true)
{
ShowProductList();
SelectedProductNumber = GetValidUserProductSelection();
if (SelectedProductNumber == ProductList.Count + 1)
SelectedProductId = GetValidUserProductSelection();
if (SelectedProductId.Equals("quit"))
{
UpdateCurrentUsersBalance();
break;
}
else
{
Product mySelectedProduct = ProductNameLookupById(SelectedProductId);
Console.WriteLine();
Console.WriteLine("You want to buy: " + ProductList[SelectedProductNumber-1].Name);
Console.WriteLine("You want to buy: " + mySelectedProduct.Name);
Console.WriteLine("Your balance is " + LoggedInUser.Balance.ToString("C"));

QuantityOrdered = GetValidUserProductQuantity();
if (QuantityOrdered > 0 && VerifyUserFundsForSelectedPurchase(SelectedProductNumber, QuantityOrdered) && VerifyStockOnHand(SelectedProductNumber, QuantityOrdered))
if (QuantityOrdered > 0 && VerifyUserFundsForSelectedPurchase(mySelectedProduct, QuantityOrdered) && VerifyStockOnHand(mySelectedProduct, QuantityOrdered))
{
OrderProduct(SelectedProductNumber, QuantityOrdered);
OrderProduct(mySelectedProduct, QuantityOrdered);
}
else
{
Expand All @@ -74,6 +76,12 @@ private static void OrderProducts()
}
}

private static Product ProductNameLookupById(string sSelectedProductId)
{
//Search for the selected product in our available product
return ProductList.Where(x => x.Id.Equals(sSelectedProductId)).First();
}

private static void ShowPurchaseCancelledMessage()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Expand All @@ -82,51 +90,54 @@ private static void ShowPurchaseCancelledMessage()
Console.ResetColor();
}

private static void OrderProduct(int SelectedProductNumber, int QuantityOrdered)
private static void OrderProduct(Product SelectedProduct, int QuantityOrdered)
{
UpdateBalance(SelectedProductNumber, QuantityOrdered);
RemoveItemsFromInventory(SelectedProductNumber, QuantityOrdered);
ShowOrderConfirmationMessage(SelectedProductNumber, QuantityOrdered);
UpdateBalance(SelectedProduct, QuantityOrdered);
RemoveItemsFromInventory(SelectedProduct, QuantityOrdered);
ShowOrderConfirmationMessage(SelectedProduct, QuantityOrdered);
}

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

private static void RemoveItemsFromInventory(int SelectedProductNumber, int QuantityOrdered)
private static void RemoveItemsFromInventory(Product SelectedProduct, int QuantityOrdered)
{
ProductList[SelectedProductNumber-1].Qty = ProductList[SelectedProductNumber-1].Qty - QuantityOrdered;
ProductList.Where(x => x.Equals(SelectedProduct)).First().Qty = SelectedProduct.Qty - QuantityOrdered;
// ProductList[SelectedProduct - 1].Qty = ProductList[SelectedProduct - 1].Qty - QuantityOrdered;
}

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

private static bool VerifyStockOnHand(int SelectedProductNumber, int QuantityOrdered)
private static bool VerifyStockOnHand(Product SelectedProduct, int QuantityOrdered)
{
bool stockOnHand = true;
if (ProductList[SelectedProductNumber-1].Qty <= QuantityOrdered)
int iProductCount = SelectedProduct.Qty;

if ((iProductCount <= 0) || iProductCount < QuantityOrdered)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("Sorry, " + ProductList[SelectedProductNumber-1].Name + " is out of stock");
Console.WriteLine("Sorry, " + SelectedProduct.Name + " is out of stock");
Console.ResetColor();
stockOnHand = false;
}
return stockOnHand;
}

private static bool VerifyUserFundsForSelectedPurchase(int SelectedProductNumber, int QuantityOrdered)
private static bool VerifyUserFundsForSelectedPurchase(Product SelectedProduct, int QuantityOrdered)
{
bool fundsAvailable = true;
if ((LoggedInUser.Balance - (ProductList[SelectedProductNumber-1].Price * QuantityOrdered)) < 0)
if ((LoggedInUser.Balance - (SelectedProduct.Price * QuantityOrdered)) < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Expand Down Expand Up @@ -190,27 +201,39 @@ private static void UpdateCurrentUsersBalance()
File.WriteAllText(@"Data/Products.json", json2);
}

private static int GetValidUserProductSelection()
private static string GetValidUserProductSelection()
{
int productNumber;
string productNumber;
while (true)
{
Console.WriteLine("Enter the product number:");
{
Console.WriteLine("Enter the product Id:");
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 string productId)
{
bool validProductSelected = false;

if (Int32.TryParse(ProductNumberEntered, out productNumber) && (productNumber <= ProductCount + 1))

//NOTE: productId may consist of Alpha-numerics without special characters
if (ProductIdEnteredIsValid(ProductNumberEntered, out productId) && ProductIdEnteredExists(productId))
{
validProductSelected = true;
}


//if (Int32.TryParse(ProductNumberEntered, out productNumber) && (productNumber <= ProductCount + 1))
//{

//}
else if (ProductNumberEntered.ToLower().Equals("quit"))
{
productId = ProductNumberEntered.ToLower();
validProductSelected = true;
}
else
Expand All @@ -220,6 +243,26 @@ private static bool validateProduct(string ProductNumberEntered, out int product
return validProductSelected;
}

private static bool ProductIdEnteredIsValid(string sUserSelection, out string sProductIdSelected)
{
//remove unwanted characters in the selection.
sProductIdSelected = Regex.Replace(sUserSelection.Trim(), @"[\\/':*?<>|]", "");

//Make sure the productSelected is not empty or null
return !String.IsNullOrWhiteSpace(sProductIdSelected);
}

private static bool ProductIdEnteredExists(string sUserSelection)
{
bool bFound = false;
foreach (var prod in ProductList)
{
if (prod.Id.Equals(sUserSelection))
bFound = true;
}
return bFound;
}

private static void ShowProductNumberInvalidMessage()
{
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -236,9 +279,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") + ")");
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 +308,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 +319,7 @@ private static bool LoginUser()
ShowFailedCredentialsMessage();
}
return validatedUser;
}
}

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



private static void ShowWelcomeMessage()
{
Expand Down
Loading