Simple simulation 2 points Use javautilLinkedList to create

Simple simulation (2 points)

Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at the bank and it is first come, first served.

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.

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.

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

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

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

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

//*******************************************************************
// 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();
}
}

Solution

import java.io.*;
import java.util.*;

public class OneTellerSimulation {

public static void main(String[] args) throws Exception{
Customer incomingCustomer;
Customer servedCustomer;
Customer queueNew = null;

Queue<Customer> queue;
Scanner reader;

reader = new Scanner( new File(\"bank customer.txt\") );
queue = new LinkedList<Customer>();

incomingCustomer = null;
servedCustomer = null;

Queue<Customer> teller1 = new LinkedList<Customer>();
Queue<Customer> teller2 = new LinkedList<Customer>();
Queue<Customer> teller3 = new LinkedList<Customer>();

int tellerTime1=0, tellerTime2=0, tellerTime3 = 0;

incomingCustomer = Customer.parseCustomer( reader.nextLine() );

for (int time=1; time<=360; time++) {
System.out.println( String.format(\"%d:%02d\", time/60, time%60) );

if (incomingCustomer.arrivalTime==time) {

System.out.printf(\"\\tCustomer #%d arrives to %s\ \",
incomingCustomer.id, incomingCustomer.transaction );

System.out.printf(\"\\tCustomer #%d joins the queue with %d in line\ \",
incomingCustomer.id, queue.size() );

if(queue.size()<=29){
queue.offer( incomingCustomer ); // enqueue the customer
}
if (reader.hasNext()) {

incomingCustomer = Customer.parseCustomer( reader.nextLine() );
queueNew = Customer.parseCustomer( reader.nextLine() );
}
}

if (tellerTime1>0) { // teller is BUSY
tellerTime1 --;
if (tellerTime1==0) {
System.out.printf(\"\\tCustomer #%d completes %s.\ \",
servedCustomer.id, servedCustomer.transaction );
} else {
System.out.printf(\"\\tTeller is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime1, tellerTime1>1?\"s\":\"\" );
}
}
if(tellerTime2>0){
tellerTime2 --;
if(tellerTime2==0){
System.out.printf(\"\\tCustomer #%d completes %s.\ \",
servedCustomer.id, servedCustomer.transaction );
}else{
System.out.printf(\"\\tTeller is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime2, tellerTime2>1?\"s\":\"\" );
}
}
if(tellerTime3>0){
tellerTime3 --;
if(tellerTime3==0){
System.out.printf(\"\\tCustomer #%d completes %s.\ \",
servedCustomer.id, servedCustomer.transaction );
}else{
System.out.printf(\"\\tTeller is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime3, tellerTime3>1?\"s\":\"\" );
}
}else{
System.out.println(\"Teller is Idle\");
}



if(tellerTime1==0){
teller1.offer(queueNew);
if (!teller1.isEmpty() ) { // ... so get the first customer in the queue
int timeInQueue1;

servedCustomer = teller1.poll(); // dequeue the customer, i.e. poll() method
// does the dequeue operation

tellerTime1 = servedCustomer.transLength;
timeInQueue1 = time-servedCustomer.arrivalTime;

System.out.printf(\"\\tCustomer #%d leaves queue. Waited %d minutes in the queue.\ \",
servedCustomer.id, timeInQueue1 );

System.out.printf(\"\\tTeller1 is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime1, tellerTime1>1?\"s\":\"\" );

}
}
if(tellerTime2>0){
teller2.offer(queueNew);

if(!teller2.isEmpty()){
int timeInQueue2;
servedCustomer = teller2.poll();
tellerTime2 = servedCustomer.transLength;
timeInQueue2 = time-servedCustomer.arrivalTime;

System.out.printf(\"\\tCustomer #%d leaves queue. Waited %d minutes in the queue.\ \",
servedCustomer.id, timeInQueue2 );
System.out.printf(\"\\tTeller2 is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime2, tellerTime2>1?\"s\":\"\" );

}
}
if(tellerTime3>0){
teller3.offer(queueNew);

if(!teller3.isEmpty()){
int timeInQueue3;
servedCustomer = teller3.poll();
tellerTime3 = servedCustomer.transLength;
timeInQueue3 = time-servedCustomer.arrivalTime;

System.out.printf(\"\\tCustomer #%d leaves queue. Waited %d minutes in the queue.\ \",
servedCustomer.id, timeInQueue3 );
System.out.printf(\"\\tTeller3 is busy with Customer #%d. \" +
\"%s will be completed in %d minute%s.\ \",
servedCustomer.id, servedCustomer.transaction,
tellerTime3, tellerTime3>1?\"s\":\"\" );
}
}
}
reader.close();
}
}


class Customer {
int id;
int arrivalTime;
String transaction;
int transLength;

private Customer() {}

public static Customer parseCustomer(String data) {
String[] temp = data.split(\",\");
Customer c = new Customer();

c.id = Integer.parseInt( temp[0] );
c.arrivalTime = Integer.parseInt( temp[1] );
c.transaction = temp[2];

if (c.transaction.equals(\"DEPOSIT\")) {
c.transLength = 3;
} else if (c.transaction.equals(\"WITHDRAWAL\")) {
c.transLength = 5;
} else if (c.transaction.equals(\"UPDATE\")) {
c.transLength = 1;
} else { // NOT a valid TRANSACTION
c.transLength = 0;
throw new IllegalArgumentException(c.transaction + \" is NOT a valid transaction\");
}

return c;
}
}

Simple simulation (2 points) Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at
Simple simulation (2 points) Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at
Simple simulation (2 points) Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at
Simple simulation (2 points) Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at
Simple simulation (2 points) Use java.util.LinkedList to create a system to simulate customers being served at the bank.Assume there is only one service line at

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site