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
Binary file added Docs/Basic Unit Testing.pptx
Binary file not shown.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# refactoring-training
Refactoring Training hands-on example
# unit-test-training
Unit Test Training hands-on example

* Clone project in Visual Studio
* Refactor!
* Test!

[![Build Status](https://travis-ci.org/bkraitberg/refactoring-training-2.2.svg?branch=master)](https://travis-ci.org/bkraitberg/refactoring-training-2.2)
[![Build Status](https://travis-ci.org/roddewit/unit-test-training.svg?branch=master)](https://travis-ci.org/roddewit/unit-test-training)
4 changes: 2 additions & 2 deletions Refactoring.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Refactoring", "Refactoring\Refactoring.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tusc", "Refactoring\Tusc.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{1FAF99A1-CCBF-435D-B599-4DAE1DAD5105}"
EndProject
Expand Down
4 changes: 2 additions & 2 deletions Refactoring/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Authenticator(List<User> users)
this.users = users;
}

internal User Authenticate(string username, string password)
public User Authenticate(string username, string password)
{
return FindUserByCredentials(username, password);
}
Expand All @@ -24,7 +24,7 @@ private User FindUserByCredentials(string username, string password)
{
if (IsValidUsername(username))
{
return users.FirstOrDefault(user => user.Name.Equals(username) && user.Password.Equals(password));
return users.FirstOrDefault(user => user.Name.Equals(username) && (password == null || user.Password.Equals(password)));
}
else
{
Expand Down
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
}
]
1 change: 0 additions & 1 deletion Refactoring/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static User LogIn(List<User> users)
{
WriteInvalidLoginMessage();

// Exit gracefully
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
Console.ReadLine();
Expand Down
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
41 changes: 25 additions & 16 deletions Refactoring/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Refactoring
public class Store
{
private readonly User user;
private readonly List<Product> products;
private readonly DataManager dataManager;

public Store(User user, DataManager dataManager)
Expand All @@ -17,8 +18,10 @@ public Store(User user, DataManager dataManager)
this.dataManager = dataManager;
}

public void Purchase(Product product, int quantity)
public void Purchase(string productId, int quantity)
{
Product product = this.GetProductById(productId);

if (!UserHasFundsForPurchase(product, quantity))
{
throw new InsufficientFundsException();
Expand All @@ -29,52 +32,58 @@ public void Purchase(Product product, int quantity)
throw new OutOfStockException();
}

product.Quantity = product.Quantity - quantity;
product.Quantity = product.Quantity - quantity+1;
user.Balance = user.Balance - product.Price * quantity;

dataManager.SaveUser(user);
dataManager.SaveProduct(product);
}

public bool UserHasFundsForPurchase(Product product, int quantity)
private bool UserHasFundsForPurchase(Product product, int quantity)
{
double totalPurchasePrice = product.Price * quantity;
return user.Balance >= totalPurchasePrice;
}

public bool StoreHasStockForPurchase(Product product, int quantity)
private bool StoreHasStockForPurchase(Product product, int quantity)
{
return product.Quantity >= quantity;
}

public void WriteProductList()
public string GetProductList()
{
// Prompt for user input
Console.WriteLine();
Console.WriteLine("What would you like to buy?");
string output = "\n";
output += "What would you like to buy?\n";

foreach (var item in dataManager.Products.Select((product, index) => new { index, product }))
foreach (var product in dataManager.Products.Where(p => p.Quantity > 0))
{
string productDisplay = GetFormattedProductText(item.product, item.index + 1);
Console.WriteLine(productDisplay);
string productDisplay = GetFormattedProductText(product);
output += productDisplay + "\n";
}

Console.WriteLine(dataManager.Products.Count + 1 + ": Exit");
output += "Type quit to exit the application\n";

return output;
}

public int NumberOfProducts()
{
return dataManager.Products.Count;
}

public Product GetProductByIndex(int index)
public Product GetProductById(string productId)
{
return dataManager.Products.FirstOrDefault(p => p.Id.Equals(productId));
}

public bool ContainsProduct(string productId)
{
return dataManager.Products[index];
return dataManager.Products.Count(p => p.Id.Equals(productId)) > 0;
}

private static string GetFormattedProductText(Product product, int productIndex)
private static string GetFormattedProductText(Product product)
{
return String.Format("{0}: {1} ({2:C})", productIndex, product.Name, product.Price);
return String.Format("{0}: {1} ({2:C})", product.Id, product.Name, product.Price);
}
}
}
38 changes: 14 additions & 24 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public void Run()
bool done = false;
while (!done)
{
store.WriteProductList();
Console.Write(store.GetProductList());

int productIndex = ReadProductIndex(store.NumberOfProducts());
string productId = ReadProductId();

if (productIndex == store.NumberOfProducts() + 1)
if (productId.Equals("quit"))
{
done = true;
}
else
else if (!productId.Equals(""))
{
Product product = store.GetProductByIndex(productIndex-1);
Product product = store.GetProductById(productId);

WriteProductToPurchaseMessage(product);

Expand All @@ -46,7 +46,8 @@ public void Run()
{
if (purchaseQuantity > 0)
{
store.Purchase(product, purchaseQuantity);
store.Purchase(productId, purchaseQuantity);
WriteSuccessfulPurchaseMessage(product, purchaseQuantity);
}
else
{
Expand Down Expand Up @@ -138,28 +139,17 @@ private static string ReadText(string message)
return Console.ReadLine();
}

private static int ReadProductIndex(int numProducts)
private string ReadProductId()
{
int productIndex;
bool validIntegerEntered = Int32.TryParse(ReadText("Enter a number: "), out productIndex);

while (!validIntegerEntered || !IsValidProductSelected(numProducts, productIndex))
string productId = ReadText("Enter a product ID: ");
if (!productId.Equals("quit") && !store.ContainsProduct(productId))
{
Console.WriteLine("Invalid number entered, please enter a valid number");
Console.WriteLine("Invalid product ID entered, please enter a valid ID");
productId = "";
}

return productIndex;
}

private static bool IsExitProductSelected(List<Product> products, int enteredProductIndex)
{
return enteredProductIndex == products.Count + 1;
}

private static bool IsValidProductSelected(int numProducts, int enteredProductIndex)
{
return enteredProductIndex > 0 || enteredProductIndex <= numProducts;
return productId;
}


private static void WriteCurrentBalanceMessage(User loggedInUser)
{
Expand Down
File renamed without changes.
Loading