gpt4 book ai didi

java - 读取Orders并填充ArrayList

转载 作者:行者123 更新时间:2023-12-01 14:07:55 24 4
gpt4 key购买 nike

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

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。常规订单

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 can't be resolved to a type.

以及第一个 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 - 读取Orders并填充ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com