Я строю программу с классом Item (в Item.java) и классом Receipt (в Receipt.java).Они оба в одной упаковке.Я хочу, чтобы метод конструктора Receipt инициализировался с ArrayList экземпляров объекта Item.Как я могу сделать это?Я получаю сообщение об ошибке «Не удается найти символ», когда я компилирую код / запускаю файл 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);
}
}
как я вызываю свой код вКвитанция.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) {
}
} ```