Advertisement
Guest User

Untitled

a guest
Feb 14th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.89 KB | None | 0 0
  1. type Item (name : string, price : float) =
  2.     member this.Price =
  3.         price;
  4.     member this.Name =
  5.         name;
  6.  
  7. type Customer (shoppingList : list<Item>, inBasket : list<Item>) =
  8.     member this.ShoppingList =
  9.         shoppingList;
  10.     member this.Shop =
  11.         this.AddItem(shoppingList.Head);
  12.     member this.AddItem(item : Item) =
  13.         new Customer(List.filter (fun x -> x <> item) shoppingList, List.append inBasket [item]);
  14.     member this.Done =
  15.         shoppingList.IsEmpty
  16.  
  17. type Checkout (customers : list<Customer>) =
  18.     member this.AddCustomer(customer : Customer) =
  19.         Checkout(List.append customers [customer]);
  20.     member this.Tick =
  21.         // todo - remove items from customer basket, remove customer when empty
  22.         Checkout(customers);
  23.  
  24. type Supermarket (checkouts : list<Checkout>, customers : list<Customer>) =
  25.     member this.Tick =
  26.         let tickedCustomers = List.map (fun (x:Customer) -> x.Shop) customers;
  27.         let tickedCheckouts = List.map (fun (x:Checkout) -> x.Tick) checkouts;
  28.         let doneCustomers = List.filter (fun (x:Customer) -> x.Done) tickedCustomers;
  29.         let notDoneCustomers = List.filter (fun (x:Customer) -> not x.Done) tickedCustomers;
  30.         let filledCheckouts = this.SendCustomersToCheckouts(doneCustomers, tickedCheckouts);
  31.         Supermarket(filledCheckouts, notDoneCustomers);
  32.  
  33.     member this.SendCustomersToCheckouts (doneCustomers : list<Customer>, readyCheckouts : list<Checkout>) =
  34.         // todo - add customers to checkouts
  35.         readyCheckouts
  36.  
  37. let rec simulate (supermarket : Supermarket) =
  38.     simulate supermarket.Tick;
  39.  
  40. let singleCustomerShoppingList = [Item("Fishycakes", 5.0); Item("Nommycakes", 7.0)];
  41. let singleCustomer = Customer(singleCustomerShoppingList, []);
  42. let singleCheckout = Checkout([]);
  43. let supermarket = Supermarket([singleCheckout], [singleCustomer]);
  44. simulate(supermarket);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement