0% found this document useful (0 votes)
10 views

Java

The document describes a Java project that uses multithreading to simulate a smart grocery manager, with one thread continuously producing random grocery items into a shared queue and another thread allowing a user to purchase available items and view prices, demonstrating an interactive producer-consumer problem solution. The code includes Producer and Consumer classes that implement the Runnable interface and use a blocking queue to share grocery item strings between the threads.

Uploaded by

CHANDU S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java

The document describes a Java project that uses multithreading to simulate a smart grocery manager, with one thread continuously producing random grocery items into a shared queue and another thread allowing a user to purchase available items and view prices, demonstrating an interactive producer-consumer problem solution. The code includes Producer and Consumer classes that implement the Runnable interface and use a blocking queue to share grocery item strings between the threads.

Uploaded by

CHANDU S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Project Using Multithreading :

Project Name : Smart Grocery Manager.


Code :

package Java;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.List;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

class Producer implements Runnable {


private BlockingQueue<String> queue;
private List<String> groceries = Arrays.asList(
"Milk", "Bread", "Eggs", "Rice", "Pasta", "Fresh fruits (e.g., apples,
bananas, oranges)",
"Fresh vegetables (e.g., lettuce, tomatoes, carrots)", "Chicken", "Beef",
"Fish", "Cheese",
"Yogurt", "Butter", "Cereal", "Potatoes", "Onions", "Garlic", "Olive oil",
"Canned beans",
"Frozen vegetables"
);
Map<String, Double> prices;

public Producer(BlockingQueue<String> queue) {


this.queue = queue;
this.prices = new HashMap<>();
prices.put("Milk", 60.0);
prices.put("Bread", 30.0);
prices.put("Eggs", 40.0);
prices.put("Rice", 50.0);
prices.put("Pasta", 45.0);
prices.put("Fresh fruits (e.g., apples, bananas, oranges)", 70.0);
prices.put("Fresh vegetables (e.g., lettuce, tomatoes, carrots)", 55.0);
prices.put("Chicken", 120.0);
prices.put("Beef", 150.0);
prices.put("Fish", 100.0);
prices.put("Cheese", 90.0);
prices.put("Yogurt", 35.0);
prices.put("Butter", 50.0);
prices.put("Cereal", 65.0);
prices.put("Potatoes", 40.0);
prices.put("Onions", 30.0);
prices.put("Garlic", 25.0);
prices.put("Olive oil", 80.0);
prices.put("Canned beans", 55.0);
prices.put("Frozen vegetables", 60.0);
}
@Override
public void run() {
try {
Random rand = new Random();
while (!Thread.currentThread().isInterrupted()) {
String grocery = groceries.get(rand.nextInt(groceries.size()));
queue.put(grocery);
System.out.printf("%-60sProduced: %s%n", "", grocery);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

class Consumer implements Runnable {


private BlockingQueue<String> queue;
private Map<String, Double> prices;

public Consumer(BlockingQueue<String> queue, Map<String, Double> prices) {


this.queue = queue;
this.prices = prices;
}
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
try {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Enter the name of the grocery to buy (or 'q' to quit):
");
System.out.print("> ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("q")) {
break;
}
if (queue.contains(input)) {
System.out.println("The grocery '" + input + "' is available.");
Double price = prices.getOrDefault(input, 0.0);
System.out.println("Price of '" + input + "': Rs " + price);
System.out.println("Processing purchase...");
System.out.println("Purchase of '" + input + "' is complete.");
} else {
System.out.println("Sorry, the grocery '" + input + "' is not
available.");
}
}
} finally {
scanner.close();
}
}
}
public class Main {
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);

Producer producer = new Producer(queue);


Consumer consumer = new Consumer(queue, producer.prices);

Thread producerThread = new Thread(producer);


Thread consumerThread = new Thread(consumer);

producerThread.start();

consumerThread.start();

try {

consumerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

producerThread.interrupt();
}
}

Demonstration:
1. Upon running the program, the "Smart Grocery Manager" interface is displayed in
the terminal.
2. Groceries are continuously produced in the background and shown on the right
side of the terminal.
3. The user is prompted to enter the name of the grocery item they want to purchase
or to enter 'q' to quit.
4. The program checks if the entered item is available in the list of groceries.
5. If the item is available, the program displays its price in rupees and simulates the
purchase process.
6. The purchased item is removed from the list of available groceries.
7. Steps 3-6 are repeated until the user decides to quit by entering 'q'
Out put :

You might also like