【Java】【OS】操作系统理发店问题通过应用小程序动态实现

 📢博客主页:🏀九九舅舅酒酒🏀
📢欢迎点赞 👍 收藏 ⭐留言 📝 欢迎讨论!👏
📢本文由 【九九舅舅酒酒】 原创,首发于 CSDN🙉🙉🙉
📢由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!✨
📖精品专栏(不定时更新)【JavaSE】 【MySQL】【LeetCode】【Web】【操作系统】


运行效果截图:

 


目录

一、问题描述:

二、问题分析:

三、代码实现

(1)Barber.java

(2)BarberShop.java

(3)BarberShopApplet.java

(4)Cusomer.java

(5)MessageCanvas.java

(6)BarberShop.html

四、调用



一、问题描述:

理发店包含一间接待室和一间工作室,有一名理发师,接待室内有n(n≥1)把椅子,而工作室只有1把椅子。如果没有顾客,理发师就去睡觉;如果理发师在睡觉;则顾客会唤醒他;如果理发师在忙且接待室有空闲椅子,那么此顾客会坐在其中1把空闲的椅子上等待;如果来时所有椅子都有人,那么顾客离去。


二、问题分析:

进程个数:2,理发师与顾客

信号量个数:5,空闲椅子情况empty,full;理发师状态cut;椅子总数N;互斥信号量mutex

共享资源:count

同时,剪发师可以看作生产者与消费者的问题,顾客可以看作读者写者问题。


三、代码实现

(1)Barber.java

/*  Barber.java
 *
 *  The Barber is implemented as thread, and its main activities are sleeping for a random time,
 *  and then call the methods in Barbershop class.
 *

 */

public class Barber extends Thread{
	private BarberShop shop;
	private BarberShopApplet tapplet;
	private int pid;
	int delay 		= 2500;
	int status 		= 0;
	int customerID  = 0;

	public Barber(BarberShopApplet applet, BarberShop iq, int id){
		shop 	= iq;
		tapplet = applet;
		pid 	= id;
	}

	public void run(){

		while(true){
			try{
				status = 0;
				tapplet.mc.println(status, "b", pid);
				sleep((int)(Math.random()*delay));
				shop.cutHair(tapplet, pid);
				sleep((int) (Math.random()*delay));
				shop.finishCut(tapplet, pid);

			} catch(InterruptedException e){
				System.err.println("Exception " +  e.toString());
			}
		}
	}

}

(2)BarberShop.java

/*	BarberShop.java
 *
 *  This class is the program that controls all the activities of customers and barbers.
 *  This barber shop has maximum capacity of 8 customers. There are 3 barbers in the
 *  shop.
 *	There are 4 sofa seats and 3 barber chairs in the shop. Three barbers will spend
 *	their time on sleeping for a while, cutting hair and performing as a cashier while
 *	he/she is not cutting hair.
 *  The customer will wait outside of the shop if there are already 8 customers in the shop.
 *	After the customer enters into the shop, he/she will wait in the standing area first, then
 *	sit on sofa in the order that he/she arrives. He/she will sit on the barber chair if there
 *	is any barber chair available. After the barber finishes the hair cut for him/her, this
 *  customer will wait to pay, then exit from the barbershop.
 *
 */


import java.awt.*;

public class BarberShop extends Canvas
{
	private int chairSize   = 3;
	private int sofaSize    = 4;
	private int frameDelay  = 3560;

	private int[] customerSofaQ;		//the queue to hold the customers on the sofa
	private int[] customerStandQ;		//the queue to hold the customers on the standing area
	private int[] customerChairQ;		//the queue to hold the customers on Barber Chairs
	private int[] customerPayQ;			//the queue to hold the customers waiting for paying.
	private int[] customerReady; 		//cutomerReady[i] = 1, customer i is ready for barber 1
	private int[] finishedCustomerQ; 	//the array to hold the cut finish flags
	private int[] paidCustomerQ;
	private int[] exitArray;			//the array hold all the customers in the order they exit shop

	private int sofaTop, sofaBottom;		//for customerSofaQ
	private int chairTop, chairBottom;		//for customerChairQ
	private int payTop, payBottom;			//for customerPayQ
	private int customerTop, customerBottom;//for customersQ
	private int customerOnSofa; 			//the count of customers on the sofa
	private int customerOnChair;			//the count of customers on the barber chairs
	private int customerStandCount; 		//the count of customers standing
	private int wantPayCount; 				//the count of customers waiting for paying
	private int hasCashier;
	private int cashierID;					//the barber ID for who is performing as a cashier
	private int exitID;						//the customer ID who is leaving the barbershop
	private int exitTop;

	private int customerCount;
	private int size;
	private int[] customerOut;
	private int outTop;
	private int outBottom;
	private int repaintFlag = 0;

	private Font font;
	private FontMetrics fm;
	private int x;     							//customer consumed item

