递减存储在队列中的值 [英] Decrementing value stored in a queue

查看:58
本文介绍了递减存储在队列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个队列项目,该项目模拟了一家杂货店.在我的程序中,我有一个方法调用,它设置一个随机变量,表示为队列中的客户提供服务所需的时间.总迭代次数为 60,表示分钟.假设第一个客户有 4 分钟的等待时间,我需要能够在每分钟后减少时间,直到它达到 0.我不知道如何减少名为 myQueue.有什么建议可以减少每分钟后存储在队列中的值吗?

I'm working on a queue project where the program is simulating a grocery store. In my program, I have a method call that sets up a random variable that represents the time that it takes to service the customer in queue. The total iterations are 60, signifying minutes. Say if the first customer is given a 4 minute wait time, I need to be able to decrement the time after each minute until it reaches 0. I cannot figure out how to decrement the value stored in the queue named myQueue. Any suggestions how I can decrease the value stored in the queue after each minute?

import java.util.*;
import java.util.Random;

public class GroceryStore{
public static void main (String[] args){

int newCust=0;  //to hold random variable 1-4 for 25% chance of new customer
Queue<Integer> myQueue = new LinkedList<Integer>(); //instantiates new queue
int wait = 0;
int numCust = 0;                        //holds counter for number of  customer             

for (int i = 1; i <= 60; i++)  //iterator to cycle through 60 minutes
{

    Random randomNum = new Random();    
    newCust = randomNum.nextInt(4)+1;  //gives random #1-4, if 1, new cust added

    if(newCust == 1)                            //if statement to execute code if new cust added
    {
        Customer cust = new Customer();
        wait = cust.getServiceTime();                                           //stores wait time in variable
        myQueue.add(wait);                                                      //adds customer to the queue by wait time
        System.out.println("New customer added to queue, queue length is now " + myQueue.size());                       
    }

    if(myQueue.isEmpty())                                       //if to check if queue is empty and skip other conditionals
        System.out.println("-----------");
    else if(myQueue.peek()==0)                                  //if top of queue is at 0, remove from queue
    {
        myQueue.remove();
        System.out.println("Customer removed");
    }
    else    
          //THIS IS WHERE I AM TRYING TO DECREASE THE VALUE IN THE TOP QUEUE
}

推荐答案

Integer 是不可变的,所以在你自己的类中包装一个 int :

Integer is immutable, so wrap an int in your own class:

class Customer {

    int time;

    public Customer(int time) {
        this.time = time;
    }

    // getter, setter
}

并定义一个对应的Queue:

Queue<Customer> myQueue = new ...;

实例化一个java.util.Timer;在相应的 java.util.TimerTask 中,使用 for-each 循环遍历 Queue,依次更改或删除每个:

Instantiate a java.util.Timer; in the corresponding java.util.TimerTask, iterate through the Queue using a for-each loop, altering or removing each in turn:

for (Customer c : myQueue) { ... }

这篇关于递减存储在队列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