I am building a program with a class Item (in Item.java) and class Receipt (in Receipt.java). They both are in the same package. I want the Receipt constructor method to initialize with an ArrayList of instances of the Item object. How can I accomplish this? I keep getting a "cannot find symbol" error when I compile my code / run the Receipt.java file.

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

    }
}

how i'm calling my code in the Receipt.java main. I get the same "cannot find symbol" error on those lines when I call them:

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

I expected the program to recognize Item as an object in the program because they are in the same class. But I keep getting a "cannot find symbol" error when I compile my code.

For those asking about the Item class:

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

    }
} ```



1 Answers

0
Community On

Probably you are getting this error because of this:

package com.calculator.*;

Package keyword does not support wildcards.