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
152 changes: 152 additions & 0 deletions domain-model.md

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions exercise.main/Basket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using exercise.main.Discount;

namespace exercise.main
{
public class Basket
{
private List<Product> products = new List<Product>();
private Inventory _inventory;


private int _capacity;
private int Capacity { get => _capacity; }
private int NrOfItems { get => products.Count; }
private DiscountManager _DM;
private DiscountManager DM { get => _DM; }
private List<string> _warnings = new List<string>();
public List<string> Warnings {
get {
var temp = _warnings.ToList();
_warnings.RemoveRange(0, _warnings.Count);
return temp;
}
}
public Basket(Inventory inventory, DiscountManager dm ,int capacity = 15)
{
this._inventory = inventory;
this._capacity = capacity;
this._DM = dm;
}

public void setCapacity(int newCapacity)
{
this._capacity = newCapacity;
if (this.Capacity < products.Count)
{
var tempProducts = products.ToList().GetRange(0,this.Capacity);
products = tempProducts;
}
}


public void addProduct(string productSku, int amount = 1)
{
if (amount < 1)
{
_warnings.Add("Amount to add must be positive");
return;
}

int productStock = this._inventory.getStock(productSku);
if (productStock < amount)
{
_warnings.Add($"Can't add {amount} {productSku}, as the there's {productStock} in stock ");
return;
}

if (NrOfItems + amount > Capacity )
{
_warnings.Add($"Can't add {amount} {productSku}, will be more than Baskets Capacity of {Capacity} items");
return;
}


for (int i = 0; i < amount; i++)
{
this.products.Add(_inventory.createProductType(productSku));
}
this._inventory.decreaseStock(productSku, amount);
}
public bool removeProduct(string productSku, int amount = 1)
{
if (amount < 1)
{
_warnings.Add($"Amount to remove must be positive");
return false;

}

int amountProductInBasket = this.countProductTypes(productSku);
int nrToRemove = Math.Min(amountProductInBasket, amount);
var itemsToRemoveList = this.products.Where(x => x.SKU == productSku).ToList();
if (itemsToRemoveList.Count == 0)
{
_warnings.Add($"Can't remove {productSku}, as its not in the basket");
return false;
}
for (int i = 0; i < nrToRemove; i++)
{
this.products.Remove(itemsToRemoveList[i]);
}

this._inventory.IncreaseStock(productSku, nrToRemove);
return true;
}
public int countProductTypes(string SKU)
{
return products.Where(x=>x.SKU == SKU).Count();

}
public List<Product> getProducts()
{
// Return a deep copy of the list...
var cpyList = new List<Product>();
foreach (Product product in this.products)
{
cpyList.Add(product);
}
return cpyList;
}
public Dictionary<string, int> getAmountPerSku()
{
var productList = getProducts();
Dictionary<string, int> tempSku = new Dictionary<string, int>();
foreach (var product in productList)
{
if (!tempSku.ContainsKey(product.SKU))
{
tempSku[product.SKU] = 0;
}
tempSku[product.SKU]++;
}
return tempSku;
}
public bool isNotEmpty()
{
return this.products.Count > 0;
}

public float getTotal()
{
var orderDataDict = DM.calculateTotalWithDiscount(this);

return orderDataDict.Values.Sum(x => x.total_price);

}

public float getUndiscountedTotal()
{
return this.products.Sum(x => x.ProductPrice);

}

public string stringify(DiscountManager dm)
{
var cacledBasket = dm.calculateTotalWithDiscount(this);
string ret = string.Format("{0,7} {1,-25}{2,-10}{3,-10}\n", "Type", "Name", "Amount", "Cost");

foreach (var x in cacledBasket.ToList())
{
if (x.Value.UsedDiscount == null)
{
ret += "\n"+ string.Format("{0,7} {1,-25}{2,-10}{3,-10}", _inventory.getProductType(x.Value.name) ,_inventory.getName(x.Value.name), x.Value.amount, x.Value.total_price);
}
else
{
ret += "\n"+ string.Format("{0,7} {1,-25}", "Deal", x.Value.UsedDiscount.stringify());
ret += "\n"+ string.Format("{0,7} {1,-25}{2, -10}{3, -10}","", "", x.Value.amount, x.Value.total_price);
}
}

ret += $"\n\n Total: {getTotal()}";

return ret;
}

}
}
62 changes: 62 additions & 0 deletions exercise.main/CashRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using exercise.main;
using exercise.main.Discount;

namespace exercise.tests
{
public class CashRegister
{

private Inventory _inventory ;
private DiscountManager _dm;
private Order currentOrder;
private Basket currentBasket;
public CashRegister(Inventory inventory,DiscountManager dm)
{
_inventory = inventory;
_dm= dm;
currentOrder = null;
currentBasket = null;
}
public void registerBasket(Basket basket)
{

var cacledBasket = this._dm.calculateTotalWithDiscount(basket);
currentBasket = basket;

string ret = $"Pay:\n\t Total: {basket.getTotal()}";

currentOrder = new Order(cacledBasket);

Console.WriteLine(ret);
}
private string createReciept()
{
return currentOrder.createReciept(_inventory, currentBasket);
}
public string finalizePurchase(bool successfulPayment)
{
// Function is called to confirm the payment
// If payment failed, successPayment will be false
// This is just to demonstrate... it's not applicable in real life situation...

string returnMsg = "";

if(successfulPayment)
{
// return Reciept
returnMsg = createReciept();
}
else
{
// return error message
returnMsg = "You failed paying for your bagels...";
}

this.currentOrder = null;
this.currentBasket = null;

return returnMsg;
//basket.
}
}
}
24 changes: 24 additions & 0 deletions exercise.main/Discount/DiscountBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace exercise.main.Discount
{
public abstract class DiscountBase
{
public DiscountBase(float discountedPrice, Inventory inventory)
{
discountPrice = discountedPrice;
_inventory = inventory;
}

private float discountPrice;
protected Inventory _inventory;

public float DiscountPrice { get => discountPrice; }

public abstract bool checkCondition(Basket basket);
public abstract DiscountedProductCount getDiscountedPrice(Basket basket);
public abstract string stringify();
}




}
Loading