Write a python application using the concepts of multithreading for a flower
shop where flowers are delivered based on order using Synchronization.
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Flower thread1=new Flower();
Flower thread2=new Flower();
thread1.start();
//thread2.start();
}
}
class Flower extends Thread
{
private int no_of_flowers;
private String destination_address;
private int stock;
public void getData(){
Scanner input=new Scanner(System.in);
System.out.println("Enter the number of flowers remaining in the stock: ");
stock=input.nextInt();
System.out.println("Enter the number of flowers to deliver: ");
no_of_flowers=input.nextInt();
System.out.println("Enter the destination address: ");
destination_address=input.next();
}
public void deliver(){
if (stock<=0 || no_of_flowers>stock){
System.out.println("There are no enough flowers to deliver!");
}
else{
stock-=no_of_flowers;
System.out.println(no_of_flowers+" has been delived to "+destination_address+"\nRemaining number of flowers in the stockis "+stock);
}
}
public void run()
{
try
{
Thread.sleep(500);
}catch(InterruptedException e){System.out.println(e);}
getData();
deliver();
}
}
Comments
Leave a comment