/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vacationprice;
import java.lang.Throwable;
import java.text.NumberFormat;
/**
*
* @author j
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.NumberFormat;
import java.lang.Throwable;
import java.lang.Exception;
import java.util.InputMismatchException;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class VacationPrice {
public static void main(String[] args) {
JFrame frame = new VacationFrame();
frame.setVisible(true);
}
}
class VacationFrame extends JFrame {
public VacationFrame() {
setTitle("Vacation Price Calculator and Vacation Availability App");
centerWindow(this);
setSize(1200, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new VacationPanel();
this.add(panel);
}
private void centerWindow(Window w) {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width - w.getWidth()) / 2, (d.height - w.getHeight()) / 2);
}
}
class VacationPanel extends JPanel {
private JTextField weeksOnVacationTextField,
peopleNumTextField,
mealsTextField,
vacationPriceTextField,
vacationThereTextField,
vacationAvailabilityTextField;
private JLabel weeksOnVacationLabel,
peopleNumLabel,
mealsLabel,
vacationPriceLabel,
vacationThereLabel,
vacationAvailabilityLabel;
private JButton calculateButton,
checkButton,
exitButton;
public VacationPanel() {
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// weeks on vacation label
weeksOnVacationLabel = new JLabel("How many weeks are you going on vacation?");
displayPanel.add(weeksOnVacationLabel);
// weeks on vacation text field
weeksOnVacationTextField = new JTextField(2);
displayPanel.add(weeksOnVacationTextField);
// number of people label
peopleNumLabel = new JLabel("Number of people on vacation with you");
displayPanel.add(peopleNumLabel);
// number of people text field
peopleNumTextField = new JTextField(2);
displayPanel.add(peopleNumTextField);
// meals label
mealsLabel = new JLabel("Number of meals you plan on purchasing:");
displayPanel.add(mealsLabel);
// meals text field
mealsTextField = new JTextField(10);
displayPanel.add(mealsTextField);
// vacation price label
vacationPriceLabel = new JLabel("VacationPrice:");
displayPanel.add(vacationPriceLabel);
// vacation price text field
vacationPriceTextField = new JTextField(10);
vacationPriceTextField.setEditable(false);
vacationPriceTextField.setFocusable(false);
displayPanel.add(vacationPriceTextField);
// question panel
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
questionPanel.setVisible(true);
// vacation price label
vacationAvailabilityLabel = new JLabel("Enter day of month to see if it's available");
questionPanel.add(vacationAvailabilityLabel);
// vacation availability text field
vacationAvailabilityTextField = new JTextField(2);
vacationAvailabilityTextField.setEditable(true);
vacationAvailabilityTextField.setFocusable(true);
questionPanel.add(vacationAvailabilityTextField);
// vacation price label
vacationThereLabel = new JLabel("Is day available?");
questionPanel.add(vacationThereLabel);
// vacation availability text field
vacationThereTextField = new JTextField(20);
vacationThereTextField.setEditable(false);
vacationThereTextField.setFocusable(false);
questionPanel.add(vacationThereTextField);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int weeksOnVacation = Integer.parseInt(weeksOnVacationTextField.getText());
int peopleNum = Integer.parseInt(peopleNumTextField.getText());
int mealsNum = Integer.parseInt(mealsTextField.getText());
double vacationPrice = calculateVacationPrice.calculateVacationPrice(
weeksOnVacation, peopleNum, mealsNum);
NumberFormat currency = NumberFormat.getCurrencyInstance();
vacationPriceTextField.setText(currency.format(vacationPrice));
}});
buttonPanel.add(calculateButton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttonPanel.add(exitButton);
buttonPanel.add(calculateButton);
// form another panel to handle the program to see if vacation dates are available
class questionPanel extends JPanel
{
private JButton checkButton;
}
checkButton = new JButton("Enter date to see if it's available");
checkButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int daySearched = Integer.parseInt(vacationAvailabilityTextField.getText());
String s = daysAvailable.getMessage(daySearched);
vacationThereTextField.setText(s);
}});
questionPanel.add(checkButton);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.NORTH);
this.add(questionPanel, BorderLayout.SOUTH);
this.add(buttonPanel, BorderLayout.CENTER);
}
}
Comments