仅在调用save()方法时更改对象值

  • 本文关键字:对象 方法 调用 save java
  • 更新时间 :
  • 英文 :


我需要帮助通过测试,但似乎找不到解决方案。我有一个为顾客决定奖励积分的代码。我从一个文本文件得到我的信息,我需要给客户基于变量的积分。问题在于,只有在使用save()方法时才应该添加额外的积分,该方法会覆盖文本文件。我有一个CustomerRepository类:

public class CustomerRepository {
private static final String FILE_PATH = "src/poly/customer/data.txt";
public List<AbstractCustomer> customers;
public CustomerRepository() {
try {
this.customers = readFiles();
} catch (IOException e) {
e.printStackTrace();
}
}
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public List<AbstractCustomer> readFiles() throws IOException{
List<AbstractCustomer> result = new ArrayList<>();
List<String> lines = Files.readAllLines(Path.of(FILE_PATH));
for (String line : lines) {
String[] parts = line.split(";");
int points = Integer.parseInt(parts[3]);
if (parts[0].equals("REGULAR")) {
LocalDate date = LocalDate.parse(parts[4], formatter);
RegularCustomer customer = new RegularCustomer(parts[1], parts[2], points, date);
result.add(customer);
} else if (parts[0].equals("GOLD")) {
GoldCustomer customer = new GoldCustomer(parts[1], parts[2], points);
result.add(customer);
} else {
throw new IOException();
}
}
return result;
}
public void save(AbstractCustomer customer) {
if (!(customers.contains(customer))) {
customers.add(customer);
}
StringBuilder result = new StringBuilder();
for (AbstractCustomer client : customers) {
result.append(client.toString());
result.append("n");
}
try{
File file = new File(FILE_PATH);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(result.toString());
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}

这个类是工作的,但我不知道如何给客户对象点只有当他们被保存。我还有RegularCustomer和GoldCustomer类,它们扩展了AbstractCustomer类:

public abstract sealed class AbstractCustomer permits GoldCustomer, RegularCustomer {
protected String id;
protected String name;
protected int bonusPoints;
public AbstractCustomer(String id, String name, int bonusPoints) {
this.id = id;
this.name = name;
this.bonusPoints = bonusPoints;
}
public abstract void collectBonusPointsFrom(Order order);
public String getId() {
return id;
}
public String getName() {
return name;
}
public Integer getBonusPoints() {
return bonusPoints;
}

代码本身工作得很好,做了预期的事情,但我唯一的问题是通过这个测试:

@Test
public void customerIsChangedOnlyWhenSaved() {
String randomId = UUID.randomUUID().toString();
repository.save(new RegularCustomer(
randomId, "David", 0, LocalDate.now()));
AbstractCustomer customer = repository.getCustomerById(randomId).get();
assertThat(customer.getBonusPoints(), is(0));
customer.collectBonusPointsFrom(new Order(200, LocalDate.now()));
assertThat(customer.getBonusPoints(), is(not(0)));
AbstractCustomer loaded = repository.getCustomerById(randomId).get();
assertThat(loaded.getBonusPoints(), is(0));
}

这个测试创建了一个新客户并添加了一个订单,但是客户的积分不应该改变,因为它没有被保存。我的代码仍然会添加点并覆盖文件。

还有一个BonusCollector类,它从订单中收集奖励积分,但是这个类不应该被修改。

这是collectBonusPoints(Order Order)的实现:

@Override
public void collectBonusPointsFrom(Order order) {
if (order.getTotal() >= 100) {
double points = order.getTotal();
if (isWithinAMonth(order.getDate())) {
this.bonusPoints += Math.toIntExact(Math.round(points * 1.5));
this.lastOrderDate = order.getDate();
} else {
this.bonusPoints += Math.toIntExact(Math.round(points));
this.lastOrderDate = order.getDate();
}
}
}

金牌客户的实现有点不同。

问题在于持久化状态。在abstractcustomer类中,您需要有get方法,并从该方法返回对象的克隆,以便能够通过测试。

public class Customer {
protected String id;
protected String name;
protected int bonusPoints;
public Customer(String id, String name, int bonusPoints) {
this.id = id;
this.name = name;
this.bonusPoints = bonusPoints;
}
public void collectBonusPointsFrom(Order order) {
....
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Integer getBonusPoints() {
return bonusPoints;
}
public Customer get(){
//if you don`t return new object  you will work directly with the one from repo
return new Customer(id,name,bonusPoints);
}
}

完整的代码你可以找到->https://drive.google.com/file/d/1FOeF68RO-qI4CO9mJ92rKlQ8MNigWbof/view?usp=sharing

最新更新