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

r3i60tvu  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(338)

我正在尝试用java编写日历。我创建了3个类:
1.日期(包括年、月……)
2.事件(包括人、地点、上课日期…+创建日期的选项)
3.mainclass包含菜单的我的mainclass。
我的问题是,我不知道用户如何创建自己的日期,因为我必须自己创建对象终端。。。有人能帮我修一下吗?提前谢谢!

  1. public class Event {
  2. private String mDescription, mPlace, mNames;
  3. private Date mStart, mEnd;
  4. Termin(String description, String place, String names, Date start, Date end) {
  5. mBetreff = description;
  6. mOrt = place;
  7. mNamen = names;
  8. mBeginn = start;
  9. mEnde = end;
  10. }
  11. public void create() {
  12. Scanner read = new Scanner(System.in);
  13. System.out.println("Enter 1. description 2. place 3. names 4. start 5. end ein");
  14. mDescription = read.nextLine();
  15. mPlace = read.nextLine();
  16. mNames = read.nextLine();
  17. }
  18. public String toString() {
  19. return "Description : " + mDescription + "\nPlace: " + mPlace + "\nNames: " + mNames + "\nIts starts at " + mStart
  20. + " and ends at " + mEnd;
  21. }
  22. }
  23. public class Date {
  24. private int year, day, month, hours, minutes;
  25. Datum(int year, int month, int day, int hours, int minutes) {
  26. this.day= day;
  27. this.year= year;
  28. this.month= month;
  29. this.hours= hours;
  30. this.minutes= minutes;
  31. }
  32. public String toString() {
  33. return "\n" + day + "." + month + "." + year + " um " + hours+ ":" + minutes;
  34. }
  35. public void enterDate() {
  36. }
  37. }

编辑:
两年前我问过这个问题,那时我刚开始编写代码,对oop和封装一无所知。。。
回答我自己的问题,对于每一个尝试创建终端日历的新手:
日期需要以下方法:

  1. public setDate() {
  2. this.year = read.nextLine();
  3. ...
  4. }

为每个成员。
事件在构造函数或类似setter的方法中获取结果对象日期。

yx2lnoni

yx2lnoni1#

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

  1. public class Main {
  2. private int value;
  3. private Main(int value) {
  4. this.value = value;
  5. }
  6. public int getValue() {
  7. return (this.value);
  8. }
  9. public static class MainBuilder {
  10. boolean valueWasSet;
  11. int value;
  12. public MainBuilder() {
  13. this.valueWasSet = false;
  14. this.value = -1;
  15. }
  16. public void setValue(int value) {
  17. this.value = value;
  18. this.valueWasSet = true;
  19. }
  20. public Main build() {
  21. if (!this.valueWasSet) {
  22. throw new IllegalStateException("value must be set before a Main can be build.");
  23. }
  24. return (new Main(this.value));
  25. }
  26. }
  27. }

(这是一个简化的草图,展示了如何在构建 Main 通过 MainBuilder .
构造一个模型的过程 Main 可能是:

  1. MainBuilder builder = new MainBuilder();
  2. builder.setValue(100);
  3. // all following Main's will have a value of 100
  4. Main mainOne = builder.build();
  5. Main mainTwo = builder.build();
  6. builder.setValue(200);
  7. // all following Main's will have a value of 200
  8. Main mainThree = builder.build();
  9. Main mainFour = builder.build();
展开查看全部

相关问题