Use javautilLinkedList to create a system to simulate custom

Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java .

(a)Assume there is only one service line at the bank and it is first come, first served.

(b)The number of customer arriving at the bank varies from 1 to 5 and the number of tellers available to serve the customer varies from 1 to 4.

(c)Each customer will receive a sequential ID number when he/she join the line. The program should display the message “Customer xxx joins the line” to indicate the occurrence of the event.

(d)At the same token, when the teller serves the customer, the program should display “Customer xxx is being served”.

(e)If the line is empty, display “Teller waiting”.

(f)Simulate the cycle for 10 times as the end of bank’s business day.

(g)The service line should be empty after the bank is closed.

Code for needed classes:

//*******************************************************************
// Customer.java
//*******************************************************************

public class Customer
{
private int id;

//----------------------------------------------------------------
// Creates a customer with the specified id number.
//----------------------------------------------------------------
public Customer (int number)
{
id = number;
}

//----------------------------------------------------------------
// Returns a string description of this customer.
//----------------------------------------------------------------
public String toString()
{
return \"Customer \" + id;
}
}

//*******************************************************************
// Line.java   
//
//*******************************************************************

import java.util.LinkedList;

public class Line
{
LinkedList queue;

//----------------------------------------------------------------
// Creates a new line based on a queue.
//----------------------------------------------------------------
public Line()
{
queue = new LinkedList();
}

//----------------------------------------------------------------
// Removes a customer from the head of the queue.
//----------------------------------------------------------------
public Customer nextCustomer()
{
return (Customer)queue.removeFirst();
}

//----------------------------------------------------------------
// Adds a customer to the tail of the queue.
//----------------------------------------------------------------
public void addCustomer(Customer person)
{
queue.addLast(person);
}

//----------------------------------------------------------------
// Indicates whether the queue is empty.
//----------------------------------------------------------------
public boolean isEmpty()
{
return queue.isEmpty();
}

//----------------------------------------------------------------
// Returns the number of customers in the queue.
//----------------------------------------------------------------
public int size()
{
return queue.size();
}
}

I haven\'t been provided with any examples but this is how it is suppose to look like to my knowledge.

------start--------

Customer 0 joins the line

Customer 1 joins the line

Customer 2 joins the line

Customer 3 joins the line

Customer 0 is being served

Customer 1 is being served

Customer 2 is being served

Customer 3 is being served

(This will be repeated 10 times)

-----End------

So, the program follows First in, First out. Therefore customer 0 should be served before customer 1. When a teller is not being using it should say teller is waiting. Tellers are limited to 4. So to make it easier/ harder the costomers arrive in groups of 1-5. Do this 10 times.(same format as the above, of costumer arriving then being served. If they do not get surved by the next section whey be part of the next section. You can limit the number of customers to 20 if that will be of any help

Solution

Please find the source as follows,

-----------------------------------------------------------------------------------------------------------------------


public class Bank {
  
   private static final int TELLER_NO = 4;
  
   private static int CUSTOMER_COUNT = 2;
  
   static Line customerLine;
  
   static TellerQueue tellerQueue;
  
   public static void main(String[] args) {
       tellerQueue = new TellerQueue(TELLER_NO);
       customerLine = new Line();
       Bank bank = new Bank();
       for(int i=0 ; i<5 ; i++) {
           bank.customerArrived(CUSTOMER_COUNT + i);
           bank.service();
       }
   }
  
   public void customerArrived(int noOfCustomer) {
       for(int i=0; i< noOfCustomer; i++) {
           int id= i+1;
           Customer customer = new Customer(id);
           assign(customer);
       }
   }

   private void assign(Customer customer) {
       int tellerID = tellerQueue.getTeller();
       if(tellerID != -1) {
           tellerQueue.assignTeller(tellerID, customer);
       } else {
           customerLine.addCustomer(customer);
           System.out.println(\"\ \" + customer.toString() + \" joins the line\");
       }
   }
  
   private void service() {
       while(!tellerQueue.isAllTaskCompleted()) {
           waitForService(1000);
           tellerQueue.taskCompleted();
           if(!customerLine.isEmpty()) {
               assign(customerLine.nextCustomer());
           }
       }
       System.out.println(\"\ Teller is waiting...............\ \");
       waitForService(1500);
   }
  
   private void waitForService(long time) {
       try {
           Thread.sleep(time);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }
  

}

---------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------

import java.util.LinkedList;

public class Line {
   LinkedList<Customer> queue;

   // ----------------------------------------------------------------
   // Creates a new line based on a queue.
   // ----------------------------------------------------------------
   public Line() {
       queue = new LinkedList<Customer>();
   }

   // ----------------------------------------------------------------
   // Removes a customer from the head of the queue.
   // ----------------------------------------------------------------
   public Customer nextCustomer() {
       return (Customer) queue.removeFirst();
   }

   // ----------------------------------------------------------------
   // Adds a customer to the tail of the queue.
   // ----------------------------------------------------------------
   public void addCustomer(Customer person) {
       queue.addLast(person);
   }

   // ----------------------------------------------------------------
   // Indicates whether the queue is empty.
   // ----------------------------------------------------------------
   public boolean isEmpty() {
       return queue.isEmpty();
   }

   // ----------------------------------------------------------------
   // Returns the number of customers in the queue.
   // ----------------------------------------------------------------
   public int size() {
       return queue.size();
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;


public class TellerQueue {
  
   LinkedHashMap<Integer, Customer> tellerMap = new LinkedHashMap<Integer, Customer>();
  
   LinkedList<Integer> tellerQ = new LinkedList<Integer>();
  
   int size;

   public TellerQueue(int size) {
       for(int i=0; i<size; i++) {
           tellerQ.add(i + 1);
       }
       this.size = size;
   }

   public void assignTeller(int tellerID, Customer customer) {
       tellerMap.put(tellerID, customer);
       System.out.println(\"\ \"+ customer.toString() + \" is been assigned to teller \"+ tellerID);
   }
  
   public void taskCompleted() {
       Set<Integer> tellerList = tellerMap.keySet();
       if(tellerList.size() > 0) {
           int tellerID = tellerList.iterator().next();
           Customer customer = tellerMap.remove(tellerID);
           System.out.println(\"\ \"+ customer.toString() +\" is being served\");
           tellerQ.add(tellerID);
       }
   }
  
   public int getTeller() {
       if(!tellerQ.isEmpty()) {
           return tellerQ.remove();
       } else {
           return -1;
       }
   }
  
   public boolean isTellerFull() {
       return tellerQ.isEmpty();
   }
  
   public boolean isAllTaskCompleted() {
       return tellerQ.size() == size;
   }
}

-----------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------

public class Customer {
   private int id;

   // ----------------------------------------------------------------
   // Creates a customer with the specified id number.
   // ----------------------------------------------------------------
   public Customer(int number) {
       id = number;
   }

   // ----------------------------------------------------------------
   // Returns a string description of this customer.
   // ----------------------------------------------------------------
   public String toString() {
       return \"Customer \" + id;
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------

Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one
Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one
Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one
Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one
Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one
Use java.util.LinkedList to create a system to simulate customers being served at the bank. Download Customer.java & Line.java . (a)Assume there is only one

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site