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 Domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
| Classes | Methods | Scenario | Outputs |
|------------ |--------------------------------- |---------------------------------------------------------------------- |------------------ |
| Basket.cs | AddBagel(Bagel bagelType) | adds bagel to basket, if in stock | |
| Bakset.cs | RemoveBagel(Bagel bagelType) | removes bagel from basket | |
| basket.cs | IsFull() | if basket full when adding item, throw IsFullexception | throws exception |
| store.cs | ChangeBasketCapacity() | manager changes all the baskets capacities in the store | |
| basket.cs | ItemNotPresent() | throws ItemNotPresentException if trying to remove non existing item | throws exception |
| basket.cs | GetTotalCost() | gets the price of all items combined in basket | int total; |
| Bagel.cs | GetCost() | returns the cost which is a constant member set in the class | int cost; |
| Bagel.cs | AddFilling(Filling fillingType) | adds filling to the bagels list of fillings, if in stock | |
| Filling.cs | GetCost() | Returns the cost of the filling | int cost; |
| store.cs | AvaiableItems() | returns all avaiable items in stock | List<Item> items |
| basket.cs | Discount() | calculates discount | int discount; |
| basket.cs | PrintReceipts() | prints out receipte from basket, with savings | |
54 changes: 54 additions & 0 deletions exercise.main/Bagel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Bagel : Iproduct
{

public string Name { get; set; }
public string SKU { get; set; }
public float Price { get; set; }
public string Variant { get; set; }
public List<Iproduct> fillings { get; set; }
public Bagel(string name, string sKU, float price, string variant)
{
Name = name;
SKU = sKU;
Price = price;
Variant = variant;
fillings = new List<Iproduct>();
}

public float GetPrice()
{
return Price;
}
public void AddFilling(Iproduct filling)
{
fillings.Add(filling);
}

public string GetVariant()
{
return this.Variant;
}
public string GetName()
{
return this.Name;
}
public string GetSKU()
{
return SKU;
}

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

namespace exercise.main
{
public class Basket
{
private int _capacity = 10;
public HashSet<Coupon> coupons { get; set; } = new HashSet<Coupon>();
public List<Coupon> usedCoupons { get; set; } = new List<Coupon>(); //for storing used coupons
public int Capacity
{
get { return _capacity; }
set { _capacity = value; }
}
private List<Iproduct> _items = new List<Iproduct>();

public List<Iproduct> Items
{
get { return _items; }
set { _items = value; }
}

public HashSet<Coupon> GetCoupons()
{
return coupons;
}

public void AddCoupon(Coupon coupon)
{
coupons.Add(coupon);
}
public void AddBagel(Iproduct item)
{
if(!this.IsFull())
{
Items.Add(item);
}
}
public void RemoveBagel(Iproduct item)
{
if (this.ItemNotPresent(item))
{
throw new ArgumentException("Parameter cannot be null", nameof(item)); //throws exception so the user can know item is not present
}
Items.Remove(item);
}
public bool IsFull()
{
return Items.Count >= Capacity;
}
public bool ItemNotPresent(Iproduct item)
{
return !Items.Any(x => x == item);
}
public float GetTotalCost()
{
float totalCost = 0F;
Items.ForEach(x => totalCost += x.GetPrice());
return totalCost;
}

//this function returns total cost of items in basket including discounts
public float Discount()
{
float total = 0;

List<Coupon> CouponList = coupons.ToList(); //list of all coupoons added to basket
List<Iproduct> itemCopy = new List<Iproduct>(Items); // Create a copy of the Items list, removes all discounted items and calculates normal cost pluss discount
List<string> BasketSKUs = new List<string>();
List<string> CouponSKUs = new List<string>();
bool allItemsPresent = true;
//adding all sku to coupon and basket lists
Items.ForEach(x => BasketSKUs.Add(x.GetSKU()));
CouponList.ForEach(item => item.items.ForEach(x => CouponSKUs.Add(x.GetSKU())));//im sorry about this

//loop through all coupons and if all items in coupon is present in basket, remove them and from basket item copy and add discounts
foreach (Coupon c in CouponList.ToList())
{
//Console.WriteLine("dsfdfdsUHFUISFDUFSGDJIFSDGFGSDOIJFGDSJIOFGDIOSJFIOGSIJFDSGIJFSDG");
foreach (Iproduct item in c.items)
{
if(BasketSKUs.Contains(item.GetSKU()))
{
BasketSKUs.Remove(item.GetSKU());

}
else
{

allItemsPresent = false;
break;
}
}
if (allItemsPresent)
{
foreach (Iproduct i in c.items)
{
for (int j = 0; j < itemCopy.Count; j++)
{
if (itemCopy[j].GetSKU() == i.GetSKU())
{
itemCopy.RemoveAt(j);
break;
}
}
}
total += c.discount;
usedCoupons.Add(c);//adding to used copons so we can see it in the receipt later
CouponList.Remove(c); // Removing from the copy of the coupons list

}
}
//adding on the rest of the non discount cost
itemCopy.ForEach(x => total += x.GetPrice());
return total;
}
public List<Coupon> GetAppliedDiscounts()
{
return usedCoupons;
}
//extesnion 1 is a brute force receipt without discounts, as far as i understood
public void PrintReceipt()
{
//first count items of same type in basket
Dictionary<string, int> dict = new Dictionary<string, int>();//SKU is key and int is amount of the item in the basket
HashSet<String> printed = new HashSet<String>(); //keeping track of whiich items are printed
float total = 0;
Items.ForEach(x => total += x.GetPrice());
Items.ForEach(x =>
{
if (!dict.ContainsKey(x.GetSKU()))
{
dict.Add(x.GetSKU(), 1);
}
else
{
dict[x.GetSKU()] ++;
}
});

Console.WriteLine(" ~~~Bob's Bagels ~~~ ");
Console.WriteLine($" {DateTime.Now.ToString()} ");
Console.WriteLine("---------------------------");
foreach(Iproduct x in Items)
{
if(!printed.Contains(x.GetSKU()))
{
Console.WriteLine($"{x.GetVariant()}{x.GetName()} {dict[x.GetSKU()]} ${x.GetPrice()* dict[x.GetSKU()]}");
printed.Add(x.GetSKU());
}

}
Console.WriteLine("---------------------------");
Console.WriteLine($"Total ${GetTotalCost()}");
Console.WriteLine("Thank you for your order!");
}
public void PrintReceiptWithDiscounts()
{
//first count items of same type in basket
Dictionary<string, int> dict = new Dictionary<string, int>();//SKU is key and int is amount of the item in the basket
HashSet<String> printed = new HashSet<String>(); //keeping track of whiich items are printed
Items.ForEach(x =>
{
if (!dict.ContainsKey(x.GetSKU()))
{
dict.Add(x.GetSKU(), 1);
}
else
{
dict[x.GetSKU()]++;
}
});
List<float> discounts = new List<float>();
this.GetAppliedDiscounts().ForEach(x => discounts.Add(x.discount));
float total = 0;
Items.ForEach(x => total += x.GetPrice() );

Console.WriteLine(" ~~~Bob's Bagels ~~~ ");
Console.WriteLine($" {DateTime.Now.ToString()} ");
Console.WriteLine("---------------------------");
foreach (Iproduct x in Items)
{
if (!printed.Contains(x.GetSKU()))
{
Console.WriteLine($"{x.GetVariant()}{x.GetName()} {dict[x.GetSKU()]} ${x.GetPrice()* dict[x.GetSKU()]}");
Console.WriteLine($"Discounts: -{total - Discount()}");
printed.Add(x.GetSKU());
}

}
Console.WriteLine("---------------------------");
Console.WriteLine($"Total ${Discount()}");
Console.WriteLine("Thank you for your order!");
}
}
}
2 changes: 2 additions & 0 deletions exercise.main/ClassDiagram1.cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram />
55 changes: 55 additions & 0 deletions exercise.main/Coffee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Coffee : Iproduct
{

public string Name { get; set; }
public string SKU { get; set; }
public float Price { get; set; }
public string Variant { get; set; }

public Coffee(string name, string sKU, float price, string variant)
{
Name = name;
SKU = sKU;
Price = price;
Variant = variant;
}

public float GetPrice()
{
return this.Price;
}

public void AddFilling(Iproduct filling)
{
throw new NotImplementedException();
}

public List<Iproduct> GetFillings()
{
throw new NotImplementedException();
}

public string GetName()
{
return this.Name;
}

public string GetVariant()
{
return this.Variant;
}

public string GetSKU()
{
return this.SKU;
}
}
}
25 changes: 25 additions & 0 deletions exercise.main/Coupon.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.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Coupon
{
/*
Discounts is stored as X amount of items in the discounts list.
If a basket contains all the items in the discounts list, the discount is applied.
*/
public List<Iproduct> items { get; set; }
public float discount { get; set; }
public Coupon(List<Iproduct> discountItems, float price)
{
items = discountItems;
discount = price;
}

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

namespace exercise.main
{
public class Customer : Iperson
{
public Basket Basket { get; set; }
private string _type;
public string Type
{
get { return _type; }
set { _type = value; }
}
public Customer()
{
Basket = new Basket();
Type = "Customer";
}

public Basket GetBasket()
{
return Basket;
}
public void ChangeBasketCapacity(int capacity)
{
return; //Not authorized to make changes
}


}
}
Loading