I have written a GUI prgram that calculates the total expense incurred by a busi
ID: 3625367 • Letter: I
Question
I have written a GUI prgram that calculates the total expense incurred by a business person taking a trip. The program is suppose to calculate the total expense, total allowed expense, the amount saved, and the amount is owed by the travelerI created a UserInput panel that contains JTextFields so the user can input their cost.
import javax.swing.*;
import java.awt.*;
/**
The UserInputPanel class allows users to enter
all the necessary information to calculate the
total expenses, of the business travel
*/
public class UserInputPanel extends JPanel
{
//JLabels and JTextField Boxes for input and labels
private JTextField days;
private JTextField parking;
private JTextField airfare;
private JTextField rental;
private JTextField milesDrive;
private JTextField taxi;
private JTextField seminar;
private JTextField lodging;
//labels
private JLabel parkMsg;
private JLabel daysMsg;
private JLabel airMsg;
private JLabel rentalMsg;
private JLabel milesMsg;
private JLabel taxiMsg;
private JLabel semMsg;
private JLabel lodgeMsg;
/**
Constructor
*/
public UserInputPanel()
{
//Create a GridLayout
//7 rows 2 columns
setLayout(new GridLayout(8,2));
//create JLabels and JTextField
daysMsg=new JLabel("Enter the number of days");
days=new JTextField(4);
airMsg=new JLabel("Airfare");
airfare=new JTextField(4);
rentalMsg=new JLabel("Car rental");
rental=new JTextField(4);
milesMsg=new JLabel("Private car miles driven");
milesDrive=new JTextField(4);
taxiMsg=new JLabel("Taxi");
taxi=new JTextField(4);
semMsg=new JLabel("Seminar Fees");
seminar=new JTextField(4);
lodgeMsg=new JLabel("Lodging");
lodging=new JTextField(4);
//add border
setBorder(BorderFactory.createTitledBorder("Report"));
//add components to panel
add(daysMsg);
add(days);
add(airMsg);
add(airfare);
add(rentalMsg);
add(rental);
add(milesMsg);
add(milesDrive);
add(taxiMsg);
add(taxi);
add(semMsg);
add(seminar);
add(lodgeMsg);
add(lodging);
}
/**
getExpense method
@return the cost of the travelers expenses
*/
public double getExpense()
{
double expense=0.0;
String input;
String input2;
String input3;
String input4;
String input5;
String input6;
double air;
double rent;
double txi;
double sem;
double l;
double lodg;
double day;
input=airfare.getText();
air=Double.parseDouble(input);
input2=rental.getText();
rent=Double.parseDouble(input2);
input3=taxi.getText();
txi=Double.parseDouble(input3);
input4=seminar.getText();
sem=Double.parseDouble(input4);
input5=lodging.getText();
l=Double.parseDouble(input5);
input6=days.getText();
day=Double.parseDouble(input6);
lodg=l*day;
expense=lodg+sem+txi+rent+air;
return expense;
}
/**
getAllowed method calculates the total amount
of covered expenses
*/
public double getAllowed()
{
double PAID_PARKING=10.00;
double PAID_TAXI=20.00;
double PAID_LODGING=95.00;
double PAID_MILE=.27;
double allowedPark;
double allowedTaxi;
double allowedLodging;
double allowedMiles;
double allowedExpenses;
double day;
double mil;
String input;
input=days.getText();
day=Double.parseDouble(input);
input=milesDrive.getText();
mil=Double.parseDouble(input);
allowedPark=day*PAID_PARKING;
allowedTaxi=day*PAID_TAXI;
allowedLodging=day*PAID_LODGING;
allowedMiles=mil*PAID_MILE;
//calculate allowed expenses
allowedExpenses=allowedPark + allowedTaxi + allowedLodging + allowedMiles;
return allowedExpenses;
}
}
/** I also created a method that is designated to calculate the allowed expense and total expenses
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
The TravelCostPanel class allows totals to be
visible
*/
public class TravelCostPanel extends JPanel
{
private UserInputPanel user;
private JPanel buttonPanel; // To hold the buttons
private JButton calcButton; // To calculate the cost
private JButton exitButton; // To exit the application
private JLabel owedMsg;
private JLabel savedMsg;
private JLabel expenseMsg;
private JLabel allowedMsg;
private JTextField exp;
private JTextField allw;
private JTextField owe;
private JTextField save;
private JPanel panel;
/**
Constructor
*/
public TravelCostPanel()
{
//create a GridLayout
//5 rows,2 columns
setLayout(new GridLayout(5,2));
//create labels
expenseMsg=new JLabel("Total Expense");
allowedMsg=new JLabel("Expenses Allowed");
owedMsg=new JLabel("Amount Owed");
savedMsg=new JLabel("You Saved");
//set up text field
exp=new JTextField(0);
owe=new JTextField(4);
save=new JTextField(4);
allw=new JTextField(4);
//create the button panel
buildButtonPanel();
//add border
setBorder(BorderFactory.createTitledBorder("Trip Expenses"));
add(expenseMsg);
add(exp);
add(allowedMsg);
add(allw);
add(owedMsg);
add(owe);
add(savedMsg);
add(save);
add(buttonPanel);
}
/**
Build buildButtonPanel method build the button panel
*/
private void buildButtonPanel()
{
// Create a panel for the buttons.
buttonPanel = new JPanel();
// Create the buttons.
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
// Register the action listeners.
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
// Add the buttons to the button panel.
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
/**
Private inner class that handles the event when
the user clicks the Calculate button.
*/
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//variables to hold expense, allowed, owed, saved
double expense, allowed, owed, saved,owed1,owed2;
expense=user.getExpense();
exp.setText("Expense " + expense);
allowed=user.getAllowed();
allw.setText("Allowed " + allowed);
owed=expense-allowed;
owe.setText("Owed " + owed);
saved=expense-allowed;
save.setText("Saved " + saved);
}
}
/**
Private inner class that handles the event when
the user clicks the Exit button.
*/
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
/**
THE PROGRAM RUNS IN A TRIP METHOD
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
The TripGUI class allows totals to be
visible
*/
public class Trip extends JFrame
{
private UserInputPanel user; //for the users data panel
private TravelCostPanel cost; //to import the panel containing fields
private JPanel panel;
private final int WINDOW_WIDTH=350;
private final int WINDOW_HEIGHT=450;
/**
Constructor
*/
public Trip()
{
//action for close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create custom panel
user=new UserInputPanel();
cost=new TravelCostPanel();
panel= new JPanel();
//add panel
add(panel);
//add components to content panel
panel.add(user);
panel.add(cost);
pack();
setVisible(true);
}
}
AND I CREATED AN INSTANCE OF THE GUI IN
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TravelGUI
{
public static void main(String[]args)
{
Trip te = new Trip();
}
}
/**I BELIEVE THE ISSUE IS IN THE TRAVELCOSTPANEL.JAVA FILE
LINE 102
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//variables to hold expense, allowed, owed, saved
double expense, allowed, owed, saved,owed1,owed2;
expense=user.getExpense();
exp.setText("Expense " + expense);
allowed=user.getAllowed();
allw.setText("Allowed " + allowed);
owed=expense-allowed;
owe.setText("Owed " + owed);
saved=expense-allowed;
save.setText("Saved " + saved);
}
}
EVERYTHING SUCCESSFULLY COMPILES, AND APPEARS BUT DOES NOT CALCULATE AND SHOW RESULTS HELP!!!
Explanation / Answer
//Okay i fixed it finally. So basically the problem was that in your trip class you have a UserInputPanel and a TravelCostPanel but also in the TravelCostPanel you had another new UserInputPanel. Im guessing you wanted the UserInputPanel in the TravelCostPanel since you tried to get data from there. And so i altered your Trip class to pass the UserInputPanel to TravelCostPanel and set the UserInputPanel to the one that was changed. So i changed in all only 3 lines of codes. I commented on the 3 parts that i changed so you can see where i made changes and so dont forget to take the comments out. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Trip extends JFrame { private UserInputPanel user; //for the users data panel private TravelCostPanel cost; //to import the panel containing fields private JPanel panel; private final int WINDOW_WIDTH=350; private final int WINDOW_HEIGHT=450; /** Constructor */ public Trip() { //action for close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create custom panel user=new UserInputPanel(); cost=new TravelCostPanel(user);//Only thing i changed was i sent in user//Take this comment out panel= new JPanel(); //add panel add(panel); //add components to content panel panel.add(user); panel.add(cost); pack(); setVisible(true); } }//END trip // import javax.swing.*; import java.awt.*; import java.awt.event.*; /** The TravelCostPanel class allows totals to be visible */ public class TravelCostPanel extends JPanel { private UserInputPanel user; private JPanel buttonPanel; // To hold the buttons private JButton calcButton; // To calculate the cost private JButton exitButton; // To exit the application private JLabel owedMsg; private JLabel savedMsg; private JLabel expenseMsg; private JLabel allowedMsg; private JTextField exp; private JTextField allw; private JTextField owe; private JTextField save; private JPanel panel; /** Constructor */ public TravelCostPanel(UserInputPanel p)//Changed constructor to take a UserInputPanel//Take this comment out { user=p;//Set user to p which was sent in by Trip// Take this comment out //create a GridLayout //5 rows,2 columns setLayout(new GridLayout(5,2)); //create labels expenseMsg=new JLabel("Total Expense"); allowedMsg=new JLabel("Expenses Allowed"); owedMsg=new JLabel("Amount Owed"); savedMsg=new JLabel("You Saved"); //set up text field exp=new JTextField(0); owe=new JTextField(4); save=new JTextField(4); allw=new JTextField(4); //create the button panel buildButtonPanel(); //add border setBorder(BorderFactory.createTitledBorder("Trip Expenses")); add(expenseMsg); add(exp); add(allowedMsg); add(allw); add(owedMsg); add(owe); add(savedMsg); add(save); add(buttonPanel); } /** Build buildButtonPanel method build the button panel */ private void buildButtonPanel() { // Create a panel for the buttons. buttonPanel = new JPanel(); // Create the buttons. calcButton = new JButton("Calculate"); exitButton = new JButton("Exit"); // Register the action listeners. calcButton.addActionListener(new CalcButtonListener()); exitButton.addActionListener(new ExitButtonListener()); // Add the buttons to the button panel. buttonPanel.add(calcButton); buttonPanel.add(exitButton); } /** Private inner class that handles the event when the user clicks the Calculate button. */ private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { //variables to hold expense, allowed, owed, saved double expense, allowed, owed, saved,owed1,owed2; expense=user.getExpense(); exp.setText("Expense " + expense); allowed=user.getAllowed(); allw.setText("Allowed " + allowed); owed=expense-allowed; owe.setText("Owed " + owed); saved=expense-allowed; save.setText("Saved " + saved); } } /** Private inner class that handles the event when the user clicks the Exit button. */ private class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } }//END TravelCostPanel