	public BarberShop( ){
		size 		= 4;    					//default buffer size
		customerTop = customerBottom = 1;
		payTop		= payBottom 	 = 1;
		chairTop	= chairBottom 	 = 0;
		sofaTop 	= sofaBottom 	 = 0;
		customerCount 	= 0;
		customerOnSofa 	= 0;
		customerOnChair = 0;
		customerStandCount = 0;
		wantPayCount	   = 0;
		hasCashier 		   = 0;
		cashierID 		   = 0;
		exitID 			   = 0;
		exitTop 		   = 0;
		finishedCustomerQ  = new int[11];
		customerOut        = new int[2];
		outTop     		   = outBottom = 0;

		setSize(size);
		resize(500, 300);
		setBackground(Color.white);
		font = new Font("TimesRoman", Font.BOLD, 18);
		fm = getFontMetrics(font);
 	 }


	public void setSize(int s)
	{
		size = s;
		if(size > 8) customerStandCount = 8;
		else  		 customerStandCount = size;
		int tmpCount = 0;
		if(size > 8)
		{ tmpCount = size - 8;
		  System.out.println("the tmpCount is " + tmpCount);
		  for(int i = 0; i < tmpCount; i++)
		 {
		 	customerOut[i] = 9+i;
		 }
	    }
	    outBottom = 0;
		outTop = 1;
		customerSofaQ = new int[sofaSize];
		customerChairQ = new int[chairSize+1];
		customerPayQ = new int[11];
		customerReady = new int[size+1]; //the maximum customer size is 10
		paidCustomerQ = new int[size+1];
		exitArray 	  = new int[size];

		/* Initialize the array*/
		for(int i = 1; i <=size ; i++)
		{
			customerReady[i] = 0;
		}
		repaint();
	}

	public synchronized boolean chairFull(){
			return customerOnChair == chairSize;
	}

	public synchronized boolean sofaFull(){
				return customerOnSofa == sofaSize;
	}

	/**
	 * If there is no customer on this barber chair, then check if there is a cashier's job.
	 * If he/she is doing a cashier's job, he/she will finish that first. Then the barber
	 * will wait for the customer ready. After customer ready, he/she will cutting the hair
	 * for a random time period.
	 * The barber will just cutting  hair for the customer sitting on
	 * his/her chair. i.e. barber 1 will just cutting hair for the customer
	 * sitting on the barber chair 1.
	 */
	public synchronized void cutHair(BarberShopApplet applet, int id)
	{
		if(customerReady[id] == 0) getCashierLock(applet, id);
		if(cashierID == id) 	   performCashier(applet, id);
		while(customerReady[id] == 0)	//if there is no customer is waiting
		{
			updateBarberStatus(applet, id, 4);
			try{ wait(); }catch(InterruptedException e){
				System.err.println("Exception " +  e.toString());
		    }
		}

		System.out.println("customerReady are: ");
		for(int i = 0; i <= 3; i++)
		{
			System.out.println(Integer.toString(customerReady[i]));
		}

		int x = customerReady[id];
		applet.b[id].customerID = x;
		applet.c[x].barberID = id;
		System.out.println("x is " + x);
		applet.b[id].status = 1;
		applet.mc.println(applet.b[id].status, "b", id, x);
		updateCustomerStatus (applet, x, 1);   //cutting Hair
		//repaint();
		notifyAll();
	}

	/* When the barber finishes the haircutting, he/she will signal the customer finish flag
	 * and update corresponding variables.
	 */
	public synchronized void finishCut(BarberShopApplet applet, int id)
	{
		customerReady[id] = 0;
		int y = applet.b[id].customerID;
		/* The following codes added to animate one of the unfair situation:
		 * The process hold the resources unnecessarily.
		 * The finished process has to wait until the prior process to finish
		 * (the first process in this program).
		 */
		if(applet.haltFlag == 1)
		{
			if(y != 1)
			{
				updateCustomerStatus(applet, y, 10);
				updateBarberStatus(applet, id, 1);
			}
			else
			{
				while(true)			/* to keep the barber status in cutting hair */
				{
					try {	wait(); } catch(InterruptedException e) {}
				}
			}
		}
		else if(applet.requestFlag == 1)
		{
			System.out.println("process is " + y);
			if(y == 1)
			{
				while(finishedCustomerQ[2] != 1)
				{	try {	wait(); } catch(InterruptedException e) {}
				}

				updateCustomerStatus(applet, y, 11);
			}
			else if(y==3)
			{
				while(finishedCustomerQ[2] != 1)
				{
					try { wait(); } catch(InterruptedException e) {}
				}
				updateCustomerStatus(applet, y, 7);
			}
			else
			{
				updateCustomerStatus(applet, y, 7);   //waiting for pay
			}
				customerChairQ[id]		= 0;
				applet.b[id].customerID = 0;
				applet.c[y].barberID 	= 0;
				repaint();
				finishedCustomerQ[y] = 1;
				notifyAll();
				wantPayCount ++;
				customerPayQ[y] = y;
				repaint();
				customerOnChair --;
				notifyAll();
				if(wantPayCount > 0) getCashierLock(applet, id);
				if(cashierID == id)	 performCashier(applet, id);
				else   updateBarberStatus(applet, id, 4);
		}
		else	// To handle the processes in fair situation
		{
			updateCustomerStatus(applet, y, 7);
			customerChairQ[id]		= 0;
			applet.b[id].customerID = 0;
			applet.c[y].barberID 	= 0;
			repaint();
			System.out.println("customer " + y + " finish cutting");

			finishedCustomerQ[y] = 1;
			wantPayCount ++;
			customerPayQ[payTop] = y;
			payTop++;
			repaint();
			customerOnChair --;
			notifyAll();
			if(wantPayCount > 0) getCashierLock(applet, id);
			if(cashierID == id)	 performCashier(applet, id);
			else   updateBarberStatus(applet, id, 4);
		}
	}


