mardi 5 mai 2015

How to display event handler

ReservationsGUI.java

public class ReservationsGUI extends javax.swing.JFrame {
    private Object OutputPane;

/**
 * Creates new form ReservationsGUI
 */
public ReservationsGUI() {
    initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    nameTextField = new javax.swing.JTextField();
    nameLabel = new javax.swing.JLabel();
    phoneNumberLabel = new javax.swing.JLabel();
    phoneNumberTextField = new javax.swing.JTextField();
    numberInPartyLabel = new javax.swing.JLabel();
    timeLabel = new javax.swing.JLabel();
    reserveButton = new javax.swing.JToggleButton();
    reservationsLabel = new javax.swing.JLabel();
    reservationsTextField = new javax.swing.JTextField();
    displayAllButton = new javax.swing.JToggleButton();
    numberCombo = new javax.swing.JComboBox();
    timeCombo = new javax.swing.JComboBox();
    jMenuBar2 = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    clearMenuItem = new javax.swing.JMenuItem();
    exitMenuItem = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    nameTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nameTextFieldActionPerformed(evt);
        }
    });

    nameLabel.setText("Name:");

    phoneNumberLabel.setText("Phone Number:");

    phoneNumberTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            phoneNumberTextFieldActionPerformed(evt);
        }
    });

    numberInPartyLabel.setText("Number in Party");

    timeLabel.setText("Time");

    reserveButton.setText("Reserve");
    reserveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            reserveButtonActionPerformed(evt);
        }
    });

    reservationsLabel.setText("Tonight's Reservations");

    displayAllButton.setText("Display All");
    displayAllButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            displayAllButtonActionPerformed(evt);
        }
    });

    numberCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
    numberCombo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            numberComboActionPerformed(evt);
        }
    });

    timeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00", "7:15", "7:30", "7:45", "8:00", "8:15", "8:30", " " }));
    timeCombo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            timeComboActionPerformed(evt);
        }
    });

    fileMenu.setText("File");

    clearMenuItem.setText("Clear");
    clearMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            clearMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(clearMenuItem);

    exitMenuItem.setText("Exit");
    exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(exitMenuItem);

    jMenuBar2.add(fileMenu);

    setJMenuBar(jMenuBar2);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel)
                                .addComponent(phoneNumberLabel))
                            .addGap(27, 27, 27)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(phoneNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(numberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(timeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(numberInPartyLabel)
                        .addComponent(timeLabel)))
                .addGroup(layout.createSequentialGroup()
                    .addGap(45, 45, 45)
                    .addComponent(reserveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(reservationsLabel)
                    .addGap(40, 40, 40)
                    .addComponent(displayAllButton)
                    .addGap(144, 144, 144))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(reservationsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(39, 39, 39))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(reservationsLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField)
                    .addComponent(displayAllButton)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(phoneNumberLabel)
                        .addComponent(phoneNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(numberInPartyLabel)
                        .addComponent(numberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(timeLabel)
                        .addComponent(timeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(reserveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(reservationsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(67, 67, 67))
    );

    pack();
}// </editor-fold>                        

private void phoneNumberTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                     
    // TODO add your handling code here:
}                                                    

private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
}                                             

private void clearMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                              
     if(JOptionPane.showConfirmDialog(null,"Are you sure ","Request",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
     {  
        reservationsTextField.setText("");
        nameTextField.setText("");
        phoneNumberTextField.setText("");
     }


}                                             

private void numberComboActionPerformed(java.awt.event.ActionEvent evt) {                                            


}                                           

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
   if(JOptionPane.showConfirmDialog(null,"Are you sure ","Request",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
     {  
          System.exit(0);
     }

}                                            

private static String getFilename()
{
    String filename;

    Date today = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyy");
    filename = "C:\\Users\\User\\Documents\\Computer science\\reservations" + dateFormat.format(today) + ".txt";

    return filename;
}
private void reserveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              

 try {
        String filename=getFilename();

        BufferedWriter outputWriter= new BufferedWriter(new FileWriter(filename));

        if (!nameTextField.getText().equals("") && !phoneNumberTextField.getText().equals(""))
        {
            outputWriter.write("Name: " + nameTextField.getText()); 
            outputWriter.newLine();
            outputWriter.write("Phone number: " + phoneNumberTextField.getText());
            outputWriter.newLine();
            outputWriter.write("Number in Party: " + numberCombo.getSelectedItem());
            outputWriter.newLine();
            outputWriter.write("Time: " + timeCombo.getSelectedItem());;;;;

            outputWriter.flush();
            outputWriter.close();
            JOptionPane.showMessageDialog(null, " Your reservation has been save. You can preview details at " + filename);
            nameTextField.setText("");
            phoneNumberTextField.setText("");
    }
        else 
        {
            JOptionPane.showMessageDialog(null, " Please make sure all your values are filled out");

        }

        }


catch(IOException e){
  e.printStackTrace();

}

}                                             



private void timeComboActionPerformed(java.awt.event.ActionEvent evt) {                                          

}                                         

private void displayAllButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 

}                                                



 //delete this later
 public static void main(String[] args) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ReservationsGUI().setVisible(true);
        }

    });

}

