【问题标题】:create an ArrayList from objects from another class but the same package从另一个类但相同包的对象创建一个 ArrayList
【发布时间】:2019-05-16 00:43:32
【问题描述】:

我正在构建一个带有类 Item(在 Item.java 中)和类 Receipt(在 Receipt.java 中)的程序。它们都在同一个包中。我希望 Receipt 构造方法使用 Item 对象实例的 ArrayList 进行初始化。我怎样才能做到这一点?编译代码/运行 Receipt.java 文件时,我不断收到“找不到符号”错误。

Receipt.java

package com.calculator;
import java.util.ArrayList;

// Receipt model

public class Receipt {

    public ArrayList<Item> items;

    // initialized with a list of item objects
    public Receipt(ArrayList<Item> lineItems) {
        items = lineItems;
    }

    // calculates total
    public double totalWithSalesTax() {

    }

    // calculates total sales tax
    public double totalSalesTax() {
        double salesTax = 0;
        for (Item item: items) {
            salesTax = salesTax + item.calculateTax();
        }
        return salesTax;
    }

    // goes through each item and creates a string that you'd see on the receipt output

    public static void main(String[] args) {
        Item one = new Item("1 packet of headache pills at 9.75");
        Item two = new Item("1 bottle of perfume at 18.99");
        Item three = new Item("1 box of imported chocolates at 11.25");
        ArrayList<Item> list = new ArrayList<>();
        list.add(one);
        list.add(two);
        list.add(three);
        System.out.println(list);

    }
}

我如何在 Receipt.java 主程序中调用我的代码。当我调用它们时,我在这些行上得到相同的“找不到符号”错误:

    public static void main(String[] args) {
        // the Item class is initialized with a string
        Item i = new Item("1 imported box of chocolates at 10.00");
        System.out.println(i.isImported);
        System.out.println(i.isExempt);
        System.out.println(i.quantity);
        System.out.println(i.productName);
        System.out.println(i.initialPrice);
        System.out.println(i.calculateTax());
        System.out.println(i.totalItemPriceWithTax());
    }

我希望程序将 Item 识别为程序中的一个对象,因为它们在同一个类中。但是当我编译我的代码时,我不断收到“找不到符号”的错误。

对于那些询问 Item 类的人:

package com.calculator;
import java.util.ArrayList;

public class Item {

    // instance variables
    private boolean isImported = false;
    private boolean isExempt = false;
    private String productName;
    private int quantity;
    private double initialPrice;


    // class variables
    private static ArrayList<String> exemptItems = new ArrayList<String>();


    // create a list of exempt items
    static {
        exemptItems.add("book");
        exemptItems.add("chocolate");
        exemptItems.add("pills");
    }

    public Item(String input) {
        String[] strSplit = input.split(" at ");

        // set initial price
        initialPrice = Double.parseDouble(strSplit[1]);

        // set quanitity
        quantity = Integer.parseInt(strSplit[0].substring(0, strSplit[0].indexOf(" ")));

        // set productname
        String[] description = strSplit[0].split(" ", 2);
        productName = description[1];

        // set isExempt & isImported
        setImported();
        setExempt();
    }

    // method that checks if isImported
    private void setImported() {
        if (productName.contains("imported")) {
            isImported = true;
        }
    }
    // method that checks if isExempt
    private void setExempt() {
        if (getExemptItems().parallelStream().anyMatch(productName::contains)) {
            isExempt = true;
        }
    }

    // write a method that determines how much tax per item
    public double calculateTax() {
        double salesTax = 0.10;
        double importTax = 0.05;
        double precision = 0.05;
        double tax = 0;

        if (isImported) {
            tax = tax + (initialPrice * importTax);
        }

        if (!isExempt) {
            tax = tax + (initialPrice * salesTax);
        }

        // rounding to nearest .05
        tax = Math.ceil(tax / precision) * precision;
        return tax;
    }

    // write a method that represent total with tax
    private double totalItemPriceWithTax() {
        return this.calculateTax() + initialPrice;
    }

    private static ArrayList<String> getExemptItems() {
        return exemptItems;
    }

    public static void main(String[] args) {

    }
} ```



【问题讨论】:

  • 你是怎么调用这个代码的?请显示您的堆栈跟踪
  • 问题可能出在您的测试上。您能否向我们展示测试(如果它们是代码),或者如果您正在以其他方式进行测试,请描述它们。
  • 我刚刚更新了我的代码调用方式!
  • 好的,所以你说 Receipt 和 Item 在同一个包中。你有两个java文件在同一个目录吗?
  • 是的,它们都在 com.calculator 中

标签: java class object arraylist


【解决方案1】:

您可能因此收到此错误:

包 com.calculator.*;

包关键字不支持通配符。

【讨论】:

  • 哦,哎呀!我尝试删除它,但仍然遇到同样的问题。
  • 你能分享一些关于你如何编译你的代码的细节吗?此外,在您的 items 类中,您有 3x 反引号......您的代码中是否也存在这些反引号?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多