• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class help  RSS feed

 
Genesis Garcia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I recently finished writing a code that returns an insurance quote based on the information provided by the user. My problem is that I am supposed to separate all the calculations into a different class, but I am not fully aware of how to make that happen. Since the program already does everything I wanted it to do all I need to do is move all the messy stuff into a different file (Insurance) and keep all the questions that require user input in the main class. I am a total beginner and honestly don't really know where to start, but the code I have so far is here:
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Genesis Garcia wrote: My problem is that I am supposed to separate all the calculations into a different class, but I am not fully aware of how to make that happen.


Here would be an easy way.

For example change this



to this. Make all you calculations methods in the class Calculations. Also -- note what happens if you enter a letter instead of a number. Never let something like that crash the program.



 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down all your thoughts on paper and try what thoughts are related and can be clubbed together and separate with others. Also read about SOLID principle.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever I see a large switch statement or a bunch of if-else statements checking whether a variable has a certain constant value, I generally opt to use enums instead.

Make enums called Sex, VehicleType and VehicleBrand. Associate each constant with a multiplier. Make a Quote class that you instantiate with the age, sex, number of tickets, and the type and brand of the vehicle. All your main class has to do now is convert user input to strongly typed values, which you can use to create a Quote with. An instance of Quote can calculate its value in a compact method.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an example, here's what VehicleType could look like:
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To further clarify, when you're working with money, NEVER USE FLOATING POINT ARITHMETIC. Sorry for shouting, but this is important.

Float and double are like couches. They eat your loose change.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can find more about big decimal here (read whole of thread because there are later additions) and (two shorter threads: 1 2). Remembering that 0.1D is slightly larger than 0.1, there is no chance of the loop I showed in link 2 running more than 10× (unless you try it out ‍).
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: . . . Float and double are like couches. They eat your loose change.
They can spit out the change later, like in that loop which never runs more than 10×.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually yeah, couches are more reliable than doubles. At least when I lose my change in a couch, I know where to find it later.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whereas a double may never find it again.

Shall we get back to the original subject of the thread?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!