我正在阅读一个.txt
文件,它需要能够使用产品地图中的产品密钥 5020 更新条目,从而将购买次数增加 2。到目前为止,我可以通过客户地图来做到这一点,但我需要制作产品地图的另一个显示也是如此。
每当我尝试以与客户地图相同的方式进行操作时,我都无法获得文件中的所有条目。我必须使用 HashMap 并且我不熟悉它。
我认为问题在于产品映射的 if 语句。
public class StoreSales {
public static void main(String[] args) {
List<Customer> customer = new ArrayList<>();
try {
readFile("Sales.txt", customer);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(customer);
}
public static void readFile(String file, List<Customer> cust) throws IOException, ClassNotFoundException {
Map<Integer, Customer> customers = new HashMap<>();
Map<Integer, Customer> product = new HashMap<>();
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String line;
while ((line = in.readLine()) != null) {
String[] arr = line.split(" ");
cust.add(new Customer(Integer.parseInt(arr[0]), arr[1], arr[2],
Integer.parseInt(arr[3]), arr[4], Double.parseDouble(arr[5]),
Integer.parseInt(arr[6])));
if (customers.containsKey(Integer.parseInt(arr[0]))) {
Customer c = customers.get(Integer.parseInt(arr[0]));
customers.get(Integer.parseInt(arr[0])).getSingleItemPrice();
c.addTotal(Double.parseDouble(arr[5]) * Integer.parseInt(arr[6]));
customers.put(Integer.parseInt(arr[0]), c);
} else {
customers.put(Integer.parseInt(arr[0]),
new Customer(Integer.parseInt(arr[0]), arr[1], arr[2],
Integer.parseInt(arr[3]), arr[4], Double.parseDouble(arr[5]),
Integer.parseInt(arr[6])));
}
if (product.containsKey(Integer.parseInt(arr[3]))) {
Customer p = product.get(Integer.parseInt(arr[3]));
customers.get(Integer.parseInt(arr[3])).getItemsPurchased();
p.addTotal(Double.parseDouble(arr[5]) * Integer.parseInt(arr[6]));
product.put(Integer.parseInt(arr[3]), p);
} else {
product.put(Integer.parseInt(arr[3]),
new Customer(Integer.parseInt(arr[0]), arr[1], arr[2],
Integer.parseInt(arr[3]), arr[4], Double.parseDouble(arr[5]),
Integer.parseInt(arr[6])));
}
}
}
}
}
这是文件:
1001 Martha Washington 5001 dress 120 1
1002 John Adams 5002 shirt 55 3
1002 John Adams 5003 tie 20 2
1003 George Washington 5003 tie 20 1
1004 Benjamin Franklin 5010 hat 60 1
1005 Abigail Adams 5020 blouse 45 2
1005 Abigail Adams 5013 skirt 80 1
1004 Benjamin Franklin 5015 coat 500 1
1004 Benjamin Franklin 5012 umbrella 15 1
1006 Deborah Read 5001 dress 120 2
1007 Robert Livingston 5003 tie 20 1
1002 John Adams 5010 hat 60 1
1001 Martha Washington 5014 gloves 20 1
1005 Abigail Adams 5014 gloves 20 2
1006 Deborah Read 5012 umbrella 15 4
1006 Deborah Read 5013 skirt 80 1
1004 Benjamin Franklin 5003 tie 20 5
1006 Deborah Read 5007 jacket 70 1
1001 Martha Washington 5013 skirt 80 1
1003 George Washington 5015 coat 500 1
1007 Robert Livingston 5008 pants 75 1