2016-11-25 56 views
1

我正在研究這個小項目,我被授予了驅動程序,並且必須爲它編寫助手類。Java初學者煩惱;從方法調用。希望小問題,

司機:

public class MyBookDriver { 

private static final Scanner KBD = new Scanner(System.in); 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) {   
    // Constructors 
    MyBookAccount bbSheldon = new MyBookAccount("Sheldon", true); 
    MyBookAccount bbPenny = new MyBookAccount("Penny", false); 
    MyBookAccount bbAmy = new MyBookAccount("Amy", "Montreal", true); 
    MyBookAccount bbLeonard = new MyBookAccount("Leonard"); 
    System.out.println("\n" + MyBookAccount.getNumAccounts() 
      + " MyBook accounts have been created."); 

    // Mybook ID 
    System.out.println("\nMyBook Accounts:"); 
    System.out.println(" Sheldon's ID: " + bbSheldon.ID); 
    System.out.println(" Penny's ID: " + bbPenny.ID); 
    System.out.println(" Amy's ID: " + bbAmy.ID); 
    System.out.println(" Leonard's ID: " + bbLeonard.ID); 
    pause(); 

    // logged in 
    System.out.println("\nMyBook Accounts:"); 
    System.out.println(" Sheldon is " 
      + (bbSheldon.isLoggedIn() ? "" : "not ") + "logged in"); 
    System.out.println(" Penny is " 
      + (bbPenny.isLoggedIn() ? "" : "not ") + "logged in"); 
    System.out.println(" Amy is " 
      + (bbAmy.isLoggedIn() ? "" : "not ") + "logged in"); 
    System.out.println(" Leonard is " 
      + (bbLeonard.isLoggedIn() ? "" : "not ") + "logged in"); 
    pause(); 

    //post a wall message 
    System.out.println("\nPosting wall update:"); 
    bbSheldon.setWallPost("I like flags!"); 
    bbPenny.setWallPost("Looking for a job."); 
    bbLeonard.setWallPost("I'm just hoping I can date a girl " 
      + "from next door."); 
    System.out.println(" Sheldon's: " + bbSheldon.getWallPost() + "\n" 
      + " Penny's: " + bbPenny.getWallPost() + "\n" 
      + " Amy's: " + bbAmy.getWallPost() + "\n" 
      + " Leonard's: " + bbLeonard.getWallPost() + "\n"); 
    pause(); 

    //Sending messages 
    System.out.println("\nSending messages:"); 
    bbLeonard.sendMessage(bbPenny, "Will you go out with me tonight?"); 
    bbAmy.sendMessage(bbSheldon, "Neuroscience is a real science."); 
    bbPenny.sendMessage(bbAmy, "What a nice picture."); 
    checkMessages(bbSheldon); 
    checkMessages(bbPenny); 
    checkMessages(bbAmy); 
    checkMessages(bbLeonard); 
    pause(); 

    //toString 
    System.out.println("\nDisplaying info:"); 
    System.out.println(bbSheldon); 
    System.out.println(bbPenny); 
    System.out.println(bbAmy); 
    System.out.println(bbLeonard); 
    pause(); 
} 

private static void checkMessages(MyBookAccount user) { 
    MyBookAccount aFriend; 
    aFriend = user.getFriend(); 
    if (aFriend != null) { 
     System.out.println(" " + user.getName() + "'s message from " 
       + aFriend.getName() 
       + " is " + user.getMessage()); 
    } else { 
     System.out.println(" " + user.getName() + " has no messages"); 
    } 
} 


private static void pause() { 
    System.out.print("\n...press enter..."); 
    KBD.nextLine(); 
} 
} 

我的(凌亂未完)代碼:

