Docsity

Air Line Java - Object Oriented Programming - Assignment | CSCI 2350, Assignments of Computer Science

Toggle sidebar
bg1
1
2/*
3Blake Shiver
4CSCI 2350
5Airline.java
6
7Airline Reservation System - A small airline has just purchased a
8computer for its new automated reservation system. You have been asked to
9program the new system. You are to write an applet to assign seats on each
10 flight of the airlines only plane (capacity 10 seats).
11 Your program should allow the user to choose which section he wants to
12 sit either (1) Smoking or (2) Non -Smoking. If the person enters the code
13 for smoking, your program should assign a seat in the smoking section (seats
1-5).
14 If the person enters the code for non-smoking, your program should assign
15 a seat in the non-smoking section (seats 6-10). You program should then print
16 a boarding pass indicating the person's seat number and whether
17 it is in the smoking or nonsmoking section of the plane. When there
18 are no empty seats in a section, your program should indicate that
19 section is full. Use a single-subscripted array of primitive type boolean
20 to represent the seating chart of the plane. Initialize all the elements
21 of the array to false to indicate that all seats are empty. As each seat is
22 assigned, set the corresponding elements of the array to true to indicate that
23 the seat is no longer available. Your program should never assign a seat that
24 has already been assigned.
25 ----------
26 ---------
27 Optional: When the smoking section is full, your program should ask the
28 person if it is acceptable to be placed in the non smoking section (and
29 vice versa). If yes, make the appropriate seat assignment. IF no, print
30 the message, โ€œThe Next flight leaves in 3 hours.โ€
31
32 2/14/05
33 */
34
35
36 import javax.swing.*;
37 import java.awt.*;
38 import java.awt.event.*; //not loaded under java.awt.*
39
40 public class Airline extends JApplet implements ActionListener
41 {
42 boolean seatChart[] ={true,true,true,true,true,
43 true,true,true,true,true};
44 int countS = 0, countN = 0;
45 JButton submit;
46 Checkbox smoking;
47 Checkbox nonSmoking;
48 Container container;
49 String input;
50 int choice;
51
52 public void init()
53 {
54 submit = new JButton("submit");
55 smoking = new Checkbox("smoking",false);//go the extra mile and make it easy
with a check box!!!
56 nonSmoking = new Checkbox("Non-Smoking",false);
57 submit.addActionListener(this);
58
59 container = getContentPane();
60 container.setLayout(new FlowLayout());
61 container.add(smoking);
62 container.add(nonSmoking);
63 container.add(submit);
64
65 }//end of init
66
67 public void paint (Graphics g)
1
pf3
pf4
pf5

Partial preview of the text

Download Air Line Java - Object Oriented Programming - Assignment | CSCI 2350 and more Assignments Computer Science in PDF only on Docsity!

3 Blake Shiver 4 CSCI 2350 5 Airline.java 6 7 Airline Reservation System - A small airline has just purchased a 8 computer for its new automated reservation system. You have been asked to 9 program the new system. You are to write an applet to assign seats on each 10 flight of the airlines only plane (capacity 10 seats). 11 Your program should allow the user to choose which section he wants to 12 sit either (1) Smoking or (2) Non -Smoking. If the person enters the code 13 for smoking, your program should assign a seat in the smoking section (seats 1-5). 14 If the person enters the code for non-smoking, your program should assign 15 a seat in the non-smoking section (seats 6-10). You program should then print 16 a boarding pass indicating the person's seat number and whether 17 it is in the smoking or nonsmoking section of the plane. When there 18 are no empty seats in a section, your program should indicate that 19 section is full. Use a single-subscripted array of primitive type boolean 20 to represent the seating chart of the plane. Initialize all the elements 21 of the array to false to indicate that all seats are empty. As each seat is 22 assigned, set the corresponding elements of the array to true to indicate that 23 the seat is no longer available. Your program should never assign a seat that 24 has already been assigned. 25 ---------- 26 --------- 27 Optional: When the smoking section is full, your program should ask the 28 person if it is acceptable to be placed in the non smoking section (and 29 vice versa). If yes, make the appropriate seat assignment. IF no, print 30 the message, โ€œThe Next flight leaves in 3 hours.โ€ 31 32 2/14/ 33 / 34 35 36 import javax.swing.; 37 import java.awt.; 38 import java.awt.event.; //not loaded under java.awt.* 39 40 public class Airline extends JApplet implements ActionListener 41 { 42 boolean seatChart[] = {true,true,true,true,true, 43 true,true,true,true,true}; 44 int countS = 0, countN = 0; 45 JButton submit; 46 Checkbox smoking; 47 Checkbox nonSmoking; 48 Container container; 49 String input; 50 int choice; 51 52 public void init() 53 { 54 submit = new JButton("submit"); 55 smoking = new Checkbox("smoking", false);//go the extra mile and make it easy with a check box!!! 56 nonSmoking = new Checkbox("Non-Smoking", false); 57 submit.addActionListener(this); 58 59 container = getContentPane(); 60 container.setLayout(new FlowLayout()); 61 container.add(smoking); 62 container.add(nonSmoking); 63 container.add(submit); 64 65 }//end of init 66 67 public void paint (Graphics g)

69 super.paint(g); 70 showStatus("Select Boarding Pass Options"); 71 72 }//end of paint 73 74 public void actionPerformed(ActionEvent e) 75 { 76 if(smoking.getState() && nonSmoking.getState()) 77 { 78 JOptionPane.showMessageDialog(null, "please select only one section at a time", "error", JOptionPane.ERROR_MESSAGE); 79 smoking.setState(false);//reset check box 80 nonSmoking.setState(false);//reset check box 81 } 82 else 83 { 84 85 if(smoking.getState())//if input was for smoking 86 { 87 if(countS < 5) 88 { 89 seatChart[countS] = false; 90 countS++; 91 JOptionPane.showMessageDialog(null, "Boarding pass for seat " 92 + countS + " (smoking section)", 93 "Boarding Pass", JOptionPane. INFORMATION_MESSAGE); 94 smoking.setState(false);//reset check box 95 repaint(); 96 } 97 else 98 { 99 input = JOptionPane.showInputDialog("The smoking section is curently full. \nwould you like to try the non-smoking section?(enter 1 for yes or 0 for no)"); 100 choice = Integer.parseInt(input); 101 102 if(choice == 1)//look in the next section for available seats 103 { 104 if(countN < 5) 105 { 106 seatChart[countN +5] = false; 107 countN++; 108 JOptionPane.showMessageDialog(null, "Boarding pass for seat " 109 + (countN+5) + " (non-smoking section)", 110 "Boarding Pass", JOptionPane. INFORMATION_MESSAGE); 111 smoking.setState(false);//reset check box 112 repaint(); 113 } 114 else 115 { 116 JOptionPane.showMessageDialog(null, "The Flight is completely booked.\nThe next flight leaves in 3 hours", 117 "Flight Full", JOptionPane. ERROR_MESSAGE); 118 smoking.setState(false);//reset check box 119 repaint(); 120 } 121 } 122 else 123 { 124 JOptionPane.showMessageDialog(null, "The next flight leaves in 3 hours.", 125 "message", JOptionPane. INFORMATION_MESSAGE);

183 }//end else 184 }//end non smoking choice 185 }//end else 186 }//end of actionPerformed 187 }//end of Airline

BLAKE SHIVER

Each boarding pass is a JOption Pane Message

Smoking Seat:

Non Smoking Seat:

After requesting all the seats in non-smoking section, I request one extra. A dialog

window appears in which I can request a different section.