2015-04-07 63 views
0

我正在嘗試編寫一種方法,在該方法中,客戶必須與出納員一起花費一定的時間。如何創建一個布爾方法來確定是否有足夠的秒數已經通過

如果秒數已過期,則應返回出納員空閒並因此返回布爾值true。如果時間沒有超過,它應該返回false,因此出納員很忙。但我不確定如何做到這一點。

這些是我的出納員和客戶類我不確定如何編寫其中一種方法。

這是Customer類:

import java.util.Random; 

public class Customer { 
    //create a random number between 2 and 5 seconds it takes to process a customer through teller 
    Random number = new Random(); 

    protected int arrivalTime; //determines the arrival time of a customer to a bank 
    protected int processTime; //the amount of time the customer spend with the teller 

    //a customer determines the arrival time of a customer 
    public Customer(int at) { 
     arrivalTime = at; 
     processTime = number.nextInt(5 - 2 + 2) + 2; //the process time is between 2 and 5 seconds 
    } 

    public int getArrival() { 
     return arrivalTime; 
    } 

    public int getProcess() { 
     return processTime; //return the processTime 
    } 

    public boolean isDone() { 
     //this is the method I am trying to create once the process Time is expired 
     //it will return true which means the teller is unoccupied 

     //if the time is not expired it will return false and teller is occupied 
     boolean expired = false; 
     return expired; 
    } 
} 

這是Teller類:

public class Teller { 
    //variables 
    boolean free; //determines if teller if free 
    Customer rich; //a Customer object 

    public Teller() { 
     free = true; //the teller starts out unoccupied 
    } 

    public void addCustomer(Customer c) { //a customer is being added to the teller 
     rich = c; 
     free = false; //the teller is occupied 
    } 

    public boolean isFree() { 
     if (rich.isDone() == true) { 
      return true; //if the customer is finished at the teller the return true 
     } else { 
      return false; 
     } 
    } 
} 
+1

可能的重複[如何測量Java中的時間?](http://stackoverflow.com/questions/17700 10/how-do-i-measure-time-elapsed-in-java) – Obicere

+0

我認爲,但我的是相對於布爾事物.. –

+0

它更像是如果足夠有秒已經過去做這個,如果不這樣做 –

回答

0

您可以使用System.currentTimeMillis()

long start = System.currentTimeMillis(); 
// ... 
long current = System.currentTimeMillis(); 
long elapsed = current - start; 

並把它轉換成秒,只是1000

2
public boolean isDone() { 
    long time = System.currentTimeMillis(); 

    if (Integer.parseInt(time-arrivalTime)/1000 >= processTime) { 
     return true; 
    } 
    return false; 
} 

劃分試試這個,你必須讓你的arrivalTime到long

在構造函數初始化arrivalTimearrivalTime = System.currentTimeMillis();

相關問題