我正在用Java做一个项目,我需要创建一个队列方法。
每次有人被添加到房间时,都需要将它们添加到队列中。
我需要在程序中创建自己的队列对象,并在对象中编写方法以添加到队列并从队列中获取。
每次将客户添加到房间时,都应该使用 队列对象方法,将客户的名称添加到队列中。
当用户选择显示最后3个客户的名称时,代码应逐个从队列中删除它们(先进先出)并在删除时显示它们。
队列应该基于一个数组并保存7个项目。
当队列项到达数组末尾时,应将它们添加到开头或数组中。
如果队列已满,则应显示错误消息,并且最早的队列项应自动删除并显示。
这是主体:
package hotelcoursework2;
import java.util.*;
public class HotelCourseWork2 {
public static void main(String[] args) {
Room[] myHotel = new Room[10];
Queue mq = new Queue();
int guests = 0;
String roomName = null;
int roomNum = 0;
String letter = "";
for (int i = 0; i < myHotel.length; i++) {
myHotel[i] = new Room();
}
do {
System.out.println("Press E to display empty rooms");
System.out.println("Press A to add customers to room");
System.out.println("Press V to view all rooms ");
System.out.println("Press D to delete customers from rooms");
System.out.println("Or enter 1 to add to queue, 2 to take from queue or 3 display queue");
System.out.println("Press S to save");
System.out.println("Press L to load");
System.out.println("Press X to exit");
System.out.println("Enter a Letter: ");
Scanner scan = new Scanner(System.in);
letter = scan.next();
if (letter.equals("A")) {
add(myHotel, roomNum, roomName, guests);
mq.addqueue();
}
if (letter.equals("V")) {
view(myHotel);
}
if (letter.equals("E")){
empty(myHotel);
}
if(letter.equals("D")){
delete(myHotel, roomNum);
mq.takequeue();
}
if (letter.equals("S")){
}
if(letter.equals("3")){
mq.displayqueue();
}
} while (letter.charAt(0) != 'X');
System.out.println("Exit successfull");
}
private static void add(Room myHotel[], int roomNum, String roomName, int guests) {
Scanner input = new Scanner(System.in);
System.out.println("Enter room number (0-9) or 10 to stop:");
roomNum = input.nextInt();
if (roomNum == 10) {
System.out.println("Exit successful");
System.exit(0);
}
System.out.println("Enter name for room " + roomNum + " :");
roomName = input.next();
myHotel[roomNum].setName(roomName);
System.out.println("Enter how many guests: ");
guests = input.nextInt();
myHotel[roomNum].setGuestsInRoom(guests);
System.out.println("Add customers to queue: ");
for (int i = 0; i < 10; i++) {
System.out.println("Customer " + myHotel[i].getName() + " has been allocated to room " + i
+ " with " + myHotel[i].getGuestsInRoom() + " guests. ");
}
}
private static void view(Room myHotel[]) {
for (int i = 0; i < 10; i++) {
System.out.println("room " + i + " occupied by " + myHotel[i].getName() );
}
}
private static void empty(Room myHotel[]) {
for (int i = 0; i < 10; i++) {
if (myHotel[i].getName().equals("no-one")) {
System.out.println("room " + i + " is empty");
}
}
}
private static void delete(Room myHotel[], int roomNum){
Scanner input = new Scanner(System.in);
System.out.println("Enter room number you want to delete a customer from: ");
roomNum = input.nextInt();
myHotel[roomNum].setName("no-one");
}
}
这是我为队列创建的类,但现在我被卡住了。队列应该先进先出。
package hotelcoursework2;
import java.util.Scanner;
public class Queue {
// be careful - this queue can go over the end of the array
//the array should be used in a circular way
private static String qitems[] = new String[7];
private static int front = 0, end = 0;
static void addqueue() {
Scanner input = new Scanner(System.in);
System.out.print("Enter someone to the queue :");
qitems[end] = input.next();
end++;
}
static void takequeue() {
if (end > front) {
System.out.println("Person remove :" + qitems[front]);
front++;
} else {
System.out.println("Empty queue");
}
}
static void displayqueue() {
System.out.println("Queue display: ");
for (int look = front; look < end; look++) {
System.out.print(" " + qitems[look]);
}
System.out.println("");
}
}
答案 0 :(得分:0)
所以基本上你想知道如何实现一个简单的队列。
除非必须,否则使用数组会更复杂,因为您必须处理调整数组大小并移动其内容。
最简单的队列结构是一个链表,如下所示:
public class Queue<T>
{
private Node head;
private Node tail;
public void add(T value)
{
if (head == null)
{
head = new Node(null, value);
tail = head;
}
else
{
tail = new Node(tail, value);
}
}
public T peek()
{
return head == null ? null : head.value;
}
public T take()
{
if (head == null)
{
return null;
}
else
{
T value = head.value;
head = head.next;
return value;
}
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
Node current = head;
while (current != null)
{
stringBuilder.append(current.value).append(",");
current = current.next;
}
return stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "]").toString();
}
private class Node
{
private Node next;
private T value;
public Node(Node previous, T value)
{
this.value = value;
if (previous != null)
{
previous.next = this;
}
}
}
}
当添加新值并修改head
引用时,此类会引用tail
和Node
Node
调整head
链中的引用当取值时。