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
14 changes: 14 additions & 0 deletions exercise.main/BagelShop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class BagelShop
{
public int _basketSize { get; set; }

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

namespace exercise.main
{
public class Basket
{

private int _basketSize;
public List<IProduct> ProductList = new List<IProduct>();
private Inventory _inventory;

public Basket(int basketSize, Inventory inventory)
{
_inventory = inventory;
_basketSize = basketSize;
}

public void Add(IProduct product)
{
if (!IsFull())
{
ProductList.Add(product);
}
}

public void Remove(int placeInList)
{
ProductList.RemoveAt(placeInList);
}

public bool IsFull()
{
return ProductList.Count < _basketSize ? false : true;
}

public int BasketSize { get { return _basketSize; } set { _basketSize = value; } }

public bool ItemInBasket(string type)
{
return ProductList.Any(x => x.SKU_NAME == type);
}

public float getBagelCost(string bagelSKU)
{
return _inventory.inventory[bagelSKU].Price;
}

public float getTotalCostForBagels()
{
return ProductList.OfType<Bagel>().Sum(x => x.Price);
}

public float getTotalCost()
{
return ProductList.Sum(x => x.getTotalPrice());
}

public float getTotalPriceForAllFillings()
{
return (float)Math.Round(ProductList.OfType<Bagel>().Sum(x => x.costForFillings()), 2);
}

public float getFillingCost(string fillingSKU)
{
return _inventory.getFillingCost(fillingSKU);
}

public bool inInventory(string item)
{
return _inventory.inInventory(item);
}

public Dictionary<string, int> getItemCount()
{
Dictionary<string, int> itemCount = new Dictionary<string, int>();
itemCount = ProductList.GroupBy(x => x).ToDictionary(g => g.Key.SKU_NAME, g => g.Count());

return itemCount;
}

public float caluculateDiscount()
{
Dictionary<string, int> itemCount = getItemCount();
float discountPrice = 0;
if (checkIfBundleDealAvaiable(itemCount))
{
foreach (var item in itemCount)
{
if (item.Value >= 6 && _inventory.inventory[item.Key].Type == Type.Bagel)
{
discountPrice += getPriceForItemWithBundleDiscount(item.Key, item.Value);
}
else
{
discountPrice += item.Value * _inventory.inventory[item.Key].Price;
}
}
return discountPrice;
}
else if (checkIfCoffeeDealAvailable(itemCount))
{
List<string> coffeeList = TypeList(itemCount, Type.Coffee);
List<string> bagelList = TypeList(itemCount, Type.Bagel);
int numberOfCoffee = coffeeList.Count;
int numberOfBagel = bagelList.Count;

for (int i = 0; i < Math.Min(numberOfCoffee, numberOfBagel); i++)
{
itemCount[coffeeList[i]] -= 1;
itemCount[bagelList[i]] -= 1;
discountPrice += 1.25f;
}
foreach (var item in itemCount)
{

discountPrice += item.Value * _inventory.inventory[item.Key].Price;
}
return discountPrice;
}
else
{
foreach (var item in itemCount)
{

discountPrice += item.Value * _inventory.inventory[item.Key].Price;
}
return discountPrice;
}
}

public List<string> TypeList(Dictionary<string, int> dict, Type type)
{
List<string> typeList = new List<string>();
foreach (var item in dict)
{
if (_inventory.inventory[item.Key].Type == type)
{
for (int i = 0; i < item.Value; i++)
{
typeList.Add(item.Key);
}
}
}
return typeList;
}

public bool checkIfBundleDealAvaiable(Dictionary<string, int> dict)
{
foreach (var item in dict)
{
if (item.Value >= 6 && _inventory.inventory[item.Key].Type == Type.Bagel)
{
return true;
}
}
return false;
}

public bool checkIfCoffeeDealAvailable(Dictionary<string, int> dict)
{
return (dict.Any(x => _inventory.inventory[x.Key].Type == Type.Bagel)
&& dict.Any(y => _inventory.inventory[y.Key].Type == Type.Coffee));
}

public float getPriceForItemWithBundleDiscount(string bagel, int amount)
{
int twelveBundle = 0;
int sixBundle = 0;
int singles = 0;

if (amount >= 12)
{
twelveBundle = amount/12;
singles = amount%12;
if (singles > 6)
{
sixBundle = singles/6;
singles = singles%6;
}
}
else if (amount >= 6)
{
sixBundle = amount / 6;
singles = amount % 6;

}
return twelveBundle * 3.99f + sixBundle * 2.49f + singles * _inventory.inventory[bagel].Price;
}


public string BuildReceipt()
{
StringBuilder receipt = new StringBuilder();
DateTime dateTime = DateTime.Now;
Dictionary<string, int> itemCount = getItemCount();

receipt.AppendLine("***** Bob's Bagels ******");
receipt.AppendLine($" {dateTime.ToString()}");
receipt.AppendLine("--------------------------");
foreach (var item in itemCount)
{
receipt.AppendLine($"{item.Key,-15}{item.Value,-6}£{_inventory.inventory[item.Key].Price * item.Value}");
}
receipt.AppendLine("--------------------------");
receipt.AppendLine($"Total:{Math.Round(getTotalCost(),2),20}");
return receipt.ToString();
}

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

namespace exercise.main
{
public enum Type
{
Bagel,
Coffee,
Filling
}
}
19 changes: 19 additions & 0 deletions exercise.main/IProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public interface IProduct
{
string SKU_NAME { get; set; }

float Price { get; set; }

Type type { get; }

float getTotalPrice();
}
}
29 changes: 29 additions & 0 deletions exercise.main/InvItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class InvItem
{
private int _id;
private float _price;
private Type _type;

public InvItem(int setId, float setPrice, Type type)
{
_price = setPrice;
_id = setId;
_type = type;
}

public int ID { get { return _id; } }
public float Price { get { return _price; } }
public Type Type { get { return _type; } }



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

namespace exercise.main
{
public class Inventory
{
public Dictionary<string, InvItem> inventory = new Dictionary<string, InvItem>();


public Inventory()
{
inventory.Add("BGLO", new InvItem(0, 0.49f, Type.Bagel));
inventory.Add("BGLP", new InvItem(1, 0.39f, Type.Bagel));
inventory.Add("BGLE", new InvItem(2, 0.49f, Type.Bagel));
inventory.Add("BGLS", new InvItem(3, 0.49f, Type.Bagel));
inventory.Add("FILB", new InvItem(4, 0.12f, Type.Filling));
inventory.Add("FILE", new InvItem(5, 0.12f, Type.Filling));
inventory.Add("FILC", new InvItem(6, 0.12f, Type.Filling));
inventory.Add("FILX", new InvItem(7, 0.12f, Type.Filling));
inventory.Add("FILS", new InvItem(8, 0.12f, Type.Filling));
inventory.Add("FILH", new InvItem(9, 0.12f, Type.Filling));
inventory.Add("COFB", new InvItem(10, 0.99f, Type.Coffee));
inventory.Add("COFW", new InvItem(11, 1.19f, Type.Coffee));
inventory.Add("COFC", new InvItem(12, 1.29f, Type.Coffee));
inventory.Add("COFL", new InvItem(13, 1.29f, Type.Coffee));

}

public float getFillingCost(string fillingSKU )
{
return inventory[fillingSKU].Price;
}

public bool inInventory(string SKU)
{
return inventory.ContainsKey(SKU);
}
}
}
44 changes: 44 additions & 0 deletions exercise.main/Products/Bagel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.Products
{

public class Bagel : IProduct
{
private string _NAME;
private List<Filling> fillings;
private float _cost;
private Type _type;

public Bagel(string name, Inventory inventory)
{
_NAME = name;
fillings = new List<Filling>();
_cost = inventory.inventory[name].Price;
_type = inventory.inventory[name].Type;
}
public string SKU_NAME { get { return _NAME; } set { _NAME = value; } }
public float Price { get { return _cost; } set { _cost = value; } }
public List<Filling> Fillings { get { return fillings; } }

public Type type { get { return _type; } }

public void addFilling(Filling filling)
{
fillings.Add(filling);
}
public float costForFillings()
{
return fillings.Sum(x => x.Price);
}
public float getTotalPrice()
{
return costForFillings() + _cost;
}
}
}
Loading