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
10 changes: 10 additions & 0 deletions Domain Model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| Classes | Methods/Properties | Scenario | Outputs |
|----------|---------------------------------------|-----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| Store.cs | AddItem(string i) | Adds an item of type Bagel or Coffee into the basket and checks against item capacity and inventory | Bool that is true if capacity is not reached and item is in inventory |
| Store.cs | AddFilling(string item, string bagel) | Adds Filling to a given Bagel if bagel exists in basket | Bool that is true when bagel exists and filling is in inventory |
| Store.cs | RemoveItem(string i) | Removes item from basket | Bool that is true if item is in basket |
| Store.cs | ChangeCapacity(int capacity) | Changes the capacity of the basket | -- |
| Store.cs | CheckTotal() | Checks total cost of items in basket | decimal type with the total cost of basket |
| Store.cs | CheckItemCost(string ska) | Checks cost of item with the given ska | cost of item in decimal type |
| Store.cs | Discount() | Adds discount to items in Basket based on a priority based list of discounts | -- |
| Store.cs | Receipt() | Shows Basket items and discounted items with a total according to image in extension 2 | return string of a reciept like shown in extention 2 |
24 changes: 24 additions & 0 deletions exercise.main/Bagel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

public Bagel(string sku, decimal price, string name, string variant)
{
this.Sku = sku;
this.Price = price;
this.Name = name;
this.Variant = variant;
}
}
}
25 changes: 25 additions & 0 deletions exercise.main/Coffee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

public Coffee(string sku, decimal price, string name, string variant)
{
this.Sku = sku;
this.Price = price;
this.Name = name;
this.Variant = variant;
}

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

namespace exercise.main
{
public class DoubleDiscount : IDiscounts
{
public string Item { get; set; } = string.Empty;
public string Item2 { get; set; } = string.Empty;
public int Amount { get; set; }
public decimal Price { get; set; }


public DoubleDiscount(string name, string name2, int amount, decimal price)
{
this.Item = name;
this.Item2 = name2;
this.Amount = amount;
this.Price = price;
}
}
}
24 changes: 24 additions & 0 deletions exercise.main/Filling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Filling : Iitems
{
public string Sku { set; get; }
public decimal Price { set; get; }
public string Name { set; get; }
public string Variant { set; get; }
public Filling(string sku, decimal price, string name, string variant)
{
this.Sku = sku;
this.Price = price;
this.Name = name;
this.Variant = variant;
}

}
}
16 changes: 16 additions & 0 deletions exercise.main/IDiscounts.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 IDiscounts
{
string Item { get; set; }
string Item2 { get; set; }
int Amount { get; set; }
decimal Price { get; set; }
}
}
17 changes: 17 additions & 0 deletions exercise.main/Iitems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public interface Iitems
{
string Sku { set; get; }
decimal Price { set; get; }
string Name { set; get; }
string Variant { set; get; }

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

namespace exercise.main
{
public class RegularDiscount : IDiscounts
{
public string Item { get; set; } = string.Empty;
public int Amount { get; set; }
public decimal Price { get; set; }

public string Item2 { get; set; } = string.Empty;

public RegularDiscount(string name, int amount, decimal price)
{
this.Item = name;
this.Amount = amount;
this.Price = price;
}
}
}
209 changes: 209 additions & 0 deletions exercise.main/Store.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Store
{
public List<Iitems> inventory = new List<Iitems>
{
new Bagel("BGLO", 0.49m, "Bagel", "Onion"),
new Bagel("BGLP", 0.39m, "Bagel", "Plain"),
new Bagel("BGLE", 0.49m, "Bagel", "Everything"),
new Bagel("BGLS", 0.49m, "Bagel", "Sesame"),
new Coffee("COFB", 0.99m, "Coffee", "Black"),
new Coffee("COFW", 1.19m, "Coffee", "White"),
new Coffee("COFC", 1.29m, "Coffee", "Capuccino"),
new Coffee("COFL", 1.29m, "Coffee", "Latte"),
new Filling("FILB", 0.12m, "Filling", "Bacon"),
new Filling("FILE", 0.12m, "Filling", "Egg"),
new Filling("FILC", 0.12m, "Filling", "Cheese"),
new Filling("FILX", 0.12m, "Filling", "Cream Cheese"),
new Filling("FILS", 0.12m, "Filling", "Smoked Salmon"),
new Filling("FILH", 0.12m, "Filling", "Ham"),
};

//The order of the discounts in the list is the priority of the discounts. An item can only have 1 discount.
public List<IDiscounts> discounts = new List<IDiscounts>
{
new RegularDiscount("Bagel", 12, 3.99m),
new RegularDiscount("Bagel", 6, 2.49m),
new DoubleDiscount("Bagel", "Coffee", 1, 1.25m)
};

public List<decimal> discounted = new List<decimal>();

public Dictionary<string, string> Fills = new Dictionary<string, string>();

public List<Iitems> Basket;

public List<Iitems>? CopiedBasket;
public int capacity { set; get; } = 10;

public Store()
{
Basket = new List<Iitems>();
}

public bool AddItem(string item)
{
if (Basket.Count < capacity)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].Sku == item && item.StartsWith("B"))
{
Iitems added = new Bagel(inventory[i].Sku, inventory[i].Price, inventory[i].Name, inventory[i].Variant);
Basket.Add(added);
return true;
}
if (inventory[i].Sku == item && item.StartsWith("C"))
{
Iitems added = new Coffee(inventory[i].Sku, inventory[i].Price, inventory[i].Name, inventory[i].Variant);
Basket.Add(added);
return true;
}
}
return false;
}
return false;
}

