Code:
import java.awt.*;
import javax.swing.*;
public class Lab5 extends JApplet implements ActionListener
{
public void actionPerformed(ActionEvent e);
{
//determines what will occur when the buttons are pressed
if(e.getSource()==submit)
performCalculations();
else
ClearGUI();
}
public void performCalculations()
{
double billAmountEntered = Double.parseDouble(totalBill.getText().trim());
int numCustomers = Integer.parseInt(people.getText().trim());
string country = (string)countries.getSelectedItem();
string typeOfTip = (string)tip.getSelectedIndex();
pic = getImage(getCodeBase(),"Images/"+country+".png");
double percentOfTip;
double taxPercentUsed;
double totalCalculation;
double amountPerPersonCalculation;
double tipAmount;
double taxAmount;
//calculates the tip according to the user input
if(country==0)
;
else if(country==1)
typeOfTip = tip.getSelectedIndex();
taxPercentUsed = .08;
billAmount.setText(billAmountEntered);
taxAmountDisplay.setText(taxAmount);
totalBill.setText(totalCalculation);
personAmount.setText(amountPerPersonCalculation);
if(typeOfTip==0)
tip=0.0;
else if(typeOfTip==1)
tip=.13;
else if(typeOfTip==2)
tip=.15;
else
tip=.20;
percentOfTip = tip;
tipAmount = billAmountEntered * percentOfTip;
taxAmount = billAmountEntered * taxPercentUsed;
totalCalculation = billAmountEntered + tipAmount + taxAmount;
else if(country==2)
typeOfTip = tip.getSelectedIndex();
taxPercentUsed = .05;
billAmount.setText(billAmountEntered);
taxAmountDisplay.setText(taxAmount);
totalBill.setText(totalCalculation);
personAmount.setText(amountPerPersonCalculation);
if(typeOfTip==0)
tip=0.0;
else if(typeOfTip==1)
tip=.13;
else if(typeOfTip==2)
tip=.15;
else if(typeOfTip==3)
tip=.20;
percentOfTip = tip;
tipAmount = billAmountEntered * percentOfTip;
taxAmount = billAmountEntered * taxPercentUsed;
totalCalculation = billAmountEntered + tipAmount + taxAmount;
else if(country==3)
typeOfTip = tip.getSelectedIndex();
taxPercentUsed = .08;
billAmount.setText(billAmountEntered);
taxAmountDisplay.setText(taxAmount);
totalBill.setText(totalCalculation);
personAmount.setText(amountPerPersonCalculation);
if(typeOfTip==0)
tip=0.0;
else if(typeOfTip==1)
tip=.10;
else if(typeOfTip==2)
tip=.12;
else if(typeOfTip==3)
tip=.15;
percentOfTip = tip;
tipAmount = billAmountEntered * percentOfTip;
taxAmount = billAmountEntered * taxPercentUsed;
totalCalculation = billAmountEntered + tipAmount + taxAmount;
}
public void ClearGui()
{
//sets the applet to its original state
tip.setSelectedIndex(0);
countries.setSelectedIndex(0);
includeTax.setSelected(true);
validate();
}
//the layout code
public void init()
{
setLayout(new BorderLayout());
JLabel title = new JLabel("Multi Country Tip Calculator");
JComboBox countries = new JComboBox();
JLabel checkAmount = new JLabel("Check Amount:");
JTextField billAmount = new JTextField(8);
JLabel tax = new JLabel("Tax:");
JTextField taxAmountDisplay = new JTextField(8);
JCheckBox includeTax = new JCheckBox("Include Tax");
DefaultListModel myModel = new DefaultListModel();
JList tip = new JList(myModel);
JLabel tipPercentUsed = new JLabel("Tip % Used:");
JTextField tipPercentSelection = new JTextField(8);
JLabel total = new JLabel("Total:");
JTextField totalBill = new JTextField(8);
JLabel numberOfPeople = new JLabel("Number of People:");
JTextField people = new JTextField(3);
JLabel amountPerPerson = new JLabel("Amount Per Person:");
JTextField personAmount = new JTextField(8);
JButton submit = new JButton("Submit");
JButton clear = new JButton("Clear");
//adds a JPanel and its components to the north portion of the border layout
JPanel north = new JPanel(new FlowLayout());
add(north, BorderLayout.NORTH);
north.add(title);
//adds a JPanel and its components to the west portion of the border layout
JPanel west = new JPanel(new FlowLayout());
add(west, BorderLayout.WEST);
west.add(countries);
//adds a JPanel and its components to the east portion of the border layout
JPanel east = new JPanel(new GridLayout(2,1));
add(east, BorderLayout.EAST);
east.add(tip);
east.add(includeTax);
//adds a JPanel and its components to the center portion of the border layout
JPanel center = new JPanel(new GridLayout(5,2));
add(center, BorderLayout.CENTER);
center.add(tipPercentUsed);
center.add(tipPercentSelection);
center.add(checkAmount);
center.add(billAmount);
center.add(tax);
center.add(taxAmountDisplay);
center.add(numberOfPeople);
center.add(people);
//adds a JPanel and its components to the south portion of the border layout
JPanel south = new JPanel(new GridLayout(4,4));
add(south, BorderLayout.SOUTH);
south.add(total);
south.add(totalBill);
south.add(amountPerPerson);
south.add(personAmount);
south.add(submit);
south.add(clear);
//sets the title's font, color, size, and location
title.setForeground(Color.blue);
title.setFont(new Font("Serif",Font.BOLD,40));
title.setLocation(200,200);
//adds items to the combo box
countries.addItem("Please Select A Country");
countries.addItem("United States");
countries.addItem("Italy");
countries.addItem("France");
//adds items to the list
myModel.addElement("No tip");
myModel.addElement("Minimum tip");
myModel.addElement("Normal tip");
myModel.addElement("High tip");
//changes the background color
north.setBackground(Color.ORANGE);
south.setBackground(Color.ORANGE);
west.setBackground(Color.ORANGE);
center.setBackground(Color.ORANGE);
east.setBackground(Color.ORANGE);
//sets all the JLabels' colors
total.setForeground(Color.blue);
amountPerPerson.setForeground(Color.blue);
checkAmount.setForeground(Color.blue);
tax.setForeground(Color.blue);
tipPercentUsed.setForeground(Color.blue);
numberOfPeople.setForeground(Color.blue);
//sets the size of "Total" and "Amount Per Person"
total.setFont(new Font("Serif",Font.BOLD,20));
amountPerPerson.setFont(new Font("Serif",Font.BOLD,20));
//sets the color of the JButtons
submit.setBackground(Color.red);
submit.setForeground(Color.blue);
clear.setBackground(Color.red);
clear.setForeground(Color.blue);
//sets the colors in the JList
tip.setBackground(Color.white);
tip.setForeground(Color.blue);
//sets the colors of the JCheckBox
includeTax.setForeground(Color.blue);
includeTax.setBackground(Color.white);
//sets the colors of the JComboBox
countries.setForeground(Color.blue);
countries.setBackground(Color.white);
}
