// the package name should be hotel. // NOTE: put this in a class called main package Hotel; import java.io.*; class Hotel { public static void main(String args[]) { GUI gui = new GUI(); } } // end Main Class // NOTE: put this in a class named "Hotel" package Hotel; import javax.swing.*; //Importing these make the program work. import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class GUI extends JFrame { int room_NUM_real = 0; // this is used to store special values. int roomsnumbers = 20; // this defines the size of the hotlel String booked_STORE[] = new String[roomsnumbers]; // An array to match the sixe of the hotel. private Button bookButton, unbookButton, viewButton, exitButton; // The buttons declaired. public class Book_IN extends JFrame { // Input box class (for booking) public JTextField room; //make a public textfield public Book_IN ib = this; //make an input BOX. public class OK1EventHandler implements ActionListener { /*This Action is performed when * Ok is clicked with in the * Booking panel */ public void actionPerformed(ActionEvent e) { int inputint = Integer.parseInt(room.getText()); if(inputint<=roomsnumbers) { if(booked_STORE[inputint-1].equals("Booked")) { System.out.println("Room number: " + inputint + " Has Already been booked"); } else { booked_STORE[inputint-1] = "Booked"; ib.hide(); System.out.println("you Just Booked Room: " + inputint); } } else { System.out.println("There is no such room numbered: " + inputint); } } } /* The above fragment of code, tests the room is in range, hasn't been booked, and * Follows the correct steps to cause the array to acknoledge a booking */ // This next part builds the Book room panel. public Book_IN() { setTitle("Book Room"); Panel p1 = new Panel(); p1.add(new Label("Please enter the room")); room = new JTextField(2); room.setEditable(true); p1.add(room); Button b; p1.add(b = new Button("OK")); b.addActionListener(new OK1EventHandler()); this.getContentPane().add(p1); this.pack(); } } /* The Next class Handles Booking in, and Implements ActionListener */ public class BookEventHandler extends Book_IN implements ActionListener { public BookEventHandler () { } public void actionPerformed(ActionEvent e) { this.show(); } } /* The NEXT section is used to create a UI * for the Booking out senario. * It follows the same code as booking in, but slightly in reverse. */ public class Book_OUT extends JFrame { public JTextField room; public Book_OUT ib2 = this; public class OK2EventHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int inputint = Integer.parseInt(room.getText()); if(inputint<=roomsnumbers) { if(booked_STORE[inputint-1].equals("Unbooked")) { System.out.println("Room number: " + inputint + " Has not been booked"); } else { booked_STORE[inputint-1] = "Unbooked"; ib2.hide(); System.out.println("you Just Unbooked Room: " + inputint); } } else { System.out.println("There is no such room numbered: " + inputint); } } } public Book_OUT() { setTitle("Unbook Room"); Panel p2 = new Panel(); p2.add(new Label("Please enter the room")); room = new JTextField(2); room.setEditable(true); p2.add(room); Button u; p2.add(u = new Button("OK")); u.addActionListener(new OK2EventHandler()); this.getContentPane().add(p2); this.pack(); } } public class UnbookEventHandler extends Book_OUT implements ActionListener { public void actionPerformed(ActionEvent e) { this.show(); } } /* This Section is for the VIEWING of the array. * That is, it shows the user what rooms have and have not been booked. * it uses the array booked_STORE, and and Int called room_NUM_real as * well as an index (i) . */ public class ViewEventHandler implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<20;i++) { room_NUM_real = i+1; if(booked_STORE[i].equals("Booked")) { System.out.println("Room Number: " + room_NUM_real + " Has been booked"); } else { System.out.println("Room Number: " + room_NUM_real + " Is available"); } } } } /* This Contorls The EXIT button. * */ public class ExitEventHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } /* This is the first Window.. * It creates buttons and uses the ActionListener to wait * for input from the moouse. */ public GUI() { Panel p1 = new Panel(); bookButton = new Button("Book"); unbookButton = new Button("Unbook"); viewButton = new Button("View"); exitButton = new Button("Exit"); bookButton.addActionListener(new BookEventHandler()); unbookButton.addActionListener(new UnbookEventHandler()); viewButton.addActionListener(new ViewEventHandler()); exitButton.addActionListener(new ExitEventHandler()); p1.add(bookButton); p1.add(unbookButton); p1.add(viewButton); p1.add(exitButton); setTitle("Room Bookings"); this.getContentPane().add(p1); this.pack(); this.show(); // AMENDED. /* the following loop is added as to * fill the array, to rid of empty values.*/ for(int i2=0;i2<20;i2++) { booked_STORE[i2] = "Unbooked"; } } }