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
8 changes: 8 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
| User stories | Classes | Methods | Considerations | Output/Function |
|--------------|-----------|----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| 1,3,7,10 | Customer | AddProduct(IProduct product) (1) | Exception handling for full basket (3) Exception handling for invalid product (10) Inform customer of the price before adding (7) | Add product to costumers basket |
| 2,5 | Customer | RemoveProduct(IProduct product) (2) | Exception handling for nonexistent item (5) | Remove product from customers basket |
| 6 | Customer | ComputeCost(List<IProduct> basket) (6) | | Compute the total cost of the items in the basket |
| 4 | Manager | ChangeCapacity(int capacity) (4) | | Change the capacity of baskets |
| 8,9 | Bagel | AddFilling(Bagel filling) (8) | Inform customer about the price of a filling (9) | Add fillings to a Bagel |
| | Basket | AddProduct(IProduct product) | | Add product to the basket instance | | |
42 changes: 42 additions & 0 deletions exercise.main/Bagel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Bagel : IProduct
{
public decimal Price { get; set; }
public string Name { get; set; }
public string Variant { get; set; }

public decimal _fillingPrice { get; set; } = 0;

public List<Filling> fillings { get; set; }

public Bagel(string SKU)
{
//Name = "Bagel";
//Variant = variant;
//Price = _prices[variant];
//_fillingPrice = 0;
var info = ProductCatalog.GetProductInfo(SKU);
Price = info.Price;
Name = info.Name;
Variant = info.Variant;
fillings = new List<Filling>();

}

public void AddFilling(Filling filling)
{
fillings.Add(filling);
//_fillingPrice += filling.Price;
//Price += filling.Price;
}

public List<Filling> GetFillings() { return fillings; }
}
}
161 changes: 161 additions & 0 deletions exercise.main/Basket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Basket
{
private List<IProduct> _basket { get; }
private List<Filling> _bagelFillings { get; } = new List<Filling>();
public static int Capacity = 3;

public Basket()
{
_basket = new List<IProduct>();
}

public void AddProduct(IProduct product)
{
if (_basket.Count() == Capacity)
{
throw new InvalidOperationException("Basket capacity full - you can not add more items");
}
else
{
if (product is Bagel)
{
Bagel bagel = (Bagel)product;
bagel.GetFillings().ForEach(x => _bagelFillings.Add(x));
}
_basket.Add(product);
}
}

public void RemoveProduct(IProduct product)
{
//_basket.Remove(product);
if (_basket.Contains(product))
{
_basket.Remove(product);
}
else
{
throw new InvalidOperationException("Product does not exist in basket");
}
}

public decimal ComputeCost()
{
//return _basket.Sum(x => x.Price) - this.ComputeDiscounts();
var info = this.ComputeDiscounts();
decimal total = info.Discounts + info.RemainingItems.Sum(x => x.Price) + _bagelFillings.Sum(x => x.Price);
return total;
}

public (decimal Discounts, List<IProduct> RemainingItems) ComputeDiscounts()
{
List<IProduct> bagels = _basket.Where(x => x is Bagel).OrderBy(v => v.Price).ToList();
List<IProduct> coffees = _basket.Where(x => x is Coffee).OrderBy(v => v.Price).ToList();

decimal sixBagels = 0m;
decimal twelveBagels = 0m;
decimal coffeeBagels = 0m;

int remainingBagels = 0;
int mod = bagels.Count % 12;

if (bagels.Count > 11)
{
// USe list List.RemoveRange() starting at zero
int val = bagels.Count - mod;
twelveBagels = val * 3.99m / (12m);
bagels.RemoveRange(0,val);
}
if (mod > 5)
{
sixBagels = 2.49m;
remainingBagels = mod - 6;
bagels.RemoveRange(0, 6);
}
else
{
remainingBagels = mod;
}

foreach (IProduct coffee in coffees)
{
if (coffee.Variant == "Black" && remainingBagels > 0)
{
coffeeBagels += 1.25m;
remainingBagels--;
bagels.RemoveAt(0);
//coffees.Remove(coffee);
coffee.Price = 0m;
}
}

decimal discounts = twelveBagels + sixBagels + coffeeBagels;
List<IProduct> remainingItems = bagels.Concat(coffees).ToList();

return (discounts, remainingItems);
}

public string GetReceipt()
{
StringBuilder sb = new StringBuilder();
Dictionary<string, (string Name, int count, decimal priceTotal)> productCount = new();
foreach (IProduct item in _basket)
{
if (productCount.ContainsKey(item.Variant))
{
int val = productCount[item.Variant].count + 1;
decimal price = productCount[item.Variant].priceTotal;
productCount[item.Variant] = (item.Name, val, price+item.Price);
}
else
{
productCount[item.Variant] = (item.Name, 1, item.Price);
}
}
int totalWidth = 30;
string title = "~~~ Bob's Bagels ~~~";
string time = DateTime.Now.ToString();
string end = "Thank you for your order!";

sb.AppendFormat("{0," + (((totalWidth - title.Length) / 2) + title.Length) + "}", title);
sb.AppendLine();
sb.AppendLine();
sb.AppendFormat("{0," + (((totalWidth - time.Length) / 2) + time.Length) + "}", time);
sb.AppendLine();
sb.AppendLine();
sb.AppendLine(new String('-', 30));
sb.AppendLine();
//sb.AppendLine();

foreach (var item in productCount)
{
sb.AppendFormat("{0,-20}{1,-5}{2,-5}", item.Key, item.Value.count, item.Value.priceTotal);
sb.AppendLine();
}

sb.AppendLine();
sb.AppendLine(new String('-', 30));
sb.AppendLine();
sb.AppendFormat("{0," + (((totalWidth - end.Length) / 2) + end.Length) + "}", end);

return sb.ToString();
}


public int Count()
{
return _basket.Count;
}

public List<IProduct> GetBasket { get { return _basket; } }
}
}
23 changes: 23 additions & 0 deletions exercise.main/Coffee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Coffee : IProduct
{
public decimal Price { get; set; }
public string Name { get; set; }
public string Variant { get; set; }

public Coffee(string SKU)
{
var info = ProductCatalog.GetProductInfo(SKU);
Price = info.Price;
Name = info.Name;
Variant = info.Variant;
}
}
}
42 changes: 42 additions & 0 deletions exercise.main/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Customer
{
private Basket _basket { get; set; }

public Customer()
{
_basket = new Basket();
}

public void AddProduct(IProduct product)
{
_basket.AddProduct(product);
}

public void RemoveProduct(IProduct product)
{
_basket.RemoveProduct(product);
}

public decimal BasketCost()
{
return _basket.ComputeCost();
}

public string GetReceipt()
{
return _basket.GetReceipt();
}

public Basket GetBasket { get { return _basket; } }
}


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

