do I edit the code to correctly display the number of available burgers and the waiting customers? import java.util.Scanner; import java.util.Random; class Exam implements Runnable { private String cName = new String(""); public Exam (String cName, int x) { this.cName = cName; this.x = x; } static int x; @Override public void run() {
How do I edit the code to correctly display the number of available burgers and the waiting customers?
import java.util.Scanner;
import java.util.Random;
class Exam implements Runnable {
private String cName = new String("");
public Exam (String cName, int x) {
this.cName = cName;
this.x = x;
}
static int x;
@Override
public void run() {
Random r = new Random();
int randNum, j=0, avail=0;
for(int i = 1; i <= this.x; i++) {
randNum = (int) Math.floor(Math.random() * (this.x - 1 + 1) + 1);
if (cName == "Consumer #1" || cName == "Consumer #2"){
avail = i - 1;
System.out.println(cName + " of Thread #" + i + " buys 1 burger # of available burgers [" + avail + "] Customer(s) waiting [" + j++ + "]");
}else {
int num = this.x * 2;
avail = i - 1;
for(i = 1; i <= num; i++){
System.out.println(cName + " is cooking burger #" + i + "\t\t\t # of available burgers [" + i + "] Customer(s) waiting [" + j + "]");
}
}
}
}
}
public class MidtermExam{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("# of burgers to cook per consumer: ");
int input = sc.nextInt();
System.out.println();
Exam r1 = new Exam("Producer",input);
Thread Producer = new Thread(r1);
Producer.start();
Exam r2 = new Exam("Consumer #1",input);
Thread Consumer1 = new Thread(r2);
Consumer1.start();
Exam r3 = new Exam("Consumer #2",input);
Thread Consumer2 = new Thread(r3);
Consumer2.start();
}
}
Step by step
Solved in 2 steps