java Write a program which asks for the following information: the date the name
ID: 3805252 • Letter: J
Question
java
Write a program which asks for the following information: the date the name of the person who the check is for the amount of the check Using this information you should display a check formatted exactly like the example. the cash amount should be formatted to two decimal places. the hardest part about this problem will be formatting the amount written out into words. You will need to figure out how to play with the cash amount in order to determine which words to put together for the amount in words. To get full credit your program must be able to correctly generate a check for all possible amounts between 0.00 and 5000.00 print f will be useful in solving this problem. Input Validation: Do not accept values greater than 5,000. Do not accept negative amounts. A check for only a cents amount should be valid. What is the amount of the check? 1920.85 Enter the date of the check: 1/21/2015 Who is the check for?: John Smith Pay to the Order of: John Smith One thousand nine hundred twenty and 85 centsExplanation / Answer
CODE
package chegg;
import java.util.HashMap;
import java.util.Scanner;
public class FormatInvoice {
public static void main(String[] args) {
// TODO Auto-generated method stub
String date;
String name;
double amount = -1;
Scanner s = new Scanner(System.in);
System.out.println("What is the amount of the check?:");
amount = s.nextDouble();
System.out.println(amount);
while ((amount<=0) || (amount>=5000)) {
System.out.println("Entered value is not betwen 0 to 5000- reenter again");
System.out.println("What is the amount of the check?:");
amount = s.nextDouble();
}
System.out.println("Enter the date of the check?:");
date = s.next();
System.out.println("Who is the check for?:");
name = s.next();
HashMap <Integer, String> words= new HashMap<>();
words.put(0, "zero");
words.put(1, "one");
words.put(2, "two");
words.put(3, "three");
words.put(4, "four");
words.put(5, "five");
words.put(6, "six");
words.put(7, "seven");
words.put(8, "eight");
words.put(9, "nine");
String count[] ={"", "ten", "hundred", "thousand"};
System.out.println("-------------------------------------");
System.out.printf("%30s %s","Date:",date);
System.out.println(" ");
System.out.printf("Pay of the Order of : %s $ %.2f",name,amount);
System.out.println(" ");
int cents = (int) ((amount*100)%100);
int rupee = (int) amount;
String op = "";
if (cents >0) {
op = "and "+cents +" cents";
}
int j = 0;
while(rupee>0) {
int val = rupee%10;
rupee = rupee/10;
if (val>0) {
op = words.get(val)+ " "+count[j] + " "+ op;
}
j++;
}
String s1 = op.substring(0, 1).toUpperCase();
String nameCapitalized = s1 + op.substring(1);
System.out.println(nameCapitalized);
}
}
Sample output
What is the amount of the check?:
3891.12
3891.12
Enter the date of the check?:
1/2/2016
Who is the check for?:
Naren
-------------------------------------
Date: 1/2/2016
Pay of the Order of : Naren $ 3891.12
Three thousand eight hundred nine ten one and 12 cents
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.