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

Simple Finance Calculator - any tips?  RSS feed

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey - just wipped this up:



Any hints or suggestions to improve this?
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Updated:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain lines 13-15? You are taking an int, converting it to double and then to int again.

You may improve this by not using static methods. Also consider not mixing getting user input with calculations.
Split your program into smaller methods (also read this).

Do not use magic numbers in the code (like 50000 or 100/80). Create constants for those values.

Use meaningful variable names. What does d or i stand for?

A double should not be used for money calculations (rounding errors). Consider using BigDecimal instead.
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Can you explain lines 13-15? You are taking an int, converting it to double and then to int again.

You may improve this by not using static methods. Also consider not mixing getting user input with calculations.
Split your program into smaller methods (also read this).

Do not use magic numbers in the code (like 50000 or 100/80). Create constants for those values.

Use meaningful variable names. What does d or i stand for?

A double should not be used for money calculations (rounding errors). Consider using BigDecimal instead.


I was trying to convert an integer to double or cast. Failed obviously - edited now.

How do I declare constants?

Thanks for the tip.

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constant is usually reclared like
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Turner wrote:How do I declare constants?


Like so...



Here public means it's accessible to any other class -- right now you don't have any other classes but in a larger application this might be an issue.

And static means there's only one instance of the variable which belongs to all instances of the class.

And final means that no code anywhere can assign a new value to the variable, which is the part which makes it a constant.

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Name of class Project1 describes nothing what precisely this class do so you can rename It something like FinancialCalculator Or something else more descriptive. As you are already adviced about variable names also, you can read about naming convention in this article, click here ---> Java Programming Style Guide
  • Pawel gave you a link, I hope you might have gone through It. You can create two separate method one for input from user and other to calculate income. Perhaps getInput() and calculateIncome() Or something you may think more expressive.
  • Go step by step, you can make the changes you are already asked and repost your new code for more improvement If there is any need to.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!