public ReceiptAttempt ProcessPurchaseAttempt (string username, string password, Item clientDescriptionOfItem) {
			return ReceiptAttempt.Make(() => {
				Global.DoWrappingExceptions<GildedRoseClientComplaintException>(() => {
					username = username.NullHandlingNormalize();
					password = password.NullHandlingNormalize();
					clientDescriptionOfItem = clientDescriptionOfItem.WithNormalizedStrings();
					Assert.NotNull(username);
					Assert.NotNull(password);
					clientDescriptionOfItem.AssertNotNullAndInvariantsIfAny();
				}, "Invalid data.");
				lock (model) {//todo find a way to not do this.
					var customer = model.AuthenticateOrThrow(username, password);
					return model.ConductSaleOrThrow(customer, clientDescriptionOfItem);
				}
			});
		}
		public Receipt ConductSaleOrThrow (Customer customer, Item clientDescriptionOfItem) {
			var actualItem = ItemMatching(clientDescriptionOfItem);
			if (actualItem == null) Global.ThrowComplaint("Item not carried.", true);
			if (!customer.CanAfford(actualItem)) Global.ThrowComplaint("Insufficient money.", true);
			return ConductSale(customer, actualItem);
		}
		///<summary>Precondition: sale is valid.</summary>
		public Receipt ConductSale (Customer customer, Item item) {
			customer.MoneyInWallet -= item.Price;
			MoneyInRegister += item.Price;
			inventory.Remove(item);
			return new Receipt(customer, item);
		}
		public Item ItemMatching (Item description) {return inventory.ItemMatching(description);}
		public void Remove (Item item) {items.Remove(item);}
		public Item ItemMatching (Item description) {return items.FirstOrDefault(i => i.InterchangeableWith(description));}
Beispiel #7
0
		uint price = 10;//Invariant: positive.
		public bool InterchangeableWith (Item other) {return name == other.name && description == other.description && price == other.price;}
		public bool CanAfford (Item item) {return item.Price <= MoneyInWallet;}
			[Theory] public static void Invariants (Item item) {
				Assume.That(item != null);
				// ReSharper disable once PossibleNullReferenceException
				item.AssertInvariants();
			}