Click here to watch in Youtube : https://www.youtube.com/watch?v=VIzWKPRG9lo
Click the below Image to Enlarge
State Design pattern - Implementation [Vending Machine] |
State Design pattern - Implementation [Vending Machine] - Class Diagram |
VendingMachineState.java
public interface VendingMachineState
{
public void selectProductAndInsertMoney(int amount,String productName);
public void dispenseProduct();
}
HasMoneyState.java
{
@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
System.out.println("Already Vending machine has money and product selected,So wait till it finish the current dispensing process ... ");
}
@Override
public void dispenseProduct()
{
System.out.println("Vending Machine dispensed the product ...");
}
}
NoMoneyState.java
public class NoMoneyState implements VendingMachineState
{
@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
System.out.println(amount + "Rs has been inserted and " + productName + " has been selected...");
}
@Override
public void dispenseProduct()
{
System.out.println("Vending Machine cannot dispense product because money is not inserted and product is not selected...");
}
}
VendingMachine.java
// This is like Context
public class VendingMachine implements VendingMachineState
{
private VendingMachineState vendingMachineState;
public VendingMachine()
{
vendingMachineState = new NoMoneyState();
}
public VendingMachineState getVendingMachineState()
{
return vendingMachineState;
}
public void setVendingMachineState( VendingMachineState vendingMachineState )
{
this.vendingMachineState = vendingMachineState;
}
@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
vendingMachineState.selectProductAndInsertMoney(amount, productName);
VendingMachineState hasMoneyState = new HasMoneyState();
/*
* Money has been inserted so vending Machine changed the
* internal state to 'hasMoneyState'
*/
if( vendingMachineState instanceof NoMoneyState )
{
setVendingMachineState(hasMoneyState);
System.out.println("VendingMachine internal state has been moved to : "
+ vendingMachineState.getClass().getName());
}
}
@Override
public void dispenseProduct()
{
VendingMachineState noMoenyState = new NoMoneyState();
vendingMachineState.dispenseProduct();
/*
* Product has been dispensed so vending Machine changed the
* internal state to 'NoMoneyState'
*/
if( vendingMachineState instanceof HasMoneyState )
{
setVendingMachineState(noMoenyState);
System.out.println("VendingMachine internal state has been moved to : "
+ vendingMachineState.getClass().getName());
}
}
}
Client.java
public class Client
{
public static void main( String[] args )
{
/*
* Initially Vending Machine will be 'noMoneyState'
*/
VendingMachine vendingMachine = new VendingMachine();
System.out.println("Current VendingMachine State : "
+ vendingMachine.getVendingMachineState().getClass().getName()+"\n");
vendingMachine.dispenseProduct();
vendingMachine.selectProductAndInsertMoney(100, "Pepsi");
/*
* Money has been inserted so vending Machine changed the
* internal state to 'hasMoneyState'
*/
System.out.println("\nCurrent VendingMachine State : "
+ vendingMachine.getVendingMachineState().getClass().getName()+"\n");
vendingMachine.selectProductAndInsertMoney(100, "Fanta");
vendingMachine.dispenseProduct();
/*
* Product has been dispensed so vending Machine changed the
* internal state to 'NoMoneyState'
*/
System.out.println("\nCurrent VendingMachine State : "
+ vendingMachine.getVendingMachineState().getClass().getName());
}
}
Output
Current VendingMachine State : NoMoneyState
Vending Machine cannot dispense product because money is not inserted and product is not selected...
100Rs has been inserted and Pepsi has been selected...
VendingMachine internal state has been moved to : HasMoneyState
Current VendingMachine State : HasMoneyState
Already Vending machine has money and product selected,So wait till it finish the current dispensing process ...
Vending Machine dispensed the product ...
VendingMachine internal state has been moved to : NoMoneyState
Current VendingMachine State : NoMoneyState
ReplyDeleteGreat, Thank you for sharing with us. Very nice article & have great information.
candy vending machines
I Like to add one more important thing here, The Vending Machine Market is expected to be around US$ 68.65 Billion by 2027 at a CAGR of 9.96% in the given forecast period.
ReplyDeleteNice Post…. Thank you for sharing this information….
ReplyDeleteGeorgia offers a wide range of Vending Machine in Gurgaon. We offer all the vending machines at very reasonable prices, and our brands are known for their quality and durability. Whether you need one machine or several, Georgia will be able to meet your needs and help you get the best possible price.
Hot and Cold Water Dispenser
Coffee Vending Machine in Gurgaon
Tea Coffee Premix Supplier in Gurgaon
Nice Post…. Thank you for sharing this information….
ReplyDeleteGeorgia offers a wide range of Vending Machine in Gurgaon. We offer all the vending machines at very reasonable prices, and our brands are known for their quality and durability. Whether you need one machine or several, Georgia will be able to meet your needs and help you get the best possible price.
Hot and Cold Water Dispenser
Coffee Vending Machine in Gurgaon
Tea Coffee Premix Supplier in Gurgaon
ReplyDeleteVerfügbare Snackautomaten Germany
Verfügbare Snackautomaten
https://automatenkaufen.de/getranke-snackautomat/
ReplyDeleteYou are probably in the market looking for a good vending machine to buy. We have a wide variety of these machines and will do well to provide you nothing but the best. Our machines have a warranty period of 1 year so you don’t have to be worried if your machines develop any complications. We will repair or replace the machine without any extra costs.
Cigarette Vending Machine for Sale
Buy Vending Machine UK
Cigarette Vending Machine for Sale
Coffee Vending Machine For Sale
Cigarette Vending Machine for Sale
Coffee Vending Machine For Sale
Nice Post. Thank You for sharing this information.
ReplyDeleteGeorgia's Tea and Coffee Machine brings cafe-quality beverages to your home or workplace. With user-friendly controls and a variety of options, enjoy the perfect cup of tea or coffee at your convenience. Elevate your beverage experience with Georgia's reliable and stylish machine.
Hot and Cold Water Dispenser
Coffee Vending Machine in Gurgaon
Cold Coffee Ice Vending Machine in Gurgaon
Lipton & Nescafe Vending Machine in Gurgaon
Water Dispenser Hot & Cold in Gurgaon
Vending Machine in Gurgaon
Tea and Coffee Dispenser