question archive Part 1: Wk 2 Assignment: GUI and Input, Output, and Variable Assignment Content: Create Java™ travel authorization request graphical user interface (GUI) application via NetBeans to calculate the cost of a business trip
Subject:Computer SciencePrice:6.86 Bought11
Part 1: Wk 2 Assignment: GUI and Input, Output, and Variable
Assignment Content: Create Java™ travel authorization request graphical user interface (GUI) application via NetBeans to calculate the cost of a business trip.
The user will enter the name of the traveler and the cost of each item for a trip request. When the user clicks the calculate button, 10% of the total amount will be added to the trip total and be displayed in the Summary field. The use of variables are required to store the costs of the individual items. Submit your assignment as follows the Netbeans cut/paste the source to a MS Word document and a screen shoot of the output.
Part 2: Wk 3 Assignment: GUI and Input, Output, and Variable
Assignment Content:
Modify the deliverable from Week 2 to:
Week 3 Assignment Output is only required. With Screen shot of Output
==================================== TravelAuthorization.java =========================================
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Asus */ public class TravelAuthorization extends javax.swing.JFrame { /** * Creates new form TravelAuthorization */ public TravelAuthorization() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { summaryField = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); nameTextField = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); airfareTextField = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); foodTextField = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); hotelTextField = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); carTextField = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); shuttleTextField = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); calculateButton = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); citiesComboBox = new javax.swing.JComboBox<>(); crc1 = new javax.swing.JRadioButton(); crc2 = new javax.swing.JRadioButton(); crc3 = new javax.swing.JRadioButton(); jLabel10 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jLabel1.setText("Traveler Name: "); jLabel2.setText("Airfare Cost:"); jLabel3.setText("Food Cost:"); foodTextField.setEditable(false); jLabel4.setText("Hotel Cost:"); jLabel5.setText("Car Cost:"); jLabel6.setText("Shuttle Cost:"); jLabel7.setText("SUMMARY:"); calculateButton.setText("CALCULATE"); calculateButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { calculateButtonActionPerformed(evt); } }); jLabel8.setText("TRAVEL AUTHORIZATION REQUEST"); jLabel9.setText("Location:"); citiesComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); citiesComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { citiesComboBoxActionPerformed(evt); } }); crc1.setText("CRC1"); crc1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { crc1ActionPerformed(evt); } }); crc2.setText("CRC2"); crc2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { crc2ActionPerformed(evt); } }); crc3.setText("CRC3"); crc3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { crc3ActionPerformed(evt); } }); jLabel10.setText("Car Rental Company:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( 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(jLabel4) .addComponent(jLabel2) .addComponent(jLabel9) .addComponent(jLabel3)) .addGap(41, 41, 41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(airfareTextField) .addComponent(foodTextField) .addComponent(citiesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(hotelTextField, javax.swing.GroupLayout.Alignment.TRAILING))) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(26, 26, 26) .addComponent(nameTextField)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(crc1) .addGap(28, 28, 28) .addComponent(crc2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE) .addComponent(crc3)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent(jLabel6)) .addGap(44, 44, 44) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(shuttleTextField) .addComponent(carTextField)))) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(62, 62, 62) .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(summaryField)) .addGroup(layout.createSequentialGroup() .addGap(124, 124, 124) .addComponent(calculateButton))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel8) .addGap(76, 76, 76)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel8) .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(nameTextField, 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(airfareTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9) .addComponent(citiesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(foodTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(hotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addGap(13, 13, 13) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel10) .addComponent(crc1) .addComponent(crc2) .addComponent(crc3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(carTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(shuttleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7) .addComponent(summaryField)) .addGap(18, 18, 18) .addComponent(calculateButton) .addContainerGap(25, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) { //When the calculate button is clicked, variables will be initiated with values from their corresponding text fields. String travelerName = nameTextField.getText(); //gets the text from the nameTextField text field and passes it to the travelerName variable. The same goes with the other variables Double airfareCost = Double.parseDouble(airfareTextField.getText()); Double foodCost = Double.parseDouble(foodTextField.getText()); Double hotelCost = Double.parseDouble(hotelTextField.getText()); Double carCost = Double.parseDouble(carTextField.getText()); Double shuttleCost = Double.parseDouble(shuttleTextField.getText()); //tripTotal is the base total of all the costs entered by the traveler. TotalCost is the price after a 10% charge has been added to tripTotal. Double tripTotal = airfareCost + foodCost + hotelCost + carCost + shuttleCost; Double totalCost = tripTotal + (tripTotal * 0.10); summaryField.setText(totalCost.toString()); //In the summary field, totalCost will be shown. } private void formWindowOpened(java.awt.event.WindowEvent evt) { citiesComboBox.removeAllItems(); //once the GUI is opened, citiesComboBox's items will be reset. Thus we'll have to add items. citiesComboBox.addItem(""); //first item is "" so that the initial selected item is blank. citiesComboBox.addItem("Sydney"); //these are the 5 cities. You can change them depending on your own liking. citiesComboBox.addItem("Berlin"); citiesComboBox.addItem("Seoul"); citiesComboBox.addItem("Milan"); citiesComboBox.addItem("London"); //an actionlistener is added to the combobox so that the foodtextfield's value will change depending on what city is selected. citiesComboBox.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { if(citiesComboBox.getSelectedIndex()==0){ foodTextField.setText(""); }else if(citiesComboBox.getSelectedIndex()==1){ foodTextField.setText("70"); }else if(citiesComboBox.getSelectedIndex()==2){ foodTextField.setText("80"); }else if(citiesComboBox.getSelectedIndex()==3){ foodTextField.setText("85"); }else if(citiesComboBox.getSelectedIndex()==4){ foodTextField.setText("90"); }else { foodTextField.setText("95"); } } }); } //if crc1 radiobutton is selected, crc2 and crc3 will both deselected since the traveler can only choose 1. Same goes with the other radiobuttons. private void crc1ActionPerformed(java.awt.event.ActionEvent evt) { crc2.setSelected(false); crc3.setSelected(false); } private void crc2ActionPerformed(java.awt.event.ActionEvent evt) { crc1.setSelected(false); crc3.setSelected(false); } private void crc3ActionPerformed(java.awt.event.ActionEvent evt) { crc1.setSelected(false); crc2.setSelected(false); } private void citiesComboBoxActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TravelAuthorization.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TravelAuthorization.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TravelAuthorization.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TravelAuthorization.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TravelAuthorization().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField airfareTextField; private javax.swing.JButton calculateButton; private javax.swing.JTextField carTextField; private javax.swing.JComboBox<String> citiesComboBox; private javax.swing.JRadioButton crc1; private javax.swing.JRadioButton crc2; private javax.swing.JRadioButton crc3; private javax.swing.JTextField foodTextField; private javax.swing.JTextField hotelTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JTextField nameTextField; private javax.swing.JTextField shuttleTextField; private javax.swing.JLabel summaryField; // End of variables declaration }
In this project, a JFrame was used for the GUI. Here, the inputs and outputs will be displayed.
EventListeners and ActionListeners were also used for the functionality of the system.
Please see the attached file for the complete solution