Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

java - Reading from data file to fill ArrayList

I have a program with an abstract class 'Orders' with three different classes implementing it everything in the program runs fine when I tested it with hard coded orders.

Here is the abstract class:

public abstract class Order {
protected String location;
protected double price;

public Order(double price, String location){
    this.price = price;
    this.location = location;
}

public abstract double calculateBill();

public String getLocation() {
    return location;
}

public double getPrice() {
    return price;
}

public abstract String printOrder(String format);

}

And here is one of the implementing classes for reference

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" + "
Location: " + getLocation() +  "
Total Price: " + getPrice();
    String Short = "Non-Profit Order-Location: " + getLocation() + ", " + "Total Price: " + getPrice();
    if (format.equals("Long")){
        return Long;
    }
    else{
        return Short;
    }
}
}

This is what I have for a tester so far, I know it is wrong and pretty messy, but take it easy. Iv'e been messing around trying to get something to work, but no luck.

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;
}

}

The data file I need to read from looks like this:

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

First off I am having trouble reading the file, I know i need a loop to add the data to the arrayList, but I'm not sure how . What would be the easiest way to read and loop all in one method? If possible.

I updated to add the somewhat of a switch statement that I have, that doesn't work. Instead of case 1,2,3, would I instead need to use something with N, O, R to match up with the file and I'm having trouble fixing the error "type mismatch".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Scanner provides an easy way to walk through the file. .next() .hasNext() .nextLine() .hasNextLine() are very useful methods in your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...