namespace exercise.main
{
public class Filling : IProduct
{
public decimal Price { get; set; }
public string Name { get; set; }
public string Variant { get; set; }

public Filling(string SKU)
{
var info = ProductCatalog.GetProductInfo(SKU);
Price = info.Price;
Name = info.Name;
Variant = info.Variant;
}
}
}
16 changes: 16 additions & 0 deletions exercise.main/IProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{

public interface IProduct
{
decimal Price { get; set; }
string Name { get; set; }
string Variant { get; set; }
}
}
20 changes: 20 additions & 0 deletions exercise.main/Manager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Manager
{
public Manager()
{
}

public void ChangeBasketCapacity(int newCapacity)
{
Basket.Capacity = newCapacity;
}
}
}
38 changes: 38 additions & 0 deletions exercise.main/ProductCatalog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public static class ProductCatalog
{
private static readonly Dictionary<string, (string Name, decimal Price, string Variant)> _products = new()
{
{ "BGLO", ("Bagel", 0.49m, "Onion") },
{ "BGLP", ("Bagel", 0.39m, "Plain") },
{ "BGLE", ("Bagel", 0.49m, "Everything") },
{ "BGLS", ("Bagel", 0.49m, "Sesame") },
{ "COFB", ("Coffee", 0.99m, "Black") },
{ "COFW", ("Coffee", 1.19m, "White") },
{ "COFC", ("Coffee", 1.29m, "Capuccino") },
{ "COFL", ("Coffee", 1.29m, "Latte") },
{ "FILB", ("Filling", 0.12m, "Bacon") },
{ "FILE", ("Filling", 0.12m, "Egg") },
{ "FILC", ("Filling", 0.12m, "Cheese") },
{ "FILX", ("Filling", 0.12m, "Cream Cheese") },
{ "FILS", ("Filling", 0.12m, "Smoked Salmon") },
{ "FILH", ("Filling", 0.12m, "Ham") }
};

public static (string Name, decimal Price, string Variant) GetProductInfo(string code)
{
if (_products.TryGetValue(code, out var product))
{
return product;
}
throw new KeyNotFoundException($"Product code '{code}' not found");
}
}
}
1 change: 1 addition & 0 deletions exercise.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "exercise.tests", "exercise.
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{825CCFE7-4F2E-4770-8393-FEB732F66EE4}"
ProjectSection(SolutionItems) = preProject
domain-model.md = domain-model.md
extension1.md = extension1.md
extension2.md = extension2.md
extension3.md = extension3.md
Expand Down
Loading