java设计一个单向队列式的排队管理系统,类似于售票窗口,所有购票者在窗口前排队,每有一个窗口前的人完成购票,则进行出队操作,后面的人整体前移。 本系统的角色要求有队列管理器和购票的人: 购票者: 需要有ID号和姓名。 队列管理器: 允许最多容纳10人的队列,建议内部使用数组进行管理。功能有:入队、出队、插队(插到某个指定ID的人之前)、查询某个ID的购票者排在第几位、让指定ID购票者直接出队(移除)。 统计当前正在有多少人排队、总计共有多少人排队、总计一共有多少人出队的功能。
时间: 2024-02-20 07:59:34 浏览: 147
好的,根据您的需求,我可以给您提供以下Java代码实现:
```
// 购票者类
class TicketBuyer {
private int id;
private String name;
public TicketBuyer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
// 队列管理器类
class QueueManager {
private static int MAX_SIZE = 10;
private TicketBuyer[] queue;
private int currentSize;
private int totalEnqueue;
private int totalDequeue;
public QueueManager() {
queue = new TicketBuyer[MAX_SIZE];
currentSize = 0;
totalEnqueue = 0;
totalDequeue = 0;
}
// 入队
public boolean enqueue(TicketBuyer buyer) {
if (currentSize < MAX_SIZE) {
queue[currentSize++] = buyer;
totalEnqueue++;
return true;
} else {
return false;
}
}
// 出队
public TicketBuyer dequeue() {
if (currentSize > 0) {
TicketBuyer buyer = queue[0];
for (int i = 1; i < currentSize; i++) {
queue[i - 1] = queue[i];
}
queue[currentSize - 1] = null;
currentSize--;
totalDequeue++;
return buyer;
} else {
return null;
}
}
// 插队
public boolean insertBefore(TicketBuyer buyer, int id) {
int index = -1;
for (int i = 0; i < currentSize; i++) {
if (queue[i].getId() == id) {
index = i;
break;
}
}
if (index >= 0) {
for (int i = currentSize - 1; i >= index; i--) {
queue[i + 1] = queue[i];
}
queue[index] = buyer;
currentSize++;
totalEnqueue++;
return true;
} else {
return false;
}
}
// 查询某个ID的购票者排在第几位
public int getPositionById(int id) {
for (int i = 0; i < currentSize; i++) {
if (queue[i].getId() == id) {
return i + 1;
}
}
return -1;
}
// 让指定ID购票者直接出队
public boolean removeById(int id) {
int index = -1;
for (int i = 0; i < currentSize; i++) {
if (queue[i].getId() == id) {
index = i;
break;
}
}
if (index >= 0) {
for (int i = index + 1; i < currentSize; i++) {
queue[i - 1] = queue[i];
}
queue[currentSize - 1] = null;
currentSize--;
totalDequeue++;
return true;
} else {
return false;
}
}
// 统计当前正在有多少人排队
public int getCurrentSize() {
return currentSize;
}
// 总计共有多少人排队
public int getTotalEnqueue() {
return totalEnqueue;
}
// 总计一共有多少人出队
public int getTotalDequeue() {
return totalDequeue;
}
}
// 测试类
public class QueueTest {
public static void main(String[] args) {
QueueManager manager = new QueueManager();
TicketBuyer buyer1 = new TicketBuyer(1, "张三");
TicketBuyer buyer2 = new TicketBuyer(2, "李四");
TicketBuyer buyer3 = new TicketBuyer(3, "王五");
TicketBuyer buyer4 = new TicketBuyer(4, "赵六");
TicketBuyer buyer5 = new TicketBuyer(5, "刘七");
manager.enqueue(buyer1);
manager.enqueue(buyer2);
manager.enqueue(buyer3);
manager.enqueue(buyer4);
manager.enqueue(buyer5);
System.out.println("当前排队人数:" + manager.getCurrentSize());
System.out.println("总共排队人数:" + manager.getTotalEnqueue());
System.out.println("总共出队人数:" + manager.getTotalDequeue());
System.out.println("查询ID为3的购票者排在第几位:" + manager.getPositionById(3));
manager.insertBefore(new TicketBuyer(6, "陈八"), 2);
System.out.println("当前排队人数:" + manager.getCurrentSize());
System.out.println("总共排队人数:" + manager.getTotalEnqueue());
System.out.println("总共出队人数:" + manager.getTotalDequeue());
System.out.println("查询ID为3的购票者排在第几位:" + manager.getPositionById(3));
manager.removeById(3);
System.out.println("当前排队人数:" + manager.getCurrentSize());
System.out.println("总共排队人数:" + manager.getTotalEnqueue());
System.out.println("总共出队人数:" + manager.getTotalDequeue());
System.out.println("查询ID为3的购票者排在第几位:" + manager.getPositionById(3));
while (manager.getCurrentSize() > 0) {
TicketBuyer buyer = manager.dequeue();
System.out.println("出队购票者:" + buyer.getName());
}
System.out.println("当前排队人数:" + manager.getCurrentSize());
System.out.println("总共排队人数:" + manager.getTotalEnqueue());
System.out.println("总共出队人数:" + manager.getTotalDequeue());
}
}
```
以上代码是一个简单的单向队列式的排队管理系统的实现,其中购票者类和队列管理器类分别用于表示购票者和队列管理器,测试类则用于测试队列管理器类的各种功能。
阅读全文