我从 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;
}