readOrders和fill ArrayList

时间:2013-09-12 01:34:07

标签: java arraylist readfile

我正在尝试读取这样的数据文件:

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 

并使用它来填充arrayList

我的程序由一个抽象类和三个实现它的类组成:

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,或者我将如何引用每种类型的订单?此外,我遇到“类型不匹配”错误,但我无法修复它。

1 个答案:

答案 0 :(得分:1)

您未正确初始化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并进行投射。但是,如果在基类中声明了方法并且只在子类中覆盖它们,则可以避免这种情况,然后当您从List中读取时,首先在子类中查找该方法,如果找不到它,则查找在基类中,如果它不在基类中,显然会出现错误。这有一个抽象基类是有用的,这意味着你可以避免这样做

Order ord = orders.get(anIndex)

if(ord instanceof NonProfitOrder)
    ((NonProfitOrder)ord).aNonprofitOrderSpecificMethod()
else if (ord instanceof RegularOrder())
    ((RegularOrder)ord).aRegularOrderSpecificMethod();
//.....