Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 1, 2, 3, 4, and bonus #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions Refactoring/Data/Products.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,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",
"Name": "Soup",
"Price": 1.25,
"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("Id")]
public string Id;
[JsonProperty("Name")]
public string Name;
[JsonProperty("Price")]
Expand Down
37 changes: 22 additions & 15 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,21 @@ public static void Start(List<User> usrs, List<Product> prods)
// Prompt for user input
Console.WriteLine();
Console.WriteLine("What would you like to buy?");
for (int i = 0; i < 7; i++)
for (int i = 0; i < prods.Count; i++)
{
Product prod = prods[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(prods.Count + 1 + ": Exit");
Console.WriteLine("Type quit to exit the application");

// Prompt for user input
Console.WriteLine("Enter a number:");
Console.WriteLine("Enter an option:");
string answer = Console.ReadLine();
int num = Convert.ToInt32(answer);
num = num - 1; /* Subtract 1 from number
num = num + 1 // Add 1 to number */

// Check if user entered number that equals product count
if (num == prods.Count)
if (answer.ToUpper() == "QUIT")
{
// Update balance
foreach (var usr in usrs)
Expand Down Expand Up @@ -138,8 +137,11 @@ public static void Start(List<User> usrs, List<Product> prods)
}
else
{
string id = answer;
var productIndex = GetProductIndexById(prods, id);

Console.WriteLine();
Console.WriteLine("You want to buy: " + prods[num].Name);
Console.WriteLine("You want to buy: " + prods[productIndex].Name);
Console.WriteLine("Your balance is " + bal.ToString("C"));

// Prompt for user input
Expand All @@ -148,7 +150,7 @@ public static void Start(List<User> usrs, List<Product> prods)
int qty = Convert.ToInt32(answer);

// Check if balance - quantity * price is less than 0
if (bal - prods[num].Price * qty < 0)
if (bal - prods[productIndex].Price * qty < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -159,12 +161,12 @@ public static void Start(List<User> usrs, List<Product> prods)
}

// Check if quantity is less than quantity
if (prods[num].Qty <= qty)
if (prods[productIndex].Qty < qty)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock");
Console.WriteLine("Sorry, " + prods[productIndex].Name + " is out of stock");
Console.ResetColor();
continue;
}
Expand All @@ -173,14 +175,14 @@ public static void Start(List<User> usrs, List<Product> prods)
if (qty > 0)
{
// Balance = Balance - Price * Quantity
bal = bal - prods[num].Price * qty;
bal = bal - prods[productIndex].Price * qty;

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

Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You bought " + qty + " " + prods[num].Name);
Console.WriteLine("You bought " + qty + " " + prods[productIndex].Name);
Console.WriteLine("Your new balance is " + bal.ToString("C"));
Console.ResetColor();
}
Expand Down Expand Up @@ -226,5 +228,10 @@ public static void Start(List<User> usrs, List<Product> prods)
Console.WriteLine("Press Enter key to exit");
Console.ReadLine();
}

private static int GetProductIndexById(List<Product> prods, string id)
{
return prods.FindIndex(prod => prod.Id.Equals(id));
}
}
}
86 changes: 63 additions & 23 deletions UnitTestProject/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Test_StartingTuscFromMainDoesNotThrowAnException()
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Expand All @@ -66,7 +66,7 @@ public void Test_TuscDoesNotThrowAnException()
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Expand Down Expand Up @@ -134,7 +134,7 @@ public void Test_UserCanCancelPurchase()
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n0\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n0\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Expand All @@ -157,7 +157,7 @@ public void Test_ErrorOccursWhenBalanceLessThanPrice()
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Expand All @@ -173,13 +173,13 @@ public void Test_ErrorOccursWhenProductOutOfStock()
{
// Update data file
List<Product> tempProducts = DeepCopy<List<Product>>(originalProducts);
tempProducts.Where(u => u.Name == "Chips").Single().Qty = 0;
tempProducts.Where(u => u.Name == "Chips").Single().Qty = 1;

using (var writer = new StringWriter())
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n2\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Expand All @@ -197,35 +197,75 @@ public void Test_ProductListContainsExitItem()
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\n8\r\n\r\n"))
using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Tusc.Start(users, products);
}

Assert.IsTrue(writer.ToString().Contains("8: Exit"));
Assert.IsTrue(writer.ToString().Contains("Type quit to exit the application"));
}
}

//[Test]
//public void Test_UserCanExitByEnteringQuit()
//{
// using (var writer = new StringWriter())
// {
// Console.SetOut(writer);
[Test]
public void Test_UserCanPurchaseProductWhenOnlyOneInStock()
{
products[0].Qty = 1;

using (var writer = new StringWriter())
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\n1\r\n1\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Tusc.Start(users, products);
}

// using (var reader = new StringReader("Jason\r\nsfa\r\nquit\r\n\r\n"))
// {
// Console.SetIn(reader);
Assert.IsFalse(writer.ToString().Contains("is out of stock"));
}
}

// Tusc.Start(users, products);
// }
[Test]
public void Test_UserCanExitByEnteringQuit()
{
using (var writer = new StringWriter())
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

// Assert.IsTrue(writer.ToString().Contains("Type quit to exit the application"));
// Assert.IsTrue(writer.ToString().Contains("Press Enter key to exit"));
// }
//}
Tusc.Start(users, products);
}

Assert.IsTrue(writer.ToString().Contains("Type quit to exit the application"));
Assert.IsTrue(writer.ToString().Contains("Press Enter key to exit"));
}
}

[Test]
public void Test_ProductsWithZeroQuantityDoNotAppearInMenu()
{
products[0].Qty = 0;

using (var writer = new StringWriter())
{
Console.SetOut(writer);

using (var reader = new StringReader("Jason\r\nsfa\r\nquit\r\n\r\n"))
{
Console.SetIn(reader);

Tusc.Start(users, products);
}

Assert.IsFalse(writer.ToString().Contains(": Chips"));
}
}

private static T DeepCopy<T>(T obj)
{
Expand Down