简体   繁体   English

用Java获取输入数组的方法

[英]Method to get an array of inputs in Java

I have to do an assignment for school that is basically a small online shop with 10 items. 我必须为学校做作业,基本上是一个只有10个物品的小型网上商店。

The way I've done it is with having 2 premade arrays that include the price, and product names. 我这样做的方法是使用2个预制阵列,其中包括价格和产品名称。 Then I made an array for storing user inputs and an array for storing the multiplied result of input by price. 然后,我创建了一个用于存储用户输入的数组和一个用于存储输入乘以价格的乘积结果的数组。 The program works, however the problem is we needed to have a separate file that will contain the item and price list and then call it by using a separate method. 该程序有效,但是问题是我们需要有一个单独的文件,其中包含商品和价目表,然后使用单独的方法进行调用。

I've been trying to figure out a way to do it but it's a bit above my head to be honest and I'm running out of time. 我一直在试图找到一种方法来做到这一点,但是说实话,这有点超出我的头了,我已经没时间了。 If anyone can advise me how I could separate my functions and product/price lists using methods it would be much appreciated. 如果有人可以建议我如何使用方法将功能和产品/价格表分开,将不胜感激。

This is my program 这是我的程序

    import java.util.Scanner;
    public class dunnes{

    public static void main(String args[]){

Scanner in = new Scanner(System.in);

String product[]={"Coat", "Jeans", "Shirt", "Shoes", "Skirt", "Dress", "Socks","Scarf", "Handbag","Vest"};
double price[]={99.99, 53.59, 9.99, 29.99, 24.99, 83.16, 5.99, 10.95, 23.99, 18.99};
int howMany[]=new int[10];
double multiplied[]=new double[10];
int i =0;
boolean err = false;
boolean flag = true;

for(i = 0; i<price.length; i++){
    System.out.print(+i+1+ " Please enter how many ");
    System.out.print(product[i]+ " you would like to buy (");
    System.out.print(price[i]+ ") euro each \n");
do{
    err = false;
    try{
    howMany[i] = Integer.parseInt(in.next());
    }catch (Exception e){
    err = true;
    System.out.print("Incorrect input. Must be whole numbers. \n");
    System.out.print(+i+1+ " Please enter how many ");
    System.out.print(product[i]+ " you would like to buy (");
    System.out.print(price[i]+ ") euro each \n");
    }


    }while(err);
}




for(i = 0; i<price.length; i++){
    multiplied[i]=howMany[i]*price[i];
    multiplied[i] = Math.round(multiplied[i] * 100.0) / 100.0;
}

    System.out.print("\nUnsorted total bill:\n\n");
for(i = 0; i<price.length; i++){
    System.out.print(product[i]+"\t\t");
    System.out.print(""+multiplied[i]+"\n");}

while(flag){
flag=false;
for(i=0;i<multiplied.length-1;i++){
    if(multiplied[i]>multiplied[i+1]){
        double temp = multiplied[i];
         multiplied[i] = multiplied[i+1];
         multiplied[i+1]= temp;

         String temp2=product[i];
         product[i]=product[i+1];
         product[i+1]=temp2;
         flag = true;

    }
}
}

System.out.print("\nSorted total bill:\n\n");
for(i = 0; i<price.length; i++){
    System.out.print(product[i]+"\t\t");
    System.out.print(""+multiplied[i]+"\n");
}


     }
     }

So, first instead of using multiple arrays of primitives, use one java.util.List of Java objects. 因此,首先使用一个Java对象的java.util.List而不是使用多个基元数组。 You can have a Java class called Product with the fields name and price and getters and setters and then create one Product instance for each of your products and put them in the list. 您可以拥有一个名为Product的Java类,其中包含名称和价格以及getter和setter字段,然后为每个产品创建一个Product实例并将其放在列表中。 It's doesn't seems like much now, but you'll see this will make making changes to your code much more easier. 现在看起来还不多,但是您会看到,这将使对代码的更改更加容易。 Like this: 像这样:

public class Product {
    private String name;
    private Integer price;

    public Product(String name, Integer price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {return name;}
    public Integer getPrice() {return price;}
    public void setName(String name) {this.name = name;}
    public void setPrice(Integer price) {this.price = price;}
}

Now on the big part, as I understand you have to load the product list from a file, it's suggest using a properties files to do that, it's a file with on each line a key, the equals sign and a value, and there is already utilities on Java to parse those files, java.util.Properties. 现在,大体上,据我了解,您必须从文件加载产品列表,建议使用属性文件来执行此操作,该文件的每一行上都有一个键,等号和一个值,并且Java上已有实用程序来解析这些文件java.util.Properties。 There are not exactly designed for this kind of job, but if you are short on time and you don't know how to create a parser yourself, this might be your best solution. 并非针对此类工作而专门设计的,但是如果您时间紧缺并且不知道如何自己创建解析器,那么这可能是最好的解决方案。 You can do something like: 您可以执行以下操作:

Properties prop = new Properties();
try (InputStream stream = new FileInputStream("path/filename")) {
  prop.load(stream);
} finally {
  stream.close();
}
List<Product> products = new ArrayList<>();
for(Object key : prop.keySet()) {
    String name = (String) key;
    Integer price = Integer.valueOf((String) prop.getProperty(key));
    products.add(new Product(name, price));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM