Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Organising output from methods  RSS feed

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Hopefully someone can help me here because its really starting to confuse me, I have all my code written and it works as is required but how to output this to the console has be stumped (i've already reorganised my code three times but still I have problems).

Basically I have adopted a Test Driven Design using JUnit and have many testcases which test the numerical output from my methods.

Part of my specification says I must output to the console a series of reports, these reports are basically the totals from my methods.

So at first I created my calculating methods which perform the mathematics and thus the tests pass, but now I need to output this to screen, basically my question is where do I put the output statments?
I don't want to put them in the JUnit tests as this doesn't seem good practice, if I put the output statements in the method code then when other TOTALLINGmethods call these lower methods the output gets duplicated making the console output incredibly messy.

I find it difficult to believe I must write two almost identical methods one for returning the numerical value and one for outputting values?

Any advice at all would be appreciated.

Thank you!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't want to put them in the JUnit tests as this doesn't seem good practice

Actually, this is probably the best, since the report is part of the testing process. If you want, you could write a method that is called by the test method that prints the results, but hat might be more trouble than its worth. Since I don't know what your code actually looks like, what follows is a template that should (hopefully) guide you:


[ December 07, 2004: Message edited by: Joel McNary ]
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel,

What if the methods loop through an ArrayList of objects and I want to output say the Name and Value while also summing the total value of the array?

Then my method returns the total but i'd also need someway of printing the names?

Thanks
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to post an example to make my last post slightly more understandable:



So I call this code from calculateEntireRegion (and I want the contents and value output to the screen) but then I also call it to include the fourcourts value in the calculateCompanyAssets() method but this time its being added with other values to calculate a different figure and this time I don't want the figures output to the screen just a simple return of the value.

How do I get around the problem of printing the object contents for every item in the ArrayList but not printing them when this function is called again?

I find it difficult to believe acceptable practice is to create two near identical methods but one outputs the values to screen and the other returns the value to a calculating method.. e.g...




[ December 07, 2004: Message edited by: David Dickinson ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!