public bool AddFilling(string item, string bagel)
{
if (inventory.Any(n => n.Sku == item) && Basket.Count < capacity)
{
Filling? full = (Filling?)inventory.Find(n => n.Sku == item);

if (inventory.Any(n => n.Sku == bagel)) {
Bagel? b = (Bagel?)Basket.Find(n => n.Sku == item);
Fills[bagel] = item;
}
Basket.Add(full);
return true;
}
else
{
return false;
}
}

public bool RemoveItem(string item)
{
if (Basket.Any(n => n.Sku == item))
{
Iitems? full = Basket.Find(n => n.Sku == item);
Basket.Remove(full);
return true;
}
else if (Basket.Any(n => n.Name == item)) {
Iitems? full = Basket.Find(n => n.Name == item);
Basket.Remove(full);
return true;
}
return false;
}

public decimal CheckTotal()
{
CopiedBasket = new List<Iitems>(Basket);
Discount();
decimal total = Basket.Sum(x => x.Price) + discounted.Sum();
return total;
}

public decimal CheckItemCost(string item)
{
Iitems? cost = inventory.Find(n => n.Sku == item);
if (cost == null)
{
return 0;
}
return cost.Price;
}

public void ChangeCapacity(int capacity)
{
this.capacity = capacity;
}

public string PrintReceipt()
{
var b = new System.Text.StringBuilder();
b.AppendLine(" ~~~ Bob's Bagels ~~~");
b.AppendLine();
b.AppendLine(DateTime.Now.ToString());
b.AppendLine();
b.AppendLine("----------------------------");
b.AppendLine();

CopiedBasket.ForEach(item => b.AppendLine(item.Name + " " + item.Variant));
b.AppendLine();
b.AppendLine("----------------------------");
b.AppendLine(
$" Total cost is £{CheckTotal()}"
);
b.AppendLine();
b.AppendLine($" Total discount is £{CheckTotal() - CopiedBasket.Sum(x => x.Price)}");
b.AppendLine();
b.AppendLine("Thank you for your order!");
return b.ToString();

}

public void Discount()
{

for (int i = 0; i < discounts.Count; i++)
{
if (discounts[i] is RegularDiscount) {
foreach (Iitems ware in inventory)
{
if (Basket.Where(item => item.Sku == ware.Sku).ToList().Count >= discounts[i].Amount)
{
discounted.Add(discounts[i].Price);
for (int j = 0; j < discounts[i].Amount; j++)
{
RemoveItem(ware.Sku);
}
}
}
}

if (discounts[i] is DoubleDiscount)
{
int counter = 0;
int counter2 = 0;
foreach (var ware in Basket)
{
if (ware.Name == discounts[i].Item)
{
counter++;
}
if (ware.Name == discounts[i].Item2)
{
counter2++;
}
}
while (counter > 0 && counter2 > 0)
{
discounted.Add(discounts[i].Price);
counter -= 1;
counter2 -= 1;
for (int j = 0; j < discounts[i].Amount; j++)
{
RemoveItem(discounts[i].Item);
RemoveItem(discounts[i].Item2);
}
}
}

}

}
}
}
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