我正在尝试编写一种方法,其中客户必须与出纳员一起花费一定的时间。
如果第二个数量已过期,则应该返回出纳员是空闲的,因此返回布尔值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;
}
}
}
答案 0 :(得分:2)
public boolean isDone() {
long time = System.currentTimeMillis();
if (Integer.parseInt(time-arrivalTime)/1000 >= processTime) {
return true;
}
return false;
}
试试这个,你必须让你的到达时间到long
将构造函数中的arrivalTime
初始化为
arrivalTime = System.currentTimeMillis();
答案 1 :(得分:0)
您可以使用System.currentTimeMillis()
。
long start = System.currentTimeMillis();
// ...
long current = System.currentTimeMillis();
long elapsed = current - start;
要将其转换为秒,只需除以1000。