2014-10-25 2 views
0

Так что мне нужно создать проект симулятора банка для класса, и до сих пор у меня есть несколько методов, которые были реализованы. Но в настоящее время у меня возникают проблемы с методом doBusiness в классе ServiceCenter. Вот код, который у меня есть.Банковский симулятор - doBusiness метод help LinkedList/Queue

Главная: BankSimulator

import java.util.Scanner; 

public class BankSimulator { 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.print("What is the time duration (in minutes) to be simulated? "); 
    int maxTime = input.nextInt(); 
    System.out.print("What percentage of the time (0-100) does a customer arrive? "); 
    int arrivalProb = input.nextInt(); 

    ServiceCenter bank = new ServiceCenter(); 
    while (bank.getTime() <= maxTime || bank.customersRemaining()) { 
     if (bank.getTime() <= maxTime && customerArrived(arrivalProb)) { 
      bank.addCustomer(); 
     } 
     bank.doBusiness(); 
    } 
    //bank.displayStats(); 
} 

private static boolean customerArrived(int prob) 
{ 
    return (Math.random()*100 <= prob); 
} 
} 

класс сервера:

public class Server { 
private static int nextID = 1; 
private int serverID; 
private int endTime; 
private Customer serving; 

public Server() 
{ 
    serverID = nextID; 
    nextID++; 
    endTime = 0; 
    serving = null; 
} 

/** 
* Accessor method for getting the server ID number. 
* @return the server ID 
*/ 
public int getID() 
{ 
    return serverID; 
} 

/** 
* Assigns a customer to the server and begins the transaction. 
* @param c the new customer to be served 
* @param time the time at which the transaction begins 
*/ 
public void startCustomer(Customer c, int time) 
{ 
    serving = c; 
    endTime = time + c.getJobLength(); 
} 

/** 
* Accessor method for getting the current customer. 
* @return the current customer, or null if no customer 
*/ 
public Customer getCustomer() 
{ 
    return serving; 
} 

/** 
* Identifies the time at which the server will finish with 
* the current customer 
* @return time at which transaction will finish, or 0 if no customer 
*/ 
public int busyUntil() 
{ 
    return endTime; 
} 

/** 
* Finishes with the current customer and resets the time to completion. 
*/ 
public void finishCustomer() 
{ 
    serving = null; 
    endTime = 0; 
} 
} 

класс Customer:

import java.util.Random; 

public class Customer { 
private static final int MAX_LENGTH = 8; 
private static final Random rand = new Random(); 
private static int nextID = 1; 

private int customerID; 
private int arrivalTime; 
private int jobLength; 

/** 
* Constructs a customer with the next available ID number, 
* the specified arrival time, and a random job length. 
* @param arrTime the time at which the customer arrives 
*/ 
public Customer(int arrTime) 
{ 
    customerID = nextID; 
    nextID++; 
    arrivalTime = arrTime; 
    jobLength = rand.nextInt(MAX_LENGTH)+1; 
} 

/** 
* Accessor method for getting the customer's ID number. 
* @return the customer ID 
*/ 
public int getID() 
{ 
    return customerID; 
} 

/** 
* Accessor method for getting the customer's arrival time. 
* @return the time at which the customer arrived 
*/ 
public int getArrivalTime() 
{ 
    return arrivalTime; 
} 

/** 
* Accessor method for getting the length of the job 
* @return the job length (in minutes) 
*/ 
public int getJobLength() 
{ 
    return jobLength; 
} 
} 

мне пришлось создать следующий класс с именем ServiceCenter где GetTime возвращает время в которое начинается с 0 и увеличивается на каждом шаге. Метод addCustomer, в котором мы добавляем клиента в очередь и выводим сообщение. Клиент, который возвращает true, если клиент в настоящее время ждет. Наконец, метод doBusiness, который увеличивает время, если сервер закончил с клиентом, удалит их из очереди, и если сервер свободен и в очереди есть клиент, начните обслуживать их. У меня есть первые несколько, за исключением метода doBusiness, на который я застрял. У кого-нибудь есть подсказки?

Код, указанный выше, был создан Дейвом Ридом, и я не беру на себя ответственность за код.

класс ServiceCenter:

import java.util.LinkedList; 
import java.util.Queue; 

public class ServiceCenter { 

private int arrivalTime, departureTime; 
private Queue <Customer> customer; 
private Server server; 
public ServiceCenter() 
{ 
    server = new Server(); 
    customer = new LinkedList <Customer>(); 
} 

public String addCustomer() 
{ 
    Customer customer1 = new Customer (arrivalTime); 
    customer.add(customer1); 
    String result = "" + customer1.getArrivalTime() + "" + customer1.getID() + "" + customer1.getJobLength(); 
    return result; 
} 

public void doBusiness() 
{ 
    if(!customer.isEmpty()) 
    { 
     if(server == null) 
     { 
      customer.remove(); 
     } 
    } 
} 

public int getTime() 
{ 
    return departureTime - arrivalTime; 
} 

public boolean customersRemaining() 
{ 
    if (!(customer.isEmpty())) 
     return true; 
    else 
     return false; 
} 
} 

Любая помощь на всех или советы будут весьма благодарны. Я пытался понять это уже неделю, но почему-то очереди - это моя слабость. Спасибо за чтение и жаль, что он так долго. Просто хотел, чтобы он был подробным.

ответ

0

Вы используете new Customer (arrivalTime);, но в вашем конструкторе класса ServiceCenter у вас нет даты прибытия.