IN JAVA, Write a method that takes a monetary value less than $1000.00 and write
ID: 3779583 • Letter: I
Question
IN JAVA,
Write a method that takes a monetary value less than $1000.00 and writes the word equivalent of that amount.
Test your method in a program that prompts the user to enter a double representing a certain amount of money, then prints out the same value in word form.
I have most of it done, I just need help adding the "monetary value less than $1000.00" also with some outputs for example, 100.50 prints one hundred and 5/100 not one hundred and 50/100. I don't know how to code around that. I could post my code if needed.
import java.util.Scanner;
public class CheckAmount
{
public static final String[] "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };
public static final String[] tens = {"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };
public static final String[] teens = {"", "ten ", "twenty ", "thirty ", "fourty ", "fifty ","sixty ", "seventy ", "eighty ", "ninety " };
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
String temp = "";
//Get Dollar Value
System.out.print("Enter check amount: ");
double amount = input.nextDouble();
int dollar = (int) amount;
//get any change
double change = amount - dollar;
//if there is a hundreds component
if (dollar >= 100)
temp += getMoney(dollar/100) + "hundred ";
//output the rest of the dollars
temp += getMoney(dollar % 100) + "and ";
//output the change
temp += getChange ( change );
System.out.print(temp);
}
public static String getMoney( int dollar )
{
//if there is less than ten dollars
if (dollar < 10)
//access the ones array
return ones [dollar];
//if the amount is in the teens
else if ( dollar < 20 )
//access the teens array
return tens [ dollar - 10];
//if the amount is more than 20
else
//create the amount with the tens and ones array
return teens [ dollar / 10 ] + ones [ dollar % 10 ];
} //end method getMoney
public static String getChange ( double amount )
{
// calculate the change as an integer value
double change = (double) (amount * 100);
// output the change of 100
//System.out.print(Math.round(change));
return (Math.round(change)) + "/100";
}
}
Explanation / Answer
import java.util.Scanner;
public class CheckAmount
{
public static final String[] "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };
public static final String[] tens = {"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };
public static final String[] teens = {"", "ten ", "twenty ", "thirty ", "fourty ", "fifty ","sixty ", "seventy ", "eighty ", "ninety " };
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
String temp = "";
//Get Dollar Value
System.out.print("Enter check amount: ");
double amount = input.nextDouble();
int dollar = (int) amount;
//get any change
double change = amount - dollar;
// if there is a thousands component
if(dollar <=9999 && dollar>999){
temp+= getMoney(dollar/1000) + "thousand ";
dollar = dollar - (dollar/1000)*1000;
}
//if there is a hundreds component
if (dollar <= 999 && dollar>99){
temp += getMoney(dollar/100) + "hundred ";
dollar = dollar - (dollar/100)*100;
}
//output the rest of the dollars
if(dollar <= 99 && dollar>0){
temp += getMoney(dollar % 100) ;
}
//output the change
if(change>0){
temp += " and "+ getChange ( change );
}
System.out.print(temp);
}
public static String getMoney( int dollar )
{
//if there is less than ten dollars
if (dollar < 10)
//access the ones array
return ones [dollar];
//if the amount is in the teens
else if ( dollar < 20 )
//access the teens array
return tens [ dollar - 10];
//if the amount is more than 20
else
//create the amount with the tens and ones array
return teens [ dollar / 10 ] + ones [ dollar % 10 ];
} //end method getMoney
public static String getChange ( double amount )
{
// calculate the change as an integer value
double change = (double) (amount * 100);
// output the change of 100
//System.out.print(Math.round(change));
return (Math.round(change)) + "/100";
}
}
/*
sample output
Enter check amount: 9999.99
nine thousand nine hundred ninety nine and 99/100
*/