	/* The customer will first wait for his/her turn, then a available seat on the sofa.
	 */
	public synchronized void sitSofa(BarberShopApplet applet, int id)
	{
		while(customerBottom != id)
		{
			System.out.println("customer " + id + " is waiting for the turn");
			try{ wait(); } catch(InterruptedException e) {}
		}
		customerCount++;
		notifyAll();

		if(id > 8)
		{ customerStandCount ++;
		  outBottom ++;
		  repaint();
	    }
		while(sofaFull())
		{
			try	{ wait();	}catch(InterruptedException e) {}
		}

		customerBottom++;
		notifyAll();
		customerOnSofa ++;
		customerStandCount --;
		customerSofaQ[sofaTop] = id;
		sofaTop =(sofaTop+1)%sofaSize;
		repaint();
		updateCustomerStatus(applet, id, 5);  //sitting on sofa
		notifyAll();
	}

	/* The customer will first wait for his/her turn, then an available barber chair.
	 * He/she will spend a random time on the chair before send the ready flag to the barber.
	 */
	public synchronized void sitBarberChair(BarberShopApplet applet, int id)
	{
		  while(customerSofaQ[sofaBottom] != id)
		  {
			  System.out.println("Customer " + id + "is waiting for the chair turn");
			  try{ wait(); } catch(InterruptedException e) { }
		  }

		  while(chairFull())
			{
				try	{	wait();	}catch(InterruptedException e) {}
			}
			customerSofaQ[sofaBottom] = 0;
			sofaBottom =(sofaBottom+1)%sofaSize;		//get up from sofa
			customerOnSofa --;
			customerOnChair ++;
			for(int i = 1; i <= chairSize; i++)
			{
				if(customerChairQ[i] == 0)
				{
					customerChairQ[i] = id;
					customerReady[i] = id;
					i = chairSize;			// get out of the loop
				}
			}
			updateCustomerStatus(applet,id, 6);
			repaint();

		  try{
				applet.c[id].sleep((int) (Math.random()*frameDelay));
			}catch(InterruptedException e) { }

			notifyAll();
	}


	 /* If there is a customer waiting and no cashier, a barber will be a cashier when he/she
	  * is not cutting hair.
	  */
	 public synchronized void getCashierLock(BarberShopApplet applet, int bid)
	 {
	    if((wantPayCount > 0) && (hasCashier!= 1))
	    {
	  	 	 hasCashier= 1;
	  	 	 cashierID = bid;
	  	 	 //updateBarberStatus(applet, bid, 9); // a cashier right now
	  	 	 repaint();
	  	 	 System.out.println("Barber " + bid + " got the cashier Lock right now");
	  	 	 notifyAll();
	    }

	 }

     public synchronized void performCashier(BarberShopApplet applet, int bid)
     {
		 while(wantPayCount > 0)
		 {
			 System.out.println("Barber " + bid + " is a cashier right now");
			 updateBarberStatus(applet, bid, 2);
			 try{ wait(); } catch(InterruptedException e) {}
		}
		 cashierID = 0;
		 hasCashier= 0;
		 notifyAll();
	 }

	 /* The customer will wait for a cashier first, then wait for his/her turn to pay.
	  * It will take random time to get the receipt, the customer then will leave the shop.
	  */
     public synchronized void waitPay(BarberShopApplet applet, int cid)
     {

		while(customerPayQ[payBottom] != cid)
		{
			 if((applet.requestFlag == 1) && (cid == 3))
			 	repaintFlag = 1;
			 try{ wait(); } catch(InterruptedException e) { }
		}

		if(applet.requestFlag == 1)		// for 1st process in unfair situation
		{
			while(true)
			{
				try{ applet.c[cid].sleep((int) (Math.random()*frameDelay)); }
				catch(InterruptedException e) { }
			}
		}

		while(hasCashier!= 1)
		{
			 try{ wait(); } catch(InterruptedException e) { }
		}
		try{ applet.c[cid].sleep((int) (Math.random()*frameDelay)); }
		catch(InterruptedException e) {}
		updateCustomerStatus(applet, cid, 9);
		payBottom++;
		wantPayCount --;
		exitID = cid;
		exitArray[exitTop] = cid;
		exitTop ++;
		customerCount --;
		repaint();
		notifyAll();
	 }

