我正在尝试读取这样的数据文件:
N 1000.0 纽约 R 2000.0 CA 0.09 R 500.0 GA 0.07 N 2000.0 怀俄明 O 3000.0 日本 0.11 20.0 N 555.50 加利福尼亚州 O 3300.0 厄瓜多尔 0.03 30.0 R 600.0 NC 0.06
并用它来填充一个arrayList
我的程序由一个抽象类和三个实现它的类组成:
1. 非营利组织
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.常规订单
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,或者我将如何引用每种类型的订单?我也有“类型不匹配”错误,但我无法修复它。