0

我从 CSV 文件中读取了以下数据。

Pedro|groceries|apple|1.42 
Nitin|tobacco|cigarettes|15.00 
Susie|groceries|cereal|5.50 
Susie|groceries|milk|4.75 
Susie|tobacco|cigarettes|15.00 
Susie|fuel|gasoline|44.90 
Pedro|fuel|propane|9.60 

我想将每个客户的费用分组,例如

佩德罗的费用:

杂货 - 1.42 燃料 - 9.62

我还想对每个客户的费用总和进行分组,例如

每个客户的总费用为:

佩德罗 - (1.42 + 9.60) 尼丁 - 15.00 苏西 - (5.50 + 4.75 + 15.00 + 44.90)

有人可以帮助我如何对元素进行分组并总结它们的值。

我能够读取文件并打印单独的值。我将每个小组成员的费用与他们的费用进行了映射。但不知道如何总结他们的价值观

有人能帮我吗?

这是我的代码,我确定这是不正确的

static String[] inputArray ;


public static void main(String[] args) throws IOException {

    groceryReport gr = new groceryReport();

    try {
        gr.readFile();
        System.out.println(gr.customerPurchase(inputArray).toString());
        }
    catch (FileNotFoundException e) {

        e.printStackTrace();
    }
}

@SuppressWarnings("null")
private void readFile() throws IOException {

    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\....\\grocery.csv")); 
    String input= "";
    try{
        System.out.println("The total revenue for each customer is:");

    while((input = br.readLine()) != null )
    {
    inputArray= input.split("\\|");
    }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



    public String customerPurchase(String[] inputArray){

    String sum = "" ; float sum1=0; float sum2 = 0;
    ArrayList<String[]> alist = new ArrayList<String[]>();
    alist.add(inputArray);
    String value = "";
    System.out.println(alist);
    Map<String, String> map = new HashMap<String, String>();

    map.put(inputArray[0], inputArray[3]);
    System.out.println(map.get(inputArray));
    Iterator ite = map.entrySet().iterator();

    while (ite.equals("Susie"))
        ite.next();
        value = map.get("Susie");

            sum = sum+ value;


            return sum;


}
4

2 回答 2

0

我的解决方案可以在这里找到,但我会给你一个简短的细分。

首先,我Purchase为 csv 文件中的每一行创建了一个类名作为抽象。这只是一个简单的类,用于将有关购买的信息(项目类型、特定项目和价格)组合在一起。

createReport方法将 csv 行作为输入,并将返回一个数据结构 a Map<String, List<Purchase>,地图的关键将是人员的姓名,以及他们相关的购买。如果我想获取Pedro购买的所有商品,我只需使用mapVariable.get("Pedro");它,它就会返回List<Purchases>Pedro 购买的所有商品。

因此,从方法中可以看出,找到 Pedro 购买的总和是一项简单的任务sum

这最终给出了一个如下所示的报告:

---------------------------------------
Name.                             Nitin
---------------------------------------
tobacco                            15.0
---------------------------------------
Total:                             15.0
---------------------------------------

---------------------------------------
Name.                             Pedro
---------------------------------------
groceries                          1.42
fuel                                9.6
---------------------------------------
Total:                            11.02
---------------------------------------

---------------------------------------
Name.                             Susie
---------------------------------------
groceries                           5.5
groceries                          4.75
tobacco                            15.0
fuel                               44.9
---------------------------------------
Total:                            70.15
---------------------------------------
于 2013-11-06T03:27:39.533 回答
0

在面向对象的语言中,当您想要对特定的数据进行分组时,这是通过使用类来完成的。我假设这是一个家庭作业,所以我不能提供完整的细节。但是您想要一个包含所需信息字段的人员类。从那里你可以 iimplemet 方法来输出你想要的东西?

于 2013-11-06T02:30:36.497 回答