Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

ThisassessmentitemisdesignedtotestyourunderstandingoftopicssuchasGUIinput/output,arrays/ arrayLists, methodswithparametersandsearching.Theassessmenttaskistowrite,compileand executea...

1 answer below »

ThisassessmentitemisdesignedtotestyourunderstandingoftopicssuchasGUIinput/output,arrays/

arrayLists, methodswithparametersandsearching.Theassessmenttaskistowrite,compileand

executea Javaprogramusingthe above mentioned topics.

Details

For this assignment, you are required to develop a Windowed GUI Java Program to demonstrate you can use Java constructs including input/output via a GUI interface, Java primitive and built-in types, Java defined objects, arrays, selection and looping statements and various other Java commands. Your program must produce the correct results. The code for the GUI interface is supplied and is available on the course website, you must write the underlying code to implement the program. The command buttons are linked to appropriate methods in the given code. Please spend a bit of time looking at the given code to familiarise yourself with it and where you have to complete the code. You will need to write comments in the supplied code as well as your own additions.

Assignment Specification

In assignment one we read in multiple customer names, license numbers and days hired using both Scanner and GUI dialogs. In this assignment we are going to read in the data and output information via a GUI interface, the code for the GUI interface CarHireGUI.java is supplied (via the Moodle web site) which supplies the basic functionality of the interface. We are also going to store the information in an array of CarHire objects. The following image is the GUI interface when the CarHireGUI program runs.

Look at the code supplied and trace the execution and you will see the buttons are linked to blank methods (stubs) which you will implement the various choices via the buttons. The GUI interface contains three JLabels for the prompts. There are three JTextFields in which the customer name, license number and days hired are read. Four JButtons are after the days hired JTextField which link to blank methods for implementing the functionality of the application. There is also a JTextArea for displaying the output.

CarHire class

First step is to create a class called CarHire (CarHire.java) it will not contain a main method.

The CarHire class will be very simple it will contain three private instance variables:

· customerName as a String

· licenseNumber as a String

· days hired as an integer

The following public methods will have to be implemented:a

· A default constructor

· A parameterised constructor

· Three set methods (mutators)

· Three get methods (accessors) o calculateHireRental( ) method*

*You will also create a public value returning method to return the calculated rental. The rental can be derived from the calculation according to the daily rental rates as Assignment 1 (also see below for the daily hire rates). In other words the rental will be retrieved via your calculateHireRental() method, which is a user-defined method inside the CarHire class and it does not contain the keyword static.

2018 XYZ Car Hire Prices

1-3 days: $34.50 per day

From 4 to 7 days: $30.5 per day

More than 7 days: $22.5 per day

CarHireGUI class

Once the CarHire class is implemented and fully tested we can now start to implement the functionality of the GUI interface.

Data structures

For this assignment we are going to store the customer names, license numbers and days hired in an array of CarHire objects. Do not use the ArrayList data structure.

Declare an array of CarHire objects as an instance variable inside the CarHireGUI class, the array should hold ten entries. Use a constant for the maximum entries allowed.

private CarHire [] carHireArray = new CarHire[MAX_NUM];

We need another instance variable (integer) to keep track of the number of the customer being entered and use this for the index into the array of CarHire objects.

private int currentCustomer = 0;

Button options

1. Enter button: enterData()

For assignment two we are going to use the JTextFields for our input. An entry with sample data in three textFields is shown below

When the Enter button is pushed the program will transfer to the method: enterData(), this is where we read the customer name, license number and days hired and add them to the CarHire array.

The text in the JTextFields is retrieved using the getText() method:

String customerName = nameField.getText();

String licenseNumber = licenseField.getText();

When we read the days hired input we are reading a string from the GUI, we will need to convert this to an integer using the Integer wrapper class as per assignment one.

int days = Integer.parseInt(daysField.getText());

We need to add these values of customer name, license number and days hired to the array of CarHire objects, when we declared our array using the new keyword, only an array of references were created, we need to create an instance of each of the CarHire objects. When we create the CarHire object we can pass the customer name, license number and days hired to the object via the parameterised constructor as follows:

carHireArray[currentCustomer] = new

CarHire(customerName,licenseNumber, days);

Remember to increment currentCustomer at the end of the enterData () method

Alternatively, you can use the setCustomerName( ), setLicenseNumber(), and setDays( ) methods to a CarHire object. Next we will output the entry including the rental in the JTextArea as below when the Enter button is clicked.

To retrieve the values from a CarHire object (as an element of the array - carHireArray), you can use the get and calculateHireRental method in your CarHire class in such a way as:

String customerName=carHireArray[index].getCustomerName();

double rental=carHireArray[index].calculateHireRental();

