用户如何在我的日历(java)中创建日期



我目前正在尝试用java编写日历。我创建了 3 个类:

1. 日期(包括年、月...

2.事件(包括人物,地点,班级日期... +创建日期的选项)

3. 主类 包含菜单的主类。

我的问题是我不知道用户如何能够创建自己的日期,因为我必须自己创建对象 Termin......那么,有人可以帮助我解决这个问题吗?提前感谢!

public class Event {
   private String mDescription, mPlace, mNames;
   private Date mStart, mEnd;
Termin(String description, String place, String names, Date start, Date end) {
    mBetreff = description;
    mOrt = place;
    mNamen = names;
    mBeginn = start;
    mEnde = end;
}
public void create() {
    Scanner read = new Scanner(System.in);
    System.out.println("Enter 1. description 2. place 3. names 4. start 5. end ein");
    mDescription = read.nextLine();
    mPlace = read.nextLine();
    mNames = read.nextLine();
}
public String toString() {
    return "Description : " + mDescription + "nPlace: " + mPlace + "nNames: " + mNames + "nIts starts at " + mStart
            + " and ends at " + mEnd;
 }
}
public class Date {
   private int year, day, month, hours, minutes;
Datum(int year, int month, int day, int hours, int minutes) {
    this.day= day;
    this.year= year;
    this.month= month;
    this.hours= hours;
    this.minutes= minutes;
}
public String toString() {
    return "n" + day + "." + month + "." + year + " um " + hours+ ":" + minutes;
}
public void enterDate() {
}
}

编辑:

我在 2 年前问过这个问题,当时我刚开始编码,对 oop 和封装一无所知......

为了回答我自己的问题,对于每个也尝试创建终端日历的新手:

日期需要以下方法:

public setDate() {
   this.year = read.nextLine();
   ...
}

对于每个成员。

Event 采用结果对象 Date,无论是在构造函数中还是在类似 setter 的方法中。

创建一个实例方法来创建约会是一种...奇怪,因为需要创建一个约会(在您的情况下称为Termin)来创建约会。一种可能性是构建器模式。通过拥有公共静态内部构建器类,可以将构造函数设置为私有并强制使用该构建器:

 public class Main {
    private int value;
    private Main(int value) {
        this.value = value;
    }
    public int getValue() {
        return (this.value);
    }
    public static class MainBuilder {
        boolean valueWasSet;
        int value;
        public MainBuilder() {
            this.valueWasSet = false;
            this.value = -1;
        }
        public void setValue(int value) {
            this.value = value;
            this.valueWasSet = true;
        }
        public Main build() {
            if (!this.valueWasSet) {
                throw new IllegalStateException("value must be set before a Main can be build.");
            }
            return (new Main(this.value));
        }
    }
}

(这是一个简化的草图,展示了如何在构造Main之前通过MainBuilder断言某些值设置的核心机制。

构建Main的过程将是:

MainBuilder builder = new MainBuilder();
builder.setValue(100);
// all following Main's will have a value of 100
Main mainOne = builder.build();
Main mainTwo = builder.build();
builder.setValue(200);
// all following Main's will have a value of 200
Main mainThree = builder.build();
Main mainFour = builder.build();

最新更新