• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need some light please  RSS feed

 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I guess this is the first time I'm actually opening a topic here, but I've actually been around for a little while

So, I'm working on this case for one of my classes. I have most of it already written, but there are a few things I'm trying to figure out and need help with. I'm supposed to write a class called ChatModel to hold ChatMessage Beans, such that:



Now here is the Bean I wrote. Nothing fancy :P



The ChatMessage class works as intended. Now here is what I have for the ChatModel class.



when I run this in the main method of ChatModel (just to test it)


This is what I get on the console:

is Ben in testModel? true
is Lucas in testModel? true
is Jack in testModel? true
is Bob in testModel? true
is message1 in testModel --> { Bob } ? true
is message2 in testModel --> { Ben } ? true
is message3 in testModel --> { Bob } ? true
is message4 in testModel --> { Jack } ? true
is message5 in testModel --> { Bob } ? true
is message6 in testModel --> { Jack } ? true
is message7 in testModel --> { Ben } ? true
is message8 in testModel --> { Lucas } ? true
is message9 in testModel --> { Ben } ? true
Receiver: Bob
Messsage: nothing

I'm puzzled all the tests come true, but the ArrayList I'm getting from getMessagesFor() apparently has only one ChatMessage object for "Bob" when it should have 3. When I add breakpoints to the code on Eclipse and watch it stepping through the code, it seems "message" is getting all the entries correctly, hence the containsKey() tests all came true. But when I look the ArrayList returned by getMessagesFor() there is only one entry when there should be more. Another thing I noticed is that the message entry seems to always be the last one added to the treemap.

By the way, I apologize for the inelegant and noobish code I wrote. Please feel free to point out style tips and anything whatsoever you might be thinking of. I welcome you advice! :P

Anyway, Java gurus out there, please share some of your knowledge with me!

Thanks in advance!

-Ben
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == false or == true. Not only are they poor style, but one day you will write = instead of == and the compiler won’t notice. It’s if (b) .... and if (!bb) ...
Use spaces not tabs for indenting. Avoid lots of blank lines inside methods; double-spaced code isn’t particularly easy to read.
Declare the three String fields in the message class in three lines, rather than one multiple declaration. It takes more space, but it easier to read.

Why have you got a TreeMap<String, ChatMessage> rather than TreeMap<Date, ChatMessage>? The java.util.Date class appears to have properly-overridden equals() and hashCode() methods, so you could use a Date. Beware: since Date has setXXX methods, albeit deprecated, and the sent field isn’t final, you have at least two possible ways to try to get a message from that Map with a different Date from that which it was “put” in with, and as you doubtless know, you won’t find anything in a Map if that happens.
I presume you are using a TreeMap so you can iterate it and get the messages in date order.
I would suggest you declare the type of listOfMessages as List<E> like thisYou are right to return an empty List if you don’t find anything.

I can’t see offhand why you are not getting an output. You appear to be adding messages all right, and the rest of the code looks good. I shall have to try executing your code to see what happens.
Have you tried printing the size of the List before you return it? Or simply print the List to System.out; that should display all the messages too.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[campbell@campbell-Inspiron-1110 java]$ java ChatModelis Ben in testModel? true
is Lucas in testModel? true
is Jack in testModel? true
is Bob in testModel? true
is message1 in testModel --> { Bob } ? true
is message2 in testModel --> { Ben } ? false
is message3 in testModel --> { Bob } ? true
is message4 in testModel --> { Jack } ? false
is message5 in testModel --> { Bob } ? true
is message6 in testModel --> { Jack } ? false
is message7 in testModel --> { Ben } ? false
is message8 in testModel --> { Lucas } ? false
is message9 in testModel --> { Ben } ? false
Receiver: Bob
Messsage: Hello World!
Receiver: Bob
Messsage: How are you?
Receiver: Bob
Messsage: What's up
Receiver: Bob
Messsage: nothing
What is happening is that you are creating all those messages so close together that their when field or whatever you call it is always the same. A Map can only have one copy of each “K”, so you are “putting” all your different “V”s for the same “K”, and the later “V”s are overwriting the earlier ones. I got it to work by making two changes to your main method:
  • 1: Wrap the entire method body in a try-catch
  • 2: Write Thread.sleep(1000) after each new Message call.
  • So the thing sits there apparently doing nothing for 9 seconds, before printing the four messages to Bob, each message now having a different time sent.
    Design problem: what is going to happen if two messages occur simultaneously? What is the granularity of the timers on different computers?
     
    Wendy L Gibbons
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:

    Why have you got a TreeMap<String, ChatMessage> rather than TreeMap<Date, ChatMessage>? The java.util.Date class appears to have properly-overridden equals() and hashCode() methods, so you could use a Date. Beware: since Date has setXXX methods, albeit deprecated, and the sent field isn7#x2019;t final, you have at least two possible ways to try to get a message from that Map with a different Date from that which it was “put” in with, and as you doubtless know, you won’t find anything in a Map if that happens.
    n output. You appear to be adding messages all right, and the rest of the code looks good. I shall have to try executing your code to see what happens.


    i think the answer to that is the diagram at the top was provided by the teacher and asks for strings not dates
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why let things like “you must use a String as your K” get in the way of a good story?
     
    Wendy L Gibbons
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    he does need to use a time in mils as the key, or a formatted time date string, if it needs to be human readable
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I see your point.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ben, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' responses.

    https://forums.oracle.com/forums/thread.jspa?threadID=2338022&tstart=0
    http://www.coderanch.com/t/565563/java/java/some-light-please
     
    Ben Alex Barreto
    Greenhorn
    Posts: 15
    Java Mac Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:Ben, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' responses.

    https://forums.oracle.com/forums/thread.jspa?threadID=2338022&tstart=0
    http://www.coderanch.com/t/565563/java/java/some-light-please


    I apologize for the cross posting. I didnt think about that until I read your post, so I'm really sory!

    And thank you guys SO much for helping me with my code. The UML, was indeed provided by my teacher, but he said we are to implement and improve it whatever way we think is better, but then when we meet with him we must explain why we decided to implement it the way we did. I'm saying "we" because those cases are supposed to be group projects... lol nevermind.

    Anyway, I can't tell you how much i appreciate all the sugestions! Thank you so much!

    Edit: And for the problem of receiving simoutineous messages, i thought about writing a control statement inside the addMessage method to compare the message's time stamp to the key entries in the hashmap, and if they are the same, as well as the sender of the message, just append the two messages together. What do you think?
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you are sorting messages by sender before sorting by time sent, doesn’t that obviate that particular problem?

    That is for you to answer; after all it is your project!

    And . . . “you’re welcome
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!