A new class will be added to the startup package to start up the event-driven ap
ID: 3777163 • Letter: A
Question
A new class will be added to the startup package to start up the event-driven application. This should be a small class to create the main window (JFrame) and make it visible. The other new classes are to be placed in a new package called ‘gui’.
When the project first starts, a window should appear and give the user six options. Five of the options will be buttons to press, while the sixth will be a prompt and a text field. Each of these options will be discussed.
The first button will be one to initiate the task of adding a flight to the system. When it is pressed, a new window should appear. In this window, there should be prompts and fields for the entry of the flight’s number, width and capacity. Provide a Submit button that will cause the data to be collected from the fields and passed to the command to add the flight. If the command fails, the error message should appear in the window, and the window remains visible for the user to correct the information. If the command is successful, the window should disappear, so that the user is returned to the main menu.
The second button is for the addition of a new passenger. Its affect will be very similar to the add flight button: a new window will appear for the entry of the data for the new passenger. This time, instead of a Submit button, the command to add a passenger should be created and executed whenever the user presses the Enter key in either one of the fields. If the addition of a passenger fails, the error message should appear in the window for the user to correct the data. If the addition was successful, the window to add a passenger should remain visible (with the fields blank) for the user to enter another passenger. The user can close the window via the X in the upper right corner, or you can supply a close button (the close button is optional).
The third and fourth buttons are to display the all passengers in the system, and to display all the flights in the system. In both cases, when the button is pressed, a new window should appear with the information. It should have a 2-D field to display the information, and scroll bars should be provided in case there is too much information for the field. This can be done by
and put myPane into a suitable JPanel, or directly into a JFrame. The preferred size of the scroll pane may want to be set to a suitable size if it is placed in a JPanel. If a scroll pane is directly added into a JFrame, it will take on the size of the available window. Don’t worry about formatting the text in a fancy way.
The fifth button is to terminate the project. The project can also be terminated by the X in the right hand corner of the main window. However, selecting the X in another window should just close that window (the default action for that (built in) button).
The sixth option in the main window are a prompt and a text field to handle an existing flight. When the user enters a number in the field (presumably the number of a flight) and presses the Enter key, a new window should appear. The flight number should appear in the title bar of the window, and a line near the top should also show the flight number and the remaining capacity of the flight. If the number is not a valid
2
number of a flight, the window should just show an appropriate error message. On the other hand, for a valid flight number, this window will show information about the flight, and provide the option to do any of the operations on the flight. However, such a window is too complex for the time available for you to do this assignment. Therefore, a class (HandleFlightFrame ) will be given to you that handles this window. You should not need to change it, but you will need to design your code so that the frame is created and made visible at the appropriate time.
For the initial window, the six options can be arranged in any pleasing fashion. Don’t spend too much time making it fancy, but it should be decent. The same applies to the other windows, although they are simpler (except for the HandleFlightFrame which is more complex).
Explanation / Answer
Please find the answer to the question as follows:-
App.java
package com.chegg.flightsimulator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.gui.AddFlight;
import com.chegg.flightsimulator.gui.AddPassenger;
import com.chegg.flightsimulator.gui.DisplayFlight;
import com.chegg.flightsimulator.gui.DisplayPassenger;
import com.chegg.flightsimulator.gui.HandleFlight;
public class App extends JFrame {
public App() {
this.setVisible(true);
this.setSize(600, 300);
this.setLayout(new BorderLayout());
this.setTitle("Flight Simulator");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
App myApp = new App();
JPanel buttonPanel = new JPanel();
GridLayout buttonLayout = new GridLayout(2, 2);
buttonPanel.setLayout(buttonLayout);
JButton button1 = new JButton("Add Flight");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
AddFlight addFlight = new AddFlight();
}
});
buttonPanel.add(button1);
JButton button2 = new JButton("Add Passenger");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
AddPassenger addPassenger = new AddPassenger();
}
});
buttonPanel.add(button2);
JButton button3 = new JButton("Display Passenger");
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
DisplayPassenger displayPassenger = new DisplayPassenger();
}
});
buttonPanel.add(button3);
JButton button4 = new JButton("Display Flights");
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
DisplayFlight displayFlight = new DisplayFlight();
}
});
buttonPanel.add(button4);
JButton button5 = new JButton("Close");
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
myApp.setDefaultCloseOperation(EXIT_ON_CLOSE);
myApp.dispose();
}
});
buttonPanel.add(button5);
JButton button6 = new JButton("Handle an existing Flight");
button6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
int flightNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter the Flight Number"));
if(AppData.isValidFlight(flightNumber)!=-1){
HandleFlight handleFlight = new HandleFlight(flightNumber);
}else{
JOptionPane.showMessageDialog(null, "Invalid Flight Number");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
});
buttonPanel.add(button6);
myApp.add(buttonPanel, BorderLayout.CENTER);
}
}
AddFlight.java
package com.chegg.flightsimulator.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.model.FlightModel;
public class AddFlight extends JFrame {
public AddFlight() {
this.setTitle("Add a Flight");
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new BorderLayout());
initWindow();
}
public void initWindow() {
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 0));
JLabel flightNumber = new JLabel("Flight Number");
formPanel.add(flightNumber);
JTextField flightNumberTextBox = new JTextField();
formPanel.add(flightNumberTextBox);
JLabel flightWidth = new JLabel("Flight Width");
formPanel.add(flightWidth);
JTextField flightWidthTextBox = new JTextField();
formPanel.add(flightWidthTextBox);
JLabel flightCapacity = new JLabel("Flight Capacity");
formPanel.add(flightCapacity);
JTextField flightCapacityTextBox = new JTextField();
formPanel.add(flightCapacityTextBox);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
List<FlightModel> flights = AppData.flights;
FlightModel newFlight = new FlightModel();
newFlight.setFlightNumber(Integer.parseInt(flightNumberTextBox.getText()));
newFlight.setFlightWidth(Integer.parseInt(flightWidthTextBox.getText()));
newFlight.setFlightCapacity(Integer.parseInt(flightCapacityTextBox.getText()));
flights.add(newFlight);
AppData.flights = flights;
JOptionPane.showMessageDialog(null, "Flight Added Successfully");
dismissWindow();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
});
formPanel.add(submitButton);
this.add(formPanel, BorderLayout.CENTER);
}
public void dismissWindow() {
this.dispose();
}
}
AddPassenger.java
package com.chegg.flightsimulator.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.model.PassengerModel;
public class AddPassenger extends JFrame {
public AddPassenger() {
this.setTitle("Add a Passenger");
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new BorderLayout());
initWindow();
}
public void initWindow() {
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 0));
JLabel flightNumber = new JLabel("Flight Number");
formPanel.add(flightNumber);
JTextField flightNumberTextBox = new JTextField();
formPanel.add(flightNumberTextBox);
JLabel passengerName = new JLabel("Passenger Name");
formPanel.add(passengerName);
JTextField passengerNameTextBox = new JTextField();
formPanel.add(passengerNameTextBox);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if(AppData.isValidFlight(Integer.parseInt(flightNumberTextBox.getText()))!=-1){
List<PassengerModel> passengers = AppData.passengers;
PassengerModel newPassenger = new PassengerModel();
newPassenger.setFlightNumber(Integer.parseInt(flightNumberTextBox.getText()));
newPassenger.setPassengerName(passengerNameTextBox.getText());
passengers.add(newPassenger);
AppData.passengers = passengers;
int flightCapacity = AppData.flights.get(AppData.isValidFlight(Integer.parseInt(flightNumberTextBox.getText()))).getFlightCapacity();
AppData.flights.get(AppData.isValidFlight(Integer.parseInt(flightNumberTextBox.getText()))).setFlightCapacity(flightCapacity-1);
JOptionPane.showMessageDialog(null, "Passenger Added Successfully");
flightNumberTextBox.setText("");
passengerNameTextBox.setText("");
flightNumberTextBox.requestFocus();
}else{
JOptionPane.showMessageDialog(null, "Invalid Flight Number");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
});
this.getRootPane().setDefaultButton(submitButton);
formPanel.add(submitButton);
this.add(formPanel, BorderLayout.CENTER);
}
}
AppData.java
package com.chegg.flightsimulator.data;
import java.util.ArrayList;
import java.util.List;
import com.chegg.flightsimulator.model.FlightModel;
import com.chegg.flightsimulator.model.PassengerModel;
public class AppData {
public static List<FlightModel> flights = new ArrayList<FlightModel>();
public static List<PassengerModel> passengers = new ArrayList<PassengerModel>();
public List<FlightModel> getFlights() {
return flights;
}
public void setFlights(List<FlightModel> flights) {
this.flights = flights;
}
public List<PassengerModel> getPassegers() {
return passengers;
}
public void setPassegers(List<PassengerModel> passegers) {
this.passengers = passegers;
}
public static int isValidFlight(int flightNumber){
for(int i=0; i<flights.size(); i++){
if(flightNumber == flights.get(i).getFlightNumber()){
return i;
}
}
return -1;
}
}
PassengerModel.java
package com.chegg.flightsimulator.model;
public class PassengerModel {
String passengerName;
public String getPassengerName() {
return passengerName;
}
public void setPassengerName(String passengerName) {
this.passengerName = passengerName;
}
public int getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(int flightNumber) {
this.flightNumber = flightNumber;
}
int flightNumber;
}
FlightModel.java
package com.chegg.flightsimulator.model;
public class FlightModel {
int flightNumber;
public int getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(int flightNumber) {
this.flightNumber = flightNumber;
}
public int getFlightWidth() {
return flightWidth;
}
public void setFlightWidth(int flightWidth) {
this.flightWidth = flightWidth;
}
public int getFlightCapacity() {
return flightCapacity;
}
public void setFlightCapacity(int flightCapacity) {
this.flightCapacity = flightCapacity;
}
int flightWidth;
int flightCapacity;
}
DisplayFlight.java
package com.chegg.flightsimulator.gui;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.model.FlightModel;
public class DisplayFlight extends JFrame{
public DisplayFlight(){
this.setTitle("Display Passenger");
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new BorderLayout());
String textToBeDisplayed = "";
List flight = AppData.flights;
for(int i=0; i<flight.size(); i++){
FlightModel myFlight = (FlightModel) flight.get(i);
textToBeDisplayed += myFlight.getFlightNumber();
textToBeDisplayed += " ";
textToBeDisplayed += myFlight.getFlightWidth();
textToBeDisplayed += " ";
textToBeDisplayed += myFlight.getFlightCapacity();
textToBeDisplayed += " ";
}
JTextArea myTextArea = new JTextArea(textToBeDisplayed);
myTextArea.setEditable(false); // the user is not allowed to edit the text area
JScrollPane myPane = new JScrollPane(myTextArea);
this.add(myPane);
}
}
DisplayPassenger.java
package com.chegg.flightsimulator.gui;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.model.PassengerModel;
public class DisplayPassenger extends JFrame {
public DisplayPassenger() {
this.setTitle("Display Passenger");
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new BorderLayout());
String textToBeDisplayed = "";
List passenger = AppData.passengers;
for(int i=0; i<passenger.size(); i++){
PassengerModel myFlight = (PassengerModel) passenger.get(i);
textToBeDisplayed += myFlight.getFlightNumber();
textToBeDisplayed += " ";
textToBeDisplayed += myFlight.getPassengerName();
textToBeDisplayed += " ";
}
JTextArea myTextArea = new JTextArea(textToBeDisplayed);
myTextArea.setEditable(false); // the user is not allowed to edit the text area
JScrollPane myPane = new JScrollPane(myTextArea);
this.add(myPane);
}
}
HandleFlight.java
package com.chegg.flightsimulator.gui;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.chegg.flightsimulator.data.AppData;
import com.chegg.flightsimulator.model.FlightModel;
public class HandleFlight extends JFrame {
public HandleFlight(int flightNumber) {
this.setTitle("Displaying Details of Flight Number "+ flightNumber);
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new BorderLayout());
String textToBeDisplayed = "";
List<FlightModel> flight = AppData.flights;
FlightModel myFlight = (FlightModel) flight.get(AppData.isValidFlight(flightNumber));
textToBeDisplayed += myFlight.getFlightNumber();
textToBeDisplayed += " ";
textToBeDisplayed += myFlight.getFlightCapacity();
textToBeDisplayed += " ";
JTextArea myTextArea = new JTextArea(textToBeDisplayed);
myTextArea.setEditable(false);
JScrollPane myPane = new JScrollPane(myTextArea);
this.add(myPane, BorderLayout.CENTER);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.