Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.88 KB | None | 0 0
  1. /* * cs142_hotelbooker.java */
  2. package cs142_hotelbooker;
  3.  
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import Calendar.DateAD;
  8. import Calendar.BasicCalendar;
  9. import java.io.*;
  10.  
  11. /**
  12. * Hotel Booker
  13. *
  14. * a project that user can set up hotel reservations <br>
  15. * the project also creates a text document with the hotel reservation <br>
  16. *
  17. * @author Michael Ji and Raymon Yee
  18. * @version 2.0
  19. *
  20. * Compiler: Java 1.8 <br>
  21. * OS: OSX and Windows 7 <br>
  22. * Hardware: PC and MacBook Pro <br>
  23. *
  24. * April 25, 2016 <br>
  25. * MJ RY completed v 2.0
  26. */
  27. public class cs142_hotelbooker extends JFrame {
  28.  
  29. private static final int FRAME_WIDTH = 1000;
  30. private static final int FRAME_HEIGHT = 500;
  31. private static final int TEXT_WIDTH = 25;
  32. private static final String enterString = "Enter Start and End Dates";
  33. private static final String enterName = "Enter your name:";
  34. private static final String buttonName = "Book it!";
  35. private static final int daysInACalendar = 42;
  36. private static final int yearsInComboBox = 10;
  37. private static final int monthDimension = 7;
  38. private static final int weekLoop = 6;
  39. private static final int startEndRadioButtonGeneration = 2;
  40. private static final int southPanelRows = 4;
  41. private static final int southPanelColumn = 1;
  42. private short todaysMonth = 0;
  43. private short todaysYear = 0;
  44. private int todaysDay = 0;
  45.  
  46. /**
  47. * allows the user to set up start and end dates for hotel reservations then
  48. * will print it to a text document start date on calender end date on
  49. * calender
  50. *
  51. * @param args the command line arguments
  52. */
  53. public static void main(String[] args) {
  54. JFrame frame = new cs142_hotelbooker();
  55. frame.setLocationRelativeTo(null);
  56. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57. frame.setVisible(true);
  58. }
  59.  
  60. /**
  61. * initializes the gui frame that the program will run off of
  62. */
  63. public cs142_hotelbooker() {
  64. setLayout(new BorderLayout());
  65. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  66. today = new DateAD();
  67. todaysDay = today.getDayOfMonth();
  68. todaysMonth = today.getMonth();
  69. todaysYear = today.getYear();
  70. startDate = new DateAD();
  71. endDate = startDate.getTomorrow();
  72.  
  73. setNorthPanel();
  74.  
  75. add(northPanel, BorderLayout.NORTH);
  76. makeYearComboBox();
  77. northPanel.add(yearComboBox);
  78.  
  79. setSouthPanel();
  80. add(southPanel, BorderLayout.SOUTH);
  81. createBookButton();
  82.  
  83. setCenterPanel();
  84. }
  85.  
  86. /**
  87. * this is intended to be the north pane of the gui it has the labels to
  88. * show the date on the calender
  89. */
  90. public void setNorthPanel() {
  91.  
  92. northPanel = new JPanel();
  93. northPanel.setLayout(new FlowLayout());
  94. dayLabel = new JLabel(Short.toString(today.getDayOfMonth()));
  95. enterLabel = new JLabel(enterString);
  96. monthComboBox = new JComboBox(DateAD.MONTHNAMES);
  97. // monthComboBox.setSelectedIndex(today.getMonth());
  98. northPanel.add(dayLabel);
  99. northPanel.add(monthComboBox);
  100. northPanel.add(enterLabel);
  101. monthComboBox.setSelectedIndex(today.getMonth());
  102.  
  103. /**
  104. * changeStatusListener
  105. *
  106. * a class that contains an action listener <br>
  107. * class will be called when its action is needed
  108. * <br>
  109. *
  110. * @author Michael Ji &amp Raymon Yee
  111. * @version 2.0
  112. *
  113. * Compiler: Java 1.8 <br>
  114. * OS: OSX &amp Windows 7 <br>
  115. * Hardware: PC and MacBook Pro <br>
  116. *
  117. * April 25, 2016 <br>
  118. * MJ RY completed v 2.0
  119. */
  120. class changeStatusListener implements ActionListener {
  121.  
  122. /**
  123. * action lister for when clicking on different days of the month
  124. * actions also depend on which radio button start or end is
  125. * selected
  126. *
  127. * @param event is the action that is triggering the action listener
  128. * which is selecting days and switching radio buttons
  129. */
  130. @Override
  131. public void actionPerformed(ActionEvent event) {
  132. int monthIndex = monthComboBox.getSelectedIndex();
  133. DateAD futureDate = new DateAD((short) monthIndex, today.getYear());
  134. if (monthIndex >= todaysMonth && todaysYear == startDate.getYear()
  135. || today.getYear() > todaysYear) {
  136.  
  137. if (startButton.isSelected()) {
  138. startDate.setMonth((short) monthIndex);
  139. today.setMonth((short) monthIndex);
  140. startButton.setText("Start Date: " + today.toString());
  141. int compareDate = startDate.compareTo(endDate);
  142. //if the new start date is set beyond the endDate,
  143. //move the end date to today.getTomorrow
  144. if (compareDate > 0) {
  145. endDate = today.getTomorrow();
  146. endButton.setText("End Date: " + endDate.toString());
  147. }
  148. //if end Button selected, check if the new month being
  149. //set is not before the startDate's month. If it is,
  150. //then set endDate to the day after startDate.
  151. } else if (endButton.isSelected()) {
  152. if (futureDate.compareTo(startDate) > 0) {
  153. today.setMonth((short) monthIndex);
  154. endDate.setMonth((short) monthIndex);
  155. endButton.setText("End Date: " + endDate.toString());
  156. } else {
  157. endDate = today.getTomorrow();
  158. endButton.setText("End Date: " + endDate.toString());
  159. }
  160. }
  161. setCenterPanel();
  162. } else {
  163. //If the monthIndex is before today's month, then
  164. //set the monthcombo to what it was before the faulty
  165. //selection was made and reset the centerPanel.
  166. monthComboBox.setSelectedIndex(today.getMonth());
  167. setCenterPanel();
  168.  
  169. }
  170. }
  171. }
  172. monthComboBox.addActionListener(new changeStatusListener());
  173. }
  174.  
  175. // make the initial yearComboBox, rebuild the centerPanel
  176. //after yearComboBox is changed.
  177. /**
  178. * make the year combo box and action listener for changing year
  179. *
  180. */
  181. public void makeYearComboBox() {
  182. String[] yearArray = new String[yearsInComboBox];
  183. for (int i = 0; i < yearsInComboBox; i++) {
  184. String yearString = "" + (today.getYear() + i);
  185. yearArray[i] = (yearString);
  186. }
  187. yearComboBox = new JComboBox(yearArray);
  188.  
  189. /**
  190. * changeStatusListener
  191. *
  192. * a class that contains an action listener <br>
  193. * class will be called when its action is needed
  194. * <br>
  195. *
  196. * @author Michael Ji &amp Raymon Yee
  197. * @version 2.0
  198. *
  199. * Compiler: Java 1.8 <br>
  200. * OS: OSX &amp Windows 7 <br>
  201. * Hardware: PC and MacBook Pro <br>
  202. *
  203. * April 25, 2016 <br>
  204. * MJ RY completed v 2.0
  205. */
  206. class changeStatusListener implements ActionListener {
  207.  
  208. /**
  209. * action lister for the year combo box logic sets start and end
  210. * date buttons
  211. *
  212. * @param event is the action to trigger, adjusts start and end date
  213. */
  214. @Override
  215. public void actionPerformed(ActionEvent event) {
  216. int monthIndex = monthComboBox.getSelectedIndex();
  217. String yearIndex = (String) yearComboBox.getSelectedItem();
  218. short yearShort = Short.parseShort(yearIndex);
  219. // startDate.setYear((short) monthIndex);
  220. //if the selected year is the same as today's, then the month
  221. //must be greater than or equal to today's month too. If not,
  222. //then it can pass the check as long as the selected year is greater
  223. //than today's year
  224. if ((monthIndex >= todaysMonth && todaysYear == yearShort)
  225. || yearShort > todaysYear) {
  226. // String yearIndex = (String) yearComboBox.getSelectedItem();
  227. // short yearShort = Short.parseShort(yearIndex);
  228. today.setYear(yearShort);
  229. if (startButton.isSelected()) {
  230. startDate.setYear(yearShort);
  231. startButton.setText("Start Date: " + startDate.toString());
  232. int compareDate = today.compareTo(endDate);
  233. //if startDate year is being set beyond endDate's
  234. //date, then make endDate set to the day after startDate
  235. if (compareDate > 0) {
  236. endDate = today.getTomorrow();
  237. endButton.setText("End Date: " + endDate.toString());
  238. }
  239. //endButton must be selected, set the endButton properties.
  240. } else {
  241. endDate.setYear(yearShort);
  242. endButton.setText("End Date: " + endDate.toString());
  243.  
  244. }
  245. setCenterPanel();
  246. } else {
  247. // if it makes it here, then the year selected did not
  248. //pass the tests, and the calendar will be remade
  249. //with the old date.
  250. yearComboBox.setSelectedItem("" + today.getYear());
  251. setCenterPanel();
  252. }
  253. }
  254. }
  255. yearComboBox.addActionListener(new changeStatusListener());
  256. }
  257.  
  258. //Creates the initial and updated calendars
  259. /**
  260. * intended to be the center panel this is the calender of each month
  261. */
  262. public void setCenterPanel() {
  263. if (centerPanel != null) {
  264. getContentPane().remove(centerPanel);
  265. }
  266. centerPanel = new JPanel();
  267. centerPanel.setLayout(new GridLayout(monthDimension, monthDimension));
  268. //makes a calendar to find the first day of the month
  269. calendar = new BasicCalendar(today.getMonth(), today.getYear());
  270. int daysInMonth = today.daysInMonth(today.getMonth(), today.getYear());
  271. int firstDayOfMonth = 0;
  272. // int dayOfMonth = today.getDayOfMonth();
  273. //This loop finds the index for the grid calendar's
  274. //first day
  275. String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday",
  276. "Thursday", "Friday", "Saturday"};
  277. for (int i = 0; i <= weekLoop; i++) {
  278. JLabel name = new JLabel(dayNames[i]);
  279. centerPanel.add(name);
  280. name.setHorizontalAlignment(JLabel.CENTER);
  281. }
  282. for (int i = 0; i <= weekLoop; i++) {
  283. if (calendar.at(0, i) == 1) {
  284. firstDayOfMonth = i;
  285. break;
  286. }
  287. }
  288. //Creates the calendar based on today's date.
  289. //Lays out the empty buttons and the buttons
  290. //with text at the same time
  291. //JBUTTON LOGIC HERE
  292. makeCalendar(daysInMonth, firstDayOfMonth);
  293. add(centerPanel, BorderLayout.CENTER);
  294. //this refreshes the changes to the frame
  295. //after I remove centerPanel, build a new one,
  296. //and add it to the Frame
  297. invalidate();
  298. validate();
  299.  
  300. }
  301.  
  302. //Separate method because the logic here is long
  303. /**
  304. * makes calender in gui with days to be selectable
  305. *
  306. * @param daysInMonth in that gets the number of days in a selected month
  307. * from calendar
  308. * @param firstDayOfMonth first day of month to know where to start the
  309. * month
  310. */
  311. public void makeCalendar(int daysInMonth, int firstDayOfMonth) {
  312.  
  313. /**
  314. * changeStatusListener
  315. *
  316. * a class that contains an action listener <br>
  317. * class will be called when its action is needed
  318. * <br>
  319. *
  320. * @author Michael Ji &amp Raymon Yee
  321. * @version 2.0
  322. *
  323. * Compiler: Java 1.8 <br>
  324. * OS: OSX &amp Windows 7 <br>
  325. * Hardware: PC and MacBook Pro <br>
  326. *
  327. * April 25, 2016 <br>
  328. * MJ RY completed v 2.0
  329. */
  330. class changeStatusListener implements ActionListener {
  331.  
  332. /**
  333. * action lister for choosing the day
  334. *
  335. * @param event is the action to trigger, changes the selected day
  336. * of selected month
  337. */
  338. @Override
  339. public void actionPerformed(ActionEvent event) {
  340.  
  341. JButton pressedButton = (JButton) event.getSource();
  342. //if the Jbutton is not empty and is not null and isn't
  343. //before today's date in the current month, then press stuff
  344. if (pressedButton.getText() != null && !pressedButton.getText().isEmpty()) {
  345. int buttonValue = Integer.parseInt(pressedButton.getText());
  346. //use this object to check against current dates.
  347. DateAD futureObject
  348. = new DateAD((short) buttonValue, today.getMonth(), today.getYear());
  349. //if the same year as todays year, then the buttons value
  350. //must be greater than or equal to todays day of Month.
  351. //or if it isnt the same year, its ok, or if not the same month
  352. //its ok
  353. if (buttonValue >= todaysDay && todaysYear == startDate.getYear()
  354. || today.getYear() > todaysYear || startDate.getMonth() != todaysMonth) {
  355. if (startButton.isSelected()) {
  356. //setting buttons to red if they get clicked
  357. startJButton.setForeground(Color.BLACK);
  358. pressedButton.setForeground(Color.RED);
  359. dayLabel.setText(pressedButton.getText());
  360. startJButton = pressedButton;
  361. //make the startDate set to the day of the buttonValue,
  362. //update text.
  363. startDate = new DateAD((short) buttonValue,
  364. startDate.getMonth(), startDate.getYear());
  365. startButton.setText("Start Date: " + startDate.toString());
  366. //if the button pressed for the startDate is beyond
  367. //the endDate, then set endDate to the day after start.
  368. if (endDate.compareTo(startDate) <= 0) {
  369. endDate = startDate.getTomorrow();
  370. endButton.setText("End Date: " + endDate.toString());
  371. }
  372. } //Else the endButton must be selected.
  373. else if (endButton.isSelected()) {
  374. //check to see if the new buttonValue is going
  375. //to put endDate before startDate. If it does, then
  376. //we will instead set endDate to startdate.getTomorrow.
  377. if (futureObject.compareTo(startDate) < 0) {
  378. endDate = startDate.getTomorrow();
  379. endButton.setText("End Date: " + endDate.toString());
  380. } else {
  381. endDate = new DateAD((short) buttonValue,
  382. today.getMonth(), today.getYear());
  383. endButton.setText("End Date: " + endDate.toString());
  384. startJButton.setForeground(Color.BLACK);
  385. pressedButton.setForeground(Color.RED);
  386. dayLabel.setText(pressedButton.getText());
  387. startJButton = pressedButton;
  388. }
  389. }
  390.  
  391. }
  392. }
  393. }
  394. }
  395.  
  396. //makes calendar and Jbuttons
  397. for (int i = 0; i < daysInACalendar; i++) {
  398.  
  399. JButton calendarButton = new JButton();
  400. calendarButton.setForeground(Color.BLUE);
  401. //start adding buttons that have text
  402. if (i >= firstDayOfMonth && i < daysInMonth + firstDayOfMonth) {
  403. calendarButton = new JButton("" + (i - firstDayOfMonth + 1));
  404. calendarButton.setForeground(Color.BLACK);
  405. //These buttons need to be gray, because they are
  406. //before today's day in the month. Only make them gray
  407. //if they are in the same month and also same year
  408. if (i < todaysDay + firstDayOfMonth - 1
  409. && todaysYear == today.getYear() && todaysMonth == today.getMonth()) {
  410. calendarButton.setForeground(Color.GRAY);
  411. }
  412. }
  413. if (i == today.getDayOfMonth() + firstDayOfMonth - 1) {
  414. startJButton = calendarButton;
  415. calendarButton.setForeground(Color.RED);
  416. }
  417. calendarButton.addActionListener(new changeStatusListener());
  418. centerPanel.add(calendarButton);
  419.  
  420. ButtonGroup dateButtons = new ButtonGroup();
  421. dateButtons.add(calendarButton);
  422. }
  423. }
  424.  
  425. //makes radio buttons and their action listeners
  426. /**
  427. * is the pane for show the time of the reservations
  428. */
  429. public void setSouthPanel() {
  430.  
  431. radioArray = new JRadioButton[startEndRadioButtonGeneration];
  432. southPanel = new JPanel();
  433. southPanel.setLayout(new GridLayout(southPanelRows, southPanelColumn)); //row,column
  434.  
  435. // how to set grid layout panels as flow, then add each label specifically
  436. todayPanel = new JPanel();
  437. startPanel = new JPanel();
  438. endPanel = new JPanel();
  439. namePanel = new JPanel();
  440.  
  441. southPanel.add(todayPanel);
  442. southPanel.add(startPanel);
  443. southPanel.add(endPanel);
  444. southPanel.add(namePanel);
  445.  
  446. JLabel todayLabel;
  447. todayLabel = new JLabel("Today: " + today.toString());
  448. todayPanel.add(todayLabel);
  449.  
  450. startButton = new JRadioButton();
  451.  
  452. startButton.setSelected(true);
  453. startButton.setText("Start Date: " + today.toString());
  454.  
  455. endButton = new JRadioButton();
  456. endButton.setText("End Date: " + today.getTomorrow().toString());
  457.  
  458. radioArray[0] = startButton;
  459. radioArray[1] = endButton;
  460. ButtonGroup group = new ButtonGroup();
  461. group.add(startButton);
  462. group.add(endButton);
  463.  
  464. startPanel.add(startButton);
  465. endPanel.add(endButton);
  466.  
  467. nameTextField = new JTextField();
  468. nameTextField.setColumns(30);
  469. nameLabel = new JLabel(enterName);
  470. namePanel.add(nameLabel);
  471. namePanel.add(nameTextField);
  472.  
  473. /**
  474. * changeStatusListener
  475. *
  476. * a class that contains an action listener <br>
  477. * class will be called when its action is needed
  478. * <br>
  479. *
  480. * @author Michael Ji &amp Raymon Yee
  481. * @version 1.0
  482. *
  483. * Compiler: Java 1.8 <br>
  484. * OS: OSX &amp Windows 7 <br>
  485. * Hardware: PC and MacBook Pro <br>
  486. *
  487. * April 25, 2016 <br>
  488. * MJ RY completed v 2.0
  489. */
  490. class changeStatusListener implements ActionListener {
  491.  
  492. /**
  493. * action lister for choosing the day
  494. *
  495. * @param event is the action to trigger, sets the radio buttons
  496. * date when day is changed and sets current day
  497. */
  498. @Override
  499. public void actionPerformed(ActionEvent event) {
  500. if (radioArray[0].isSelected()) {
  501. today = new DateAD(startButton.getText());
  502. } else {
  503. today = new DateAD(endButton.getText());
  504. }
  505. monthComboBox.setSelectedIndex(today.getMonth());
  506. yearComboBox.setSelectedIndex(today.getYear() - 2016);
  507. dayLabel.setText("" + today.getDayOfMonth());
  508. setCenterPanel();
  509. }
  510. }
  511.  
  512. ActionListener listener = new changeStatusListener();
  513. for (int i = 0; i < 2; i++) {
  514. radioArray[i].addActionListener(listener);
  515. }
  516. }
  517.  
  518. /**
  519. * creates a button to book the reservation
  520. */
  521. public void createBookButton() {
  522. bookButton = new JButton(buttonName);
  523. namePanel.add(bookButton);
  524.  
  525. /**
  526. * changeStatusListener
  527. *
  528. * a class that contains an action listener <br>
  529. * class will be called when its action is needed
  530. * <br>
  531. *
  532. * @author Michael Ji &amp Raymon Yee
  533. * @version 2.0
  534. *
  535. * Compiler: Java 1.8 <br>
  536. * OS: OSX &amp Windows 7 <br>
  537. * Hardware: PC and MacBook Pro <br>
  538. *
  539. * April 25, 2016 <br>
  540. * MJ RY completed v 2.0
  541. */
  542. class changeStatusListener implements ActionListener {
  543.  
  544. /**
  545. * When button is pressed, prints name, arrive date, and departure
  546. * date to an output file
  547. *
  548. * @param event the triggering event object pressing book button
  549. */
  550. @Override
  551. public void actionPerformed(ActionEvent event) {
  552. writeReservation();
  553. }
  554. }
  555. bookButton.addActionListener(new changeStatusListener());
  556. }
  557.  
  558. /**
  559. * opens the file writer to write the reservation into the text file input
  560. * is the text in the name text field and the start and end date output is
  561. * to to the reservations text file
  562. */
  563. public void writeReservation() {
  564. if (nameTextField.getText().equals("")) {
  565. nameTextField.setText("Enter Name");
  566. }
  567. if (nameTextField.getText().equals("Enter Name")) {
  568. return;
  569. }
  570. try {
  571. PrintWriter filePointer
  572. = new PrintWriter(new FileOutputStream("Reservations.txt", true));
  573.  
  574. filePointer.println(nameTextField.getText());
  575. filePointer.println("Start Date: " + startDate.toString());
  576. filePointer.println("End Date: " + endDate.toString());
  577. filePointer.println();
  578. filePointer.close();
  579. } catch (IOException exp) {
  580. exp.printStackTrace();
  581. }
  582. }
  583.  
  584. // instance variables
  585. JTextField nameTextField;
  586. JPanel radioButtonPanel;
  587. JPanel northPanel;
  588. JPanel southPanel;
  589. JPanel buttonPanel;
  590. JPanel centerPanel;
  591. JLabel dayLabel;
  592. JLabel enterLabel;
  593. JLabel nameLabel;
  594. JButton bookButton;
  595. JButton startJButton;
  596. JButton endJButton;
  597. ButtonGroup group;
  598. JComboBox monthComboBox;
  599. JComboBox yearComboBox;
  600. DateAD today;
  601. BasicCalendar calendar;
  602. String[] monthNames;
  603. JRadioButton[] radioArray;
  604. PrintStream output;
  605.  
  606. JPanel todayPanel;
  607. JPanel startPanel;
  608. JPanel endPanel;
  609. JPanel namePanel;
  610.  
  611. JRadioButton startButton;
  612. JRadioButton endButton;
  613.  
  614. JRadioButton calendarButton;
  615. DateAD startDate;
  616. DateAD endDate;
  617.  
  618. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement