Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Item (name : string, price : float) =
- member this.Price =
- price;
- member this.Name =
- name;
- type Customer (shoppingList : list<Item>, inBasket : list<Item>) =
- member this.ShoppingList =
- shoppingList;
- member this.InBasket =
- inBasket;
- member this.Shop =
- this.AddItem(shoppingList.Head);
- member this.AddItem(item : Item) =
- Customer(List.filter (fun (x:Item) -> x <> item) shoppingList, List.append inBasket [item]);
- member this.BagItem(item : Item) =
- Customer(shoppingList, List.filter (fun (x:Item) -> x <> item) inBasket);
- member this.Done =
- shoppingList.IsEmpty
- type Checkout (customers : list<Customer>) =
- member this.AddCustomer(customer : Customer) =
- Checkout(List.append customers [customer]);
- member this.Tick =
- if customers.IsEmpty then
- this
- else
- let currentCustomer = customers.Head;
- if currentCustomer.InBasket.IsEmpty then
- Checkout(List.filter (fun (x:Customer) -> x <> currentCustomer) customers);
- else
- let baggedCustomer = currentCustomer.BagItem currentCustomer.InBasket.Head;
- Checkout(List.append [baggedCustomer] (List.filter (fun (x:Customer) -> x <> currentCustomer) customers));
- type Supermarket (checkouts : list<Checkout>, customers : list<Customer>) =
- member this.Tick =
- let tickedCustomers = List.map (fun (x:Customer) -> x.Shop) customers;
- let tickedCheckouts = List.map (fun (x:Checkout) -> x.Tick) checkouts;
- let doneCustomers = List.filter (fun (x:Customer) -> x.Done) tickedCustomers;
- let notDoneCustomers = List.filter (fun (x:Customer) -> not x.Done) tickedCustomers;
- let filledCheckouts = this.SendCustomersToCheckouts(doneCustomers, tickedCheckouts);
- Supermarket(filledCheckouts, notDoneCustomers);
- member this.SendCustomersToCheckouts (doneCustomers : list<Customer>, readyCheckouts : list<Checkout>) =
- let rec addHeadCustomer(customers : list<Customer>, checkouts : list<Checkout>) =
- if customers.IsEmpty then
- checkouts
- else
- let updatedCheckout = checkouts.Head.AddCustomer customers.Head;
- let updatedCheckoutList =
- List.append (List.filter (fun (x:Checkout) -> x <> checkouts.Head) checkouts) [updatedCheckout];
- addHeadCustomer(List.filter (fun (x:Customer) -> x <> customers.Head) customers, updatedCheckoutList);
- addHeadCustomer(doneCustomers, readyCheckouts)
- let rec simulate (supermarket : Supermarket) =
- simulate supermarket.Tick;
- let singleCustomerShoppingList = [Item("Fishycakes", 5.0); Item("Nommycakes", 7.0)];
- let singleCustomer = Customer(singleCustomerShoppingList, []);
- let singleCheckout = Checkout([]);
- let supermarket = Supermarket([singleCheckout], [singleCustomer]);
- simulate(supermarket);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement