• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment Help

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question - How do I get the producer to produce items with four different items? Below is the problem:

The assignment deals with the problem of restaurants buying food on markets. There are three restaurants which
specialise in a particular kind of food, i.e. there is one fish restaurant, one vegetarian restaurant and one meat
restaurant. These three restaurants have their main ingredient (fish, vegetable, meat) in sufficient supply. However, to
create a meal they need two more food items. These are offered by a supplier on a market. The supplier has a range
of food items available in sufficient quantities. These are: bottles of wine, vegetables, meat starters, and fish starters.
Not all ingredients are suitable for every restaurant: the fish restaurant does not require fish starters (they have
sufficient fish anyway). Similarly, the meat restaurant does not require meat starters. The vegetarian restaurant does
not require meat or vegetables starters (but can use fish). Wine is suitable for all three restaurants. The restaurants will
buy two items suitable for them. Afterwards, a meal will be cooked and be served to customers. Only afterwards, the
restaurant will get back to the market. The restaurants are very concerned about fresh ingredients!
You are asked to design a computer program which models this behaviour. For simplicity we assume that money is not
an issue and the financial transactions are dealt with in a separate system. The market stall used by the supplier is to
be modelled as a blocking bounded buffer (there is limited space on the market stall). The supplier will input goods into
the buffer (put the goods on the market stall) and the restaurants will remove items from the buffer (pick up goods from
the stall). As said above, restaurants require two items from the stall to be able to cook a meal. Clearly, restaurants can
turn up in front of the market stall at the same time (concurrently).
After every meal served the restaurants need to keep track of how many meals they served, and how many goods they
bought from the market. They store this information into a central database (all restaurants use the same database!).
You should model this database as a file. The file should contain the following structure and information:
Field Contents
1 Total number of meals provided
2 Number of meals served in the fish restaurant
3 Number of meals served in the meat restaurant
4 Number of meals served in the vegetarian restaurant
5 Number of fish starter portions bought
6 Number of meat starter portions bought
7 Number of vegetable portions bought
8 Number of bottles of wine bought
Additionally, the program should provide a log on the screen.



Here is the current code -
the producer class

public class Supplier extends Thread{

String[] items = {"wine","fish", "meat", "veg"};
public Supplier(MarketStall m){
mStall = m; }

public void run(){
while(true) {
Item message = new Item();
try{
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
mStall.send(message);

}
}
private MarketStall mStall;
}


The consumer
public class Restaurant<Item> extends Thread {

RandomAccessFile file;

public Restaurant(MarketStall m){
mStall = m;
}

public void run(){
while(true){
Item message = (Item)mStall.receive();
if(message != null) {
try{
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println("Item Purchased");

}
}
private MarketStall mStall;
}

The buffer

public class MarketStall <Item> {

private Vector<Item> marketStallQueue;

static int maxSize = 20;



public MarketStall(){
marketStallQueue = new Vector<Item>();

}

public synchronized void send(Item message) {
if(marketStallQueue.size()== maxSize){ //Makes sure there is enough room on the stall to put the item on the stall.
System.out.println("No more room on stall");
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
else {
notify();
marketStallQueue.add(message);
System.out.println("Item added to the market stall, ready for purchase.");
}
}

public synchronized Item receive() {
if(marketStallQueue.isEmpty()){ //Makes sure there is an item to purchase, if not then wait until an item is made available.
System.out.println("Market Stall is empty currently");
try{
wait();
} catch(InterruptedException e){
System.out.println("InterruptedException caught");
}
}
else{
notify();
return marketStallQueue.remove(0);
}
return null;
}

}

The main class

public class Synchronizer extends Thread{

public Synchronizer(RandomAccessFile file) throws FileNotFoundException {
file = new RandomAccessFile("file.txt" , "rw");

MarketStall mailbox = new MarketStall();
Supplier producerThread = new Supplier(mailbox);
Restaurant consumerMeatThread = new Restaurant(mailbox);
Restaurant consumerFishThread = new Restaurant(mailbox);
Restaurant consumerVegThread = new Restaurant(mailbox);
producerThread.start();
consumerMeatThread.start();
consumerFishThread.start();
consumerVegThread.start();
}

public static void main (String args[]) throws FileNotFoundException{
Synchronizer synchronizer = new Synchronizer(null);

}
}


public class Item {

String[] items = {"wine","fish", "meat", "veg"};
}

Currently it produces an item and consumes an item but I need the program to produce different items for the different consumers.
Please help.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags in your post.

What do you think is going on here?

Al Do wrote:Item message = new Item();



Al Do wrote:public class Item {

String[] items = {"wine","fish", "meat", "veg"};
}



How are 2 instances of class Item are different from each other? More specifically, how is their state different from each other.
reply
    Bookmark Topic Watch Topic
  • New Topic