5 Replies - 299 Views - Last Post: 24 July 2012 - 05:08 PM Rate Topic: -----

#1 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Change Return Program

Posted 19 July 2012 - 05:55 PM

Hello,

I am a beginner in Java and I have created this program simply for learning purposes. This program does currently work correctly, but I am wondering from some of the more experienced Java developers, if you could give me some ideas on how to make this program better.
I am more than open to criticism and I would appreciate any and all help. Like I said before, I am just trying to learn so I would love to hear anything at all that would benefit this program.

Thanks.

Class


import java.text.DecimalFormat;

public class getChange {

	private final double Quarter_Value = .25;
	private final double Dime_Value = .10;
	private final double Nickel_Value = .05;
	private final double Pennie_Value = .01;
	
	DecimalFormat twoDForm = new DecimalFormat("#.##");

	// find number of quarters
	public int Quarters(double change) {

		change -= (int) change;
		change /= Quarter_Value;

		return (int) change;

	}

	public int Dimes(double change) {

		change -= (int) change;
		change %= Quarter_Value;
		change = Double.valueOf(twoDForm.format(change));
		change /= Dime_Value;

		return (int) change;

	}

	public int Nickels(double change) {

		change -= (int) change;
		change %= Quarter_Value;
		change %= Dime_Value;
		change = Double.valueOf(twoDForm.format(change));
		change /= Nickel_Value;

		return (int) change;

	}

	
	public int Pennies(double change) {

		change -= (int) change;
		change %= Quarter_Value;
		change %= Dime_Value;
		change %= Nickel_Value;
		change = Double.valueOf(twoDForm.format(change));
		change /= Pennie_Value;

		return (int) change;

	}

}








Main Method

import java.text.DecimalFormat;
import java.util.Scanner;

public class changeReturn {

	/**
	 * @param args
	 * @return 
	 */
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		getChange co = new getChange();
		
		double cost = 0;
		double amt = 0;
		boolean ok = false;
		
		while (ok == false) {
			
			System.out.print("Please enter the Cost of your item: ");
			cost = sc.nextDouble();
			
			System.out.print("Please enter the Amount you have payed: ");
			amt = sc.nextDouble();
			
			if(cost<=amt){
				System.out.println("\nThank you for your payment.");
				ok = true;
			}else if(cost>amt){
				System.out.println("\nYou have entered an invalid amount.  Please try again.\n");
			}
			
		}
		
		
		DecimalFormat d = new DecimalFormat("$#0.00");
		
		double change = amt-cost;
		
		System.out.println("\nYour change back comes to: " + d.format(change));
		
		System.out.println("\nNumber of quarters to recieve back: "+co.Quarters(change));
		System.out.println("\nNumber of dimes to recieve back: "+co.Dimes(change));
		System.out.println("\nNumber of nickels to recieve back: "+co.Nickels(change));
		System.out.println("\nNumber of pennies to recieve back: "+co.Pennies(change));
				
		
		System.out.println("\nThank You.");
		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Change Return Program

#2 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

Re: Change Return Program

Posted 19 July 2012 - 06:29 PM

How about turn it into a cash register?
Multiple prices (with item name); tax; discount; or put the whole thing on JFrame for the inputs and everything
Was This Post Helpful? 1
  • +
  • -

#3 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Re: Change Return Program

Posted 19 July 2012 - 06:38 PM

View PostSilverEleak, on 19 July 2012 - 06:29 PM, said:

How about turn it into a cash register?
Multiple prices (with item name); tax; discount; or put the whole thing on JFrame for the inputs and everything


Thank you, SilverEleak that is a good idea. I have been messing around with GUI stuff a little and I think this could become a pretty cool program.

I was also hoping someone may be able to give a general criticism on the code I have provided. As I said, I am a beginner and I'm trying to learn as much as possible. If you see any blatant mistakes concerning code placement or anything, maybe a more advanced concept that could benefit my code that a beginner may not have considered. This would be a great help to me as well.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,819
  • Joined: 10-September 10

Re: Change Return Program

Posted 20 July 2012 - 06:56 AM

Class getChange:

By convention, class names in Java begin with capital letters: GetChange

Another convention: constants are all caps: Quarter_Value = QUARTER_VALUE, etc. (Not sure you really need the "_VALUE" part of each variable name, but that's up to you.)

(As an aside, it seems the underscore character is not used as frequently in Java variable names as in other languages. However, one of the common uses is in the names of capitalized constants to separate words.)

Comments throughout your code would be helpful. For example, this statement occurs throughout:

change -= (int) change;

It's odd looking, so an explanation of its purpose would be helpful. Also, the same statement occurs 4 times, once each at the beginning of every method.

Upon further inspection, each method has a lot of repeated code. Since repeated code is a red flag for wasted effort and/or execution time, I wonder if the 4 methods couldn't be cascaded (or rolled into a single method) so that calling a single method takes advantage of all that code without actually repeating it in the 4 methods as it's done now.

To your main() method:

Use better variable names. They're not too bad, though I'm not in favor of one- and two-letter names like 'd', 'sc', 'ok', and 'co'.

Good use of the while statement to ensure the user inputs valid amounts, but while ( ok == false ) is poor practice. while ( ok ) is sufficient. (Another example of a bad variable name: what does ok mean? Names like 'getInput' or 'inputNeeded' are better.)

Most prefer the use of whitespace around arithmetic and assignment operators and comparators: /, *, +, -, <=, >=, ==, etc. You're practice is currently to be inconsistent, so decide instead to be consistently good.

Keep source code lines to reasonable lengths. Most use 80 columns of a fixed-width font in which the one and lower-case 'L' characters can't be confused.

Program operation:

No comments really. Seems to work fine. Perhaps an option to repeat until done, but that's a "nice to have" for testing and may not be how you intended the program to work.

Add the paper money (or $1 coins) returned someday.

A minor spelling thing: "Amount you have payed" should be "amount you have paid" plus the other random capitalization you did with 'Cost.'

Hope this helps.

This post has been edited by GregBrannon: 20 July 2012 - 06:57 AM

Was This Post Helpful? 2
  • +
  • -

#5 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Change Return Program

Posted 20 July 2012 - 12:18 PM

Cent are int no need to define them as double

Then you can do:

long cents = (long) change * 100.0;
cents %= 100;

then quarter is cent % 25 and so on
Was This Post Helpful? 1
  • +
  • -

#6 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Re: Change Return Program

Posted 24 July 2012 - 05:08 PM

View PostGregBrannon, on 20 July 2012 - 06:56 AM, said:

Class getChange:

By convention, class names in Java begin with capital letters: GetChange

Another convention: constants are all caps: Quarter_Value = QUARTER_VALUE, etc. (Not sure you really need the "_VALUE" part of each variable name, but that's up to you.)

(As an aside, it seems the underscore character is not used as frequently in Java variable names as in other languages. However, one of the common uses is in the names of capitalized constants to separate words.)

Comments throughout your code would be helpful. For example, this statement occurs throughout:

change -= (int) change;

It's odd looking, so an explanation of its purpose would be helpful. Also, the same statement occurs 4 times, once each at the beginning of every method.

Upon further inspection, each method has a lot of repeated code. Since repeated code is a red flag for wasted effort and/or execution time, I wonder if the 4 methods couldn't be cascaded (or rolled into a single method) so that calling a single method takes advantage of all that code without actually repeating it in the 4 methods as it's done now.

To your main() method:

Use better variable names. They're not too bad, though I'm not in favor of one- and two-letter names like 'd', 'sc', 'ok', and 'co'.

Good use of the while statement to ensure the user inputs valid amounts, but while ( ok == false ) is poor practice. while ( ok ) is sufficient. (Another example of a bad variable name: what does ok mean? Names like 'getInput' or 'inputNeeded' are better.)

Most prefer the use of whitespace around arithmetic and assignment operators and comparators: /, *, +, -, <=, >=, ==, etc. You're practice is currently to be inconsistent, so decide instead to be consistently good.

Keep source code lines to reasonable lengths. Most use 80 columns of a fixed-width font in which the one and lower-case 'L' characters can't be confused.

Program operation:

No comments really. Seems to work fine. Perhaps an option to repeat until done, but that's a "nice to have" for testing and may not be how you intended the program to work.

Add the paper money (or $1 coins) returned someday.

A minor spelling thing: "Amount you have payed" should be "amount you have paid" plus the other random capitalization you did with 'Cost.'

Hope this helps.



Wow, thank you so much Greg, this is exactly the type of response I was hoping for! Thank you as well pbl, that is also helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1