     public synchronized void updateCustomerStatus(BarberShopApplet applet, int cid, int status)
     {
		 applet.c[cid].status = status;
		 applet.mc.println(status, "c", cid);
	 }

	 public synchronized void updateBarberStatus(BarberShopApplet applet, int bid, int  status)
	 {
		 applet.b[bid].status = status;
		 applet.mc.println(status, "b", bid);
	 }


	 public void clear()
	 {
		 size 				= 4;    							//default buffer size
		 customerCount 		= 0;
		 payTop 	= payBottom 	= 1;
		 chairTop 	= chairBottom 	= 0;
		 sofaTop 	= sofaBottom 	= 0;
		 customerTop= customerBottom= 1;
		 outTop 	= outBottom 	= 0;
		 finishedCustomerQ 	= new int[11];
		 customerOut		= new int[2];
		 customerOnSofa 	= 0;			//the count of customers on the sofa
		 customerOnChair 	= 0;		//the count of customers on the barber chairs
		 customerStandCount = 0;		//the count of customers standing
		 wantPayCount 		= 0;			//the count of customers waiting for paying
		 hasCashier			= 0;
		 cashierID 			= 0;					//the barber ID for who is performing as a cashier
		 exitID 			= 0;
		 exitTop			= 0;
		 repaintFlag 		= 0;
	 }

	/*
	 * Draw the barber shop on the canvas
	 */
	public void paint(Graphics g){
		g.setFont(new Font("TimesRoman", Font.BOLD, 12));
		g.setColor(Color.blue);
		int xpos = 120;
		int ypos = 10;
		g.setFont(new Font("TimesRoman", Font.BOLD, 18));
		/************************************/
		/* Draw Barber Chairs on the canvas */
		/************************************/
		g.drawString("Barber Chairs", xpos+150, ypos+5);
		for(int i = 1; i <= chairSize; i++)
		{
			g.draw3DRect(xpos+100+70*(i-1), ypos+20, 28, 28, true);
			if(i != cashierID)	g.drawString("B"+i, xpos+103+70*(i-1), ypos+70);
		}

		g.setColor(Color.red);
		for(int j=1;  j <= chairSize; j ++)
		{
			if(customerChairQ[j] != 0)
			{
				g.drawString(Integer.toString(customerChairQ[j]), xpos + 105 + 70*(j-1), ypos+35);
				g.draw3DRect(xpos+100+70*(j-1), ypos+20, 28, 28, true);
			}
		}

		/**********************************/
		/* Draw Cashier's waiting queue */
		/*********************************/
		g.setColor(Color.blue);
		g.drawString("Cashier", xpos+410, ypos+45);
		g.setFont(new Font("TimesRoman", Font.BOLD, 14));
		if(cashierID != 0)
		{
			g.drawString("B "+cashierID, xpos+430, ypos+20);
		}
		g.draw3DRect(xpos+410, ypos+60, 60, 20, true);
		g.setFont(new Font("TimesRoman", Font.BOLD, 12));
		int b = payBottom;
		System.out.println("wantPaycount is " + wantPayCount);
		if(repaintFlag == 1)
		{
			for(int i = 0; i < 3; i++)
			{
				if(customerPayQ[i+b] != 0)
				g.drawString("C"+customerPayQ[i+b], xpos+430, ypos+100);
				ypos += 20;
			}
		}

		else
		{
			for(int i = 0; i < wantPayCount; i++)
			{
				if(customerPayQ[i+b] != 0)
				g.drawString("C"+customerPayQ[i+b], xpos+430, ypos+100);
				ypos += 20;
			}
		}

		/**********************************/
		/* Draw standing room on canvas   */
		/**********************************/
		g.setFont(new Font("TimesRoman", Font.BOLD, 12));
		ypos = 10;
		g.drawString("Standing Room Area", xpos-100, ypos+160);
		b = customerBottom;
		for(int i = 0; i < customerStandCount; i++)
		{
			g.drawString("C"+(i+b), xpos+80-25*i, ypos+120);
		}

		g.setColor(Color.green);
		g.drawString("Entrance", xpos-110, ypos+100);
		g.drawString("--------->", xpos-110, ypos+105);
		g.setColor(Color.red);
		System.out.println("outTop is: " + outTop);
		for(int i = outBottom; i <= outTop; i++)
		{
			if(customerOut[i] > 0)
			g.drawString("C "+customerOut[i], xpos-80, ypos+80-20*i);
		}
		g.setColor(Color.red);
		g.drawString("Exit", xpos+530, ypos+10);
		for(int i = 0; i < exitTop; i++)
		{
			if(exitArray[i] != 0)
			g.drawString("C" + exitArray[i], xpos+530, ypos+25+15*i);
		}

		/**********************************/
		/* Draw waiting Sofa on canvas   */
		/**********************************/
		xpos = 100;
		ypos = 10;
		g.setColor(Color.blue);
		g.drawString("Sofa", xpos+225, ypos+185);

		for(int i = 0; i < sofaSize; i++)
		{
			g.draw3DRect(xpos+180+28*(i), ypos+140, 28, 28, true);
		}


		g.setColor(Color.red);
		int k = sofaBottom;
		for(int j=0; j < customerOnSofa; j++)
		{
				g.drawString(Integer.toString(customerSofaQ[k]), xpos + (190+28*3) -28*(j), ypos+155);
				g.draw3DRect(xpos+180+28*3 - 28*(j), ypos+140, 28, 28, true);
				k = (k+1)%sofaSize;

		}

	}
}


