Write a java application using the concepts of multithreading for a flower
shop where flowers are delivered based on order.
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 num;
private String 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: ");
num=input.nextInt();
System.out.println("Enter the destination address: ");
address=input.next();
}
public void deliver(){
if (stock<=0 || num>stock){
System.out.println("There are no enough flowers to deliver!");
}
else{
stock-=num;
System.out.println(num+" has been delived to "+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