Question: (Java pls) Can someone pls help me fix my code i keep geting errors in it. import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Queue; import

(Java pls) Can someone pls help me fix my code i keep geting errors in it.

import java.util.Date;

import java.util.LinkedList;

import java.util.List;

import java.util.Queue;

import java.util.Scanner;

import java.util.concurrent.TimeUnit;

public class SleepingBarberClass {

public static void main(String a[]) {

BarberShop shop = new BarberShop();

Barber barber = new Barber(shop);

Queue cashier = new Queue ();

Queue Couch = new Queue ();

Queue line = new Queue ();

TheCustomerGenerator cg = new TheCustomerGenerator(shop);

Thread thbarber = new Thread(barber);

Thread thcg = new Thread(cg);

thcg.start();

thbarber.start();

}

}

class Barber implements Runnable {

BarberShop shop;

public Barber(BarberShop shop) {

this.shop = shop;

}

public void run() {

try {

Thread.sleep(10000);

} catch (InterruptedException iex) {

iex.printStackTrace();

}

System.out.println("Barbar has started..");

while (true) {

shop.cutTheHair();

}

}

}

class Customer implements Runnable {

String name;

Date inTime;

BarberShop shop;

public Customer(BarberShop shop) {

this.shop = shop;

}

public String getName() {

return name;

}

public void setInTime(Date inTime) {

this.inTime = inTime;

}

public Date getInTime() {

return inTime;

}

public void setName(String name) {

this.name = name;

}

public void run() {

goForTheHairCut();

}

private synchronized void goForTheHairCut() {

shop.add(this);

}

}

class TheCustomerGenerator implements Runnable {

BarberShop shop;

public TheCustomerGenerator(BarberShop shop) {

this.shop = shop;

}

public void run() {

while (true) {

Customer customer = new Customer(shop);

customer.setInTime(new Date());

Thread thcustomer = new Thread(customer);

customer.setName("Customer " + thcustomer.getId());

thcustomer.start();

try {

TimeUnit.SECONDS.sleep((long) (Math.random() * 10));

} catch (InterruptedException iex) {

iex.printStackTrace();

}

}

}

}

class BarberShop {

int nchair;

List listCustomer;

public BarberShop() {

nchair = 3;

listCustomer = new LinkedList();

}

public void add(Customer customer) {

System.out.println("Customer : " + customer.getName() + " is entering the shop at " + customer.getInTime());

synchronized (listCustomer) {

if (listCustomer.size() == nchair) {

System.out.println("There is no chair available for customer " + customer.getName());

couch.add(customer);

System.out.println("Customer " + customer.getName() + "Exists...");

return;

}

((LinkedList) listCustomer).offer(customer);

System.out.println("Customer : " + customer.getName() + " got the chair.");

if (listCustomer.size() == 1)

listCustomer.notify();

}

}

public void cutTheHair() {

Customer customer;

System.out.println("Barber is waiting for the lock.");

synchronized (listCustomer) {

while (listCustomer.size() == 0) {

System.out.println("Barber waiting for the customer.");

try {

listCustomer.wait();

} catch (InterruptedException iex) {

iex.printStackTrace();

}

}

System.out.println("Barber has found customer in chair.");

customer = (Customer) ((LinkedList) listCustomer).poll();

long duration = 0;

try {

System.out.println("Cutting hair of the Customer : " + customer.getName());

duration = (long) (Math.random() * 10);

TimeUnit.SECONDS.sleep(duration);

} catch (InterruptedException iex) {

iex.printStackTrace();

}

Scanner reader = new Scanner(System.in);

System.out.println("Enter a number: ");

int n = reader.nextInt();

System.out.println("Entered Value: " + n);

System.out.println("Completed Cutting hair of the Customer : " + customer.getName() + " in only " + duration

+ " seconds.");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!