(3)BarberShopApplet.java

/* File: BarberShopApplet.java
 *
 * This is a Java applet file for Barbershop problem animation. The GUI
 * of this applet contains three parts: animation canvas, message canvas
 * and a button panel.
 * The animation canvas is where the Barbershop animation is displayed.
 * The message canvas is where the statues of barbers and customers are displayed.
 * The button panel has 6 basic buttons: START, STOP, PAUSE, CONTINUE, FASTER,
 * SLOWER.
 *
 * This applet will allow user to choose from a fair barbershop or unfair barbershop.
 * In the fair barbershop, unless the user choose the number of customers, default
 * number of the customers is 4.
 * The number of customers in the unfair barbershop is also 4. And there are two
 * unfair situations that the user can choose from.
 *
 */


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.Applet;
import java.lang.*;

public class BarberShopApplet extends Applet
{
	private BarberShopApplet applet = this;
	private BarberShop myShop;
	private Button fastButton, slowButton, stopButton, startButton,pauseButton, continueButton;
	private Panel buttonPanel, namePanel, namePanel2;
	private Checkbox haltResource, requestResource;
	private Choice customer;
	private int customerN = 4;		//default number of customer and barber
	private int barberN   = 3;
	private Thread at;
	MessageCanvas mc;
	Customer[] c;
	Barber[] b;
	int haltFlag	 = 0;
	int requestFlag  = 0;
	synchronized void startPushed() {notify();}
	synchronized void stopPushed()  {notify();}

	public void init() {
		resize(800, 600);
		setLayout(new GridLayout(3, 1));

		myShop = new BarberShop();
		mc = new MessageCanvas();
		add(myShop);						//add BarberShop canvas at the top
		add(mc);							//add message box canvas in the middle

		buttonPanel = new Panel();
		Panel namePanel = new Panel();
		Panel bPanel = new Panel(); 		// to hold all buttons and the labels
		bPanel.setFont(new Font("TimesRoman", Font.BOLD, 14));
		bPanel.setLayout(new GridLayout(3, 1));

		buttonPanel.add(startButton 	= new Button("START"));
		buttonPanel.add(stopButton 		= new Button("STOP"));
		buttonPanel.add(pauseButton 	= new Button("PAUSE"));
		buttonPanel.add(continueButton 	= new Button("CONTINUE"));
		buttonPanel.add(fastButton 		= new Button("FAST"));
		buttonPanel.add(slowButton 		= new Button("SLOW"));

		Label titleLabel = new Label("Fair Barbershop", Label.CENTER);
		titleLabel.setFont(new Font("TimesRoman", Font.BOLD, 16));
		titleLabel.setForeground(Color.blue);
		Label textLabel = new Label("Maximum Shop Capacity is 8 Customers", Label.CENTER);

		Label titleLabel2 = new Label("Unfair Barbershop ", Label.CENTER);
		Label textLabel2 = new Label("4 Customers In The Shop", Label.CENTER);
		titleLabel2.setFont(new Font("TimesRoman", Font.BOLD, 16));
		titleLabel2.setForeground(Color.blue);

		namePanel.setLayout(new GridLayout(2,1));
		namePanel.add(titleLabel);
		namePanel.add(textLabel);

		namePanel2 = new Panel();
		namePanel2.setLayout(new GridLayout(2,1));
		namePanel2.add(titleLabel2);
		namePanel2.add(textLabel2);

		Panel titlePanel = new Panel();
		titlePanel.setLayout(new GridLayout(1,2));
		titlePanel.add(namePanel);
		titlePanel.add(namePanel2);

		Panel choicePanel = new Panel();    //to hold all the choice boxes
		choicePanel.setLayout(new GridLayout(1,2));
		customer = new Choice();
		for(int i = 1; i <=10; i++)
		{
			customer.addItem(Integer.toString(i));
		}
		customer.select("4");
		Label customerLabel = new Label("Number of Customers", 2);
		customerLabel.setBackground(Color.lightGray);
		Panel customerPanel = new Panel();
		customerPanel.add(customerLabel);
		customerPanel.add(customer);

		Panel unfairPanel = new Panel();
		unfairPanel.setLayout(new GridLayout(2,1));
		CheckboxGroup g = new CheckboxGroup();
		unfairPanel.add(haltResource = new Checkbox("Request finished, but resources are held unnecessarily", g, false));
		unfairPanel.add(requestResource = new Checkbox("Request not finished, but resources are released", g, false));
		choicePanel.add(customerPanel);
		choicePanel.add(unfairPanel);

		bPanel.add(titlePanel);
		bPanel.add(choicePanel);
		bPanel.add(buttonPanel);

		add(bPanel);
	}

	public boolean action(Event evt, Object arg)
	{
		if(evt.target == customer)
		{
			customerN = Integer.parseInt(arg.toString());
			haltResource.setEnabled(false);
			requestResource.setEnabled(false);
			return true;
		}
		else if(evt.target == haltResource)
		{
			startButton.setEnabled(false);
			customer.setEnabled(false);
			stopButton.setEnabled(true);
			haltResource.setEnabled(false);
			requestResource.setEnabled(false);
			haltFlag = 1;
			System.out.println("HaltResource");
			customerN = 4;
			myShop.setSize(customerN);
			c = new Customer[customerN+1];		//Customer[0] is a dummy slot
			b = new Barber[barberN+1];

			mc.setMessage(barberN, customerN);
			for(int i = 1; i <= customerN; i++)
			{
				c[i] = new Customer(applet, myShop, i);
			}

			for(int i = 1; i <= barberN; i++)
			{
				b[i] = new Barber(applet, myShop, i);
			}
			for(int i = 1; i <= barberN; i++)
			{
				b[i].start();
			}
			for(int i = 1; i <= customerN; i++)
			{
				c[i].start();
			}

			return true;
		}
		else if(evt.target == requestResource)
		{
			startButton.setEnabled(false);
			stopButton.setEnabled(true);
			customer.setEnabled(false);
			haltResource.setEnabled(false);
			requestResource.setEnabled(false);
			System.out.println("RequestResource");
			requestFlag = 1;

			customerN = 4;
			myShop.setSize(customerN);
			c = new Customer[customerN+1];		//Customer[0] is a dummy slot
			b = new Barber[barberN+1];

			mc.setMessage(barberN, customerN);
			for(int i = 1; i <= customerN; i++)
			{
				c[i] = new Customer(applet, myShop, i);
			}

			for(int i = 1; i <= barberN; i++)
			{
				b[i] = new Barber(applet, myShop, i);
			}
			for(int i = 1; i <= barberN; i++)
			{
				b[i].start();
			}
			for(int i = 1; i <= customerN; i++)
			{
				c[i].start();
			}

			return true;
		}
		else if(arg.equals("PAUSE"))
		{	for(int i = 1; i <= customerN; i++)
			{
				if(c[i].isAlive()) 	c[i].suspend();
			}
			for(int i = 1; i <= barberN; i++)
			{
				if(b[i].isAlive())	b[i].suspend();
			}
			fastButton.setEnabled(false);
			slowButton.setEnabled(false);
			return true;
		}
		else if(arg.equals("CONTINUE"))
		{
			for(int i = 1; i <= customerN; i++)
			{
				if(c[i].isAlive()) 	c[i].resume();
			}

			for(int i = 1; i <= barberN; i++)
			{
				if(b[i].isAlive())	b[i].resume();
			}
			fastButton.setEnabled(true);
			slowButton.setEnabled(true);
			return true;
		}
		else if(arg.equals("FASTER"))
		{
			int newDelay = b[1].delay;
			newDelay /= 2;
			newDelay = newDelay < 100 ? 100: newDelay;

			for(int i = 1; i <= customerN; i++)
			{
					c[i].delay = newDelay;
			}
			for(int i = 1; i <= barberN; i++)
			{
					b[i].delay = newDelay;
			}
			return true;
		}
		else if(arg.equals("SLOWER"))
		{
			int newDelay = b[1].delay;
			newDelay *= 2;
			for(int i = 1; i <= customerN; i++)
			{
				c[i].delay = newDelay;
			}
			for(int i = 1; i <= barberN; i++)
			{
				b[i].delay = newDelay;
			}
			return true;
		}
		else if(arg.equals("START"))
		{
			myShop.setSize(customerN);
			c = new Customer[customerN+1];		//Customer[0] is a dummy slot
			b = new Barber[barberN+1];

			mc.setMessage(barberN, customerN);
			for(int i = 1; i <= customerN; i++)
			{
				c[i] = new Customer(applet, myShop, i);
			}

			for(int i = 1; i <= barberN; i++)
			{
				b[i] = new Barber(applet, myShop, i);
			}
			for(int i = 1; i <= barberN; i++)
			{
				b[i].start();
			}
			for(int i = 1; i <= customerN; i++)
			{
				c[i].start();
			}

			applet.startPushed();
			stopButton.setEnabled(true);
			startButton.setEnabled(false);
			fastButton.setEnabled(true);
			slowButton.setEnabled(true);
			customer.setEnabled(false);
			haltResource.setEnabled(false);
			requestResource.setEnabled(false);
			return true;
		}

		else if(arg.equals("STOP"))
		{
			try{
				for(int i = 1; i <= customerN; i++)
				{
					if(c[i].isAlive()) 	c[i].stop();
					c[i] = null;
				}

				for(int i = 1; i <= barberN; i++)
				{
					if(b[i].isAlive()) 	b[i].stop();
					b[i] = null;
				}
				}catch(Exception e) {}

			myShop.clear();
			applet.stopPushed();
			haltFlag 	= 0;
			requestFlag = 0;
			startButton.setEnabled(true);
			customer.setEnabled(true);
			haltResource.setEnabled(true);
			requestResource.setEnabled(true);
			fastButton.setEnabled(true);
			slowButton.setEnabled(true);
			if(at != null) at.stop();
			at = null;
			return true;
		}
		else{ return false;}

	}

}

(4)Cusomer.java

/*  Customer.java
 *
 *  The Customer Thread's main activities are call the methods in Barbershop class.
 *
 *
 */

public class Customer extends Thread{

	private BarberShopApplet tapplet;
	private BarberShop shop;
	private int cid;
	int delay 		= 2500;
	int status		= 0;
	int cutFinish 	= 0;
	int barberID 	= 0;
	int paid 		= 0;

	public Customer(BarberShopApplet applet, BarberShop iq, int id){
		shop	 = iq;
		tapplet  = applet;
		cid 	 = id;
	}

	public void run(){

		 try{

			  status = 0;
			  tapplet.mc.println(status, "c", cid);

			  shop.sitSofa(tapplet, cid);
			  sleep(delay);
			  shop.sitBarberChair(tapplet, cid);
			  shop.waitPay(tapplet, cid);

			} catch(InterruptedException e){
				System.err.println("Customer Exception " +  e.toString());
			}
	}

}

(5)MessageCanvas.java

/* File: MessageCanvas.java
 *
 * This class provides message canvas for the applet GUI.
 * It will print the statuses of customers and barbers on the GUI.
 */

import java.awt.*;

class MessageCanvas extends Canvas
{
	private Font font;
	private FontMetrics fm;
	private int[] barberStatus;
	private int[] customerStatus;
	private int[] serviceStatus;		//store the customer id that is cutting hair for each barber
								        //serviceStatus[i]=j, Barber i is cutting hair for customer j

	private int msgHeight;
    private int msgWidth;
	private int bn, cn;
	private int frameDelay = 256;

	public MessageCanvas( )
	{
		resize(size().width, 50);
		setBackground(Color.green);
		font = new Font("TimesRoman", 1, 18);
		fm = getFontMetrics(font);
        msgHeight = fm.getHeight();

	}

   public void setMessage(int barberN, int customerN)
    {
		bn = barberN;
		cn = customerN;
		barberStatus = new int[bn+1];
		customerStatus = new int[cn+1];
		serviceStatus = new int[bn+1];
		repaint();
    }


    void println(String s)
    {
        msgWidth = fm.stringWidth(s);
        repaint();
    }

    void println(int s, String st, int id)
	{
	        if(st.equals("b"))
	       		 	barberStatus[id] = s;
			else
				customerStatus[id] = s;
	        repaint();
   }

    void println(int s, String st, int id, int cid)
   	{
   	        if(st.equals("b"))
   	       	{ 	barberStatus[id] = s;
   	       		serviceStatus[id] = cid;
			}
   			else
   				customerStatus[id] = s;
   	        repaint();
   }


    public void paint(Graphics g)
    {
        g.setFont(font);
        int xpos = 40;
        int ypos = 30;

		g.drawString("Status of Customers: ", 60, 20);
		g.drawString("Status of Barbers: ", 380, 20);
		g.setFont(new Font("TimesRoman", 1, 12));

		for(int i=1; i<=cn;i++)
		{
			g.setColor(Color.black);
			g.drawString("C" + i, xpos, ypos+(12*i+5*(i-1)));
			if(customerStatus[i] == 0)
			{
				g.setColor(Color.yellow);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Standing ...", xpos+80, ypos+(12*i + 5*(i-1)));
			}
			else if (customerStatus[i] == 1)
			{
				g.setColor(Color.gray);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Cutting Hair...", xpos+80, ypos+(12*i + 5*(i-1)));
			}
			else if (customerStatus[i] == 2)
			{
				g.setColor(Color.blue);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Waiting for Cashier", xpos+80, ypos+(12*i + 5*(i-1)));
			}
			else if (customerStatus[i] == 3)
			{
				g.setColor(Color.red);
				g.fillOval(xpos+40,ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Finished the Hair Cut", xpos+80, ypos+(12*i + 5*(i-1)));
			}
			else if (customerStatus[i] == 4)
			{
				g.setColor(Color.red);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Waiting to pay", xpos+80, ypos+(12*i + 5*(i-1)));
			}
			else if (customerStatus[i] == 5)
			{
				g.setColor(Color.blue);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Sitting on the Sofa", xpos+80, ypos+(12*i + 5*(i-1)));
			}else if (customerStatus[i] == 6)
			{
				g.setColor(Color.red);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Sitting on the BarberChair", xpos+80, ypos+(12*i + 5*(i-1)));
			}else if (customerStatus[i] == 7)
			{
				g.setColor(Color.red);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Waiting to pay", xpos+80, ypos+(12*i + 5*(i-1)));
			}else if (customerStatus[i] == 9)
			{
				g.setColor(Color.green);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Left ", xpos+80, ypos+(12*i + 5*(i-1)));
			}else if (customerStatus[i] == 10)
			{
				g.setColor(Color.gray);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("Finished, but hold the resources unnecessarily", xpos+80, ypos+(12*i + 5*(i-1)));
			}else if (customerStatus[i] == 11)
			{
				g.setColor(Color.gray);
				g.fillOval(xpos+40, ypos+(2*i+15*(i-1)), 14, 14);
				g.drawString("C1 hasn't finished but has to leave", xpos+80, ypos+(12*i + 5*(i-1)));
			}
		}
	 	xpos = 380;
	 	ypos = 40;
        for(int i=1; i<=bn; i++)
        {
			g.setColor(Color.black);
			g.drawString("B" + i, xpos, ypos+(15*i+10*(i-1)));
			if(barberStatus[i] == 0)
			{
				g.setColor(Color.yellow);
				g.fillOval(xpos+60, ypos+(2*i+22*(i-1)), 18, 18);
				g.drawString("Sleeping ...", xpos+120, ypos+(15*i + 10*(i-1)));
			}
			else if (barberStatus[i] == 1)
			{
				g.setColor(Color.gray);
				g.fillOval(xpos+60, ypos+(2*i+22*(i-1)), 18, 18);
				g.drawString("Cutting Hair for C"+serviceStatus[i], xpos+120, ypos+(15*i + 10*(i-1)));
			}
			else if (barberStatus[i] == 2)
			{
				g.setColor(Color.blue);
				g.fillOval(xpos+60, ypos+(2*i+22*(i-1)), 18, 18);
				g.drawString("Accepting Payment...", xpos+120, ypos+(15*i + 10*(i-1)));
			}
			else if (barberStatus[i] == 3)
			{
				g.setColor(Color.red);
				g.fillOval(xpos+60, ypos+(2*i+22*(i-1)), 18, 18);
				g.drawString("Finished the Hair Cut", xpos+120, ypos+(15*i + 10*(i-1)));
			}
			else if (barberStatus[i] == 4)
			{
				g.setColor(Color.gray);
				g.fillOval(xpos+60, ypos+(2*i+22*(i-1)), 18, 18);
				g.drawString("Waiting ... ", xpos+120, ypos+(15*i + 10*(i-1)));
			}


		}
    }
}

(6)BarberShop.html

<!DOCTYPE HTML><HTML><HEAD></HEAD><BODY>
<APPLET CODE="BarberShopApplet.class" CODEBASE="." WIDTH=800 HEIGHT=700></APPLET>
</BODY></HTML>


四、调用

新建文件夹内完成如上的代码之后

在cmd内使用 appletviewer 即可

 

  • 16
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
基于Java的美发店会员管理系统是一个使用SSM框架和MySQL数据库的Web应用程序。该系统旨在为美发店提供一个完整的会员管理解决方案,包括商铺管理、美发管理、用户预约管理、用户分享管理、留言管理等功能。以下是该系统的设计和实现: 1. 系统架构 该系统采用MVC架构,即模型-视图-控制器架构。其中,模型层负责处理数据,视图层负责呈现数据,控制器层负责处理用户请求并调用模型和视图。 2. 数据库设计 该系统使用MySQL数据库保存数据。数据库包括以下表: - 商铺表:保存商铺信息,包括商铺名称、地址、电话等。 - 美发师表:保存美发师信息,包括姓名、性别、工龄等。 - 用户表:保存用户信息,包括用户名、密码、手机号等。 - 预约表:保存用户预约信息,包括预约时间、美发师、服务类型等。 - 分享表:保存用户分享信息,包括分享内容、分享时间等。 - 留言表:保存用户留言信息,包括留言内容、留言时间等。 3. 功能实现 该系统实现了以下功能: - 商铺管理:管理员可以添加、修改和删除商铺信息。 - 美发管理:管理员可以添加、修改和删除美发师信息。 - 用户预约管理:管理员可以审核用户预约信息,并将预约信息分配给相应的美发师。 - 用户分享管理:管理员可以审核用户分享信息,并将分享信息发布到系统中。 - 留言管理:管理员可以查看用户留言信息,并回复用户留言。 以上是基于Java的美发店会员管理系统的设计和实现。如果您需要更详细的信息,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻根根呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值