2017-01-03 89 views
-1

我目前正在嘗試用java編碼日曆。 我創建了3類:用戶如何在我的日曆(java)中創建日期?

1日期(包括年,月...)

2. TERMIN(包括人,地方,類Date ... +一個選項創建日期)

3. Mainclass我的主菜單包含菜單。

我的問題是,我不知道用戶如何能夠創建自己的日期,因爲我必須創建對象Termin自己...所以,有人可以幫我解決這個問題嗎? Thx提前!

public class Termin { 
private String mBetreff, mOrt, mNamen; 
private Datum mBeginn, mEnde; 

Termin(String betreff, String ort, String namen, Datum beginn, Datum ende) { 
    mBetreff = betreff; 
    mOrt = ort; 
    mNamen = namen; 
    mBeginn = beginn; 
    mEnde = ende; 
} 

public void erstellen() { 
    Scanner read = new Scanner(System.in); 
    System.out.println("Gib 1. Betreff 2. Ort 3. Namen 4. Beginn 5. Ende ein"); 
    mBetreff = read.nextLine(); 
    mOrt = read.nextLine(); 
    mNamen = read.nextLine(); 
} 

public String toString() { 
    return "Betreff: " + mBetreff + "\nOrt: " + mOrt + "\nTeilnehmer: " + mNamen + "\nEs beginnt am " + mBeginn 
      + " und endet am " + mEnde; 
} 
} 

public class Datum { 
private int mJahr, mTag, mMonat, mStunden, mMinuten; 

Datum(int jahr, int monat, int tag, int stunden, int minuten) { 
    mTag = tag; 
    mJahr = jahr; 
    mMonat = monat; 
    mStunden = stunden; 
    mMinuten = minuten; 
} 

public String toString() { 
    return "\n" + mTag + "." + mMonat + "." + mJahr + " um " + mStunden + ":" + mMinuten; 
} 

public void datumEingeben() { 
} 
} 
+0

請翻譯你的代碼的關鍵部分如參數名稱堆棧溢出。是基於英語的。 –

回答

0

創建實例的方法來創建約會是怎麼樣的......奇怪,因爲人們需要創建約會(你的情況稱爲Termin)創建約會。一種可能性是builder pattern。具有公共靜態內部生成器類,你可以設置構造函數(S)私人和強制使用的是建設者:

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)); 
     } 
    } 
} 

(這是簡化的草圖顯示瞭如何斷言某些核心機制。值是通過MainBuilder構建Main之前設置

構建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(); 
+0

謝謝!我感謝您的幫助 – Coldvirus