【发布时间】:2016-02-19 05:01:45
【问题描述】:
我是 Java 语言的新手,谁能帮我解答这个问题。
问题是这样的:
目标:复习将对象读取和写入文件的概念 汽车信息以文本文件的形式提供,如下所示。 文件名:honda.txt CRV-3 CV6787272 150000.00 20.0
MRV-1 MV9890202 180000.00 20.0
思域CC9939390 82000.00 10.0
CRV-3 CV8393939 149000.00 20.0
步骤 1 创建一个名为“Car”的类,该类具有以下字段。这些字段对应于文本文件中除最后一个之外的列。
一个。车辆名称:字符串
b.引擎号:字符串
c。车辆价格:双倍
d。利润:双倍。
e。总价:双倍(总价=(车价)+(车价*利润/100)
步骤 2 使用方法更新 Car 类以执行以下操作
一个。读取文本文件(honda.txt)的内容
我。对于文件中的每一行,使用计算的总价创建一个 Car 类的实例。
二。将创建的每个实例写入另一个文件“honda_showroom.txt”。
b.显示“honda_showroom.txt”的内容
我。一个方法 ex:showCar(String eNumber),将引擎编号作为参数。并显示特定汽车的所有信息,包括总价。 第 3 步创建一个 Main 类来测试上述内容。
到目前为止我写的代码:
汽车类代码(用 BlueJ 编写):
import java.io.Serializable;
public class Car {
private String vehicleName;
private String engineNumber;
private double vehiclePrice;
private double Profit;
private double totalPrice;
totalPrice = (vehiclePrice) + ((vehiclePrice * Profit) / 100);
public double getProfit() {
return Profit;
}
public void setProfit(double Profit) {
this.Profit = Profit;
}
public String getEngineNumber() {
return engineNumber;
}
public void setEngineNumber(String engineNumber) {
this.engineNumber = engineNumber;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
public double getVehiclePrice() {
return vehiclePrice;
}
public void setVehiclePrice(double vehiclePrice) {
this.vehiclePrice = vehiclePrice;
}
}
输出或显示类:
import java.io.Serializable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class Display {
private String fileName;
public Display(String fName) {
// this.fileName = "honda_showroom";
fileName = fName;
// this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void showCar(String eNumber) throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(new File(this.fileName));
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Car> cList = (ArrayList<Car>) ois.readObject();
while (ois.readObject() != null) {
cList = new ArrayList<Car>();
int searchListLength = cList.size();
for (int i = 0; i < searchListLength; i++) {
if (cList.get(i).equals(eNumber)) {
cList.toString();
}
}
}
}
}
提前致谢!请记住,我是初学者也从网上收集了一些代码。
【问题讨论】:
-
嗯......很多文字,很多代码。但是你有什么问题吗?