java - 如何让程序连续处理方法
问题描述
我的问题是:为什么我的程序在处理文本文件中的行时不重复。
我正在编写一个管理车库的类-车库中的汽车被放入不超过 7 个的堆栈中,如果已满,它们将进入不超过 5 个的队列。有手动添加汽车的方法和一种用于读取文本文件的输入。我还获得了一个 stackInt 类,我被要求在创建堆栈对象时使用它。它只有 4 种方法,这让事情变得很麻烦:
public interface StackInt<E> {
E push(E obj);
E peek();
E pop();
boolean empty();
}
所以我不得不用一些方法来编写我的代码来规避没有其他有用的堆栈方法。我的大多数方法都按预期工作:它读取文本文件并对其进行处理,但是如果例如从车库中移除一辆车,则应该插入队列中的另一辆车,它不会重复该过程循环。这是我的代码:
import java.util.*;
import java.util.LinkedList;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class Garage{
public String line;
public String stackToString;
public int position;
public int numAdded;
LinkedList<String> queue = new LinkedList<>();
StackInt<String> stack = new LinkedStack<>();
public Garage(){
}
public Garage(String fileName){
try{
File file = new File(fileName);
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
line = scan.nextLine();
String[] data = line.split(" ");
if (data[0].equals("a"))
arrival(data[1]);
else if (data[0].equals("d"))
departure(data[1]);
}
}
catch(FileNotFoundException e) {
System.out.println("File not found");
}
}
public boolean arrival(String license){
boolean added = false;
if(numAdded < 7){
stack.push(license);
added = true;
} else if (queue.size() < 5) {
added = queue.add(license);
numAdded--;
}
if(added){
numAdded++;
}
return true;
}
public int departure(String license){
Stack<String> temp = new Stack();
while(!stack.empty()){
temp.push(stack.pop());
}
position = temp.indexOf(license);
temp.remove(license);
while(!temp.isEmpty()){
stack.push(temp.pop());
}
return position;
}
public int numberParked(){
return numAdded;
}
public int numberWaiting(){
return queue.size();
}
public String toString(){
Stack<String> tempStack = new Stack();
while (!stack.empty()){
tempStack.push(stack.pop());
stackToString = tempStack.toString().replace("[", "").replace("]", "");
}
while (!tempStack.empty()){
stack.push(tempStack.pop());
}
return "Cars in Garage: " + stackToString + "\n" + "Cars waiting: " + (queue.toString().replace("[", "").replace("]", ""));
}
}
我正在测试的是:
public class GarageTest
{
public static void main (String [] args)
{
Garage g1 = new Garage("parking.txt");
System.out.println("Number parked: " + g1.numberParked());
System.out.println("Number waiting: " + g1.numberWaiting());
System.out.println("Parking WEB445 ... " + g1.arrival("WEB445"));
System.out.println("Parking BEA345 ... " + g1.arrival("BEA345"));
System.out.println(g1);
System.out.println("Z23YTU departs after " + g1.departure("B12GFT") + " car(s) moved");
System.out.println(g1);
}
}
输出是:
Number parked: 7
Number waiting: 5
Parking WEB445 ... true
Parking BEA345 ... true
Cars in Garage: Y23456, X12345, B12GFT, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL
Z23YTU departs after 2 car(s) moved
Cars in Garage: Y23456, X12345, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL
“车库里的汽车”线的预期是:
DFGHJK CVBNMK R23EWQ W321RE Y23456 X12345 B12GFT Z23YTU
with the queue on the next line. It's removing a few that have "d", but not filling the garage
afterward
正在处理的文本文件(a 表示到达,d 表示离开):
a A123TR
a Z23YTU
a Z23YTU
a ERW345
d ERW345
a B12GFT
d a23TR
a X12345
a Y23456
a W321RE
d R23EWQ
a CVBNMK
a DFGHJK
a ERTYUI
a FGHJKL
a GHJKL9
a HJKL98
解决方案
When you say "if a car is removed from the garage, another car from the queue is supposed to be inserted" - where is this logic in your departure() method? There isn't any code in that method that removes a car from the queue?
The behavior you're expecting is missing from your code.
推荐阅读
- go - 为什么我的普罗米修斯标签不显示?
- android - 当我单击离开然后返回 Android Studio 时如何保留片段的状态
- python - AssertAlmostEqual 表示字典中的值
- python - 在 Python 中,如何从列表中删除某些数据以创建新数据?
- excel - 如果某些数字不存在,VBA Excel 删除行
- c# - 尝试在调试中查看属性会在时间延迟后给出不同的计数。这是因为延期执行吗?
- java - SFTP 连接器 DH 密钥错误
- c# - 执行单元测试时 C# 配置管理器引用 machine.config 而不是 app.config
- node.js - 当 headers 为 true 时,fast-csv 返回未定义的数据
- java - 单击按钮(清单按钮)后,“应用程序名称”已停止