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.");
}
}

New Topic/Question
Reply



MultiQuote




|