HotelReservation.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
* This application provides a GUI to operate a hotel reservation system
*
* @author Ting Shih
* @version 1.0.0
*/
public class HotelReservation extends Frame
implements ActionListener, WindowListener {
private TextField input, output;
private Panel opsPad;
private Checkbox reserveBox;
private Checkbox cancelBox;
private CheckboxGroup ops;
private Button start;
private Reservation resv;
/**
* The main routine.
*
* @param args String arguments. Not used.
*/
public static void main (String[] args) {
HotelReservation sc = new HotelReservation(550, 100);
sc.show();
}
/**
* Initializes widgets and creates layouts.
*
* @param width the width of the window
* @param height the height of the window
*/
public HotelReservation(int width, int height) {
super("Hotel Reservation");
setSize (width, height);
input = new TextField(15);
output = new TextField(30);
opsPad = new Panel();
opsPad.setLayout(new GridLayout(2,1));
CheckboxGroup ops = new CheckboxGroup();
reserveBox = new Checkbox("Reserve", ops, true);
cancelBox = new Checkbox("Cancel", ops, false);
opsPad.add(reserveBox);
opsPad.add(cancelBox);
start = new Button("Start");
setLayout(new FlowLayout());
add(input);
add(opsPad);
add(start);
add(output);
resv = new Reservation();
start.addActionListener(this);
addWindowListener(this);
}
private void reserve(String customer) {
if (resv.reserve (customer)) {
output.setText("Reservation has been made for " + customer + ".");
} else {
output.setText ("The hotel is full.");
}
input.setText("");
}
private void cancel(String customer) {
if (resv.cancel (customer)) {
output.setText("Reservation has been canceled for " + customer + ".");
} else {
output.setText (customer + " has no reservation at this hotel.");
}
input.setText("");
}
/**
* Process the button events.
*
* @param e the event generated.
*/
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(start)) {
if (reserveBox.getState()) {
reserve(input.getText());
} else {
cancel(input.getText());
}
}
}
/**
* method contains empty body
*
* @param e not used
*/
public void windowActivated(WindowEvent e) {}
/**
* method contains empty body
*
* @param e not used
*/
public void windowClosed(WindowEvent e) {}
/**
* When the close box is clicked or the close window menu item
* is chosen, this method causes the application to terminate
*
* @param e not used
*/
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
/**
* method contains empty body
*
* @param e not used
*/
public void windowDeactivated(WindowEvent e) {}
/**
* method contains empty body
*
* @param e not used
*/
public void windowDeiconified(WindowEvent e) {}
/**
* method contains empty body
*
* @param e not used
*/
public void windowIconified(WindowEvent e) {}
/**
* method contains empty body
*
* @param e not used
*/
public void windowOpened(WindowEvent e) {}
}