james Chol
james Chol

Reputation: 53

How to create a boolean method that determines if enought second have passed

I am trying to write a method in which a customer has to spend a certain amount of second with a teller.

If the amount of second has expired it should return that the teller is free and thus return a Boolean value true. If the amount of time has not yet passed it should return false and thus the teller is busy. But I am unsure how to do this.

These are my teller and customer classes I am not sure how write one of the methods.

This is the Customer class:

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;
    }
}

And this is the Teller class:

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;
        }
    }
}

Upvotes: 0

Views: 503

Answers (2)

pikachu
pikachu

Reputation: 63

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

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

Try this, and you have to make your arrivalTime to long

initialize arrivalTime in the constructor as arrivalTime = System.currentTimeMillis();

Upvotes: 2

Bubletan
Bubletan

Reputation: 3863

You can use System.currentTimeMillis().

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

And to convert it to seconds, just divide by 1000.

Upvotes: 0

Related Questions