Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- class HasCoinState : State
- {
- public HasCoinState(VendingMachine Machine) : base(Machine)
- {
- }
- public override bool BuyProduct()
- {
- Console.WriteLine("You can retreive the product.");
- this.Machine.SetMachineState(Machine.SoldState);
- return true;
- }
- public override bool Dispense()
- {
- Console.WriteLine("You ");
- return false;
- }
- public override bool EjectCoin()
- {
- Console.WriteLine("You can retreive your coin.");
- this.Machine.SetMachineState(Machine.NoCoinState);
- return true;
- }
- public override bool InsertCoin()
- {
- Console.WriteLine("You do not has posibility to insert coin, becouse is already inserted.");
- return false;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- class NoCoinState : State {
- public NoCoinState(VendingMachine Machine) : base(Machine)
- {
- }
- public override bool BuyProduct()
- {
- Console.WriteLine("Please insert coin!");
- return false;
- }
- public override bool Dispense()
- {
- Console.WriteLine("Please insert coin and chose a product!");
- return false;
- }
- public override bool EjectCoin()
- {
- Console.WriteLine("You do not has coin to eject!");
- return false;
- }
- public override bool InsertCoin()
- {
- Console.WriteLine("Coins has been inserted sucsessfully!");
- this.Machine.SetMachineState(Machine.HasCoinState);
- return true;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- public enum EUserOption
- {
- InsertCoin,
- EjectCoin,
- BuyProduct,
- InspectMachine,
- FillMachine,
- Exit
- }
- class Program
- {
- static void Main(string[] args)
- {
- VendingMachine _vendingMachine = new VendingMachine();
- Console.WriteLine(" 1.Insert coin\n" +
- " 2.Eject coin\n" +
- " 3.Buy product\n" +
- " 4.Refill\n" +
- " 5.Inspect machine\n" +
- " 6.Exit\n");
- while (true)
- {
- switch (Console.ReadKey(true).KeyChar)
- {
- case '1':
- _vendingMachine.UpdateState(EUserOption.InsertCoin);
- break;
- case '2':
- _vendingMachine.UpdateState(EUserOption.EjectCoin);
- break;
- case '3':
- _vendingMachine.UpdateState(EUserOption.BuyProduct);
- break;
- case '4':
- _vendingMachine.UpdateState(EUserOption.FillMachine);
- break;
- case '5':
- _vendingMachine.UpdateState(EUserOption.InspectMachine);
- break;
- case '6':
- _vendingMachine.UpdateState(EUserOption.Exit);
- break;
- }
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- class SoldOutState : State
- {
- public SoldOutState(VendingMachine Machine) : base(Machine)
- {
- }
- public override bool BuyProduct()
- {
- Console.WriteLine("Sold out!");
- return false;
- }
- public override bool Dispense()
- {
- Console.WriteLine("Sold out!");
- return false;
- }
- public override bool EjectCoin()
- {
- Console.WriteLine("You do not has coins inserted.");
- return false;
- }
- public override bool InsertCoin()
- {
- Console.WriteLine("You do not has posibility to insert coin, becouse sold is out.");
- return false;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- class SoldState : State
- {
- public SoldState(VendingMachine Machine) : base(Machine)
- {
- }
- public override bool BuyProduct()
- {
- Console.WriteLine("Please insert coins!");
- return false;
- }
- public override bool Dispense()
- {
- Console.WriteLine("Please insert coins!");
- return false;
- }
- public override bool EjectCoin()
- {
- Console.WriteLine("You do not has coin to eject!");
- return false;
- }
- public override bool InsertCoin()
- {
- Console.WriteLine("Coins has been inserted sucsessfully!");
- this.Machine.SetMachineState(Machine.HasCoinState);
- return true;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- abstract class State
- {
- public VendingMachine Machine { get; set; }
- public abstract bool InsertCoin();
- public abstract bool EjectCoin();
- public abstract bool BuyProduct();
- public abstract bool Dispense();
- public State(VendingMachine machine)
- {
- this.Machine = machine;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LabSabloan7._2State
- {
- class VendingMachine
- {
- private int capacity;
- private State maschineState;
- public State NoCoinState;
- public State SoldOutState;
- public State SoldState;
- public State HasCoinState;
- public VendingMachine()
- {
- NoCoinState = new NoCoinState(this);
- SoldOutState = new SoldOutState(this);
- SoldState = new SoldState(this);
- HasCoinState = new HasCoinState(this);
- this.capacity = 10;
- this.maschineState = NoCoinState;
- }
- public void UpdateState(EUserOption option)
- {
- switch (option)
- {
- case EUserOption.InsertCoin:
- InsertCoin();
- break;
- case EUserOption.EjectCoin:
- EjectCoin();
- break;
- case EUserOption.BuyProduct:
- BuyProduct();
- break;
- case EUserOption.FillMachine:
- ReFill();
- break;
- case EUserOption.InspectMachine:
- Inspect();
- break;
- case EUserOption.Exit:
- maschineState = NoCoinState;
- break;
- }
- }
- private void ReFill()
- {
- this.capacity = 10;
- }
- private bool InsertCoin()
- {
- return maschineState.InsertCoin();
- }
- private bool EjectCoin()
- {
- return maschineState.EjectCoin();
- }
- private bool BuyProduct()
- {
- if (maschineState.BuyProduct())
- {
- capacity--;
- if (IsEmpty())
- {
- maschineState = SoldOutState;
- }
- return true;
- }
- return false;
- }
- private void Inspect()
- {
- Console.WriteLine("The capacity is:"+capacity);
- }
- public bool IsEmpty()
- {
- if (capacity == 0){
- return true;
- }
- return false;
- }
- public void SetMachineState(State state)
- {
- maschineState = state;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement