gpt4 book ai didi

java - 通过读取文件创建对象,该对象也具有动态数组作为其字段

转载 作者:行者123 更新时间:2023-12-02 08:45:08 26 4
gpt4 key购买 nike

我有一个包含购买信息的文本文件,其结构如下

客户名称 tab 成员(member)类型 tab 购物日期 tab 产品名称 tab 数量 换行

客户可以一次购买多个不同的对象,因此 ProductName 和 Quantity 可以像这样出现多次

Mary    gold    26.01.2020  Sweater 1   Jeans   2
Eve silver 20.02.2020 Sweater 2 Jeans 1
Steve bronze 19.01.2020 Jeans 2 Sweater 3

我目前正在尝试从这些数据创建客户对象,这是我的实现

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws FileNotFoundException, ParseException {
Scanner shoppingList = new Scanner(new File("shoppingList.txt");
shoppingList.useDelimiter("[\t\n]");

Customer[] customers = new Customer[0];
while (shoppingList.hasNext()) {
String customerName = shoppingList.next();
String customerMembershipType = shoppingList.next();
String purchaseDate = shoppingList.next();

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = formatter.parse(purchaseDate);

String[] productNameList = new String[0];
int[] quantityList = new int[0];
while (shoppingList.hasNext()) {
//this is where i stuck
}
Customer[] newCustomer = new Customer(customerName, customerMembershipType, purchaseDate, productNameList, quantityList);
customers = addCustomer(customers, newCustomer);
}
}

private static Customer[] addCustomer(Customer[] customers, Customer customerToAdd) {
Customer[] newCustomers = new Customer[customers.length + 1];
System.arraycopy(customers, 0, newCustomers, 0, customers.length);
newCustomers[newCustomers.length - 1] = customerToAdd;
return newCustomers;
}

static class Customer {
protected String customerName;
protected String customerMembershipType;
protected Date purchaseDate;
protected String[] productNameList;
protected int[] quantityList;

public Customer(String customerName, String customerMembershipType, Date purchaseDate, String[] productNameList, int[] quantityList) {
this.customerName = customerName;
this.customerMembershipType = customerMembershipType;
this.purchaseDate = purchaseDate;
this.productNameList = productNameList;
this.quantityList = quantityList;
}
}

基本上,我想创建可以保存产品名称列表及其数量列表的 Customer 对象,但由于数组是不可变的,并且客户可以购买无限数量的产品类型,如果不使用 arrayLists,我无法找到解决方案,但是我无法在这个作业。此时我能做什么?

最佳答案

我建议您不要尝试读取客户行并解析这些行,而是单独进行操作,即将行作为行读取,每个客户一行,然后根据您的逻辑解析行。

如下所示(请注意,我更改了客户表示,并且客户现在是列表而不是数组):

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException, ParseException {
BufferedReader reader = new BufferedReader(new FileReader(new File("shoppingList.txt")));

List<Customer> customers = new ArrayList<>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
Scanner shoppingList = new Scanner(line);
String customerName = shoppingList.next();
String customerMembershipType = shoppingList.next();
String purchaseDate = shoppingList.next();

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = formatter.parse(purchaseDate);

Customer customer = new Customer(customerName, customerMembershipType, date);

while (shoppingList.hasNext()) {
customer.addProduct(shoppingList.next(), shoppingList.nextInt());
}
}
}

static class Customer {
protected String customerName;
protected String customerMembershipType;
protected Date purchaseDate;
protected Map<String, Integer> products = new HashMap<>();

public Customer(String customerName, String customerMembershipType, Date purchaseDate) {
this.customerName = customerName;
this.customerMembershipType = customerMembershipType;
this.purchaseDate = purchaseDate;
}

public void addProduct(String name, int qty) {
products.put(name, qty);
}
}
}

关于java - 通过读取文件创建对象,该对象也具有动态数组作为其字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61125368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com