Develop a Java application and use a two-dimensional array that will store three property sales for
two estate agents for January, February, and March. In your solution, include the total property
sales and the 2% commission earned by each estate agent. You are also required to display the
top-selling estate agent.
Your program must:
1. Contain a two-dimensional array to contain three property sales for January, February,
and March for two different estate agents.
ESTATE AGENT 1: Joe Bloggs
JAN: R800 000
FEB: R 1 500 000
MAR: R 2 000 000
ESTATE AGENT 2: Jane Doe
JAN: R 700 000
FEB: R 1 200 000
MAR: R 1 600 000
2. Calculate and print the total property sales for each estate agent.
3. Calculate and print the total 2% commission earned by each estate agent.
Sample Screenshot:
ESTATE AGENT SALES REPORT
JAN FEB MAR
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Joe Bloggs R 800 000 R 1 500 000 R 2 000 000
Jane Doe R 700 000 R 1 200 000 R 1 600 000
The total property sales for Joe Bloggs = R 4 , 300, 000
The total property sales for Jane Doe = R 3 , 500, 000
Sales commission for Joe Bloggs = R 86 ,000
Sales commission for Jane Doe = R 70 ,000
Top performing estate agent: Joe Bloggs
4. Make use of a class named EstateAgent that contains methods to calculate estate agent
sales, estate agent commission, and the top-performing estate agent. The EstateAgent
class must implement an IEstateAgent interface that contains the following:
public interface IEstateAgent {
double EstateAgentSales(double[] propertySales);
double EstateAgentCommission(double propertySales);
int TopEstateAgent(double[] totalSales);
}
5. 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: CalculateTotalSales_ReturnsTotalSales()
Test Purpose: To supply the property sales to the
estate agent sales method. The test
will determine that the correct total
sales value is returned from the estate
agent sales method
Test Name: CalculateTotalCommission_ReturnsCommission()
Test Purpose: To supply a total property sales
amount to the estate agent
commission method. The test will
determine that the correct estate
agent commission has been
calculated.
Test Name: TopAgent_ReturnsTopPosition()
Test Purpose: To supply total sales for each estate
agent to the top estate agent method.
The test will determine the top selling
total property sales for the estate
agent
1 answer
```java
import java.text.DecimalFormat;
// Interface for EstateAgent class
interface IEstateAgent {
double estateAgentSales(double[] propertySales);
double estateAgentCommission(double propertySales);
int topEstateAgent(double[] totalSales);
}
// EstateAgent class implementing the IEstateAgent interface
class EstateAgent implements IEstateAgent {
DecimalFormat decimalFormat = new DecimalFormat("#,##,###");
// Method to calculate the total property sales for an estate agent
public double estateAgentSales(double[] propertySales) {
double totalSales = 0;
for (double sale : propertySales) {
totalSales += sale;
}
return totalSales;
}
// Method to calculate the 2% commission earned by an estate agent
public double estateAgentCommission(double propertySales) {
return propertySales * 0.02;
}
// Method to determine the top-performing estate agent based on their total sales
public int topEstateAgent(double[] totalSales) {
double maxSales = totalSales[0];
int topAgentIndex = 0;
for (int i = 1; i < totalSales.length; i++) {
if (totalSales[i] > maxSales) {
maxSales = totalSales[i];
topAgentIndex = i;
}
}
return topAgentIndex;
}
}
// Main class to run the application
public class EstateAgentApp {
public static void main(String[] args) {
// Array to store property sales for two estate agents
double[][] propertySales = {
{800000, 1500000, 2000000},
{700000, 1200000, 1600000}
};
// Initialize EstateAgent object
EstateAgent estateAgent = new EstateAgent();
// Array to store total property sales for each estate agent
double[] totalSales = new double[2];
// Calculate and print the total property sales for each estate agent
System.out.println("ESTATE AGENT SALES REPORT");
System.out.println("\t\t\t\t\tJAN\t\t\t\t\t\tFEB\t\t\t\t\t\tMAR");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < propertySales.length; i++) {
double agentTotalSales = estateAgent.estateAgentSales(propertySales[i]);
totalSales[i] = agentTotalSales;
System.out.print("Estate Agent " + (i+1) + ": \t");
for (double sale : propertySales[i]) {
System.out.print("R " + decimalFormat.format(sale) + "\t\t\t");
}
System.out.println();
}
// Calculate and print the total 2% commission earned by each estate agent
double[] commissions = new double[2];
for (int i = 0; i < totalSales.length; i++) {
double agentCommission = estateAgent.estateAgentCommission(totalSales[i]);
commissions[i] = agentCommission;
}
System.out.println();
System.out.println("The total property sales for Estate Agent 1 = R " + decimalFormat.format(totalSales[0]));
System.out.println("The total property sales for Estate Agent 2 = R " + decimalFormat.format(totalSales[1]));
System.out.println();
System.out.println("Sales commission for Estate Agent 1 = R " + decimalFormat.format(commissions[0]));
System.out.println("Sales commission for Estate Agent 2 = R " + decimalFormat.format(commissions[1]));
System.out.println();
// Determine and print the top-performing estate agent
int topAgentIndex = estateAgent.topEstateAgent(totalSales);
System.out.println("Top performing estate agent: Estate Agent " + (topAgentIndex+1));
}
}
```
To create the unit tests, you can use a testing framework like JUnit.
Here is an example of how you can write the unit tests:
```java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EstateAgentTests {
// Test to calculate total sales for estate agent
@Test
void calculateTotalSales_ReturnsTotalSales() {
EstateAgent estateAgent = new EstateAgent();
double[] propertySales = {800000, 1500000, 2000000};
double expectedTotalSales = 4300000;
double actualTotalSales = estateAgent.estateAgentSales(propertySales);
assertEquals(expectedTotalSales, actualTotalSales);
}
// Test to calculate commission for estate agent
@Test
void calculateTotalCommission_ReturnsCommission() {
EstateAgent estateAgent = new EstateAgent();
double propertySales = 4300000;
double expectedCommission = 86000;
double actualCommission = estateAgent.estateAgentCommission(propertySales);
assertEquals(expectedCommission, actualCommission);
}
// Test to determine the top performing estate agent
@Test
void topAgent_ReturnsTopPosition() {
EstateAgent estateAgent = new EstateAgent();
double[] totalSales = {4300000, 3500000};
int expectedTopAgentIndex = 0;
int actualTopAgentIndex = estateAgent.topEstateAgent(totalSales);
assertEquals(expectedTopAgentIndex, actualTopAgentIndex);
}
}
```
Remember to add the necessary dependencies for JUnit in your project's build file (e.g., pom.xml for Maven).