Java 创建不带ArrayList字符串的目录
我的任务要求我创建产品目录。显然,我不允许在任何地方使用ArrayList字符串。我只需要在“Build catalog”方法中使用一个字符串,该方法将返回用户输入的产品的随机价格,如果该产品是目录的一部分。有人知道如何在不使用数组列表而只使用字符串的情况下创建此目录吗?我的代码在下面工作,但不只是一个字符串。。。谢谢Java 创建不带ArrayList字符串的目录,java,Java,我的任务要求我创建产品目录。显然,我不允许在任何地方使用ArrayList字符串。我只需要在“Build catalog”方法中使用一个字符串,该方法将返回用户输入的产品的随机价格,如果该产品是目录的一部分。有人知道如何在不使用数组列表而只使用字符串的情况下创建此目录吗?我的代码在下面工作,但不只是一个字符串。。。谢谢 public static void main(String[] args) { bannerPrinter(); ArrayList<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("");
}
publicstaticvoidmain(字符串[]args){
bannerPrinter();
ArrayList products=productsBuilder();
布尔productExists=getOrder(产品);
如果(产品存在){
双倍价格=getPrice();
双倍税=getTax(价格*.10);
双倍销售总额=税金+价格;
printTotal(销售总额);
}否则{
System.out.println(“未找到产品”);
}
}
公共静态无效横幅(){
System.out.println(“*************************************************************”);
System.out.println(“=======欢迎使用我的电子商务应用程序!=======”;
System.out.println(“*************************************************************”);
System.out.println();
}
公共静态ArrayList productsBuilder(){
ArrayList productsCatalog=新的ArrayList();
产品目录添加(“耳机”);
产品标签添加(“胶棒”);
productsCatalog.add(“计算器”);
返回产品目录;
}
公共静态布尔getOrder(ArrayList产品){
扫描仪输入=新扫描仪(系统输入);
字符串userProduct=“”;
System.out.print(“请输入产品名称:”);
userProduct=in.nextLine();
boolean productName=products.contains(userProduct);
如果(产品名称){
System.out.println(“真”);
}否则{
System.out.println(“假”);
}
返回产品名称;
}
公共静态双getPrice(){
双倍价格=0.0;
价格=(Math.random())*10;
退货价格;
}
公共静态双getTax(双倍价格){
双重税=0.0;
税=价格*.10;
退税;
}
公共静态双倍总收入(双倍价格,双倍税){
双重销售总额=0.0;
销售总额=价格+税金;
退货总额;
}
公共静态无效打印总计(双倍销售总计){
System.out.printf(“您的销售总额为%.2f美元”,saleTotal);
System.out.println(“”);
}
}只需使用常规列表即可。或者,你可以使用
集合
或哈希映射
,但我不建议使用映射。对不起,你能给我一个例子说明你所说的常规列表是什么意思吗?不能使用数组列表
,那么你可以使用字符串[]
数组。您尝试过了吗?如果我理解正确,您希望构建目录而不使用列表
而只使用字符串
?如果是这样,您可以将所有产品名称存储在一个由特殊字符分隔的字符串中,然后解析以检索它们。像这样String products=“耳机;胶棒;计算器”代码>抱歉,不是一个常规列表:P我尝试使用一个链接:LinkedList LinkedList=new LinkedList()代码>或者,您可以使用StringBuilder
,并且在每个append语句的末尾抛出一个分号。然后,将其转换为一个数组:String[]array=stringbuilder.toString().split(;”)