我正在使用类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 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) {
}
} ```
答案 0 :(得分:0)
可能是因为以下原因导致此错误:
包com.calculator。*;
Package关键字不支持通配符。