user9409047
user9409047

Reputation:

Printing out each increment of variable

I have a service that allows a user to "toast" bread.

It's below

public void Toasting(){
        if(breadLevel >= 0){
            while(temp < 5){
                toasting = true;
                temp++;
            }

        } else {
            toasting = false;
        }
        System.out.println(breadLevel);
    }

So if their is bread is the toaster, and while the temp is less then 5, toasting is happening, and increment the temp by one.

How would Increase the temp variable by 1 every 2 seconds.

Also I want to send a status update to the user. So each time the temp is incremented the user is notified.

@Override
    public String getStatus() {
        String message = "";

        if(toasting = true){
            message = "The current temp is" +temp;
        }

        return message;
    }

This get status method only prints when the temp reaches 5. Instead of incremently informing the user.

Upvotes: 0

Views: 54

Answers (1)

Lev Leontev
Lev Leontev

Reputation: 2615

To wait 2 seconds in Java use

Thread.sleep(2000);

For example:

if(breadLevel >= 0){
    while(temp < 5){
        toasting = true;
        temp++;
        System.out.println(getStatus())
        Thread.sleep(2000)
    }
    toasting = false; //no longer toasting so set to false
}

Upvotes: 1

Related Questions