Java 使用队列的超市模拟器
我早些时候发布了这个问题,并就如何解决这个问题寻求了帮助。我最终创建了一个customer类,该类生成一个随机的名字和姓氏,并在每次创建新customer对象时将随机数量的杂货项目分配给客户 这是代码Java 使用队列的超市模拟器,java,oop,data-structures,queue,Java,Oop,Data Structures,Queue,我早些时候发布了这个问题,并就如何解决这个问题寻求了帮助。我最终创建了一个customer类,该类生成一个随机的名字和姓氏,并在每次创建新customer对象时将随机数量的杂货项目分配给客户 这是代码 import java.util.Random; public class Customer { private String lastName; private String firstName; private int groceryItems; priv
import java.util.Random;
public class Customer {
private String lastName;
private String firstName;
private int groceryItems;
private String[] last = { "Jordan", "James", "Bryant", "Bird", "Wade",
"Bosh", "Griffin", "Durant", "WestBrook", "Anthony" };
private String[] first = { "Michael", "Lebron", "Kobe", "Larry", "Dwayne",
"Chris", "Blake", "Kevin", "Russell", "Carmelo" };
public Customer() {
Random pickLast = new Random();
lastName = last[pickLast.nextInt(10)];
Random pickFirst = new Random();
firstName = first[pickFirst.nextInt(10)];
Random pickItems = new Random();
groceryItems = pickItems.nextInt(25);
}
public String getlast() {
return lastName;
}
public String getFirst() {
return firstName;
}
public int getItems() {
return groceryItems;
}
public void display() {
System.out.println("First Name: " + firstName + " Last Name: "
+ lastName + " Items Purchased: " + groceryItems);
}
}
队列代码
public class Queue {
private int maxSize;
private int[] queArray;
private int front;
private int rear;
private int nItems;
public Queue(int s) {
maxSize = s;
queArray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void insert(int j) {
if (rear == maxSize - 1)
rear = -1;
queArray[++rear] = j;
nItems++;
}
public int remove() {
int removed = queArray[front++];
if (front == maxSize)
front = 0;
nItems--;
return removed;
}
public int peek() {
return queArray[front];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
public int size() {
return nItems;
}
public void display() {
System.out.println("First Inserted Item to Last Inserted Item");
if (rear < front && (!isEmpty())) {
for (int i = front; i < maxSize; i++) {
System.out.println(queArray[i]);
}
for (int i = 0; i <= rear; i++) {
System.out.println(queArray[i]);
}
}
else if (front >= rear && (!isEmpty())) {
for (int i = front; i <= rear; i++) {
System.out.println(queArray[i]);
}
}
else {
System.out.println("Queue is Empty!");
}
}
}
公共类队列{
私有int-maxSize;
私有int[]数组;
私人内部阵线;
私家车;
私人机构;
公共队列(INTS){
maxSize=s;
queArray=newint[maxSize];
正面=0;
后部=-1;
nItems=0;
}
公共空白插入(int j){
如果(后==最大尺寸-1)
后部=-1;
数组[++后]=j;
nItems++;
}
公共int-remove(){
int removed=queArray[front++];
如果(前==maxSize)
正面=0;
硝酸盐--;
移除返回;
}
公共int peek(){
返回数组[前];
}
公共布尔值为空(){
返回值(nItems==0);
}
公共布尔值isFull(){
返回值(nItems==maxSize);
}
公共整数大小(){
返回nItems;
}
公共空间显示(){
System.out.println(“第一个插入项到最后一个插入项”);
如果(后<前&(!isEmpty())){
对于(int i=front;i 对于(inti=front;i现在您应该添加不同的人员队列(以模拟不同的结账队列)。因此,我要做的是为您想要的每一个结账行创建一个队列。然后,正如问题所述,当按下一个键时,它会创建一个新客户。客户需要确定要转到哪一行,所以可能会将其分配给项目总数最少的行(或者如果您想让它更简单,则分配给总人数),并且在多个最短队列的情况下,优先选择最靠近的,例如左侧。然后,您需要让客户购买他们的物品,然后离开。如果您决定按按钮,如问题所示,您可以将其设置为每次按下该按钮时,都在每个客户中调用一个方法(仅限行开始处的项目,数组索引为0)将其项目减少1,并在其达到0时从队列中删除customer对象,以模拟它们离开,另一个客户取而代之。现在您需要以某种方式向用户显示这些内容。也许只需简单的“[客户名称]加入该行。当前的排队是:第1行:2人第2行:5人“等,还有一个“[客户姓名]在购买食品杂货后离开了该行。”
我希望这是您想要的,也希望它能有所帮助。您需要开始完成问题的队列部分,添加将客户添加到队列(结账通道)的功能,并添加从队列中删除它们的功能,以及添加查看每个队列状态的功能。是的,我认为这是我需要做的下一件事,只是不知道我必须创建什么样的队列?到目前为止,我只编写了创建int-queue类型的代码,其中添加了随机数。在我的情况下,我将在队列中添加客户我需要的是一个字符串队列吗?你很可能需要一个客户
队列。我建议将设置为最后一个和第一个静态
@user2733436你可能需要一个Deque,除非你被要求对你自己的队列进行编码。它要求我从上一章开始排队,我刚刚发布了队列的代码。我是但是很困惑,因为队列是int类型,所以我应该如何使用此队列来存储客户?我是否更改其类型?@user2733436对不起,我以为您使用的是Java API中的队列。您可以为每个客户分配一个特定的编号,并使用该编号。这可能是解决此问题的一个巧妙方法,您能详细说明吗?@user2733436 somewhere在Customer类中,创建一个公共int,该int对于每个客户都应该是唯一的(每次创建客户时递增一个)。然后,您可以在队列中使用该数字,并且无论何时需要从Customer类中获取内容,都可以遍历您的客户对象(可能存储在ArrayList中?)找到必要的方法。这绝对不是一种优雅的方法,但是如果你想使用Queue类,我认为这是一种很好的方法。我认为我必须在customer类之外创建唯一的数字int,对吗?如果我在customer类内创建它,它将不起作用。我感谢你的解释,我仍然是n不知道如何使用此方法将客户添加到队列中。。。