0

so i have program that need save to file and then read from it, i already have read from text file that works but i cant get read from the file working, i had it static before but teacher does not like it and make me to change it to read from file. i never used gui before and the only stuff what he show us in gui is how to place a button and text field.

private DefaultListModel<String> populateFlights() { // populate avalible flights to the list

        DefaultListModel<String> list = new DefaultListModel<String>();
        ArrayList<Flight> FlightList = MainMenu.getAirlineMgr().getFlightList();

        // loop to get flight list
        for (int i = 0; i < FlightList.size(); i++) {
            list.addElement(FlightList.get(i).getFlightNumber());
        }

        return list;
    }

    private void PopulateAvailableSeats(Flight flight, JComboBox<String> cb) { 
        Seat lSeat;
        cb.removeAllItems();
        for (int i = 0; i < flight.getSeats().size(); i++) {
            lSeat = flight.getSeats().get(i);
            if (lSeat.getSeatstatus() == Seat.Status.AVAILABLE) {
                cb.addItem(lSeat.getSeatNo());
            }
        }
    }

    private Double CalculateTotalCost(Flight flight, int discount, Boolean suitCase) {
        Double total;
        total = flight.getCost() - ((discount * flight.getCost()) / 100); // discount 


        if (suitCase) { // if statement to add suitcase if applyed
            total = total + 50;
        }
        return total;
    }

    private void ConfirmBooking(Flight flight, Passenger passenger, String seatNumber, Boolean suitCase) {
        Double totalCost = CalculateTotalCost(flight, passenger.getDiscount(), suitCase);
        Seat lSeat = flight.getSeatByNumber(seatNumber);

        // Assign the Passenger to Seat and change StatusSeat to RESERVED/booked
        passenger.setSeatNo(lSeat.getSeatNo());
        lSeat.setPassenger(passenger);
        lSeat.setSeatstatus(Seat.Status.RESERVED);

        // Create a booking
        Book booking = new Book(passenger);
        booking.setFlightToBook(flight);
        booking.setSeat(lSeat);
        booking.setSuitCase(suitCase);
        booking.setTotalCost(totalCost);

        // Assign booking to the passenger
        passenger.setReservation(booking);

        // Displays save dialog
        try {
            booking.SaveTicket();
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, "File Error");
        }

    }

    private Boolean ValidateBooking(Flight flight) {
        return (flight.getFlightStatus() == Flight.Status.AVAILABLE)
                || (flight.getFlightStatus() == Flight.Status.CHECKING);
    }

    public BookPanel() {
        setBackground(new Color(176, 224, 230));
        setLayout(null);

        JLabel label = new JLabel("Departure Date:");
        label.setBounds(10, 58, 112, 14);
        add(label);

        JLabel lblSelectAFlight = new JLabel("Select a flight:");
        lblSelectAFlight.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblSelectAFlight.setBounds(10, 306, 121, 14);
        add(lblSelectAFlight);

        JLabel label_2 = new JLabel("Departure Airport:");
        label_2.setBounds(10, 9, 112, 14);
        add(label_2);

        JLabel label_3 = new JLabel("Arrival Airport:");
        label_3.setBounds(218, 9, 112, 14);
        add(label_3);

        JLabel label_4 = new JLabel("Arrival Date:");
        label_4.setBounds(218, 58, 112, 14);
        add(label_4);

        JLabel lblFlightStatus1 = new JLabel("Flight Status:");
        lblFlightStatus1.setBounds(10, 108, 99, 14);
        add(lblFlightStatus1);

        JLabel lbDepartureAirport = new JLabel("");
        lbDepartureAirport.setForeground(Color.GREEN);
        lbDepartureAirport.setBackground(Color.ORANGE);
        lbDepartureAirport.setBounds(10, 33, 121, 14);
        add(lbDepartureAirport);

        JLabel lbDepartureDate = new JLabel("");
        lbDepartureDate.setForeground(Color.GREEN);
        lbDepartureDate.setBackground(Color.ORANGE);
        lbDepartureDate.setBounds(10, 83, 183, 14);
        add(lbDepartureDate);

        JLabel lbArrivalAirport = new JLabel("");
        lbArrivalAirport.setForeground(Color.MAGENTA);
        lbArrivalAirport.setBackground(Color.ORANGE);
        lbArrivalAirport.setBounds(218, 34, 121, 13);
        add(lbArrivalAirport);

        JLabel lbArrivalDate = new JLabel("");
        lbArrivalDate.setForeground(Color.MAGENTA);
        lbArrivalDate.setBackground(Color.ORANGE);
        lbArrivalDate.setBounds(218, 83, 183, 14);
        add(lbArrivalDate);

        JLabel lblFlightStatus = new JLabel("");
        lblFlightStatus
                .setFont(lblFlightStatus.getFont().deriveFont(lblFlightStatus.getFont().getStyle() | Font.BOLD, 13f));
        lblFlightStatus.setForeground(Color.BLACK);
        lblFlightStatus.setBackground(Color.ORANGE);
        lblFlightStatus.setBounds(10, 133, 99, 23);
        add(lblFlightStatus);

        JLabel lblPassengerInfo = new JLabel("Passenger info:");
        lblPassengerInfo.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblPassengerInfo.setBounds(10, 167, 99, 14);
        add(lblPassengerInfo);

        JLabel lblName = new JLabel("Name:");
        lblName.setBounds(10, 192, 69, 14);
        add(lblName);

        JLabel lbPassengerName = new JLabel("");
        lbPassengerName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbPassengerName.setBackground(Color.WHITE);
        lbPassengerName.setBounds(99, 192, 187, 14);
        add(lbPassengerName);

        JLabel lbSureName = new JLabel("");
        lbSureName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbSureName.setBackground(Color.WHITE);
        lbSureName.setBounds(99, 212, 187, 14);
        add(lbSureName);

        JLabel lblSureName = new JLabel("Sure Name:");
        lblSureName.setBounds(10, 212, 73, 14);
        add(lblSureName);

        JLabel lblDiscount1 = new JLabel("Discount:");
        lblDiscount1.setBounds(342, 182, 59, 14);
        add(lblDiscount1);

        JLabel lblDiscount = new JLabel("");
        lblDiscount.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblDiscount.setBounds(342, 212, 59, 14);
        add(lblDiscount);

        JLabel lblCost = new JLabel("Cost:");
        lblCost.setBounds(342, 133, 37, 14);
        add(lblCost);

        JSeparator separator = new JSeparator();
        separator.setBounds(449, 144, 1, 2);
        add(separator);

        JSeparator separator_1 = new JSeparator();
        separator_1.setOrientation(SwingConstants.VERTICAL);
        separator_1.setBounds(412, 9, 13, 393);
        add(separator_1);

        JLabel lblType = new JLabel("Type:");
        lblType.setBounds(10, 233, 73, 14);
        add(lblType);

        JLabel lbType = new JLabel("");
        lbType.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbType.setBounds(103, 233, 187, 14);
        add(lbType);

        JComboBox<String> cbSeats = new JComboBox<String>();
        cbSeats.setBounds(289, 313, 112, 20);
        add(cbSeats);

        JLabel lblAvailableSeats = new JLabel("Available seats:");
        lblAvailableSeats.setBounds(196, 316, 112, 14);
        add(lblAvailableSeats);

        JLabel lbCost = new JLabel("");
        lbCost.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbCost.setBounds(342, 157, 59, 14);
        add(lbCost);

        JLabel lblTotalCost1 = new JLabel("Total Cost:");
        lblTotalCost1.setBounds(342, 233, 69, 14);
        add(lblTotalCost1);

        JLabel lblTotalCost = new JLabel("");
        lblTotalCost.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblTotalCost.setBounds(328, 258, 73, 23);
        add(lblTotalCost);

        // Action for CheckBox
        JCheckBox chckbxNewCheckBox = new JCheckBox("Suite case (+$50)");
        chckbxNewCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblTotalCost.setText(Double.toString(
                        CalculateTotalCost(flightSelected, passenger.getDiscount(), chckbxNewCheckBox.isSelected()))
                        + "$");
            }
        });
        chckbxNewCheckBox.setBounds(180, 272, 142, 23);
        add(chckbxNewCheckBox);

        // Action for button cancel
        JButton button = new JButton("Cancel");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MainMenu.hideBook();
            }
        });
        button.setBounds(10, 379, 89, 23);
        add(button);

        // Action for button confirm
        JButton btnPay = new JButton("Confirm");
        btnPay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (ValidateBooking(flightSelected)) {
                    ConfirmBooking(flightSelected, passenger, cbSeats.getSelectedItem().toString(),
                            chckbxNewCheckBox.isSelected());

                    // JOptionPane.showMessageDialog(null,
                    // directory.getAbsolutePath()));
                    MainMenu.hideBook();
                } else {
                    JOptionPane.showMessageDialog(null, "The flight is not available");
                }
            }
        });
        btnPay.setBounds(312, 379, 89, 23);
        add(btnPay);

        // passenger info
        passenger = MainMenu.getAirlineMgr().getPassenger();
        if (passenger != null) {
            lbPassengerName.setText(passenger.getFname());
            lbSureName.setText(passenger.getSname());
            lblDiscount.setText(Integer.toString(passenger.getDiscount()) + "%");
            if (passenger.getClass() == Business.class) {
                lbType.setText("BUSINESS");
            } else {
                if (passenger.getClass() == Ordinary.class) {
                    lbType.setText("ORDINARY");
                } else {
                    if (passenger.getClass() == Island.class) {
                        lbType.setText("ISLAND");
                    }
                }
            }
        }

        // flight info
        JList list = new JList(populateFlights());
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                flightSelected = MainMenu.getAirlineMgr().getFlightByNumber(list.getSelectedValue().toString());
                lbDepartureAirport.setText(flightSelected.getDepartureAirport());
                lbArrivalAirport.setText(flightSelected.getArrivalAirport());
                lbDepartureDate.setText(flightSelected.getDepartureDate().toString());
                lbArrivalDate.setText(flightSelected.getArrivalDate().toString());
                lbCost.setText(Double.toString(flightSelected.getCost()) + "£");

                PopulateAvailableSeats(flightSelected, cbSeats);
                lblTotalCost.setText(Double.toString(
                        CalculateTotalCost(flightSelected, passenger.getDiscount(), chckbxNewCheckBox.isSelected()))
                        + "£");

                // if statement for STATUS
                if (flightSelected.getFlightStatus() == Flight.Status.AVAILABLE) {
                    lblFlightStatus.setText("AVAILABLE");
                } else {
                    if (flightSelected.getFlightStatus() == Flight.Status.BOARDING) {
                        lblFlightStatus.setText("BOARDING");
                    } else {
                        if (flightSelected.getFlightStatus() == Flight.Status.CHECKING) {
                            lblFlightStatus.setText("CHECKING");
                        } else {
                            if (flightSelected.getFlightStatus() == Flight.Status.CLOSED) {
                                lblFlightStatus.setText("CLOSED");
                            }
                        }
                    }
                }
            }
        });
        list.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
        list.setBounds(104, 314, 89, 88);
        add(list);

        JLabel lblBookAFlight = new JLabel("Book a Flight");
        lblBookAFlight.setFont(new Font("Tele-Marines", Font.PLAIN, 12));
        lblBookAFlight.setBounds(140, 142, 81, 14);
        add(lblBookAFlight);
    }
}

oh yeah if this help here is save to the file code:pic of save to file

Timothy Groote
  • 8,614
  • 26
  • 52
maniekv16
  • 7
  • 4
  • Hi maniekv16, i'm having some trouble understanding exactly what problems you are running into, but i think you're having some problems with writing to a file using `PrintWriter`. you might want to look here : http://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java – Timothy Groote Mar 29 '17 at 09:40
  • 1) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along w/ layout padding and borders 4 [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 29 '17 at 09:40
  • Also, a tip ; do not post your entire program, but rather the part you feel is needed to explain your problem. if you encounter errors, or an exception, it is also helpful if you include the error message and/or stack trace. – Timothy Groote Mar 29 '17 at 09:41
  • When i submitted the program i had this comment ( The flight details should be retrieved from a text file rather than having them static and created within the program). 2nd sorry for my wrong writing im polish so its common for me. its my 1st program on gui too so sorry for mess – maniekv16 Mar 29 '17 at 10:29

0 Answers0