我正在尝试读取这样的数据文件:
N 1000.0 NY R 2000.0 CA 0.09 R 500.0 GA 0.07 N 2000.0 WY O 3000.0 Japan 0.11 20.0 N 555.50 CA O 3300.0 Ecuador 0.03 30.0 R 600.0 NC 0.06
and use it to fill an arrayList
My program consists of a abstract class and three classes to implement it:
1. NonProfitOrder
public class NonProfitOrder extends Order {
public NonProfitOrder(double price, String location) {
super(price, location);
}
public double calculateBill() {
return getPrice();
}
public String printOrder(String format){
String Long = "Non-Profit Order" + "\nLocation: " + getLocation() + "\nTotal Price: " + getPrice();
String Short = "Non-Profit Order-Location: " + getLocation() + ", " + "Total Price: " + getPrice();
if (format.equals("Long")){
return Long;
}
else{
return Short;
}
}
}
2. RegularOrder
public class RegularOrder extends Order {
double taxRate;
public RegularOrder(double price, String location, double taxRate) {
super(price, location);
this.taxRate = taxRate;
}
private double calcTax() {
double tax;
tax = getPrice() * taxRate;
return tax;
}
public double calculateBill() {
double bill;
bill = price + calcTax();
return bill;
}
public String printOrder(String format){
String Long = "Regular Order" + "\nLocation: " + getLocation() + "\nPrice: " + getPrice() +
"\nTax: " + calcTax() + "\nTotal Price: " + calculateBill();
String Short = "Regular Order-Location: " + getLocation() + ", " + "Total Price: " + calculateBill();
if (format.equals("Long")){
return Long;
}
else{
return Short;
}
}
}
另一个非常类似于
RegularOrder
我的问题出在我的主力上。我必须使用方法
readOrders(fileName:string):ArrayList<Order>
public static ArrayList<Order> readOrders (String fileName) throws FileNotFoundException{
String type;
Scanner s = new Scanner(new File("orders.txt"));
ArrayList<Order> orders = new ArrayList<Order>();
while (s.hasNext()){
type = s.nextLine();
}
switch(type) {
case 1: type = NonProfitOrder();
break;
case 2: type = RegularOrder();
break;
case 3: type = OverseasOrder();
return orders;
}
}
我仍然不知道如何正确执行此操作
readOrders无法解析为类型。
以及第一个
readOrders
的其他问题。我用一些切换状态更新了我的代码,这是行不通的。我应该使用N,O,R而不是情况1,2,3,或者我将如何指代每种类型的订单?另外,我遇到“类型不匹配”错误,但无法解决它。
最佳答案
您没有正确初始化ArrayList和读取订单方法。此处的代码不正确,该方法在;
之后结束,不执行任何操作,其余无效
ArrayList<Order> readOrders (String fileName); {
ArrayList<Order> orders2 = new readOrders("orders.txt");
}
您需要做的是创建方法
readOrder
,类似于以下的See this post on how to read in the file。除非您遇到这种情况,否则您将需要在循环中做更多的工作来创建Order
对象public static ArrayList<Order> readOrders (String fileName){
ArrayList<Order> orders = new ArrayList<Order>();
//Read in lines from your file
//Decide which type of order it is and create it
//Add it to the list
//Repeat until end of file
return orders
}
然后在你的主要方法
public static void main(String[] args) {
ArrayList<Order> loadedOrders = readOrders("orders.txt");
//... rest of your code
}
附加编辑
要将它们插入列表,请执行以下操作
char firstChar = //Read in the first character of the line from the file
Order anOrder;
switch(firstChar)
{
case 'N':
anOrder = new NonProfitOrder(); //adding arguments to construtor as needed
break;
case 'R':
anOrder = new RegularOrder();
break;
default:
anOrder = new Order();
break;
}
loadedOrders.add(anOrder);
然后,在阅读时,可能需要使用
instanceof
和强制转换,具体取决于您需要对订单执行什么操作。但是,如果在基类中声明了方法并且仅在子类中覆盖它们,则可以避免这种情况,然后从列表中读取时,首先在子类中查找该方法,如果找不到该方法,则在寻找在基类中,那么如果它不在基类中,则显然会出现错误。这在具有抽象基类的地方很有用,这意味着您可以避免这样做Order ord = orders.get(anIndex)
if(ord instanceof NonProfitOrder)
((NonProfitOrder)ord).aNonprofitOrderSpecificMethod()
else if (ord instanceof RegularOrder())
((RegularOrder)ord).aRegularOrderSpecificMethod();
//.....
关于java - readOrders并填充ArrayList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18753955/