In: Computer Science
You work for a software company has just created a new incentive for salespeople to earn a commission on each sale. The current program only allows for a fixed salary. The Chief Information Officer (CIO) has asked you to create a new program that allows the functionality for a fixed salary and commission.
Write a Java® application, using NetBeans IDE, that calculates the total annual compensation of a salesperson.
Consider the following:
The Java® application should meet the following technical requirements:
Compile your Java® application files into a ZIP folder.
SalesCompensation.java
public class SalesCompensation {
// instance variables
private static double salary;
private double annualSales;
private static double commissionRate;
// overloaded constructor
public SalesCompensation(double annualSales)
{
commissionRate = 0.07;
this.salary = 30000;
this.annualSales = annualSales;
}
// getter method for the salary variable
public double getSalary() {
return salary;
}
// getter method for the annualSales variable
public double getAnnualSales() {
return annualSales;
}
// a method calculating and returning the commission earned
public double commissionEarned()
{
return(commissionRate * this.annualSales);
}
// a method calculating and returning the total annual
compensation
public double totalAnnualCompensation()
{
return(this.salary + commissionEarned());
}
// a toString method to return the result in a proper format
@Override
public String toString()
{
return("Total Annual Sales: $" + String.format("%.2f",
this.annualSales) + "\n"
+ "Salary: $" + String.format("%.2f", this.salary) + "\n"
+ "Commission Earned: $" + String.format("%.2f",
commissionEarned()) + "\n"
+ "Total Annual Compensation: $" + String.format("%.2f",
totalAnnualCompensation()));
}
}
SalesCompensationTester.java (Main/Driver class)
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class SalesCompensationTester {
// a static String variable for the filename
private static final String FILENAME = "sales_output.txt";
public static void main(String[] args)
{
// a Scanner object for user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter the annual sales: $");
double annualSales =
Double.parseDouble(sc.nextLine().trim());
// creating an instance of the SalesCompensation class
// by passing the annualSales as a parameter to the object
constructor
SalesCompensation salesCompensation = new
SalesCompensation(annualSales);
// displaying the result to the console
System.out.println("\nTOTAL EARNINGS:\n---------------\n" +
salesCompensation.toString() + "\n");
// writing the above result to the .txt file
writeToFile(salesCompensation.toString());
}
// method to write the result to a .txt file
private static void writeToFile(String data)
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(FILENAME));
pw = new PrintWriter(fw);
// writing the data to the file
pw.write(data);
System.out.println("Data has successfully been written to file: " +
FILENAME + ".\n");
// releasing all resources after the task has been performed
pw.flush();
fw.close();
pw.close();
} catch (IOException ex) {
System.out.println("ERROR: " + ex.getMessage());
System.exit(1);
}
}
}
**************************************************************** SCREENSHOT ***********************************************************
CONSOLE OUTPUT :
OUTPUT FILE (sales_output.txt, file name may vary)