package billing.util; import java.math.RoundingMode; import java.text.NumberFormat; import java.util.Scanner; public class CurrencyFormat { private static final int USD = 0; private static final int JPY = 1; private static final int CNY = 2; private static final String US_DOLLAR_SYMBOL = "$"; private static final String JP_YEN_SYMBOL = "\\u00A5"; private static final String CN_YUAN_SYMBOL = "\\u00A5"; // Object fields private final NumberFormat formatter; // Number formatter wrapped by this class private int currencyType; // Currency type code private String currencySymbl; // private int fractionDigits; private int maxIntDigits; /** Creates a currency object in the default format. * * * * */ public CurrencyFormat(){ currencyType = USD; currencySymbl = US_DOLLAR_SYMBOL; fractionDigits = 2; maxIntDigits = 0; formatter = NumberFormat.getInstance(); formatter.setRoundingMode(RoundingMode.HALF_UP); formatter.setMinimumFractionDigits(fractionDigits); formatter.setMaximumFractionDigits(fractionDigits); if (maxIntDigits > 0) { formatter.setMaximumIntegerDigits (maxIntDigits); } } public static CurrencyFormat getInstance(){ return new CurrencyFormat(); } public String format (double input) { //determin integer digits in number int intDigits = 0; if (input>=1) { intDigits =1 +(int) Math.log10(input); } //determines spaces for currency symbol alignment int spaces = maxIntDigits - intDigits; //Use one less alignment space if a zero is front of the decimal place is wanted if (spaces == maxIntDigits ) { spaces --; } //format the number portion. input = (Math.round (input * Math.pow(10,fractionDigits)) / (Math.pow (10.0, fractionDigits))); String baseFormat =formatter.format (input); //Start buildning the currency format . Start with the currency Symbol, then add alignment spaces. StringBuilder finalFormat = new StringBuilder (currencySymbl); for ( int i =0; i<spaces; i ++) { finalFormat.append(" "); } //Check if an additional alignment space is needed for the thousands //separator. if(maxIntDigits > 3 && intDigits <4) { finalFormat.append(" "); } //Add the formatted number to the right of the currency symbol and alignment spaces. finalFormat.append(baseFormat); return finalFormat.toString(); } public void setFractionDigits (int fractionDigits){ formatter.setMinimumFractionDigits(fractionDigits); formatter.setMaximumFractionDigits(fractionDigits); this.fractionDigits = fractionDigits; } public void setInterDigits (int intergerDigits){ maxIntDigits = intergerDigits; formatter.setMaximumIntegerDigits(maxIntDigits); } public void setCurrency (int currencyCode){ if (currencyCode == JPY){ currencyType = JPY; currencySymbl = JP_YEN_SYMBOL; } else if (currencyCode == CNY){ currencyType = CNY; currencySymbl = CN_YUAN_SYMBOL; } else if (currencyCode == USD) currencyType = USD; currencySymbl = US_DOLLAR_SYMBOL; } public static void main (String [] args) { CurrencyFormat currencyFormatter = new CurrencyFormat(); Scanner sc = new Scanner(System.in); System.out.println("Enter currency, interger digits, fraction digits, and input" + "number: "); String currency = sc.next(); int intergerDigits = sc.nextInt(); int fractionDigits = sc.nextInt(); double number = sc.nextDouble(); switch (currency){ case "JPY": currencyFormatter.setCurrency(JPY); break; case "CNY": currencyFormatter.setCurrency (CNY); break; } currencyFormatter.setInterDigits(intergerDigits); currencyFormatter.setFractionDigits(fractionDigits); System.out.println("Test Case result " + currencyFormatter.format(number)); } }
i should be able to use USD, CNY, and JPY. However it seems only the USD ($) will run... doesnt matter which currency symbol the user requests