public class MyBookAccount { 

public final int MAX_CHAR = 20; 
public final int ID; 
public static int nextId = 1; 
private String name; 
private String location; 
private Boolean loggedIn; 
private String wallPost = "(none)"; 
private String latestMessage = "(none)"; 
private MyBookAccount friend = null; 
private static int numberOfAccounts = 0; 

MyBookAccount(String n, String l, Boolean i) { 
    name = n; 
    location = l; 
    loggedIn = i; 
    ID = nextId; 
    nextId++; 
    numberOfAccounts++; 
} 

MyBookAccount(String n, Boolean i) { 
    name = n; 
    location = "Halifax"; 
    loggedIn = i; 
    ID = nextId; 
    nextId++; 
    numberOfAccounts++; 
} 

MyBookAccount(String n) { 
    name = n; 
    location = "Halifax"; 
    loggedIn = false; 
    ID = nextId; 
    nextId++; 
    numberOfAccounts++; 
} 

public static int getNumAccounts() { 
    return numberOfAccounts; 
} 

public void setLoggedIn(boolean log) { 
    loggedIn = !log; 
} 

boolean isLoggedIn() { 
    return loggedIn; 
} 

public void setWallPost(String newPost) { 
    if (newPost.length() > MAX_CHAR) { 
     System.out.println("Cannot update wall post for " + name 
       + ". Post must be 20 characters or less."); 
    } else { 
     wallPost = newPost; 
    } 

} 

public String getWallPost() { 
    return wallPost; 
} 

public String getMessage() { 
    return this.latestMessage; 
} 

public void sendMessage(MyBookAccount to, String message) { 
    friend = to; 
    if (to.loggedIn != true) { 
     System.out.println("Could not post message from " + name 
       + ". " + to.name + " is not logged in!"); 
     latestMessage = "(none)"; 
    } else if (to.loggedIn == true) { 
     latestMessage = message; 
    } 
} 

public MyBookAccount getFriend() { 
    return friend; 
} 

public void setName(String n) { 
    name = n; 
} 

public String getName() { 
    return name; 
} 

public void setLocation(String location) { 
    this.location = location; 
} 

public String getLocation() { 
    return location; 
} 

@Override 
public String toString() { 
    if (friend == null) { 
     return "MyBookAccount #" + ID + "{\n " 
       + name + " in " + location + "\n " 
       + "About me: " + wallPost + "\n " 
       + "Logged In:" + loggedIn + "\n "; 

    } else { 
     return "MyBookAccount #" + ID + "{\n " 
       + name + " in " + location + "\n " 
       + "About me: " + wallPost + "\n " 
       + "Logged In:" + loggedIn + "\n " 
       + "Message from " + friend.name + ": " 
       + latestMessage + ".\n"; 
    } 

} 
} 

我只是想不通一兩件事。

在信息部分,我從中得到了來自和混淆的人。例如, ;應該說

Sending messages: 
Could not post message from Leonard. Penny is not logged in! 
    Sheldon's message from Amy is Neuroscience is a real science. 
    Penny has no messages 
    Amy's message from Penny is What a nice picture. 
    Leonard has no messages 

,我也得到:

Sending messages: 
Could not post message from Leonard. Penny is not logged in! 
    Sheldon has no messages 
    Penny's message from Amy is What a nice picture. 
    Amy's message from Sheldon is Neuroscience is a real science. 
    Leonard has no messages 

如何解決這個問題的任何想法? 謝謝你的包。

回答

0

我並不想在這裏調試代碼,但讓我請對此作出評論:

public void sendMessage(MyBookAccount to, String message) { 
    friend = to; 
    if (to.loggedIn != true) { 
     System.out.println("Could not post message from " + name 
       + ". " + to.name + " is not logged in!"); 
     latestMessage = "(none)"; 
    } else if (to.loggedIn == true) { 
     latestMessage = message; 
    } 
} 

此代碼聞起來,因爲它會n ot發送任何消息,但只是改變這個(發送者的)對象(friend = to,latestMessage = ...)的狀態。此外,它會檢查接收者的狀況,而該消息應該只是發送消息並對任何不良結果作出反應。而是想象這樣的事情:

class MyBookAccount { 
    //.... 
    public void sendMessage(MyBookAccount receiver, String message) { 
     try{ 
     receiver.accept(this, message); 
     }catch(MessageRejectedException e){ 
     //maybe put in queue to try again later, or log the date, time and reason of failure. 
     } 
    } 
} 

其中:

class MyBookAccount { 
    private final List<String> receivedMessages = new ArrayList<>(); 
    ... 
    public void accept(MyBookAccount sender, String message){ 
     if(!loggedIn){ 
     throw new MessageRejectedException("not online"); 
     } 
     receivedMessages.add(message); 
     //you can also have a list of Objects that are like 
     //class Message{String senderName; String message; Date reveived; /*...*/} 
     //trigger UI update or fire property changed event that announces the list of messages has changed 
    } 

    public Optional<String> getLastMessage(){ 
    return receivedMessages.isEmpty() ? Optional.empty() 
      : Optional.of(receivedMessages.get(receivedMessages.size()-1)); 
    } 

這樣的消息的接收器,它需要接收消息,或有多少郵件它喜歡在條件完全控制保持,或者如果它喜歡用消息記錄日期(當收到消息時)。發送者不關心,接收者是否只是一個代理,接收者本身或發送消息必須滿足什麼條件,它只需處理可能的錯誤條件(可能很多) - 它是免費的忽略或記錄它們。

+0

好的,花了我一些時間和一些谷歌搜索瞭解你寫的,哈哈我還是很新鮮哈哈,但是,這是有道理的。非常感謝! – GnarlyTot

+0

不客氣。 – Brixomatic

0

我認爲方法sendMessage(MyBookAccount to, String message) {的開始出了問題。既然你給別人發了消息,那麼這個朋友應該被設置給接收者?

所以寧可是:

public void sendMessage(MyBookAccount to, String message) { 
    to.setFriend(this); 

然後在MyBookAccount您添加這樣一個方法:

public void setFriend(MyBookAccount friend) { 
     this.friend = friend; 
} 
+0

我認爲朋友應該以某種方式設置給發件人,但我無法弄清楚如何做到這一點。 – GnarlyTot

+0

查看我的編輯帖子 – JFPicard