2016-11-25 5 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; 
    } 
} 

このコードは、となりますので、このコードは匂いがします。 oメッセージは送信しますが、のこの(送信者の)オブジェクト(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

関連する問題