代码如下
有待改进 请指教
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static LinkedList<String> queue = new LinkedList<String>();
public static LinkedList<String> vipqueue = new LinkedList<String>();
public static int count = 0;
public static int countcall = 0;
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String str = "";
boolean flag = false;
while(!flag){
str = s.nextLine();
if(str.contains("get")){
get(str);
}else if(str.contains("call")){
call();
}else if(str.contains("delete")){
delete(str);
}else if(str.equals("count")){
count();
}else if(str.contains("countN")){
countn(str);
}else if(str.equals("reset")){
reset();
}else if(str.equals("quit")){
flag = true;
}else{
System.out.println("error");
}
}
s.close();
}
//取号
public static void get(String str){
count++;
if(str.equals("get")){
queue.add(String.valueOf(count));
System.out.println(count);
}else if(str.equals("get vip")){
vipqueue.add("vip" + String.valueOf(count));
System.out.println("vip " + count);
}else{
System.out.println("error");
}
}
//叫号
public static void call(){
if(!queue.isEmpty() || !vipqueue.isEmpty()){
countcall++;
if(countcall == 6){
System.out.println("error");
}else{
if(!vipqueue.isEmpty()){
System.out.println("vip " + vipqueue.remove(0));
}else{
System.out.println(queue.remove(0));
}
}
}
else{
System.out.println("error");
}
}
//删除
public static void delete(String str){
String[] str2 = str.split(" ");
if(str2.length != 2 ||!str2[1].matches("\\d")){
System.out.println("error");
return;
}else{
for(int i = 0; i < vipqueue.size(); i++){
if(vipqueue.get(i).contains(str2[1])){
System.out.println(vipqueue.remove(i));
return;
}
}
for(int i = 0; i < queue.size(); i++){
if(queue.get(i).contains(str2[1])){
System.out.println(queue.remove(i));
return;
}
}
System.err.println("error");
return;
}
}
//获取当前排队人数
public static void count() {
System.out.println(queue.size() + vipqueue.size());
return;
}
//获得获取号码之前的总人数
public static void countn(String str){
String[] str2 = str.split(" ");
if(str2.length != 2 ||!str2[1].matches("\\d")){
System.out.println("error");
return;
}else{
for(int i = 0; i < vipqueue.size(); i++){
if(vipqueue.get(i).contains(str2[1])){
System.out.println(i);
return;
}
}
for(int i = 0; i <queue.size(); i++){
if(queue.get(i).contains(str2[1])){
System.out.println(vipqueue.size() + i);
return;
}
}
System.out.println("error");
return;
}
}
//重置
public static void reset(){
queue.clear();
vipqueue.clear();
count = 0;
countcall = 0;
return;
}
}