gpt4 book ai didi

java - 如何在约会类中创建列表以及如何编写测试用例?

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:41 24 4
gpt4 key购买 nike

实现父类(super class)约会和子类一次性、每日和每月。
预约有描述(例如,“看牙医”)和日期。
编写一个方法 events On(intyear,intmonth,intday) 来检查约会是否发生在该日期。
例如,对于每月的约会,您必须检查该月的某一天是否匹配,然后用约会的混合填充 Appointment 对象的数组。
让用户输入一个日期并打印出该日期发生的所有约会。

public class Appointment {

private String description;
private Date date;

public Appointment(String description, int day, int month, int year)
throws ParseException {
this.description=description;
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("dd/MM/yyyy");
this.setDate(simpleDateFormat.parse(String.format("%d/%d/%d", day,
month, year)));

}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return String.format("Your Appointment is : %s. On(date): ",
this.getDescription(), this.getDate()
.toString());
}

public boolean occursOn(int year, int month, int day) throws
ParseException {
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("dd/MM/yyyy");
Date date = simpleDateFormat.parse(String.format("%d/%d/%d", day,
month, year));

return this.getDate().equals(date);
}
}

最佳答案

Appointment 基类与occurrsOn 方法一起抽象化。同时删除 date 变量,因为某些子类不需要它:

public abstract class Appointment {

private String description;

public Appointment(String description)
this.description=description;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public abstract boolean occursOn(int year, int month, int day) throws
ParseException;
}

现在我们可以开始创建子类,它将实现 occurrsOn 方法。对于 Onetime 子类,它将与原始 Appointment 类基本相同,因此我现在不会编写它。相反,让我们看一下每月,例如:

public class Monthly extends Appointment {

private String description;
private int onThisDay; //on this day of the month is the appointment

public Appointment(String description, int onThisDay) {
this.description=description;
this.onThisDay = onThisDay;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

//override tostring as well
@Override
public String toString() {
return "Your " + description + " appointment is on the " +
onThisDay + ". day of the month";
}

//implement our abstract method, month and year params have no importance here
public boolean occursOn(int year, int month, int day) throws
ParseException {
return day == onThisDay; //just check if day matches
}
}

关于如何填写约会列表,这里是:

List<Appointment> appointments = new ArrayList<>();
//2 appointments for example
appointments.addAll(new Onetime("Doctor",2019,07,21), new Monthly("Dentist",11));
appointments.forEach(app -> System.out.println(app.toString())); //print all

关于java - 如何在约会类中创建列表以及如何编写测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360826/

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