Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. package finalExam;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. //import java.util.*;
  8. import java.text.NumberFormat;
  9.  
  10.  
  11. public class vacationPanel extends JPanel {
  12.  
  13.     private JLabel hotelL, locationL, rentalL, nightsL, airlineL;
  14.     private JTextField Nights, calcResults;
  15.     private JButton vacationCalc;
  16.     private JRadioButton rentalYes, rentalNo, delta, unitedAirlines, jetBlue, bestWestern, lastResort, marriott, hawaii, tokyoJapan, perthAustralia;
  17.  
  18.     private double hotelPick, locationPick, airlinePick, nightsPick, carRentalPick, finalPrice;
  19.  
  20.     vacationPanel()
  21.     {
  22.         hotelL = new JLabel ("Choose Hotel");
  23.         locationL = new JLabel ("Choose Location");
  24.         rentalL = new JLabel ("Car Rental?");
  25.         nightsL = new JLabel ("How many nights?");
  26.         airlineL = new JLabel ("Choose Airline");
  27.  
  28.         Nights = new JTextField ( 5 );
  29.        
  30.         vacationCalc = new JButton ( "Get Price!");
  31.  
  32.         rentalYes = new JRadioButton ("yes");
  33.         rentalNo = new JRadioButton ("no");
  34.        
  35.         delta = new JRadioButton ("Delta");
  36.         unitedAirlines = new JRadioButton ("United Airlines");
  37.         jetBlue = new JRadioButton ("JetBlue");
  38.  
  39.         ButtonGroup rentalGroup = new ButtonGroup();
  40.         rentalGroup.add(rentalYes);
  41.         rentalGroup.add(rentalNo);
  42.        
  43.         ButtonGroup airlineGroup = new ButtonGroup();
  44.         airlineGroup.add(delta);
  45.         airlineGroup.add(unitedAirlines);
  46.         airlineGroup.add(jetBlue);
  47.        
  48.         ButtonGroup hotelGroup = new ButtonGroup();
  49.         hotelGroup.add(bestWestern);
  50.         hotelGroup.add(lastResort);
  51.         hotelGroup.add(marriott);
  52.        
  53.         ButtonGroup locationGroup = new ButtonGroup();
  54.         locationGroup.add(hawaii);
  55.         locationGroup.add(tokyoJapan);
  56.         locationGroup.add(perthAustralia);
  57.        
  58.         vacationCalc.addActionListener(new calcListener() );
  59.         //calcResults.addActionListener(new calcListener() );
  60.        
  61.         rentalYes.addActionListener(new rentalListener() );
  62.         rentalNo.addActionListener(new rentalListener() );
  63.        
  64.         delta.addActionListener(new airlineListener() );
  65.         unitedAirlines.addActionListener(new airlineListener() );
  66.         jetBlue.addActionListener(new airlineListener() );
  67.        
  68.         bestWestern.addActionListener(new hotelListener() );
  69.         lastResort.addActionListener(new hotelListener() );
  70.         marriott.addActionListener(new hotelListener() );
  71.        
  72.         hawaii.addActionListener(new locationListener() );
  73.         tokyoJapan.addActionListener(new locationListener() );
  74.         perthAustralia.addActionListener(new locationListener() );
  75.  
  76.         add (hotelL);
  77.         add (bestWestern);
  78.         add (lastResort);
  79.         add (marriott);
  80.  
  81.         add (locationL);
  82.         add (hawaii);
  83.         add (tokyoJapan);
  84.         add (perthAustralia);
  85.  
  86.         add (rentalL);
  87.         add (rentalYes);
  88.         add (rentalNo);
  89.  
  90.         add (nightsL);
  91.         add (Nights);
  92.  
  93.         add (airlineL);
  94.         add (delta);
  95.         add (unitedAirlines);
  96.         add (jetBlue);
  97.  
  98.         add (vacationCalc);
  99.         add (calcResults);
  100.  
  101.         setPreferredSize (new Dimension(512,288) );
  102.         setBackground (Color.blue );
  103.  
  104.     }
  105.  
  106.     private class calcListener implements ActionListener {
  107.         public void actionPerformed (ActionEvent event) {
  108.             String nightsText = Nights.getText();
  109.             nightsPick = Double.parseDouble(nightsText);
  110.             finalPrice = ((hotelPick * nightsPick * 250.00) + (locationPick * airlinePick * 1000.00) + (carRentalPick * nightsPick));
  111.             NumberFormat fmt = NumberFormat.getNumberInstance();
  112.             calcResults.setText(fmt.format(finalPrice) );
  113.         }
  114.     }
  115.  
  116.     private class rentalListener implements ActionListener {
  117.         public void actionPerformed (ActionEvent choose) {
  118.             if (choose.getSource() == rentalYes)
  119.                 carRentalPick = 50.00;
  120.             else if (choose.getSource() == rentalNo)
  121.                 carRentalPick = 00.00;
  122.         }
  123.     }
  124.    
  125.     private class hotelListener implements ActionListener {
  126.         public void actionPerformed (ActionEvent choose) {
  127.             if (choose.getSource() == bestWestern)
  128.                 hotelPick = 01.00;
  129.             else if (choose.getSource() == lastResort)
  130.                 hotelPick = 00.65;
  131.             else if (choose.getSource() == marriott)
  132.                 hotelPick = 01.85;
  133.         }
  134.     }
  135.    
  136.     private class locationListener implements ActionListener {
  137.         public void actionPerformed (ActionEvent choose) {
  138.             if (choose.getSource() == hawaii)
  139.                 locationPick = 01.00;
  140.             else if (choose.getSource() == tokyoJapan)
  141.                 locationPick = 02.85;
  142.             else if (choose.getSource() == perthAustralia)
  143.                 locationPick = 01.95;
  144.         }
  145.     }
  146.    
  147.     private class airlineListener implements ActionListener {
  148.         public void actionPerformed (ActionEvent choose) {
  149.             if (choose.getSource() == delta)
  150.                 airlinePick = 01.00;
  151.             else if (choose.getSource() == unitedAirlines)
  152.                 airlinePick = 00.85;
  153.             else if (choose.getSource() == jetBlue)
  154.                 airlinePick = 01.40;
  155.         }
  156.     }
  157.    
  158.     //this is to fix a warning message "The serializable class does not declare a static final SerialVersionUID field of type long"
  159.     JTextArea error_fix = new JTextArea(5, 100);
  160.     private static final long serialVersionUID = 1L;
  161. }
  162. vacationDemo.main(vacationDemo.java:11)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement