/* Lesley Hogan * July 14th 2003 * GetChangeGUI.java * (McCashier) */ package net.bunnie.dos; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import net.bunnie.*; public class GetChangeGUI extends JFrame implements ActionListener { // input number in xx.xx format, get change // for those mcdonalds workers out there // variables for do loop double change; int changeInt; int[] moneyArray = {2000, 1000, 500, 100, 25, 10, 5, 1}; int[] output = new int[8]; String[] outputNames = {"$20 bills", "$10 bills", "$5 bills", "$1 bills", "Quarters", "Dimes", "Nickels", "Pennies"}; String changeString; String name; String inputTextTitle; String labelTextReturned; // variables for formatting change String centsNowDollars; String centsInDollars; String totalCentsLengthString; String totalCentsString; int centsGivenLength; int centsGiven; int totalCents; int centsOnlyDollars; int totalCentsLength; // swing variables JButton inputButton; JLabel logoButton; JLabel inputLabel; JLabel outputResults; JPanel inputChange; JPanel inputButtons; JPanel outputChange; JTextField inputNameField; JTextField inputChangeField; ImageIcon mcdIcon; TitledBorder headerBorder; Font smallRedFont = new Font("Tahoma", Font.PLAIN, 13); Color darkRedish = new Color(200, 100, 100); String pressButton = "Get Change"; String htmlFont = ""; String inputLabelText = "" + htmlFont + "Please enter your name in the first field, and in the second field

enter the amount of money to be converted, in dollars:"; String outputLabelText = "" + htmlFont + "Output will appear here."; String packageName = "net\\bunnie\\dos\\"; String command; // swing public GetChangeGUI() { super("McCashier"); Container c = getContentPane(); // gridlayout(down, across) c.setLayout(new GridLayout(2,1)); c.setBackground(Color.white); inputChange = new JPanel(); inputButtons = new JPanel(); outputChange = new JPanel(); inputLabel = new JLabel(inputLabelText); inputNameField = new JTextField("Ronald McDonald"); inputChangeField = new JTextField("0.00"); inputButton = new JButton(pressButton); mcdIcon = new ImageIcon(packageName + "mcd.gif"); logoButton = new JLabel(mcdIcon); headerBorder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "McCashier", TitledBorder.LEFT, TitledBorder.TOP, smallRedFont, darkRedish); inputChange.setBorder(headerBorder); outputResults = new JLabel(outputLabelText); inputChangeField.setBackground(Color.white); inputChangeField.setFont(smallRedFont); inputChangeField.setForeground(darkRedish); inputNameField.setBackground(Color.white); inputNameField.setFont(smallRedFont); inputNameField.setForeground(darkRedish); inputLabel.setBackground(Color.white); inputLabel.setFont(smallRedFont); inputLabel.setForeground(darkRedish); outputResults.setBackground(Color.white); outputResults.setFont(smallRedFont); outputResults.setForeground(darkRedish); inputButtons.setLayout(new GridLayout(3,1)); inputButtons.add(inputNameField); inputButtons.add(inputChangeField); inputButtons.add(inputButton); inputChange.setLayout(new GridLayout(3,1)); inputChange.setBackground(Color.white); inputChange.add(logoButton); inputChange.add(inputLabel); inputChange.add(inputButtons); outputChange.setBackground(Color.white); outputChange.add(outputResults); c.add(inputChange); c.add(outputChange); inputButton.addActionListener(this); } // format input (.8 becomes .80) /*public String centsToDollars(int centsGiven) { centsInDollars = "" + centsGiven; centsGivenLength = centsInDollars.length(); switch (centsGivenLength) { case 1: centsNowDollars = "0.0" + centsGiven; break; case 2: centsNowDollars = "0." + centsGiven; break; default: centsOnlyDollars = (int) (centsGiven / 100); totalCents = centsGiven % 100; totalCentsLengthString = "" + totalCents; totalCentsLength = totalCentsLengthString.length(); if (totalCentsLength == 1) { totalCentsString = "0" + totalCents; } else { totalCentsString = "" + totalCents; } centsNowDollars = "" + centsOnlyDollars + "." + totalCentsString; break; } return centsNowDollars; }*/ // the actual method to get change public String getThatChange () { changeString = inputChangeField.getText(); name = inputNameField.getText(); try { change = Double.valueOf(changeString).doubleValue(); // convert to cents, output arrays labelTextReturned = ""; changeInt = (int) (change * 100); inputTextTitle = "Welcome, " + name + "!

The change for $" + CentsToDollars.centsToDollars(changeInt) + " is..."; labelTextReturned += "" + htmlFont + inputTextTitle + "

"; // do loop to go through arrays int i = 0; do { output[i] = changeInt / moneyArray[i]; changeInt = changeInt % moneyArray[i]; labelTextReturned += output[i] + " " + outputNames[i] + "
"; i++; } while (i < moneyArray.length); labelTextReturned += ""; return labelTextReturned; } catch (Exception e) { labelTextReturned = "" + htmlFont + "There was an error with your money input.

"; return labelTextReturned; } } // get change if button is pressed public void actionPerformed (ActionEvent e) { command = e.getActionCommand(); if (command.equals(pressButton)) { outputResults.setText(getThatChange()); } } // swing window + listener public static void main (String[] args) { GetChangeGUI getChange = new GetChangeGUI(); JCloseListener clickMe = new JCloseListener(); getChange.addWindowListener(clickMe); getChange.setSize(400, 400); getChange.setVisible(true); } } // window listener class JCloseListener extends WindowAdapter { public void windowClosing (WindowEvent ev) { System.exit(0); } }