0

I'm trying to read a data file 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 

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

and another very similar to RegularOrder

My problem comes in my main. I have to use a method 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;
}

}

I can't figure out how to do this properly as it still says

readOrders can't be resolved to a type.

As well as other issues with the first readOrders.

I updated my code with somewhat of a switch state, that doesn't work of. Instead of case 1,2,3 should I be using N,O,R or how would I refer to each type of order? Also I'm having "type mismatch" error but I'm having trouble fixing it.

user2745043
  • 189
  • 2
  • 7
  • 24
  • `ArrayList orders2 = new readOrders("orders.txt");` this sentence makes no sense.. I agree with your compiler – Karthik T Sep 12 '13 at 01:42
  • Do you just need to skip the `new`? If your function returns the array list, that will work – Karthik T Sep 12 '13 at 01:43
  • Where is the `readOrders` method?? – MadProgrammer Sep 12 '13 at 01:44
  • I deleted the 'new' I put that there by mistake. The readOrders method is my problem I'm not sure how to write it. My UML has it as readOrders(fileName:string):ArrayList. However, when I write it such as "public ArrayList readOrders(String fileName)" I get 'Illegal modifier for parameter readOrders; only final is permitted' and can't seem to fix it – user2745043 Sep 12 '13 at 01:56
  • possible duplicate of [Reading from data file to fill ArrayList](http://stackoverflow.com/questions/18753129/reading-from-data-file-to-fill-arraylist) it's similar to your previous question. – nachokk Sep 12 '13 at 02:09

1 Answers1

1

You are not correctly initialising your ArrayList and read orders method. This code here is not correct, the method ends after the ; and does nothing and the rest is invalid

ArrayList<Order> readOrders (String fileName); {
    ArrayList<Order> orders2 = new readOrders("orders.txt");
}

What you are needing to do is create a method readOrder like the following See this post on how to read in the file. Except in your case you will need to do more work in the loop to create the Order object

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
}

Then in your main method

public static void main(String[] args) {
     ArrayList<Order> loadedOrders = readOrders("orders.txt");
     //... rest of your code
}

ADDITIONAL EDIT

To insert them into the list do something like this

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

Then when reading, depending on what you need to do with the order you will might need to use instanceof and cast. However you can avoid this if in the base class you have the methods declared and only override them in the sub classes, then when you read from the List, the method is looked for in the subclass first, if its not found then is looked for in the base class, then if its not in the base class obviously you will get an error. This where having an abstract base class is useful and means you can avoid doing this

Order ord = orders.get(anIndex)

if(ord instanceof NonProfitOrder)
    ((NonProfitOrder)ord).aNonprofitOrderSpecificMethod()
else if (ord instanceof RegularOrder())
    ((RegularOrder)ord).aRegularOrderSpecificMethod();
//.....
Community
  • 1
  • 1
Java Devil
  • 10,629
  • 7
  • 33
  • 48