0

我正在阅读一个.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
4

1 回答 1

0

首先,有点不清楚你在问什么,代码很难阅读。所以我建议你以这种方式开始简化你的代码,它更容易阅读并且会简化调试:

public static void readFile(String file, List<Customer> cust) {
  Map<Integer, Customer> customers = new HashMap<>();
  Map<Integer, Customer> products = new HashMap<>();

  try (BufferedReader in = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = in.readLine()) != null) {
      String[] arr = line.split(" ");

      int v0 = Integer.parseInt(arr[0]);
      String v1 = arr[1];
      String v2 = arr[2];
      int v3 = Integer.parseInt(arr[3]);
      String v4 = arr[4];
      double v5 = Double.parseDouble(arr[5]);
      int v6 = Integer.parseInt(arr[6]);

      Customer customer = new Customer(v0, v1, v2, v3, v4, v5, v6);

      cust.add(customer);

      if (customers.containsKey(v0)) {
        Customer c = customers.get(v0);
        c.getSingleItemPrice();
        c.addTotal(v5 * v6);
        customers.put(v0, c);
      } else {
        customers.put(v0, customer);
      }

      if (products.containsKey(v3)) {
        Customer c = products.get(v3);
        c.getItemsPurchased();
        c.addTotal(v5 * v6);
        products.put(v3, c);
      } else {
        products.put(v3, customer);
      }
    }
  } catch(Exception e) {
    // Handle Exception
  }
}

这两条线似乎什么也没做(如果它们是适当的吸气剂)

c.getSingleItemPrice();

c.getItemsPurchased();

我看不出你在哪里做你正在谈论的 2 的增量。

您还可以定义用于构建客户的文件中的 7 个值是什么,并举例说明Sales.txt会产生问题的文件。

我认为问题是:

怀疑是您为同一客户提供多种产品。

您的customers地图具有customerIdas 键和Customeras 值,因此假设您有唯一的 id,每个客户将有一个条目。

在您的products地图中,您有customerIdas 键和Productas 值。如果一个客户有多个产品,那么您试图将一个客户的所有产品放在同一个键中,并且它们会相互替换,因此您的条目会更少。您可能想要products这样,Map<Integer, List<Customer>>因此您拥有每个 ProductId 的客户列表

一个可能的解决方案:

这是相同的代码,但使用Map<Integer, List<Customer>>productsMap

public static void readFile(String file, List<Customer> customersList) {
    Map<Integer, Customer> customersByIdMap = new HashMap<>();
    Map<Integer, List<Customer>> customersByProductIdMap = new HashMap<>();

    try (BufferedReader in = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = in.readLine()) != null) {
            String[] arr = line.split(" ");

            // Create the Customer
            int customerId = Integer.parseInt(arr[0]);
            String firstName = arr[1];
            String lastName = arr[2];
            int productId = Integer.parseInt(arr[3]);
            String productDescription = arr[4];
            double singleItemPrice = Double.parseDouble(arr[5]);
            int itemsPurchased = Integer.parseInt(arr[6]);
            Customer customer = new Customer(customerId, firstName, lastName, productId, productDescription, singleItemPrice, itemsPurchased);

            // Add it to the Customer List
            customersList.add(customer);

            // Add it to the customersByIdMap Map
            if (customersByIdMap.containsKey(customerId)) {
                Customer c = customersByIdMap.get(customerId);
                c.addTotal(singleItemPrice * itemsPurchased);
                customersByIdMap.put(customerId, c);
            } else {
                customersByIdMap.put(customerId, customer);
            }

            // Add it to the customersByProductIdMap Map
            if (customersByProductIdMap.containsKey(productId)) {
                // get the customers list for this product
                List<Customer> productCustomers = customersByProductIdMap.get(productId);
                // check if there is already a customer with this Id
                Customer currentCustomer = null;
                for(Customer c : productCustomers) {
                    if(c.getId() == customerId) {
                        currentCustomer = c;
                    }
                }
                // if yes, update it
                if(currentCustomer!=null) {
                    currentCustomer.addTotal(singleItemPrice * itemsPurchased);
                } else {
                    // if no, add it
                    productCustomers.add(customer);
                    customersByProductIdMap.put(productId, productCustomers);
                }
            } else {
                List<Customer> productCustomers = new ArrayList<>();
                productCustomers.add(customer);
                customersByProductIdMap.put(productId, productCustomers);
            }
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    System.out.println("customersByIdMap: ");
    System.out.println(customersByIdMap);
    System.out.println("customersByProductIdMap: ");
    System.out.println(customersByProductIdMap);
}

我使用了您提供的文件,它生成以下地图

customersByIdMap:
{
    1001=Customer@60e53b93, 
    1002=Customer@5e2de80c, 
    1003=Customer@1d44bcfa, 
    1004=Customer@266474c2, 
    1005=Customer@6f94fa3e, 
    1006=Customer@5e481248, 
    1007=Customer@66d3c617
}
customersByProductIdMap:
{
    5008=[Customer@63947c6b], 
    5010=[Customer@266474c2, Customer@2b193f2d], 
    5012=[Customer@355da254, Customer@4dc63996], 
    5013=[Customer@d716361, Customer@6ff3c5b5, Customer@3764951d], 
    5014=[Customer@4b1210ee, Customer@4d7e1886], 
    5015=[Customer@3cd1a2f1, Customer@2f0e140b], 
    5001=[Customer@60e53b93, Customer@5e481248], 
    5002=[Customer@5e2de80c], 
    5003=[Customer@7440e464, Customer@1d44bcfa, Customer@66d3c617, Customer@49476842], 
    5020=[Customer@6f94fa3e], 
    5007=[Customer@78308db1]
}
于 2018-04-09T13:30:29.220 回答