• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Suggestion on handling two different expected results using the same code in my assignment?

 
Ranch Hand
Posts: 283
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Introduction
I'm working through an online course, MOOC Java part 2 https://materiaalit.github.io/2013-oo-programming/part2/week-9/.   Week 9, section 26.  It's the last assignment for this section and we are supposed to design the solution on our own.   That being said, I'm struggling with trying to keep from repeating code within methods.  Also object oriented.  The below program isn't a finished program.  It needs the Address part , and add error handling.

I hope I can explain this well enough.  The issue really is with two commands at the moment and what to do with responses expected when the information isn't found.  It's not the entire program.  

The assignment:

The assignment is a phone and address search.   Right now I'm working through the phone part and the problem I'm struggling with is the  assignment wants slightly different expected results between #2 and #5 when the phone number isn't found.
The program is supposed to have 8 commands .  1 - 7 does different things, 8 is to quit.

Commands 1, 2 and 5:
  • 1. adds a (phone) number
  • 2. search(es) for a (phone) number
  • 5. search(es) for personal information ( i.e. phone number and address )


  • Example:



    The Issue:

    Both #2 and #5 searches for a phone number using the person's name.   But if the phone number isn't found, the expected result for #2 is "not found" where #5 it's "phone number not found".

    In the Search class.
    For command #2 I have a method name searchPhoneNumberByName(Person person, String name ).  
    For #5 I have method name searchForAllInformation(Person person) and inside that method it calls the searchPhoneNumberByName(Person person, String name ) method.  

    The searchPhoneNumberByName(Person person, String name ) doesn't return anything.  It only calls a search method on the Person class and prints the result.




    I'm stuck here
    I can't think about what to do past the following options and none seem correct.

    A.  Repeat the phone number search I used for #2 rather than have #5's method, searchForAllInformation call searchPhoneNumberByName.   But that goes against the whole DRY concept doesn't it?
    B.  change searchPhoneNumberByName to return a String and handle printing out the results on  the UserInterface class?  It seems like I should just get rid of the Search class then.
    C.  change searchPhoneNumberByName to call another method on the Search class to handle the null and empty?  


    Help Please

    I'm stuck.   I think I may just need to re-organize everything but I'm not sure what is the best way.    Should I get rid of the Search class altogether and instead do the searching in my UserInterface class ?  This would eliminate one layer at least.  I could have a method to call Person's getPhone method and another method to handle the null responses on UserInterface rather than on Search.




    Program is organized as

    I have 6 class files.
  • Main - just to start the program
  • UserInterface - gives instructions, takes user's commands.
  • Search - performs the various searches
  • Person - stores the phone and address information.  
  • Phone - "helper" class ( I think that's what it's called ).  Just stores the Phone info
  • Address - Same as Phone but for addresses











  • UserInterface class




    Search class




    Person






    Phone



    Address











    Main







     
    Rancher
    Posts: 1952
    15
    Android Java ME Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The searchPhoneNumberByName(Person person, String name ) doesn't return anything.  It only calls a search method on the Person class and prints the result.




    The return type of the method is void.  What are you trying to return from this method?
     
    Lisa Austin
    Ranch Hand
    Posts: 283
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Swastik Dey wrote:

    The searchPhoneNumberByName(Person person, String name ) doesn't return anything.  It only calls a search method on the Person class and prints the result.




    The return type of the method is void.  What are you trying to return from this method?



    This is the method called when the user enters command #2 and currently it prints "  not found" when person.getPhone returns a null or empty.      If the user enters command #5 the method searchForAllInformation(Person person) is called.  In the method it calls searchPhoneNumberByName(Person person, String name) because the assignment requires a person's phone number to be printed as part of command #5 but if person.getPhone returns a null or empty the assignment expects "  phone number not found" to be printed.

    My question is.     Is there a good option for both commands to use the method searchPhoneNumberByName(Person person, String name) and still print the two different messages for the same scenario ( if person.getPhone returns a null or empty the assignment expects " )?    

    I'm not asking a How To question .  Like I'm not asking how to get searchPhoneNumberByName(Person person, String name) to return anything.  I know it's a void right now and I can change it to a string and return those messages rather than have them print out.   That maybe what I need to do.  Have it return the Null and based on whether the user had entered "2" or "5" , it prints the appropriate response.

    I guess I was hoping someone would see how I have things organized / done and could help me see other options than what I see.


    I'm stuck here
    I can't think about what to do past the following options and none seem correct.

    A.  Repeat the phone number search I used for #2 rather than have #5's method, searchForAllInformation call searchPhoneNumberByName.   But that goes against the whole DRY concept doesn't it?
    B.  change searchPhoneNumberByName to return a String and handle printing out the results on  the UserInterface class?  It seems like I should just get rid of the Search class then.
    C.  change searchPhoneNumberByName to call another method on the Search class to handle the null and empty?





     
    Sheriff
    Posts: 6108
    157
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One of the things I noticed is that you have UI in your Person class.  addPhone() shouldn't prompt for or input a phone number, it should just be:

    Also, I think UserInterface is doing more than it should be.  The actual logic of putting data into Persons and search should be a its own class.  Let's call it PersonMaintenance.  This class would call UserInterface to print the menu and to prompt and get data.  It would contain the logic for searching Persons.  The Search class would go away in this model.  Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.  Anything with a System.out.println() or a scanner would be in UserInterface and only there.

    Does this structure make sense to you?
     
    Lisa Austin
    Ranch Hand
    Posts: 283
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:One of the things I noticed is that you have UI in your Person class.  addPhone() shouldn't prompt for or input a phone number, it should just be:

    Also, I think UserInterface is doing more than it should be.  The actual logic of putting data into Persons and search should be a its own class.  Let's call it PersonMaintenance.  This class would call UserInterface to print the menu and to prompt and get data.  It would contain the logic for searching Persons.  The Search class would go away in this model.  Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.  Anything with a System.out.println() or a scanner would be in UserInterface and only there.

    Does this structure make sense to you?



    Thank You.  I THINK it makes sense to me.  After writing this post I was skimming over class about design and I believe I saw something like what you are talking about.   I'll try and put what you suggest in practice.  Thank You

     
    Lisa Austin
    Ranch Hand
    Posts: 283
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Knute Snortum I hope you see this.      
    Can you please tell me if I have the right idea?  Also again my address part isn't finished as of yet still.


    Knute Snortum wrote:One of the things I noticed is that you have UI in your Person class.  addPhone() shouldn't prompt for or input a phone number, it should just be:




    Okay so I took out ALL user input from my Person class.


    Knute Snortum wrote:

    Also, I think UserInterface is doing more than it should be.  The actual logic of putting data into Persons and search should be a its own class.  Let's call it PersonMaintenance.  This class would call UserInterface to print the menu and to prompt and get data.  It would contain the logic for searching Persons.  The Search class would go away in this model.  Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.  Anything with a System.out.println() or a scanner would be in UserInterface and only there.

    Does this structure make sense to you?



    I did the following:
  • Removed the Search class
  • I created the PersonMaintenance class


  • On the PersonMaintenance class
  • All of the logic of putting data and searching for data


  • On the UserInterface class
  • Has all print statements ( Anything with System.out.println() )
  • Prints the menu , and prompts for UI



  • Knute Snortum wrote:

    Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.



    But yet I don't know how to make this happen .    Right now I have the UserInterface calls methods on PersonMaintenance if the method returns a null value it prints that the phone number wasn't found but I think you are suggesting the opposite?  If the phone number isn't found, the method doing the phone number search calls a methods on the UserInterface to prints the statements?  


    Here are my changes














     
    Knute Snortum
    Sheriff
    Posts: 6108
    157
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That looks better.  You have the UI isolated in a class.  But I was thinking that PersonMaintenance would be the class driving the process.  That is, it would have the start() method and that would call the printMenuoptions() from UserInterface (you will need an instance of the class unless you make the method static).  PersonMaintenance would have the enterMenuOption() method in it and so on.  So PersonMaintenance would have all the logic to "maintain" a Person (add a Person, search, etc.)  Then printing "not found" or "phone not found" would be calls to UserInterface.
     
    Lisa Austin
    Ranch Hand
    Posts: 283
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:That looks better.  You have the UI isolated in a class.  But I was thinking that PersonMaintenance would be the class driving the process.  That is, it would have the start() method and that would call the printMenuoptions() from UserInterface (you will need an instance of the class unless you make the method static).  PersonMaintenance would have the enterMenuOption() method in it and so on.  So PersonMaintenance would have all the logic to "maintain" a Person (add a Person, search, etc.)  Then printing "not found" or "phone not found" would be calls to UserInterface.



    Hi Knute Snortum  sorry so late responding.  I'll get that PersonMaintenance class updated.  I've been struggling with the last requirement of my project and I think I may need to redo everything.  The command #7 wants if the user doesn't enter a keyword then all information ( phone and address ) will print to screen , in order of how it was entered.  I have been struggling to figure out IF there is a way I can do this with how I have things structured.   With separate Person, Phone and Address classes but I've hit a wall so I'm thinking about re-working / refactoring .

    Thank You however for all your help with this and understanding my issue.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!