// Variables declaration - do not modify                     
private javax.swing.JMenuItem clearMenuItem;
private javax.swing.JToggleButton displayAllButton;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField nameTextField;
private javax.swing.JComboBox numberCombo;
private javax.swing.JLabel numberInPartyLabel;
private javax.swing.JLabel phoneNumberLabel;
private javax.swing.JTextField phoneNumberTextField;
private javax.swing.JLabel reservationsLabel;
private javax.swing.JTextField reservationsTextField;
private javax.swing.JToggleButton reserveButton;
private javax.swing.JComboBox timeCombo;
private javax.swing.JLabel timeLabel;
// End of variables declaration                   
}

Reservation.java

class Reservation {

public String name;
public String phone;
public int numInParty;
public String time;


public Reservation(String name, String phone, int numInParty, String time) {
    this.name = name;
    this.phone = phone;
    this.numInParty = numInParty;
    this.time = time;





    class ReservationsList {

    Reservation[]  reservationsArray;

    public ReservationsList(int arrayLength)
    {
        reservationsArray = new Reservation[arrayLength];
    }

    private void addArrayItem (int index, String name, String phone, int numberInParty, String time)
    {

        Reservation reservation = new Reservation(name, phone, numberInParty, time);
        reservationsArray[index] = reservation;
    }







        //swaps the elements of 2 indices
    public Reservation[] swapElement (Reservation[] a, int index1, int index2){

        Reservation swapVal = a[index1];
        Reservation swapVal2 = a[index2];

        a[index2] = swapVal;
        a[index1]=swapVal2;

        return a;
    }

    //sorts earliest to latest time and calls indexOfMaxInRange
    public Reservation[] sortArray(Reservation[] a){
        for (int i = a.length-1; i<-1; i--){
            int maxIndex = indexOfMaxInRange(a, 0, i);
            a = swapElement(a,i,maxIndex);
        }
        return a;
    }

    //finds the earliest time
    public int indexOfMaxInRange (Reservation[] a, int low, int high){
        int index = low;
        double check = convertTime(a[low].time);

        for(int i=low; i <= high; i++){
            double nextTime = convertTime(a[i].time);
            if (nextTime > check){
                index = i;
                check = nextTime;
            }
        }
        return index;
    }

    //converts time to a double
    public double convertTime (String timeString){
        String[] timeArray = timeString.split(":");

        double hour = Double.parseDouble(timeArray[0]);
        double minute = Double.parseDouble(timeArray[1]);
        double time = hour + minute/60;
        return time;

    }
}

  • Create the ReservationsList object based on the length of the file. When allocating the size of the reservation array, remember that there are four lines in the file for each reservation.
  • Load the ReservationsList array using a helper method as suggested below. Sorts the array based on time
  • Displays the results, formatted, in a text pane. You can use characters "\t" for tab and "\n" for a line feed.
  • private static int getFileLength(String filename)
  • This method needs to open the file, read every line in it using BufferedReader and count them, returning the total number of lines in the file.

    private static void loadArray (String filename, ReservationsList resList)

This method needs to open the file, read every line using BufferedReader and add Reservation objects to the reservations list array.

For these methods, you will need to catch the exceptions that can occur. You can simply print the message in the catch:

System.out.println(e.getMessage());

Okay, so I've basically been able to figure out everything else but I'm confused just putting this together. All I need to do to finish is to just finish the event handler for the the display. Yes it may be homework, but I've done most of I'm not asking for you guys to do it for me, just help with this tiny piece.

Aucun commentaire:

Enregistrer un commentaire