java - 家庭作业问题。为日历约会程序编写方法时遇到问题

标签 java methods calendar appointment

import java.util.Arrays;

/**
 * Class Day: This class is used to represent one day of a schedule. The data is
 * an array of boolean values that represent whether an appointment is scheduled
 * for a particular hour of the day. There is also a description that is
 * associated with each hour of the day - saved in an array of Strings.
 *
 * Last Modified: [10FEB2020]
 * Author: [Adam Vieth]
 */

public class Day extends HandleInput {
    // Attributes

    // There are 24 hours in a day, so this
    // array should have 24 elements. If an
    // element is true, that means an appointment
    // is scheduled during that hour. If it is
    // false, then there is no appointment scheduled
    // during that hour.
    private boolean[] isBusy;

    // This also should have 24 elements, one
    // for each hour of the day. If an appointment
    // is scheduled for an hour, then this array
    // should hold a String description of the
    // appointment.
    private String[] appointmentDescription;

    /**
     * Constructor for the Day class. This should allocate memory for the attribute
     * arrays, and should initialize each hour to have no appointments.
     * 
     */
    public Day() 
    {
         isBusy = new boolean[24];
         appointmentDescription = new String[24];   
    }

    /**
     * This method should return whether or not there is an appointment scheduled
     * for a particular time of this day.
     *
     * @param hour
     *            The hour during this day whose status is being checked.
     * @return true if there is an appointment scheduled during the requested hour,
     *         false if there is not.
     */
    public boolean checkTime(int hour) {
        if(isBusy[hour] == false)
        {
            return false;
        }
        else
        {
            return true;
        }

    }

    /**
     * This method should return whether or not there is an appointment scheduled at
     * any hour of this day.
     *
     * @return true if there is an appointment scheduled during any hour of this
     *         day, false if there are no appointments for this entire day.
     */

    public boolean checkDay() 
    {   

        for(int i = 0; i < isBusy.length; i++)
        {
            if(isBusy[i] == false)
            {
                return true;
            }       
        }
        return false;

    }

    /**
     * This method should return the appointment description for the appointment at
     * the given time of this day.
     *
     * @param hour
     *            The hour during this day whose appointment description should be
     *            returned.
     * @return If an appointment is scheduled at the given hour, then the
     *         appointment description should be returned, otherwise the text "No
     *         appointment scheduled" should be returned.
     */
    public String getDescription(int hour) 
    {
        if(isBusy[hour] == true)
        {
            return appointmentDescription[hour];
        }
        else
        {
            return null;
        }
    }

    /**
     * This method should add a new appointment to the given hour for this day. This
     * method should modify the isBusy and appointmentDescription for the given day
     * only if there is not already an appointment for the given hour.
     *
     * @param hour
     *            The hour during this day for which an appointment should be made.
     * @param description
     *            The text description of the new appointment.
     * @return false if there is already an appointment scheduled during the given
     *         hour (the previous appointment information should not be modified),
     *         and true if adding the appointment was successful (there was no
     *         previous appointment).
     */
    public boolean addAppointment(int hour, String description) {

        if(isBusy[hour] == false)
        {
            appointmentDescription[hour] = description;
            isBusy[hour] = true;
            return true;
        }
        else
        {
            return false;
        }       
    }

    /**
     * This method should remove an appointment for a given hour for this day. There
     * should be no effect if there was not an appointment scheduled in the first
     * place.
     *
     * @param hour
     *            The hour during this day for which an appointment removed.
     */
    public void removeAppointment(int hour) {
        if(isBusy[hour] == true)
        {
            isBusy[hour] = false;
            appointmentDescription[hour] = null;
        }

    }

    /**
     * This method should generate a String of all appointment times and
     * descriptions for a given hour for this day. There should be no print
     * statements in this method, only the code to create a String.
     *
     * @return A String whose text is one line for each appointment on this day.
     *         Each line should have the appointment time and the appointment
     *         description. See the program description for the exact formatting.
     */
    public String toString() {
        /* TODO - write this method */
        String output = "";
        for(int i = 0; i < isBusy.length; i++)
        {
            if(appointmentDescription[i] == null)
            {
                return "No appointments scheduled for this entire day"; 
            }
            if(appointmentDescription[i] != null)
            {
                output = appointmentDescription[i];
            }

        }
        return output;
}
}

目前我在这个程序上遇到了很多问题。我感觉我写的一些方法不太有效。我在编写 toString() 方法时尤其遇到问题。这应该返回程序运行时我在给定日期的所有约会的列表。 我是java新手,很难掌握这个技巧。任何帮助或提示将不胜感激。

最佳答案

/** * 此方法应返回是否有预约安排 * 这一天的任何时间。 * * @return true 如果在此期间的任何时间有预约 * 天,如果这一天没有预约,则为 false。 */

public boolean checkDay() 
{   

    for(int i = 0; i < isBusy.length; i++)
    {
        if(isBusy[i])
        {
         return true;
        }       
    }
    return false;
}

This method is not correct, see, at the description you said: 

return true if there is an appointment scheduled during any hour of this

but, when there is no appointment, you return true.



public String toString() {

    StringBuilder output = new StringBuilder();
    // String is an immutable object. Consider use StringBuilder

      for(int i = 0; i < isBusy.length; i++)
    {
        if(appointmentDescription[i] != null)
        {
            output.append(appointmentDescription[i]);
            if( i < isBusy.length - 1){
             output.append(", ");
             }
             }

    }
    if (output.isEmpty()) {
      return "No appointments scheduled for this entire day";
    } 
     return output.toString();

}

这个toString方法,每次迭代你都在切换值。 如果您一天中的所有时间都是真实的,您将只能获得当天的最后一个预约。 而且,如果您除最后一小时外全天都有约会,您将得到:这一整天没有安排约会

关于java - 家庭作业问题。为日历约会程序编写方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60160768/

相关文章:

java - 如何检查 Java 中的 ZIP 文件是否为空?

java - 使用瘦驱动程序的 Oracle 12c 数据库连接引发 IO 错误

java - 如何从外部类的main方法调用匿名类的方法

java - zk:扩展日期框和日历小部件

java - 自定义标签无法正常工作

java - 应用程序在加载 xml 布局文件的主线程中做太多工作

VBA变量昏暗问题

Java 访问修饰符最佳实践

html - 有没有办法表明 HTML 元素以某种方式相互关联?

c# - 取消 session 的事件?