循环文件读取器
我必须逐行读取文本文件,除了在 7 行之后我想通过将 7 行分配给 7 个不同的变量来使用读取的内容。一旦我分配了它们,我想使用这些变量。这是简单的部分,我知道该怎么做,但让我困惑的是,在使用了 7 个变量之后,我希望能够将文本文件的接下来 7 行分配给相同的变量,并使用它们再次。我知道我必须使用循环,但是,
- 如何让它在 7 行后停止,而不是仅仅将这些行重新分配给变量?
- 我应该使用 2 个循环吗?
- 或者我可以应用类似休息或某种“暂停”的东西吗?
- 或者是否有某种计数器可以与 FileReader 类一起使用?
- 或者我应该创建一个?
这就是我们老师想要的。
停车罚单
该怎么做: 本作业基于第 574 页的编程挑战问题 8,并进行了一些修订,因此请仔细阅读本讲义。如果您愿意,您可以以 2 人团队的形式工作。合作和讨论将会有所帮助。注意:这是本课程中最长的,也可能是最困难的课程,因此请仔细学习并遵循说明。
对于此作业,您将设计一组类,这些类一起工作来模拟停车员向停在咪表处的一些汽车发出停车罚单。与真实的计价器和票据的工作方式相比,这个问题可能有点人为。
您将设计四个独立的类和一个演示程序来运行所有内容。在开始编写 Java 代码之前,请先阅读整个作业。您需要了解几个类的结构并了解数据文件的组织方式。
基本设置是一组停车计时器,汽车在其中停放不同的时间。
每辆车都会购买一些以分钟记录的停车时间。每辆车都会停放一定的时间,也以分钟为单位记录。有些人的停车时间比购买的时间长,有些人的停车时间比购买的时间短。
以下是您需要开发的四个课程。 • ParkedCar 类:该类应模拟停放的汽车。该类的职责如下: - 了解汽车的品牌、型号、颜色、车牌号码 - 了解其停车计时器的身份号码。 - 有一个 toString() 方法来报告其识别信息
• ParkingMeter 类:该类将模拟停车计时器。该类的职责如下: - 了解自己的电表身份号码 - 了解已购买的分钟数 - 了解停车分钟数 - 有一个 toString() 方法来报告其信息
• ParkingOfficer 类:该类应模拟停车官员检查停放的汽车。该类的职责如下: - 有两个实例变量:一个用于姓名,一个用于军官的徽章号码 - 有一个构造函数,其中两个参数分别为停车官员的姓名和徽章号码 - 有一个 toString() 方法 - 有一个 exam() 方法,该方法接收 ParkedCar 对象和 ParkingMeter 对象作为参数。该方法判断时间是否已过期 如果时间已到,它将创建一个停车票对象,该对象作为检查()的返回值返回。 如果时间尚未到期,则返回 null。
• ParkingTicket 类:该类将模拟停车罚单。该类的职责如下: - 有一个构造函数,它接收 ParkedCar 对象、ParkingMeter 对象和 ParkingOfficer 对象作为参数,这些对象用于初始化类中相应的实例变量。请务必对每个参数对象使用复制构造函数。 - 该类还有一个用于罚款金额的实例变量。 - 罚款由 ParkingTicket 构造函数计算: 非法停放汽车的第一个小时或不足一小时罚款为 25 美元,非法停放车辆每超过一小时或不足一小时罚款 10 美元。 如果时间尚未到期,则罚款为零。 - 有一个 toString() 方法,可以按照您设计的有用格式准备罚单(尝试匹配下面显示的示例输出或进行自己的改进。它必须报告非法停车的品牌、型号、颜色和许可证号它还报告罚款金额以及开具罚单的警察的姓名和徽章号码。在这个 toString() 方法中,您必须有效地使用停车罚单中其他类的 toString() 方法。 ParkedCar、停车计时器、和停车官员)
测试班 使用 main() 编写一个测试类 TicketDemo,以使用上述类运行模拟,如下所示: • 创建一个PoliceOfficer 对象。填写您自己的姓名和徽章号码。您可以将其硬连接到您的测试程序中或要求用户输入数据(您的选择)。 • 在循环中从数据文件Asg-5_in.txt 读取数据。 (注:文件Asg-5_inExplain.txt描述了如何解释文件内容。) o 读取汽车和仪表的一个数据块,并创建 ParkedCar 对象和 ParkingMeter 对象 o 使用您的 ParkedCar 和停车计时器对象作为参数来调用停车官员的 exam() 方法。检查方法将返回票证或 null。 o 如果返回票证,请调用 toString() 方法在屏幕上打印票证。 o 如果没有开票,则打印有关汽车的识别信息(使用其 toString() 方法。 o 重复循环,直到输入文件中不再有可用数据为止。
这是我到目前为止所做的,尽管我还远远没有完成。
ParkedCar 类:
public class ParkedCar{
private String make;
private String model;
private String color;
private String license;
private String meterID;
public String getMake(String make)
{
this.make = make;
return make;
}
public String getModel(String model)
{
this.model = model;
return model;
}
public String getColor(String color)
{
this.color = color;
return color;
}
public String getLicense(String license)
{
this.license = license;
return license;
}
public String getMeterID(String meterID)
{
this.meterID = meterID;
return meterID;
}
public String toString()
{
String str = "\nMake: " + make + "\nModel: " + model + "\nColor: " + color +
"\nLicense: " + license + "\nMeter ID: " + meterID;
return str;
}
}
ParkingMeter 类:
public class ParkingMeter{
private String meterID;
private int minsPurchased;
private int minsParked;
public String getMeterID(String meterID)
{
this.meterID = meterID;
return meterID;
}
public int getMinsPurchased(int minsPurchased)
{
this.minsPurchased = minsPurchased;
return minsPurchased;
}
public int getColor(int minsParked)
{
this.minsParked = minsParked;
return minsParked;
}
public String toString()
{
String str = "\nMeter ID: " + meterID + "\nMinutes Purchased: " + minsPurchased +
"\nMinutes Parked: " + minsParked;
return str;
}
}
ParkingOfficer 类:
public class ParkingOfficer{
private String officerName;
private String badgeNumber;
public ParkingOfficer(String officerName, String badgeNumber)
{
this.officerName = officerName;
this.badgeNumber = badgeNumber;
}
public ParkingOfficer(ParkingOfficer object2)
{
officerName = object2.officerName;
badgeNumber = object2.badgeNumber;
}
public void setParkingOfficer(String officerName, String badgeNumber)
{
this.officerName = officerName;
this.badgeNumber = badgeNumber;
}
public String toString()
{
String str = "\nOfficer's Name: " + officerName + "\nOfficer's Badge Number: " + badgeNumber;
return str;
}
}
I have to read a text file line by line, except after 7 lines I want to use what's read by assigning the 7 lines to 7 different variables. Once I've assigned them, I want to use the variables. That is the easy part, which I know how to do, what has me stuck is that after I have used the 7 variables, I want to be able to assign the next 7 lines of my text file to the same variables, and use them again. I know I have to use loop(s), but ,
- how do I make it stop after 7 lines instead of just re-assigning the lines to the variables?
- Should I use 2 loops?
- Or is there something similar to a break or maybe some sort of "pause" I can apply?
- Or is there a some sort of counter that can be used with the FileReader class?
- Or should I create one?
Here is what our teacher wants.
Parking Tickets
What to Do:
This assignment is based on Programming Challenge problem-8 on page 574 with some revisions so read this handout carefully. You are allowed to work in teams of 2 people if you desire. The collaboration and discussion would be helpful. Note: This is the longest and likely the most difficult program of the course so work carefully and follow the instructions.
For this assignment you will design a set of classes that work together to simulate a parking officer issuing parking tickets to some cars parked at meters. The problem may be a little artificial when compared to how real meters and tickets work.
You will design four separate classes and a demo program to run everything. Read this whole assignment to the end before starting to write Java code. You need to understand the structure of several classes and understand how the data file is organized.
The basic setting is a group of parking meters with cars parked at them for different amounts of time.
Each car will have purchased some parking time recorded in minutes. And each car will have parked for a certain length of time also recorded in minutes. Some will have parked for more time than they purchased and some for less time than purchased.
Here are the four classes you are required to develop.
• The ParkedCar Class: This class should simulate a parked car. The class’s responsibilities are as follows:
- To know the car’s make, model, color, license number
- To know the identity number of its parking meter.
- Has a toString() method to report its identifying information
• The ParkingMeter Class: This class will simulate a parking meter. The class’s responsibility is as follows:
- to know its own meter identity number
- To know the number of minutes that have been purchased
- To know the number of minutes parked
- has a toString() method to report its information
• The ParkingOfficer Class: This class should simulate a parking officer inspecting parked cars. The class’s responsibilities are as follows:
- Has two instance variables: one for name and one for badge number of the officer
- Has a constructor with two parameters for the parking officer’s name and badge number
- Has a toString() method
- Has an examine() method which receives a ParkedCar object, and a ParkingMeter object as parameters. This method determines whether the time has expired or not
If time has expired it creates a parking ticket object which is returned as the return value from examine().
If time has not expired it returns null.
• The ParkingTicket Class: This class will simulate a parking ticket. The class’s responsibility is as follows:
- Has a constructor which receives as parameters a ParkedCar object, a ParkingMeter object and a ParkingOfficer object which are used to initialize corresponding instance variables in the class. Be sure to use copy constructors for each of these parameter objects.
- This class also has an instance variable for the amount of fine.
- The fine is calculated by the ParkingTicket constructor:
The fine is $25 for the first hour or part of an hour that the car is illegally parked, plus $10 for every additional hour or part of an hour that the car is illegally parked.
If time has not expired the fine is set to zero.
- Has a toString() method which prepares the ticket in a useful format of your design (try to match the sample output shown below or make your own improvements. It must report the make, model, color, and license number of an illegally parked car. It also reports the amount of the fine and the name and badge number of the police officer issuing the ticket. In this toString() method you must make effective use of the toString() methods for the other classes in parking ticket (i.e. the ParkedCar, the ParkingMeter, and the ParkingOfficer)
Testing Class
Write a testing class, TicketDemo, with main() to run the simulation using the above classes as follows:
• Create a PoliceOfficer object. Make up your own name and badge number. You may hard-wire this into your test program or ask the user for input data (your choice).
• In a loop read data from the data file Asg-5_in.txt. (Note: the file Asg-5_inExplain.txt describes how to interpret the file content.)
o Read one block of data for a car and a meter and create a ParkedCar object and a ParkingMeter object
o Call the parking officer examine() method using your ParkedCar and Parking meter objects as arguments. The examine method will return a ticket or null.
o If a ticket is returned, call the toString() method to print the ticket on the screen.
o If no ticket is issued print identification information about the car (use its toString() method.
o Let the loop repeat until there is no more data available from the input file.
Here is what I have so far, although I'm nowhere near being done.
ParkedCar class:
public class ParkedCar{
private String make;
private String model;
private String color;
private String license;
private String meterID;
public String getMake(String make)
{
this.make = make;
return make;
}
public String getModel(String model)
{
this.model = model;
return model;
}
public String getColor(String color)
{
this.color = color;
return color;
}
public String getLicense(String license)
{
this.license = license;
return license;
}
public String getMeterID(String meterID)
{
this.meterID = meterID;
return meterID;
}
public String toString()
{
String str = "\nMake: " + make + "\nModel: " + model + "\nColor: " + color +
"\nLicense: " + license + "\nMeter ID: " + meterID;
return str;
}
}
ParkingMeter class:
public class ParkingMeter{
private String meterID;
private int minsPurchased;
private int minsParked;
public String getMeterID(String meterID)
{
this.meterID = meterID;
return meterID;
}
public int getMinsPurchased(int minsPurchased)
{
this.minsPurchased = minsPurchased;
return minsPurchased;
}
public int getColor(int minsParked)
{
this.minsParked = minsParked;
return minsParked;
}
public String toString()
{
String str = "\nMeter ID: " + meterID + "\nMinutes Purchased: " + minsPurchased +
"\nMinutes Parked: " + minsParked;
return str;
}
}
ParkingOfficer class:
public class ParkingOfficer{
private String officerName;
private String badgeNumber;
public ParkingOfficer(String officerName, String badgeNumber)
{
this.officerName = officerName;
this.badgeNumber = badgeNumber;
}
public ParkingOfficer(ParkingOfficer object2)
{
officerName = object2.officerName;
badgeNumber = object2.badgeNumber;
}
public void setParkingOfficer(String officerName, String badgeNumber)
{
this.officerName = officerName;
this.badgeNumber = badgeNumber;
}
public String toString()
{
String str = "\nOfficer's Name: " + officerName + "\nOfficer's Badge Number: " + badgeNumber;
return str;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有很多我没有考虑的事情:
There's a lot of stuff that I didn't consider here:
您可以使用 commons-io 一次性读取文件,然后循环遍历列表。一些示例代码:
You could use commons-io to read the file in one go, and then loop over the list. A bit of example code:
基本结构如下:
A basic structure would be something like this: