hey there, I am new to Java programming.
While solving Cash Register challenge I had an issue with my java code if anyone can help me out or can give suggestion that would help me alot.
Here is the code:
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class CashRegister{
public static void main(String[] args) throws IOException{
Scanner fileScanner = (args.length > 0) ? new Scanner(new File(args[0])) : new Scanner(System.in);
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
if(!line.equalsIgnoreCase("")){
String elements[] = line.split(";");
float price = Float.parseFloat(elements[0]);
float cash = Float.parseFloat(elements[1]);
double change = cash - price;
if(change < 0){
System.out.println("ERROR");
}else if(change == 0){
System.out.println("ZERO");
}else{
System.out.println(findChange(change));
}
}
}
fileScanner.close();
}
private static String findChange(double change){
String textChange = "";
DecimalFormat df = new DecimalFormat("########.##");
int intChange = (int)(Double.valueOf(df.format(change)) * 100);
while(intChange >= 0.01){
if(intChange >= 10000){
textChange += "ONE HUNDRED,";
intChange -= 10000;
} else if(intChange >= 5000){
textChange += "FIFTY,";
intChange -= 5000;
} else if(intChange >= 2000){
textChange += "TWENTY,";
intChange -= 2000;
} else if(intChange >= 1000){
textChange += "TEN,";
intChange -= 1000;
} else if(intChange >= 500){
textChange += "FIVE,";
intChange -= 500;
} else if(intChange >= 200){
textChange += "TWO,";
intChange -= 200;
} else if(intChange >= 100){
textChange += "ONE,";
intChange -= 100;
} else if(intChange >= 50){
textChange += "HALF DOLLAR,";
intChange -= 50;
} else if(intChange >= 25){
textChange += "QUARTER,";
intChange -= 25;
} else if(intChange >= 10){
textChange += "DIME,";
intChange -= 10;
} else if(intChange >= 5){
textChange += "NICKEL,";
intChange -= 5;
} else if(intChange >= 1){
textChange += "PENNY,";
intChange -= 1;
}
}
return textChange.substring(0, textChange.length() - 1);
}
}