Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* * cs142_hotelbooker.java */
- package cs142_hotelbooker;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import Calendar.DateAD;
- import Calendar.BasicCalendar;
- import java.io.*;
- /**
- * Hotel Booker
- *
- * a project that user can set up hotel reservations <br>
- * the project also creates a text document with the hotel reservation <br>
- *
- * @author Michael Ji and Raymon Yee
- * @version 2.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX and Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- public class cs142_hotelbooker extends JFrame {
- private static final int FRAME_WIDTH = 1000;
- private static final int FRAME_HEIGHT = 500;
- private static final int TEXT_WIDTH = 25;
- private static final String enterString = "Enter Start and End Dates";
- private static final String enterName = "Enter your name:";
- private static final String buttonName = "Book it!";
- private static final int daysInACalendar = 42;
- private static final int yearsInComboBox = 10;
- private static final int monthDimension = 7;
- private static final int weekLoop = 6;
- private static final int startEndRadioButtonGeneration = 2;
- private static final int southPanelRows = 4;
- private static final int southPanelColumn = 1;
- private short todaysMonth = 0;
- private short todaysYear = 0;
- private int todaysDay = 0;
- /**
- * allows the user to set up start and end dates for hotel reservations then
- * will print it to a text document start date on calender end date on
- * calender
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- JFrame frame = new cs142_hotelbooker();
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- /**
- * initializes the gui frame that the program will run off of
- */
- public cs142_hotelbooker() {
- setLayout(new BorderLayout());
- setSize(FRAME_WIDTH, FRAME_HEIGHT);
- today = new DateAD();
- todaysDay = today.getDayOfMonth();
- todaysMonth = today.getMonth();
- todaysYear = today.getYear();
- startDate = new DateAD();
- endDate = startDate.getTomorrow();
- setNorthPanel();
- add(northPanel, BorderLayout.NORTH);
- makeYearComboBox();
- northPanel.add(yearComboBox);
- setSouthPanel();
- add(southPanel, BorderLayout.SOUTH);
- createBookButton();
- setCenterPanel();
- }
- /**
- * this is intended to be the north pane of the gui it has the labels to
- * show the date on the calender
- */
- public void setNorthPanel() {
- northPanel = new JPanel();
- northPanel.setLayout(new FlowLayout());
- dayLabel = new JLabel(Short.toString(today.getDayOfMonth()));
- enterLabel = new JLabel(enterString);
- monthComboBox = new JComboBox(DateAD.MONTHNAMES);
- // monthComboBox.setSelectedIndex(today.getMonth());
- northPanel.add(dayLabel);
- northPanel.add(monthComboBox);
- northPanel.add(enterLabel);
- monthComboBox.setSelectedIndex(today.getMonth());
- /**
- * changeStatusListener
- *
- * a class that contains an action listener <br>
- * class will be called when its action is needed
- * <br>
- *
- * @author Michael Ji & Raymon Yee
- * @version 2.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX & Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- class changeStatusListener implements ActionListener {
- /**
- * action lister for when clicking on different days of the month
- * actions also depend on which radio button start or end is
- * selected
- *
- * @param event is the action that is triggering the action listener
- * which is selecting days and switching radio buttons
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- int monthIndex = monthComboBox.getSelectedIndex();
- DateAD futureDate = new DateAD((short) monthIndex, today.getYear());
- if (monthIndex >= todaysMonth && todaysYear == startDate.getYear()
- || today.getYear() > todaysYear) {
- if (startButton.isSelected()) {
- startDate.setMonth((short) monthIndex);
- today.setMonth((short) monthIndex);
- startButton.setText("Start Date: " + today.toString());
- int compareDate = startDate.compareTo(endDate);
- //if the new start date is set beyond the endDate,
- //move the end date to today.getTomorrow
- if (compareDate > 0) {
- endDate = today.getTomorrow();
- endButton.setText("End Date: " + endDate.toString());
- }
- //if end Button selected, check if the new month being
- //set is not before the startDate's month. If it is,
- //then set endDate to the day after startDate.
- } else if (endButton.isSelected()) {
- if (futureDate.compareTo(startDate) > 0) {
- today.setMonth((short) monthIndex);
- endDate.setMonth((short) monthIndex);
- endButton.setText("End Date: " + endDate.toString());
- } else {
- endDate = today.getTomorrow();
- endButton.setText("End Date: " + endDate.toString());
- }
- }
- setCenterPanel();
- } else {
- //If the monthIndex is before today's month, then
- //set the monthcombo to what it was before the faulty
- //selection was made and reset the centerPanel.
- monthComboBox.setSelectedIndex(today.getMonth());
- setCenterPanel();
- }
- }
- }
- monthComboBox.addActionListener(new changeStatusListener());
- }
- // make the initial yearComboBox, rebuild the centerPanel
- //after yearComboBox is changed.
- /**
- * make the year combo box and action listener for changing year
- *
- */
- public void makeYearComboBox() {
- String[] yearArray = new String[yearsInComboBox];
- for (int i = 0; i < yearsInComboBox; i++) {
- String yearString = "" + (today.getYear() + i);
- yearArray[i] = (yearString);
- }
- yearComboBox = new JComboBox(yearArray);
- /**
- * changeStatusListener
- *
- * a class that contains an action listener <br>
- * class will be called when its action is needed
- * <br>
- *
- * @author Michael Ji & Raymon Yee
- * @version 2.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX & Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- class changeStatusListener implements ActionListener {
- /**
- * action lister for the year combo box logic sets start and end
- * date buttons
- *
- * @param event is the action to trigger, adjusts start and end date
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- int monthIndex = monthComboBox.getSelectedIndex();
- String yearIndex = (String) yearComboBox.getSelectedItem();
- short yearShort = Short.parseShort(yearIndex);
- // startDate.setYear((short) monthIndex);
- //if the selected year is the same as today's, then the month
- //must be greater than or equal to today's month too. If not,
- //then it can pass the check as long as the selected year is greater
- //than today's year
- if ((monthIndex >= todaysMonth && todaysYear == yearShort)
- || yearShort > todaysYear) {
- // String yearIndex = (String) yearComboBox.getSelectedItem();
- // short yearShort = Short.parseShort(yearIndex);
- today.setYear(yearShort);
- if (startButton.isSelected()) {
- startDate.setYear(yearShort);
- startButton.setText("Start Date: " + startDate.toString());
- int compareDate = today.compareTo(endDate);
- //if startDate year is being set beyond endDate's
- //date, then make endDate set to the day after startDate
- if (compareDate > 0) {
- endDate = today.getTomorrow();
- endButton.setText("End Date: " + endDate.toString());
- }
- //endButton must be selected, set the endButton properties.
- } else {
- endDate.setYear(yearShort);
- endButton.setText("End Date: " + endDate.toString());
- }
- setCenterPanel();
- } else {
- // if it makes it here, then the year selected did not
- //pass the tests, and the calendar will be remade
- //with the old date.
- yearComboBox.setSelectedItem("" + today.getYear());
- setCenterPanel();
- }
- }
- }
- yearComboBox.addActionListener(new changeStatusListener());
- }
- //Creates the initial and updated calendars
- /**
- * intended to be the center panel this is the calender of each month
- */
- public void setCenterPanel() {
- if (centerPanel != null) {
- getContentPane().remove(centerPanel);
- }
- centerPanel = new JPanel();
- centerPanel.setLayout(new GridLayout(monthDimension, monthDimension));
- //makes a calendar to find the first day of the month
- calendar = new BasicCalendar(today.getMonth(), today.getYear());
- int daysInMonth = today.daysInMonth(today.getMonth(), today.getYear());
- int firstDayOfMonth = 0;
- // int dayOfMonth = today.getDayOfMonth();
- //This loop finds the index for the grid calendar's
- //first day
- String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday",
- "Thursday", "Friday", "Saturday"};
- for (int i = 0; i <= weekLoop; i++) {
- JLabel name = new JLabel(dayNames[i]);
- centerPanel.add(name);
- name.setHorizontalAlignment(JLabel.CENTER);
- }
- for (int i = 0; i <= weekLoop; i++) {
- if (calendar.at(0, i) == 1) {
- firstDayOfMonth = i;
- break;
- }
- }
- //Creates the calendar based on today's date.
- //Lays out the empty buttons and the buttons
- //with text at the same time
- //JBUTTON LOGIC HERE
- makeCalendar(daysInMonth, firstDayOfMonth);
- add(centerPanel, BorderLayout.CENTER);
- //this refreshes the changes to the frame
- //after I remove centerPanel, build a new one,
- //and add it to the Frame
- invalidate();
- validate();
- }
- //Separate method because the logic here is long
- /**
- * makes calender in gui with days to be selectable
- *
- * @param daysInMonth in that gets the number of days in a selected month
- * from calendar
- * @param firstDayOfMonth first day of month to know where to start the
- * month
- */
- public void makeCalendar(int daysInMonth, int firstDayOfMonth) {
- /**
- * changeStatusListener
- *
- * a class that contains an action listener <br>
- * class will be called when its action is needed
- * <br>
- *
- * @author Michael Ji & Raymon Yee
- * @version 2.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX & Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- class changeStatusListener implements ActionListener {
- /**
- * action lister for choosing the day
- *
- * @param event is the action to trigger, changes the selected day
- * of selected month
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- JButton pressedButton = (JButton) event.getSource();
- //if the Jbutton is not empty and is not null and isn't
- //before today's date in the current month, then press stuff
- if (pressedButton.getText() != null && !pressedButton.getText().isEmpty()) {
- int buttonValue = Integer.parseInt(pressedButton.getText());
- //use this object to check against current dates.
- DateAD futureObject
- = new DateAD((short) buttonValue, today.getMonth(), today.getYear());
- //if the same year as todays year, then the buttons value
- //must be greater than or equal to todays day of Month.
- //or if it isnt the same year, its ok, or if not the same month
- //its ok
- if (buttonValue >= todaysDay && todaysYear == startDate.getYear()
- || today.getYear() > todaysYear || startDate.getMonth() != todaysMonth) {
- if (startButton.isSelected()) {
- //setting buttons to red if they get clicked
- startJButton.setForeground(Color.BLACK);
- pressedButton.setForeground(Color.RED);
- dayLabel.setText(pressedButton.getText());
- startJButton = pressedButton;
- //make the startDate set to the day of the buttonValue,
- //update text.
- startDate = new DateAD((short) buttonValue,
- startDate.getMonth(), startDate.getYear());
- startButton.setText("Start Date: " + startDate.toString());
- //if the button pressed for the startDate is beyond
- //the endDate, then set endDate to the day after start.
- if (endDate.compareTo(startDate) <= 0) {
- endDate = startDate.getTomorrow();
- endButton.setText("End Date: " + endDate.toString());
- }
- } //Else the endButton must be selected.
- else if (endButton.isSelected()) {
- //check to see if the new buttonValue is going
- //to put endDate before startDate. If it does, then
- //we will instead set endDate to startdate.getTomorrow.
- if (futureObject.compareTo(startDate) < 0) {
- endDate = startDate.getTomorrow();
- endButton.setText("End Date: " + endDate.toString());
- } else {
- endDate = new DateAD((short) buttonValue,
- today.getMonth(), today.getYear());
- endButton.setText("End Date: " + endDate.toString());
- startJButton.setForeground(Color.BLACK);
- pressedButton.setForeground(Color.RED);
- dayLabel.setText(pressedButton.getText());
- startJButton = pressedButton;
- }
- }
- }
- }
- }
- }
- //makes calendar and Jbuttons
- for (int i = 0; i < daysInACalendar; i++) {
- JButton calendarButton = new JButton();
- calendarButton.setForeground(Color.BLUE);
- //start adding buttons that have text
- if (i >= firstDayOfMonth && i < daysInMonth + firstDayOfMonth) {
- calendarButton = new JButton("" + (i - firstDayOfMonth + 1));
- calendarButton.setForeground(Color.BLACK);
- //These buttons need to be gray, because they are
- //before today's day in the month. Only make them gray
- //if they are in the same month and also same year
- if (i < todaysDay + firstDayOfMonth - 1
- && todaysYear == today.getYear() && todaysMonth == today.getMonth()) {
- calendarButton.setForeground(Color.GRAY);
- }
- }
- if (i == today.getDayOfMonth() + firstDayOfMonth - 1) {
- startJButton = calendarButton;
- calendarButton.setForeground(Color.RED);
- }
- calendarButton.addActionListener(new changeStatusListener());
- centerPanel.add(calendarButton);
- ButtonGroup dateButtons = new ButtonGroup();
- dateButtons.add(calendarButton);
- }
- }
- //makes radio buttons and their action listeners
- /**
- * is the pane for show the time of the reservations
- */
- public void setSouthPanel() {
- radioArray = new JRadioButton[startEndRadioButtonGeneration];
- southPanel = new JPanel();
- southPanel.setLayout(new GridLayout(southPanelRows, southPanelColumn)); //row,column
- // how to set grid layout panels as flow, then add each label specifically
- todayPanel = new JPanel();
- startPanel = new JPanel();
- endPanel = new JPanel();
- namePanel = new JPanel();
- southPanel.add(todayPanel);
- southPanel.add(startPanel);
- southPanel.add(endPanel);
- southPanel.add(namePanel);
- JLabel todayLabel;
- todayLabel = new JLabel("Today: " + today.toString());
- todayPanel.add(todayLabel);
- startButton = new JRadioButton();
- startButton.setSelected(true);
- startButton.setText("Start Date: " + today.toString());
- endButton = new JRadioButton();
- endButton.setText("End Date: " + today.getTomorrow().toString());
- radioArray[0] = startButton;
- radioArray[1] = endButton;
- ButtonGroup group = new ButtonGroup();
- group.add(startButton);
- group.add(endButton);
- startPanel.add(startButton);
- endPanel.add(endButton);
- nameTextField = new JTextField();
- nameTextField.setColumns(30);
- nameLabel = new JLabel(enterName);
- namePanel.add(nameLabel);
- namePanel.add(nameTextField);
- /**
- * changeStatusListener
- *
- * a class that contains an action listener <br>
- * class will be called when its action is needed
- * <br>
- *
- * @author Michael Ji & Raymon Yee
- * @version 1.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX & Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- class changeStatusListener implements ActionListener {
- /**
- * action lister for choosing the day
- *
- * @param event is the action to trigger, sets the radio buttons
- * date when day is changed and sets current day
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- if (radioArray[0].isSelected()) {
- today = new DateAD(startButton.getText());
- } else {
- today = new DateAD(endButton.getText());
- }
- monthComboBox.setSelectedIndex(today.getMonth());
- yearComboBox.setSelectedIndex(today.getYear() - 2016);
- dayLabel.setText("" + today.getDayOfMonth());
- setCenterPanel();
- }
- }
- ActionListener listener = new changeStatusListener();
- for (int i = 0; i < 2; i++) {
- radioArray[i].addActionListener(listener);
- }
- }
- /**
- * creates a button to book the reservation
- */
- public void createBookButton() {
- bookButton = new JButton(buttonName);
- namePanel.add(bookButton);
- /**
- * changeStatusListener
- *
- * a class that contains an action listener <br>
- * class will be called when its action is needed
- * <br>
- *
- * @author Michael Ji & Raymon Yee
- * @version 2.0
- *
- * Compiler: Java 1.8 <br>
- * OS: OSX & Windows 7 <br>
- * Hardware: PC and MacBook Pro <br>
- *
- * April 25, 2016 <br>
- * MJ RY completed v 2.0
- */
- class changeStatusListener implements ActionListener {
- /**
- * When button is pressed, prints name, arrive date, and departure
- * date to an output file
- *
- * @param event the triggering event object pressing book button
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- writeReservation();
- }
- }
- bookButton.addActionListener(new changeStatusListener());
- }
- /**
- * opens the file writer to write the reservation into the text file input
- * is the text in the name text field and the start and end date output is
- * to to the reservations text file
- */
- public void writeReservation() {
- if (nameTextField.getText().equals("")) {
- nameTextField.setText("Enter Name");
- }
- if (nameTextField.getText().equals("Enter Name")) {
- return;
- }
- try {
- PrintWriter filePointer
- = new PrintWriter(new FileOutputStream("Reservations.txt", true));
- filePointer.println(nameTextField.getText());
- filePointer.println("Start Date: " + startDate.toString());
- filePointer.println("End Date: " + endDate.toString());
- filePointer.println();
- filePointer.close();
- } catch (IOException exp) {
- exp.printStackTrace();
- }
- }
- // instance variables
- JTextField nameTextField;
- JPanel radioButtonPanel;
- JPanel northPanel;
- JPanel southPanel;
- JPanel buttonPanel;
- JPanel centerPanel;
- JLabel dayLabel;
- JLabel enterLabel;
- JLabel nameLabel;
- JButton bookButton;
- JButton startJButton;
- JButton endJButton;
- ButtonGroup group;
- JComboBox monthComboBox;
- JComboBox yearComboBox;
- DateAD today;
- BasicCalendar calendar;
- String[] monthNames;
- JRadioButton[] radioArray;
- PrintStream output;
- JPanel todayPanel;
- JPanel startPanel;
- JPanel endPanel;
- JPanel namePanel;
- JRadioButton startButton;
- JRadioButton endButton;
- JRadioButton calendarButton;
- DateAD startDate;
- DateAD endDate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement