Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

No enclosing instance of type

807588Apr 21 2009 — edited Apr 21 2009
I rewrote a java programme but when i tried to run it, I got this error msg "No enclosing instance of type Admin is accessible. Must qualify the allocation with an enclosing instance of type Admin (e.g. x.new A() where x is an instance of Admin)." Please take a look at the programme and let me know where did i make mistakes. Thanks.
public class Admin 
{
	public static void main(String [] args) 
	{
	Terminal pc = new Terminal();
	CustomerQueue cQ = new CustomerQueue();
	Thread staff1 = new Thread( new Staff(1, cQ, pc) );
	Thread staff2 = new Thread( new Staff(2, cQ, pc) );
	Thread custArr = new Thread ( new CustomerArrival(cQ) );
	staff1.start(); 
	staff2.start();
	custArr.start();
	}
	

	public class Staff implements Runnable 
	{
		private int Staffid;
		private Terminal pc;
		CustomerQueue cQ;
		public Staff (int id, CustomerQueue cQ, Terminal pc) 
		{
		this.Staffid = id;
		this.pc = pc;
		this.cQ = cQ;
		}
		public void run() 
		{ 
		for (int j = 0; j < 5; j++)
		{
		cQ.getCustWaiting(Staffid); 
		Timer.busyWithCustomer(20, Staffid);
		pc.useTerminal (this);
		}
		System.out.println("Staff " + Staffid + " ends service");
		}
		public int getStaffId()
		{
		return Staffid;
		}
	}


public class CustomerQueue 
		{ 
			private boolean customersWaiting = false;
			public synchronized void setCustWaiting()
			{ 
			customersWaiting = true; 
			System.out.println("Customers are here in a Queue");
			notifyAll();
			}
			public synchronized void getCustWaiting(int id)
			{
				try 
					{
						while (!customersWaiting)
						{
							System.out.println("Staff " + id + " wait for customers");
							wait(); 
						}
						customersWaiting = false; 
					}
				catch (InterruptedException e)
				{
					System.exit (0);
				}
			}
	}

public class CustomerArrival implements Runnable 
{
	private CustomerQueue custQueue;
	public CustomerArrival(CustomerQueue custQueue) 
	{
	this.custQueue = custQueue;
	}
	public void run ()
	{
	for (int j = 0; j < 5; j++) 
	{
	Timer.duration(15);
	System.out.println("\tCustomer arrives");
	custQueue.setCustWaiting();
	}
	System.out.println ("\tNo more customers");
	}
}

public class Terminal 
{
	public synchronized void useTerminal(Staff t)
	{
		int id = t.getStaffId();
		System.out.println ("Staff " + id + " uses PC");
		Timer.busyWithPC(50, id);
		System.out.println ("Staff " + id + " finished with PC");
	}
}

public static class Timer 
{
	public static void busyWithPC(int timeUnit, int id) 
	{
	duration(timeUnit*id);
	}
	public static void busyWithCustomer(int timeUnit, int id) 
	{
	System.out.println ("Staff " + id + " serves customer");
	duration(timeUnit*id);
	}
	public static void duration(int time) 
	{ 
	int sleepTime = (int) ( 15 * Math.random() );
	try 
	{
		Thread.sleep(sleepTime*1500); 
	}
	catch(InterruptedException e) { }
	}
	}
}

			

	
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2009
Added on Apr 21 2009
12 comments
689 views