In C#, Write an application to determine the bonus of a sales person. The sales person receives a 5% bonus on sales $1,000,000 or over and 4% of sales under $1,000,000. They also receive an additional bonus for their pay code. Pay code 1 additional $2500, pay code 2 additional $2000, pay code 3 additional $1500, and pay code 4 additional $1000. The user will be prompted to enter the salesperson’s name, sales, and code.
Create a class to calculate the bonus using an if statement to determine the bonus; a Switch statement to determine the additional bonus; and the override ToString() method with string.format to return the results. Create a Boolean data type for bad data. In class If sales are <= to 0 or a code other than 1,2,3,4 is entered, return “Bad Data”. Create Properties to receive data from class. Use curly braces for all if statements and follow C# Coding Standards. First if statement, check for sales >0. Do not need to display heading or instructions.
// C# program to create an application to determine the bonus of a sales person.
using System;
// class to hold salesperson data
class Salesperson
{
// properties
private string name;
private double sales;
private int code;
private bool badData;
// constructor to create initailize the values for the
attributes
public Salesperson(string name, double sales, int code)
{
badData = false;
this.name = name;
this.sales = sales;
if(sales <= 0) // invalid sales
badData = true;
this.code = code;
if(code < 1 || code > 4) // invalid code
badData = true;
}
// return the name
public string getName()
{
return name;
}
// return the sales
public double getSales()
{
return sales;
}
// return the code
public int getCode()
{
return code;
}
// method to calculate and return the bonus for the
salesperson
public double calculateBonus()
{
double bonus = 0;
if(badData) // if invalid data, return 0
return 0;
if(sales >= 1000000) // if sales
>= 1,000,000 bonus on sales is 5%
bonus = sales*0.05;
else // else bonus on sales is 4%
bonus = sales*0.04;
// additional bonus on
code
switch(code)
{
case 1:
bonus += 2500;
break;
case 2:
bonus += 2000;
break;
case 3:
bonus += 1500;
break;
case 4:
bonus += 1000;
break;
}
return bonus;
}
// return string representation of the object
public override string ToString()
{
if(badData) // invalid data
return "Bad Data";
else
return "Name: "+name+"\nSales: "+sales.ToString("0.00")+"\nCode:
"+code+"\nBonus: "+calculateBonus().ToString("0.00");
}
} //end Salesperson class
class Salesperson_Bonus {
static void Main() {
string name;
double sales;
int code;
// input the salesperson name, sales and code
Console.Write("Enter salesperson name: ");
name = Console.ReadLine();
Console.Write("Enter salesperson sales: ");
sales = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter salesperson code: ");
code = Convert.ToInt32(Console.ReadLine());
// create an object of Salesperson class
Salesperson person = new Salesperson(name, sales, code);
// display the details of the salesperson
Console.WriteLine(person.ToString());
}
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.