Write a pogram to prepare the monthly charge account statement for a custome of
ID: 3621274 • Letter: W
Question
Write a pogram to prepare the monthly charge account statement for a custome of CS CARD International, a credit card company. The program should take as input the previous balance on the account and the total amount of additional charges during the month. The program should then compupte the interest for the month, the total new balance (the previous balance plus additional charges plus interest), and the minimum payment due. Assume the interest is 0 if the previous balance was 0 but if the previous balance was greater than 0 the interest is 2% of the total owed (previous balance plus additional charges).Assume the minimum payment is as follows:
new balance //for a new balance less than $50
$50.00 //for a new balance between $50 and $300 (inclusive)
20% of the new balance // for a new balance over $300
So if the new balance is $38 then the person must pay the whole $38; if the balance is $128 then the person must pay $50; if the balance is $350 the minimum payment is $70 (20% of 350). The program should print the charge account statement in the format below. Print the actual dollar amounts in each place using currency format from the NumberFormantclass-see listing 3.4 of the text for an example that uses this class.
CS CARD INTERNATIONAL Statemtnt
========================
Previous Balance: $
Additional Charges: $
Interest: $
New Balance: $
Minimum Payment: $
Explanation / Answer
import java.text.NumberFormat;
import java.util.Scanner;
public class ChargeCard {
public ChargeCard() {
intro();
}
public static void main(String[] args) {
// make an Object of this class so that it won't be
// static this, static that. We gain method()s
// java keyword 'new' makes an Object in computer memory
new ChargeCard();
}
private void computeCharges(Double bal, Double chgs) {
String pattern = "#,###.##;(#,###.##)";
NumberFormat nf = NumberFormat.getCurrencyInstance();
double interest = 0.0;
System.out.println();
printHeader();
System.out.println("Previous balance: " +
nf.format(bal));
System.out.println("Additional charges: " +
nf.format(chgs));
if (bal > 0) {
interest = .02 * bal;
System.out.println("Interest @ 2%: " +
nf.format(interest));
System.out.println();
bal += interest;
}
bal += chgs;
double minPay = bal;
if (bal >= 50.0 && bal <= 300.0) {
minPay = 50.0;
} else if (bal > 300.0) {
minPay = bal * .20;
}
System.out.println("New Balance: " +
nf.format(bal));
System.out.println("Minimum Payment: " +
nf.format(minPay));
System.out.println(
"------------------------" );
}
private void intro() {
Scanner sc = new Scanner(System.in);
do {
printHeader();
mess("Enter the previous balance");
double balance = loopInput(sc);
mess("Enter any additional charges");
double charges = loopInput(sc);
computeCharges(balance, charges);
mess("Do another? [y/n]");
} while (!sc.nextLine().equalsIgnoreCase("n"));
System.out.println("Thank you, and goodbye!");
}
private Double loopInput(Scanner sc) {
String sNum;
do {
sNum = sc.nextLine();
} while (!validate(sNum));
return Double.parseDouble(sNum);
}
private boolean validate(String value) {
try {
Double.parseDouble(value);
return true;
} catch (Exception e) {
mess("----------------- ");
mess("Please enter a valid amount");
return false;
}
}
private void mess(String s) {
System.out.printf("%s ==>", s);
}
private void printHeader() {
String head =
"CS CARD International Statement ";
head += "=============================== ";
System.out.println(head);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.