Create a Java GUI application that will capture the commission earned by an estate agent for the

sale of a property. The application must capture all the required input and display the estate
agent commission report.Q.2.1 On the form, create one combo box for the estate agent location. The estate agent
locations include Cape Town, Durban, and Pretoria. You are also required to create
three text fields to capture the estate agent name, property price, and commission
percentage. Also, create a text area to display the estate agent report.
Q.2.2 A menu system is required for the user to interact with. The following menu items are
required:
File
• Exit
Tools
• Process Report.
• Clear
• Save Report
Q.2.3 When the Exit menu item is clicked, close the application.
Q.2.4 When the Process Report menu item is clicked, capture the selected agent location,
estate agent name, property price, and commission percentage, and display the estate
agent report. The commission is calculated by multiplying the property price by the
commission percentage.Q.2.5 When the Save Report menu item is clicked, save the estate agent report to a text file
named report.txt.
Sample Screenshot.Q.2.6 When clicking the Clear Menu item, set the text fields and text area to the default
state.
Q.2.7 Make use of a class named EstateAgent that contains methods to calculate the estate
agent commission and validate the data. Only valid data should be captured in the
application; the validation rules are:
VALIDATION RULE
Agent Location It cannot be empty.
Agent Name It cannot be empty.
Property Price Cannot be less than or equal to zero (0)
Commission Percentage Cannot be less than or equal to zero (0)
The EstateAgent class must implement an IEstateAgent interface that contains the
following:
public interface IEstateAgent {
double CalculateCommission(String propertyPrice, String agentCommission);
boolean ValidateData(Data dataToValidate);.Q.2.8 You are required to write unit tests for the application. Create a test package within
the application you created, which will contain the necessary unit tests.
You are required to write the following unit tests:
Test Name Test Purpose
CalculateCommission_CalculatedSuccessfully() To supply the property price and
agent commission percentage to
the Calculate Commission
method. The test will determine
that the correct commission
value is returned.
CalculateCommission_CalculatedUnSuccessfully() To supply the property price and
agent commission percentage to
the Calculate Commission
method. The test will determine
if there is a calculation error.
Validation Test You must only write one unit
test to prove that the validation
performs as expected. Examples
can include validating location is
valid, validating name is valid or
validating property price is valid..in java

Here's an example implementation of the Java GUI application:

```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class EstateAgentApp extends JFrame {
private JComboBox<String> locationComboBox;
private JTextField nameTextField;
private JTextField priceTextField;
private JTextField commissionTextField;
private JTextArea reportTextArea;

private ArrayList<EstateAgent> agentList;

public EstateAgentApp(){
setTitle("Estate Agent Commission");
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

// Initialize components
locationComboBox = new JComboBox<>(new String[]{"Cape Town", "Durban", "Pretoria"});
nameTextField = new JTextField();
priceTextField = new JTextField();
commissionTextField = new JTextField();
reportTextArea = new JTextArea();

// Create menu bar
JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);

JMenu toolsMenu = new JMenu("Tools");
JMenuItem processMenuItem = new JMenuItem("Process Report");
processMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processReport();
}
});
JMenuItem clearMenuItem = new JMenuItem("Clear");
clearMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
}
});
JMenuItem saveMenuItem = new JMenuItem("Save Report");
saveMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveReport();
}
});
toolsMenu.add(processMenuItem);
toolsMenu.add(clearMenuItem);
toolsMenu.add(saveMenuItem);
menuBar.add(toolsMenu);

setJMenuBar(menuBar);

// Create form panel
JPanel formPanel = new JPanel(new GridLayout(4, 2));
formPanel.add(new JLabel("Agent Location: "));
formPanel.add(locationComboBox);
formPanel.add(new JLabel("Agent Name: "));
formPanel.add(nameTextField);
formPanel.add(new JLabel("Property Price: "));
formPanel.add(priceTextField);
formPanel.add(new JLabel("Commission Percentage: "));
formPanel.add(commissionTextField);

add(formPanel, BorderLayout.NORTH);
add(new JScrollPane(reportTextArea), BorderLayout.CENTER);

agentList = new ArrayList<>();
}

public void processReport(){
String agentLocation = locationComboBox.getSelectedItem().toString();
String agentName = nameTextField.getText();
String propertyPriceStr = priceTextField.getText();
String commissionPercentageStr = commissionTextField.getText();

EstateAgent agent = new EstateAgent(agentLocation, agentName, propertyPriceStr, commissionPercentageStr);
if(agent.validateData()){
agent.calculateCommission();
agentList.add(agent);
updateReport();
}else{
JOptionPane.showMessageDialog(this, "Invalid data. Please check the inputs.", "Error", JOptionPane.ERROR_MESSAGE);
}
}

public void updateReport(){
reportTextArea.setText("");
for(EstateAgent agent : agentList){
reportTextArea.append(agent.getReportString() + "\n");
}
}

public void clearFields(){
locationComboBox.setSelectedIndex(0);
nameTextField.setText("");
priceTextField.setText("");
commissionTextField.setText("");
reportTextArea.setText("");
agentList.clear();
}

public void saveReport(){
try(FileWriter writer = new FileWriter("report.txt")){
for(EstateAgent agent : agentList){
writer.write(agent.getReportString() + "\n");
}
writer.flush();
JOptionPane.showMessageDialog(this, "Report saved successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(this, "Error saving report.", "Error", JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new EstateAgentApp().setVisible(true);
}
});
}
}

class EstateAgent {
private String agentLocation;
private String agentName;
private double propertyPrice;
private double commissionPercentage;
private double commission;

public EstateAgent(String agentLocation, String agentName, String propertyPriceStr, String commissionPercentageStr) {
this.agentLocation = agentLocation;
this.agentName = agentName;
this.propertyPrice = Double.parseDouble(propertyPriceStr);
this.commissionPercentage = Double.parseDouble(commissionPercentageStr);
}

public void calculateCommission(){
this.commission = propertyPrice * (commissionPercentage / 100.0);
}

public boolean validateData(){
if(agentLocation.isEmpty() || agentName.isEmpty() || propertyPrice <= 0 || commissionPercentage <= 0){
return false;
}
return true;
}

public String getReportString(){
return "Agent Location: " + agentLocation +
"\nAgent Name: " + agentName +
"\nProperty Price: R" + propertyPrice +
"\nCommission Percentage: " + commissionPercentage +
"\nCommission: R" + commission;
}
}
```

To create the unit tests, you can create a new package in the same project and add the following unit test classes:

1. CalculateCommissionTest:
```java
import org.junit.Assert;
import org.junit.Test;

public class CalculateCommissionTest {
@Test
public void calculateCommission_CalculatedSuccessfully() {
EstateAgent agent = new EstateAgent("Cape Town", "John Doe", "1000000", "2.5");
agent.calculateCommission();
double expectedCommission = 1000000 * 0.025;
Assert.assertEquals(expectedCommission, agent.getCommission(), 0.001);
}

@Test
public void calculateCommission_CalculatedUnSuccessfully() {
EstateAgent agent = new EstateAgent("Cape Town", "John Doe", "1000000", "abc");
agent.calculateCommission();
double expectedCommission = 0; // Commission calculation failed
Assert.assertEquals(expectedCommission, agent.getCommission(), 0.001);
}
}
```

2. ValidationTest:
```java
import org.junit.Assert;
import org.junit.Test;

public class ValidationTest {
@Test
public void validateData_ValidLocation() {
EstateAgent agent = new EstateAgent("Cape Town", "John Doe", "1000000", "2.5");
boolean isValid = agent.validateData();
Assert.assertTrue(isValid);
}

@Test
public void validateData_InvalidLocation() {
EstateAgent agent = new EstateAgent("", "John Doe", "1000000", "2.5");
boolean isValid = agent.validateData();
Assert.assertFalse(isValid);
}

// Similarly, you can add more tests to validate other fields
}
```

Make sure to configure your IDE to use JUnit for running the unit tests.