将用户输入与数组与布尔值进行比较

问题描述

这是我收到的作业

执行超类约会,并将子类一次,每日和每月。约会具有描述(例如,“请看牙医”),并且约会发生在一个或多个日期。编写一个方法existOn(int year,int month,int day)来检查约会是否在该日期发生。例如,对于每月约会,您必须检查每月的日期是否匹配。然后,用混合的约会填充一个约会对象数组。让用户输入日期并打印出该日期发生的所有约会。

我认为困扰我的部分是这部分

每个子类中的OccursOn方法,用于检查约会是否在该日期(OneTime),日期(Day)或月份(Month)发生。要求用户输入日期进行检查。根据用户选择的内容,每个子类中的OccursOn应该运行并显示任何匹配的约会和关联的描述。对于OneTime子类,OccursOn需要三个输入(年,月和日)进行验证,对于Day子类,OccursOn需要一个输入(日)进行验证,而对于Month子类,OccursOn需要一个输入(月)进行验证。对于不同的子类,OccursOn有所不同。

这是我的超人

class Appointment{
private int year;
private int month;
private int day;
private String description;
public Appointment(int day,int month,int year,String description){
    this.year = year;
    this.month = month;
    this.day = day;
    this.description = description;
}

public int getYear(){
    return year;
}

public int getMonth(){
    return month;
}

public int getDay(){
    return day;
}

public boolean occursOn(int day,int year){
    return (day == this.day) && (month == this.month) && (year == this.year);
}

public String toString(){
    return description;
}}

子类:

class Daily extends Appointment
{
public Daily(int day,String description)
{
    super(day,month,year,description);
}

@Override
public boolean occursOn(int year,int day)
{
    if(getDay() == day) return true;
    return false;
}}

class Monthly extends Appointment
{
public Monthly(int year,int day,String description)
{
    super(year,day,description);
}

public boolean occursOn(int year,int day)
{
    if((getDay() == day) && ((getMonth() == month))) return true;
    return false;
}}

class Onetime extends Appointment
{
public Onetime(int year,description);
}

@Override
 public boolean occursOn(int day,int year){
    return (getDay() == day) && (getMonth() == month) && (getYear() == year);
   }}

使用该方法,我如何将用户输入与提供的数组进行比较,并与每个子类的方法进行比较

import java.util.Scanner;
class AppointmentDemo
{
public static void main(String[] args)
{
Appointment[] appointment = new Appointment[5];
    appointment[0] = new Onetime(25,12,2017,"Root Canal");
    appointment[1] = new Monthly(25,"Teeth cleaning");
    appointment[2] = new Daily(25,"Filling");
    appointment[3] = new Onetime(13,"Crown");
    appointment[4] = new Monthly(15,4,"Dentures");

//ask user for input
Scanner keyboard = new Scanner(system.in);
//day
System.out.println("Please enter the day of your appointment.");
int day = keyboard.nextInt();
//Month
System.out.println("Please enter the month of your appointment.");
int month = keyboard.nextInt();
//Year
System.out.println("Please enter the year of your appointment.");
int year = keyboard.nextInt();

for (int i = 0; i < appointment.length; i++)
{
  if (appointment[i].occursOn(year,day)==true)
  {
    System.out.println(("You have an appointment for ") + appointment[i].toString() + " on " + day +     "/"+ month + "/" + year);
  }
}}} 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)