The supplied code may contain a method for printing the heading and the line underneath. Just like the JTextField the JTextArea has a method setText() to set the text in the text area, note this overwrites the contents of the text area. In addition, the JTextArea also has a method named append() that allows the new generated text being added to the existing text.

When the data has been entered and displayed, the customer name, license number and days hired JTextFields should be cleared. We can clear the contents by using: textField.setText("");

The focus should then return to the customer name JTextField by using:

nameField.requestFocus();

Data validation (you can implement this after you have got the basic functionality implemented)

If one or three textFields has not been entered data and the ‘Enter’ button is clicked, your program should pop up a message box to remind user the required input.

Use an if statement at the beginning of the method and after displaying the error dialog use the return statement to exit the method.

if (nameField.getText().compareTo("") == 0) // true when blank

Use the above code to check the name field for text if there is no text display the following error dialog and use the return statement to exit the method, the focus should return to the name field.

The license number and days hired fields should also be checked for text and the focus should be returned to the license number field and the days hired field respectively.

We will not worry about checking data types or numeric ranges in this assignment

2. Display all customer data including rental: displayAll()

When this option is selected, display all of the customer names, license numbers and days hired plus the rentals which have been calculated so far. At the end of the list display the number of entries, the average days hired and the total rental value. Here it is an example with 3 entries data.

Use a loop structure to iterate through the array, you should be sharing the printing functionality with the Enter button.

Only print the entries which have been entered so far and not the whole array, use your instance variable currentCustomer as the termination value in your loop. You can sum the days hired in the loop for the average calculation and sum the rental value and finally use the append() method of the JTextArea to display them on the text area.

If no entries have been added clear the text area and display the following error dialog, repeat this for your search.

3. Search for a customer’s booking: search()

You can just use a simple linear search which will be case insensitive. Use the

JOptionPane.showInputDialog() method to input the customer name.

If the search is successful display the details about this customer. See the following sample result.

If the search is unsuccessful display an appropriate message and clear the text area.

4. Exit the application: exit()

The exit method is called when the user pushes the exit button or when they select the system close (X at top right hand corner), try and find the code which does this.

During a typical exiting process we may want to ask the user if they want to save any files they have open or if they really want to exit, in this assignment we will just print an exit message.

Marking Scheme

Answered Same Day Sep 26, 2020

Solution

Ujjawal answered on Sep 29 2020
135 Votes
CarHire.java
CarHire.java
public class CarHire {
    /**
     * Name of custome
     *
    private String customerName;
    /**
     * License numbe
     *
    private String licenseNumber;
    /**
     * Days hired
     *
    private int daysHired;
    public CarHire() {
    }
    public CarHire(String customerName, String licenseNumber, int daysHired) {
        this.customerName = customerName;
        this.licenseNumber = licenseNumber;
        this.daysHired = daysHired;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
    public String getLicenseNumber() {
        return licenseNumber;
    }
    public void setLicenseNumber(String licenseNumber) {
        this.licenseNumber = licenseNumber;
    }
    public int getDaysHired() {
        return daysHired;
    }
    public void setDaysHired(int daysHired) {
        this.daysHired = daysHired;
    }
    /**
     * Calculates rental according to the days hired
     * 
     * @return
     *
    public double calculateHireRental() {
        if (daysHired >= 1 && daysHired <= 3) {
            return daysHired * 34.5;
        } else if (daysHired >= 4 && daysHired <= 7) {
            return daysHired * 30.5;
        } else if (daysHired > 7) {
            return daysHired * 22.5;
        }
        return 0.0;
    }
}
CarHireGUI.java
CarHireGUI.java
**
 * Supplied GUI code for ass2, coit11222, T2 2018
 * You are going to complete the code for each required method
 *
**
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 class definition
public class CarHireGUI extends JFrame implements ActionListener {
    
 GUI components building
    private JLabel nameLabel = new JLabel("Customer name");
    private JLabel licenseLabel = new JLabel("License number");
    private JLabel daysLabel = new JLabel("Hired days");
    private JTextField nameField = new JTextField(28);
    private JTextField licenseField = new JTextField(14);
    private JTextField daysField = new JTextField(7);
    private JButton enterButton = new JButton("Enter"); 
    private JButton displayButton = new JButton("Display");
    private JButton searchButton = new JButton("Search");
    private JButton exitButton = new JButton("Exit");
    private JTextArea textArea = new JTextArea(16, 38);
    private JScrollPane scrollPane; 
 scroll pane for the text area
    
 max count of car hire objects
    private final int MAX_NUM = 10;
    
 cu
ent customer numbe
    private int cu
entCustomer = 0;
    
 a
ay of car hire objects
    CarHire[] cars = new CarHire[MAX_NUM];
    private static final int FRAME_WIDTH = 490;
 window size
    private static final int...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here