This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.

Al Do

Greenhorn
+ Follow
since Oct 13, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Al Do

I am having a problem with a simple program that calculates the number of days between two dates input. It keeps returning zero and I have no idea why I have tried many different attempts with the daysBetween method.




If any one could shed some light it would be much appreciated.
12 years ago
Sorry there was some stuff linked to other parts of the program.
Here is the main



Here is the calendar:



It now compiles, sorry about that.
12 years ago
I have attached a calendar program currently being used in a bigger program. However there is a problem with saving on the component picked. Currently it changes the background colour when the button is clicked but when another square is clicked on then the background colour changes back to normal is there anyway to keep the background colour when another square is clicked on.



12 years ago
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.