import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.text.*; import java.util.*; /** This is a controller class that provides an ActionListener for the * the GenericGUI view class. All of your code will go inside this class. */ public class LocaleController implements ActionListener { // Reference to the view private GenericGUI view; /** Constructor: starts up the program. Creates a view with * max( min(i,MAX_FIELDS), 0) integer fields, * max( min(d,MAX_FIELDS), 0) double fields, and * max( min(s,MAX_FIELDS), 0) String fields * and a "ready" button */ public LocaleController(int i, int d, int s) { view = new GenericGUI(i,d,s); view.setTitle("Locale Converter"); // Place here any code that you are asked to write view.getButton().addActionListener(this); view.showWindow(); } /** Place code in the body of this method to process fields and store * results in fields. Use * * getIntField(i) for the number in int field i; 0 is first field * getdoubleField(i) for the number in double field i; 0 is first field * getStringField(i) for the number in String field i; 0 is first field * setIntField(i,v); to store int value v in field i * setDoubleField(i,v); to store double value v in field i * setStringField(i,v); to store String value v in field i*/ /** Process click of button titled "Ready!" */ public void actionPerformed(ActionEvent e) { /* Using the selected locale, place the value in double * field 0 in * String field 0 as a decimal number * String field 1 as a currency * String field 2 as a percentage */ double v= view.getDoubleField(0); print3(v); // Place here any code that you are asked to write return; } /** Create an instance of me and show it */ public static void main(String args[]) { new LocaleController(0,1,6); } /** If j < 0, print d in all available currencies * If j = 0, print d in all available percentages * If j > 0, print d in all available number formats */ public static void printInAll(double d, int j) { Locale[] locales = NumberFormat.getAvailableLocales(); NumberFormat form = null; if (j < 0){ System.out.println("Currencies"); } else if (j == 0) { System.out.println("Percentages"); } else if (j > 0) { System.out.println("Decimal format"); } // inv: the information for locales[0..i-1] has been printed for (int i= 0; i != locales.length; i= i+1) { if (locales[i].getCountry().length() == 0) { continue; // skip --it's a language only } System.out.print(locales[i].getDisplayName()); if (j < 0) { form= NumberFormat.getCurrencyInstance(locales[i]); } else if (j == 0) { form= NumberFormat.getPercentInstance(locales[i]); }else if (j > 0) { form= NumberFormat.getInstance(locales[i]); } try { System.out.print(": " + form.format(d)); } catch (IllegalArgumentException iae) {} try { System.out.println(" -> " + form.parse(form.format(d))); } catch (ParseException pe) {} } } /** Place v as a decimal number, currency, and percentage in the * first three String fields, using the selected locale */ public void print3(double v) { try { Locale loc= view.getLocale(); NumberFormat form= NumberFormat.getInstance(loc); view.setStringField(0, "Decimal: " + form.format(v)); form= NumberFormat.getCurrencyInstance(loc); view.setStringField(1, "Currency: " + form.format(v)); form= NumberFormat.getPercentInstance(loc); view.setStringField(2, "Percentage: " + form.format(v)); } catch (IllegalArgumentException iae) {} } }