/* 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:
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);
}
}