我的作业要求我创建产品目录。显然,我不允许在任何地方使用ArrayList字符串。我必须在"构建目录"中专门使用A String。如果它是目录的一部分,将返回用户输入的产品的随机价格的方法。任何人都知道如何在不使用数组列表的情况下创建此目录,而只使用字符串?我的代码在下面工作,但不是只有一个字符串...谢谢!
public static void main(String[] args) {
bannerPrinter();
ArrayList<String> products = productsBuilder();
Boolean productExists = getOrder(products);
if (productExists) {
double price = getPrice();
double tax = getTax(price * .10);
double saleTotal = tax + price;
printTotal(saleTotal);
} else {
System.out.println("Product not found.");
}
}
public static void bannerPrinter() {
System.out.println("******************************************");
System.out.println("====== Welcome to my eCommerce app! ======");
System.out.println("******************************************");
System.out.println();
}
public static ArrayList<String> productsBuilder() {
ArrayList<String> productsCatalog = new ArrayList<String>();
productsCatalog.add("Headphones");
productsCatalog.add("Glue Stick");
productsCatalog.add("Calculator");
return productsCatalog;
}
public static boolean getOrder(ArrayList<String> products) {
Scanner in = new Scanner(System.in);
String userProduct = "";
System.out.print("Please enter a product name: ");
userProduct = in.nextLine();
boolean productName = products.contains(userProduct);
if (productName) {
System.out.println("True");
} else {
System.out.println("False");
}
return productName;
}
public static double getPrice() {
double price = 0.0;
price = (Math.random()) * 10;
return price;
}
public static double getTax(double price) {
double tax = 0.0;
tax = price * .10;
return tax;
}
public static double getTotal(double price, double tax) {
double saleTotal = 0.0;
saleTotal = price + tax;
return saleTotal;
}
public static void printTotal(double saleTotal) {
System.out.printf("Your sale total is $%.2f", saleTotal);
System.out.println("");
}
}