Prime b

Prime b

  • NA
  • 810
  • 346k

First problem GUI fixed just some math problems

Mar 29 2012 3:34 PM
Well the titles says everything =)

I believe my math is wrong a little( maybe its not , could you test and see if its correct), and I want to round the numbers to 2 decimals places.

THat's the problem:



Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide:
• Number of days on the trip
• Amount of airfare, if any
•Amount of car rental fees, if any
• Number of miles driven, if a private vehicle was used
• Amount of parking fees, if any
• Amount of taxi charges, if any
• Conference or seminar registration fees, if any
Lodging charges, per night The company reimburses travel expenses according to the following policy:
• $37 per day for meals
• Parking fees, up to $10.00 per day
• Taxi charges up to $20.00 per day
• Lodging charges up to $95.00 per day
• If a private vehicle is used, $0.27 per mile driven
 




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;


/**
 *
 * @author Alexander
 */
public class Expenses extends JFrame {


    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    JPanel panel = new JPanel();
    private JLabel numberOfDaysLabel;
    private JLabel amountAirfareLabel;
    private JLabel amontRentalFeeLabel;
    private JLabel numberMilesLabel;
    private JLabel parkingFeeLabel;
    private JLabel taxiFeeLabel;
    private JLabel seminarFeeLabel;
    private JLabel lodgingFeeLabel;
    private JTextField daysField;
    private JTextField amountAirfareField;
    private JTextField rentalFeeField;
    private JTextField milesDrivenField;
    private JTextField parkingFeeField;
    private JTextField taxiFeeField;
    private JTextField smeinarFeeField;
    private JTextField lodgingFeeField;
    private JButton calculateButton;


    public Expenses() {
        setTitle("Travel Expenses");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);


    }


    private void buildPanel() {


        numberOfDaysLabel = new JLabel("Number of days on the trip: ");
        daysField = new JTextField(10);


        amountAirfareLabel = new JLabel("Amount of airfare: ");
        amountAirfareField = new JTextField(10);


        amontRentalFeeLabel = new JLabel("Amount of car rental fee: ");
        rentalFeeField = new JTextField(10);


        numberMilesLabel = new JLabel("Number of miles driven: ");
        milesDrivenField = new JTextField(10);


        parkingFeeLabel = new JLabel("Amount of parking fees: ");
        parkingFeeField = new JTextField(10);


        taxiFeeLabel = new JLabel("Taxi charges: ");
        taxiFeeField = new JTextField(10);


        seminarFeeLabel = new JLabel("Conference or seminar registration fees: ");
        smeinarFeeField = new JTextField(10);


        lodgingFeeLabel = new JLabel("Lodging charges, per night");
        lodgingFeeField = new JTextField(10);


        calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(new buttonListener());
        panel = new JPanel(new GridLayout(9, 2, 5, 10));


        panel.add(numberOfDaysLabel);
        panel.add(daysField);
        panel.add(amountAirfareLabel);
        panel.add(amountAirfareField);
        panel.add(amontRentalFeeLabel);
        panel.add(rentalFeeField);
        panel.add(numberMilesLabel);
        panel.add(milesDrivenField);
        panel.add(parkingFeeLabel);
        panel.add(parkingFeeField);
        panel.add(taxiFeeLabel);
        panel.add(taxiFeeField);
        panel.add(seminarFeeLabel);
        panel.add(smeinarFeeField);
        panel.add(lodgingFeeLabel);
        panel.add(lodgingFeeField);
        panel.add(calculateButton);


    }


    public class buttonListener implements ActionListener {


        public void actionPerformed(ActionEvent e) {
            String stringDay, stringAirfare, stringCarRental, stringMilesDriven, stringParkingFee, stringTaxiCharges, stringSeminarFee, stringLodgingFee;
            double totalAllowed, totalOwe, totalSaved, totalExpenses, numberDays, AirFare, CarRental, MilesDriven, ParkingFee, TaxiCharges, seminarFee, lodgingFee;
            final double dayMeal = 37.00;
            final double parkingFee = 10.00;
            final double taxiFee = 20.00;
            final double lodgingCharge = 95.00;
            final double rentCar = 0.27;
            double totalMilesDriven;


            stringDay = daysField.getText();
            numberDays = Double.parseDouble(stringDay);


            stringAirfare = amountAirfareField.getText();
            AirFare = Double.parseDouble(stringAirfare);


            stringCarRental = rentalFeeField.getText();
            CarRental = Double.parseDouble(stringCarRental);


            stringMilesDriven = milesDrivenField.getText();
            MilesDriven = Double.parseDouble(stringMilesDriven);


            stringParkingFee = parkingFeeField.getText();
            ParkingFee = Double.parseDouble(stringParkingFee);


            stringTaxiCharges = taxiFeeField.getText();
            TaxiCharges = Double.parseDouble(stringTaxiCharges);


            stringSeminarFee = smeinarFeeField.getText();
            seminarFee = Double.parseDouble(stringSeminarFee);


            stringLodgingFee = lodgingFeeField.getText();
            lodgingFee = Double.parseDouble(stringLodgingFee);


            totalMilesDriven = MilesDriven * rentCar;
            totalExpenses = AirFare * CarRental * MilesDriven * ParkingFee * TaxiCharges * seminarFee * lodgingFee;
            totalAllowed = numberDays * dayMeal * parkingFee * taxiFee * lodgingCharge * rentCar * totalMilesDriven;


            if (totalExpenses > totalAllowed) {
                totalOwe = totalExpenses - totalAllowed;
            } else {
                totalOwe = 0;
            }


            if (totalAllowed > totalExpenses) {
                totalSaved = totalAllowed - totalExpenses;
            } else {
                totalSaved = 0;
            }




            System.out.println("The total expenses for the trip are " + totalExpenses
                    + " \nThe total allowed to spend is " + totalAllowed + "\nThe total you owe is "
                    + totalOwe + "\nTotal saved is " + totalSaved);


        }
    }
}

Answers (1)