java - 从另一个类但同一包的对象创建一个 ArrayList

标签 java class object arraylist

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

收据.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 main 中调用我的代码。当我调用这些行时,我得到相同的“找不到符号”错误:

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

    }
} ```



最佳答案

您可能会因为以下原因收到此错误:

package com.calculator.*;

包关键字不支持通配符。

关于java - 从另一个类但同一包的对象创建一个 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159369/

相关文章:

java - 如何从 libgdx 中的文件系统读取特殊字符

javascript - 如何根据列为空或不为空的位置从类中隐藏每个 Div

python - Python.org 上的类(class)教程

JavaScript 图像对象

java - 号码跑者计划

java - 你曾经在任何项目中使用过 PhantomReference 吗?

python - balance() 方法在 python 中调用时不起作用

java - TreeMap put 未按预期工作

javascript - 从一些数组构建对象数组

java - 在java中将docx转换为pdf时出错