我的课程Appointment
包含字段:
int day
int month
int year
String desc
这会存储约会的描述以及日期。
超级班Appointment
:
public class Appointment {
int dayNum;
int monthNum;
int yearNum;
String desc;
public Appointment(String des, int day) {
//Monthly Appointments (Check day)
desc = des;
dayNum = day;
}
public Appointment(String des, int day, int month){
//Yearly Appointments (Check day AND month)
desc = des;
dayNum = day;
monthNum = month;
}
public Appointment(String des, int day, int month, int year){
//One Time (Check All)
desc = des;
dayNum = day;
monthNum = month;
yearNum = year;
}
}
以下是我的3个子类:
一次约会: Onetime
public class OneTime extends Appointment{
public OneTime(String des, int day, int month, int year){
super(des, day, month, year);
}
public String getDesc(){
return desc;
}
}
每月一次约会: Monthly
public class Monthly extends Appointment{
public Monthly(String des, int monum){
super(des, monum);
}
public String getDesc(){
return desc;
}
}
每年一次约会 Yearly
public class Yearly extends Appointment{
public Yearly(String des, int day, int month){
super(des, day, month);
}
public String getDesc(){
return desc;
}
}
问题:我被要求向Appointment
添加少量ArrayList
个对象(我知道)。但是,我必须编写一个方法occursOn(int day, int month, int year)
,它返回当天的所有约会。我不确定的部分是编写occursOn()
方法。假设我有一个关于11/22/2018的约会,我如何将每月对象与day参数/ Yearly与月和日参数/ OneTime
进行比较,与所有参数进行比较?
教科书问题:"实现超类Appointment
和子类Onetime
,Daily
和Monthly
。约会有一个描述(例如,"见牙医")和日期。编写方法occursOn(int year, int month, int day)
,检查约会是否在该日期发生。例如,对于每月预约,您必须检查当月的日期是否匹配。然后使用混合约会填充Appointment
个对象的数组。让用户输入日期并打印出该日期发生的所有约会。 "
注意:这不是我的功课。我只是在2个小时内对此进行了单元测试而且卡住了。
答案 0 :(得分:1)
首先,您可能希望声明Appointment
abstract,以便sublcasses必须实现一些方法:
public abstract class Appointment {
int dayNum;
int monthNum;
int yearNum;
String desc;
public Appointment(String des, int day){desc = des; dayNum = day;} //Monthly Appointments (Check day)
public Appointment(String des, int day, int month){desc = des; dayNum = day; monthNum = month;} //Yearly Appointments (Check day AND month)
public Appointment(String des, int day, int month, int year){desc = des; dayNum = day; monthNum = month; yearNum = year;} //One Time (Check All)
abstract boolean occursOn(int day, int month, int year);
abstract String getDesc();
}
然后,您班级中occursOn
的实施可能是:
<强>每月强>
boolean occursOn(int day, int month, int year){
return (this.day == day);
}
<强>每年强>
boolean occursOn(int day, int month, int year){
return (this.day == day) && (this.month == month);
}
OneTime
boolean occursOn(int day, int month, int year){
return (this.day == day) && (this.month == month) && (this.year == year);
}
最后,假设appointments
是一个List
,你填充了各种子类实例,有一个通用方法(可能在你的主类中),它将返回所有相应的{{ 1}}对象:
Appointment
答案 1 :(得分:0)
一种方法是使用&#34; date&#34;具有可选值的键,例如像这样:
class DateKey {
int day; //always needed
Integer month; //null in case of monthlies
Integer year; //null in case of monthlies and yearlies
//override equals() and hashCode()
}
使用地图的日期键:
//handling this would be easier with Google Guava's Multimap, if you may use it
Map<DateKey, List<Appointment>> appointments = ...;
然后使用3步查找:
List<Appointment> onetimes = appointments.get(new DateKey( day, month, year) );
List<Appointment> yearlies= appointments.get(new DateKey( day, month, null) );
List<Appointment> monthlies = appointments.get(new DateKey( day, null, null) );
最后结合3个列表。
最后,DateKey
也可以成为Appointment
类的一部分,这将使映射更加一致。我可能会使用稍微不同的名称(想想一个),因为它不仅仅是一个地图键了。
答案 2 :(得分:0)
您可以在Appointment类中添加一个方法,用于确定约会是否在这样的给定日期开启。
public class Appointment {
int dayNum;
int monthNum;
int yearNum;
String desc;
public Appointment(String des, int day){desc = des; dayNum = day;} //Monthly Appointments (Check day)
public Appointment(String des, int day, int month){desc = des; dayNum = day; monthNum = month;} //Yearly Appointments (Check day AND month)
public Appointment(String des, int day, int month, int year){desc = des; dayNum = day; monthNum = month; yearNum = year;} //One Time (Check All)
boolean occuresOn(int day, int month, int year){
if( (this.dayNum == day) && (this.monthNum == month) && (this.yearNum == year))
return true;
return false;
}
}
并在子类中覆盖此功能。
public class Monthly extends Appointment{
public Monthly(String des, int monum){
super(des, monum);
}
public String getDesc(){
return desc;
}
boolean occuresOn(int day, int month, int year){
if( (this.dayNum == day) )
return true;
return false;
}
}
public class Yearly extends Appointment{
public Yearly(String des, int day, int month){
super(des, day, month);
}
public String getDesc(){
return desc;
}
boolean occuresOn(int day, int month, int year){
if( (this.dayNum == day) && (this.monthNum == month))
return true;
return false;
}
}
现在你可以迭代你的约会并找到那些约会日期:
List<Appointment> aps = new ArrayList<>(); //initialize this
for (Appointment ap : aps){
if(ap.occuresOn(2, 3, 2016))
....
}
答案 3 :(得分:0)
OccursOn()
应该是超类中的抽象方法(约会)。 你的每个子类都必须相应地实现它。
一次 - 检查日,月和年。
每月 - 仅限查看日。
每年 - 检查日期和月份。