
| null | null | null | null | null | null | null |
| Ann | null | null | null | null | null | null |
| Ann | Bob | null | null | null | null | null |
| Ann | null | null | null | null | null | null |
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) {}
}
import java.util.*;
/**
* This class models a hotel reesrvation system
*
* @author Ting Shih
* @version 1.0.0
*/
public class Reservation {
/*
* occupancy is an array that indicates the occupant of each room.
* If there are no occupants, it is marked null
*/
private String[] occupancy;
/* MAX_ROOMS is the maximum number of rooms.and also represent the array size */
private int MAX_ROOMS = 5;
/**
* Creates a list of rooms and make them available (by setting them to null).
*/
public Reservation() {
/* creates the array of occupancy for the size of MAX_ROOMS */
occupancy = new String[MAX_ROOMS];
/*
* The following is not necessary, since a newly constructed array
* will have null values by default. But we will set them to null
* for illustrative purposes.
*/
for (int index = 0; index < occupancy.length; index++) {
occupancy[index] = null;
}
}
/**
* Adds a new customer to the hotel.
*
* @param customer is the name of the customer to be added.
* @return true if the hotel is not full and reservatoin was made successfully,
* false otherwise.
*/
public boolean reserve (String customer) {
int roomChecked = 0;
/* Use a while-loop to check if a room is available */
while (roomChecked < occupancy.length && occupancy[roomChecked] != null ) {
roomChecked ++;
}
/*
* if roomChecked is is less than the number of rooms in the hotel,
* then there is an availability in the hotel, otherwise, all the rooms
* have been searched and no availability was found
*/
if (roomChecked < occupancy.length) {
occupancy[roomChecked] = customer;
return true;
} else {
return false;
}
}
/**
* checks out a customer and marks the room as empty
*
* @param customer is the name of the customer checking out
* @return true if customer had a prior reservation so cancellation possible,
* false otherwise.
*/
public boolean cancel (String customer) {
int roomChecked = 0;
/* searches all the rooms until the customer who had a reservation is found */
while (roomChecked < occupancy.length &&
(occupancy[roomChecked] == null || !occupancy[roomChecked].equals(customer)) ) {
roomChecked++;
}
/*
* system cancels the reservation made by marking the room available
* otherwise return false to indicate the customer did not make a reservation
*/
if (roomChecked < occupancy.length) {
occupancy[roomChecked] = null;
return true;
} else {
return false;
}
}
}
| Previou page | Next page |