Advertisement
jbonanno

VacationPrice.java

Jun 18th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package vacationprice;
  6.  import java.lang.Throwable;
  7. import java.text.NumberFormat;
  8.  
  9. /**
  10.  *
  11.  * @author j
  12.  */
  13. import java.awt.BorderLayout;
  14. import java.awt.Dimension;
  15. import java.awt.FlowLayout;
  16. import java.awt.Toolkit;
  17. import java.awt.Window;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.io.IOException;
  21. import java.text.NumberFormat;
  22. import java.lang.Throwable;
  23. import java.lang.Exception;
  24. import java.util.InputMismatchException;
  25. import javax.swing.*;
  26. import javax.swing.text.JTextComponent;
  27.  
  28. public class VacationPrice {
  29.  
  30.     public static void main(String[] args) {
  31.         JFrame frame = new VacationFrame();
  32.         frame.setVisible(true);
  33.     }
  34. }
  35.  
  36. class VacationFrame extends JFrame {
  37.  
  38.     public VacationFrame() {
  39.         setTitle("Vacation Price Calculator and Vacation Availability App");
  40.         centerWindow(this);
  41.         setSize(1200, 500);
  42.         setResizable(false);
  43.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.         JPanel panel = new VacationPanel();
  45.         this.add(panel);
  46.     }
  47.  
  48.     private void centerWindow(Window w) {
  49.         Toolkit tk = Toolkit.getDefaultToolkit();
  50.         Dimension d = tk.getScreenSize();
  51.         setLocation((d.width - w.getWidth()) / 2, (d.height - w.getHeight()) / 2);
  52.     }
  53. }
  54.  
  55. class VacationPanel extends JPanel {
  56.  
  57.     private JTextField weeksOnVacationTextField,
  58.             peopleNumTextField,
  59.             mealsTextField,
  60.             vacationPriceTextField,
  61.             vacationThereTextField,
  62.     vacationAvailabilityTextField;
  63.    
  64.     private JLabel weeksOnVacationLabel,
  65.             peopleNumLabel,
  66.             mealsLabel,
  67.             vacationPriceLabel,
  68.             vacationThereLabel,
  69.     vacationAvailabilityLabel;
  70.    
  71.     private JButton calculateButton,
  72.             checkButton,
  73.             exitButton;
  74.  
  75.     public VacationPanel() {
  76.        
  77.        
  78.        
  79.        
  80.        
  81.        
  82.        
  83.        
  84.        
  85.        
  86.        
  87.        
  88.        
  89.        
  90.        
  91.         // display panel
  92.         JPanel displayPanel = new JPanel();
  93.         displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  94.  
  95.         // weeks on vacation label
  96.         weeksOnVacationLabel = new JLabel("How many weeks are you going on vacation?");
  97.         displayPanel.add(weeksOnVacationLabel);
  98.  
  99.         // weeks on vacation text field
  100.         weeksOnVacationTextField = new JTextField(2);
  101.         displayPanel.add(weeksOnVacationTextField);
  102.    
  103.  
  104.    
  105.  
  106.         // number of people label
  107.         peopleNumLabel = new JLabel("Number of people on vacation with you");
  108.         displayPanel.add(peopleNumLabel);
  109.  
  110.         // number of people text field
  111.         peopleNumTextField = new JTextField(2);
  112.         displayPanel.add(peopleNumTextField);
  113.        
  114.  
  115.         // meals label
  116.         mealsLabel = new JLabel("Number of meals you plan on purchasing:");
  117.         displayPanel.add(mealsLabel);
  118.  
  119.         // meals text field
  120.         mealsTextField = new JTextField(10);
  121.         displayPanel.add(mealsTextField);
  122.  
  123.         // vacation price label
  124.         vacationPriceLabel = new JLabel("VacationPrice:");
  125.         displayPanel.add(vacationPriceLabel);
  126.  
  127.         // vacation price text field
  128.         vacationPriceTextField = new JTextField(10);
  129.         vacationPriceTextField.setEditable(false);
  130.         vacationPriceTextField.setFocusable(false);
  131.         displayPanel.add(vacationPriceTextField);
  132.  
  133.  
  134.        
  135.        
  136.        
  137.         // question panel
  138.           JPanel questionPanel = new JPanel();
  139.         questionPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  140.         questionPanel.setVisible(true);
  141.        
  142.        
  143.            // vacation price label
  144.         vacationAvailabilityLabel = new JLabel("Enter day of month to see if it's available");
  145.         questionPanel.add(vacationAvailabilityLabel);
  146.  
  147.         // vacation availability text field
  148.         vacationAvailabilityTextField = new JTextField(2);
  149.         vacationAvailabilityTextField.setEditable(true);
  150.         vacationAvailabilityTextField.setFocusable(true);
  151.         questionPanel.add(vacationAvailabilityTextField);
  152.  
  153.  
  154.        
  155.              // vacation price label
  156.         vacationThereLabel = new JLabel("Is day available?");
  157.         questionPanel.add(vacationThereLabel);
  158.  
  159.        
  160.   // vacation availability  text field
  161.         vacationThereTextField = new JTextField(20);
  162.         vacationThereTextField.setEditable(false);
  163.         vacationThereTextField.setFocusable(false);
  164.         questionPanel.add(vacationThereTextField);
  165.  
  166.        
  167.        
  168.  
  169.         // button panel
  170.         JPanel buttonPanel = new JPanel();
  171.         buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  172.  
  173.         // calculate button
  174.         calculateButton = new JButton("Calculate");
  175.         calculateButton.addActionListener(new ActionListener() {
  176.  
  177.             public void actionPerformed(ActionEvent e) {
  178.                
  179.          
  180.                 int weeksOnVacation = Integer.parseInt(weeksOnVacationTextField.getText());
  181.            
  182.                 int peopleNum = Integer.parseInt(peopleNumTextField.getText());
  183.                 int mealsNum = Integer.parseInt(mealsTextField.getText());
  184.                 double vacationPrice = calculateVacationPrice.calculateVacationPrice(
  185.                     weeksOnVacation, peopleNum, mealsNum);
  186.                 NumberFormat currency = NumberFormat.getCurrencyInstance();
  187.                 vacationPriceTextField.setText(currency.format(vacationPrice));
  188.                
  189.              
  190.          
  191.             }});
  192.         buttonPanel.add(calculateButton);
  193.  
  194.         // exit button
  195.         exitButton = new JButton("Exit");
  196.         exitButton.addActionListener(new ActionListener() {
  197.  
  198.             public void actionPerformed(ActionEvent e) {
  199.                 System.exit(0);
  200.             }
  201.         });
  202.         buttonPanel.add(exitButton);
  203.        
  204.          buttonPanel.add(calculateButton);
  205.  
  206.          
  207.          
  208.          // form another panel to handle the program to see if vacation dates are available
  209.          
  210.          class questionPanel extends JPanel
  211. {
  212. private JButton checkButton;
  213. }
  214.          
  215.         checkButton = new JButton("Enter date to see if it's available");
  216.         checkButton.addActionListener(new ActionListener() {
  217.  
  218.             public void actionPerformed(ActionEvent e) {
  219.                
  220.            
  221.                int daySearched = Integer.parseInt(vacationAvailabilityTextField.getText());
  222.              
  223.              
  224.     String s = daysAvailable.getMessage(daySearched);
  225.                vacationThereTextField.setText(s);
  226.              
  227.              
  228.             }});
  229.         questionPanel.add(checkButton);
  230.  
  231.    
  232.  
  233.        
  234.  
  235.          
  236.      
  237.  
  238.                  
  239.              
  240.          
  241.  
  242.      
  243.  
  244.         // add panels to main panel
  245.         this.setLayout(new BorderLayout());
  246.         this.add(displayPanel, BorderLayout.NORTH);
  247.            this.add(questionPanel, BorderLayout.SOUTH);
  248.         this.add(buttonPanel, BorderLayout.CENTER);
  249.        
  250.        
  251.        
  252.      
  253.        
  254.     }
  255.  
  256.  
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement