I am attempting to have the the following program validate the data inputted for
ID: 3865413 • Letter: I
Question
I am attempting to have the the following program validate the data inputted for base pay and annual salary. I want to ensure a dollar amount is entered and the value is not negative.
The requirement is:
If any of the inputs are unreasonable, tell user with a message and force reentry of the data (e.g. Negative Sales, Annual Salary > $120,000 or < $12,000, if not 0).
I have entered this line of code to meet the requirement but it is not working:
while(double.TryParse( inValue, out basePay) == false)
{
WriteLine ("Invalid input");
Write("Please re-enter a dollar amount.");
inValue = ReadLine();
}
The following is the class and main:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Employee
{
class EmployeePay
{ //Data members, data fields, or characteristics
private string employeeFirstName;
private string employeeLastName;
private double basePay;
private double sales;
private double netSales;
//constructor with five data members
public EmployeePay(string first, string last, double salary, double commissionBase, double net)
{
employeeFirstName = first;
employeeLastName = last;
basePay = salary;
sales = commissionBase;
netSales = net;
}
//constructor with three data memebers
public EmployeePay(string first, string last, double salary)
{
employeeFirstName = first;
employeeLastName = last;
basePay = salary;
}
//constructor with two data memebers
public EmployeePay(string first, string last)
{
employeeFirstName = first;
employeeLastName = last;
}
//default constructor
public EmployeePay()
{
}
//property to change employee first name
public string EmployeeFirstName
{
get
{
return employeeFirstName;
}
set
{
employeeFirstName = value;
}
}
public string EmployeeLastName
{
get
{
return employeeLastName;
}
set
{ employeeLastName = value; }
}
public double BasePay
{
get
{
return basePay;
}
set
{
basePay = value;
}
}
public double Sales
{
get { return sales; }
set { sales = value; }
}
// Calculate monthly pay
public double CalculateBasePay()
{
basePay = basePay / 12;
return basePay;
}
//Calculate netSales
public double CalculateNetSales()
{
netSales = sales - (10 * basePay);
return netSales;
}
//Calculate Gross Pay
public double CalculateGrossPay()
{
return CalculateBasePay() + CalculateCommission();
}
//Calculate Commission
public double CalculateCommission()
{
const double TIER_ONE = .05; // commission rate for or less $10,000
const double TIER_TWO = .1; // commission rate for $15,000 -$24,999
const double TIER_THREE = .15; // commission rate for $25,000 -$49,999
const double TIER_FOUR = .20; // commission rate for sale over $50,0000
double commission; //commission
double netSales = CalculateNetSales();
if (netSales <= 0)
return (commission = 0);
else if ((netSales >= 10000) && (netSales < 10001))
return (commission = (netSales* TIER_ONE)); //commission rate for between $1 and $10,000
else if ((netSales > 10001) && (netSales<=25000))
return (commission = (netSales-10000) * TIER_TWO + 500); //commission rate for between $10,000 and $25,000
else if ((netSales > 25000) && (netSales < 50000))
return (commission = (netSales - 25000) * TIER_THREE + 2000); //commission rate for between $25,000 and $49,999
else
return (commission = (netSales -50000) * TIER_FOUR + 5750 );//commission rate for over $50,000
}
public override string ToString()
{
return employeeFirstName + " " + employeeLastName + "Base Salary:" + CalculateBasePay().ToString("C") + "Sales:" + Sales + "Commission :" + CalculateCommission().ToString("C") + "Gross Pay:" + CalculateGrossPay().ToString("C");
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Employee
{
class Program
{
//Here is List of type Employee pay
static List<EmployeePay> lstEmp = new List<EmployeePay>();
static void Main(string[] args)
{
string choice = "";
// Iterate till user inputs choice 3
do
{
WriteLine("1. Enter Employee ");
WriteLine("2. Display All Employee ");
WriteLine("3. Exit");
choice = ReadLine();
switch (choice)
{
case "1":
input();
break;
case "2":
Output();
break;
case "3":
return;
}
}
while (choice != "3");
}
static void input()
{
//Create an object of EmployeePay
EmployeePay firstEmployee = new EmployeePay();
firstEmployee.EmployeeFirstName = AskForName("Enter First Name");
firstEmployee.EmployeeLastName = AskForName("Enter Last Name");
firstEmployee.BasePay = AskForBasePay();
firstEmployee.Sales = AskForMonthlySales();
WriteLine();
WriteLine("Data saved successfully...Please enter 1 Enter Employee 2 Display All Employees 3 Exit ");
ReadLine();
// Add This object to List
lstEmp.Add(firstEmployee);
Clear();
}
static void Output()
{
// Display Output
WriteLine("OUTPUT: ");
foreach (EmployeePay ep in lstEmp)
{
WriteLine(ep.ToString());
}
}
static string AskForName(string str)
{
string name;
WriteLine(str);
name = ReadLine();
return name;
}
static double AskForBasePay()
{
string inValue;
double basePay;
Write("Enter your annual base pay:");
inValue = ReadLine();
while(double.TryParse( inValue, out basePay) == false)
{
WriteLine ("Invalid input");
Write("Please re-enter a dollar amount.");
inValue = ReadLine();
}
basePay = double.Parse(inValue);
return basePay;
}
static double AskForMonthlySales()
{
string inValue;
double monthlySales;
WriteLine("Enter the monthly sales: ");
inValue = ReadLine();
while (double.TryParse(inValue, out monthlySales) == false )
{
WriteLine("Invalid input");
Write("Please re-enter a dollar amount.");
inValue = ReadLine();
}
monthlySales = double.Parse(inValue);
return monthlySales;
}
}
}
Explanation / Answer
Added input validations to these functions ---
--------AskForBasePay()
--------AskForMonthlySales()
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Employee
{
class EmployeePay
{ //Data members, data fields, or characteristics
private string employeeFirstName;
private string employeeLastName;
private double basePay;
private double sales;
private double netSales;
//constructor with five data members
public EmployeePay(string first, string last, double salary, double commissionBase, double net)
{
employeeFirstName = first;
employeeLastName = last;
basePay = salary;
sales = commissionBase;
netSales = net;
}
//constructor with three data memebers
public EmployeePay(string first, string last, double salary)
{
employeeFirstName = first;
employeeLastName = last;
basePay = salary;
}
//constructor with two data memebers
public EmployeePay(string first, string last)
{
employeeFirstName = first;
employeeLastName = last;
}
//default constructor
public EmployeePay()
{
}
//property to change employee first name
public string EmployeeFirstName
{
get
{
return employeeFirstName;
}
set
{
employeeFirstName = value;
}
}
public string EmployeeLastName
{
get
{
return employeeLastName;
}
set
{ employeeLastName = value; }
}
public double BasePay
{
get
{
return basePay;
}
set
{
basePay = value;
}
}
public double Sales
{
get { return sales; }
set { sales = value; }
}
// Calculate monthly pay
public double CalculateBasePay()
{
basePay = basePay / 12;
return basePay;
}
//Calculate netSales
public double CalculateNetSales()
{
netSales = sales - (10 * basePay);
return netSales;
}
//Calculate Gross Pay
public double CalculateGrossPay()
{
return CalculateBasePay() + CalculateCommission();
}
//Calculate Commission
public double CalculateCommission()
{
const double TIER_ONE = .05; // commission rate for or less $10,000
const double TIER_TWO = .1; // commission rate for $15,000 -$24,999
const double TIER_THREE = .15; // commission rate for $25,000 -$49,999
const double TIER_FOUR = .20; // commission rate for sale over $50,0000
double commission; //commission
double netSales = CalculateNetSales();
if (netSales <= 0)
return (commission = 0);
else if ((netSales >= 10000) && (netSales < 10001))
return (commission = (netSales* TIER_ONE)); //commission rate for between $1 and $10,000
else if ((netSales > 10001) && (netSales<=25000))
return (commission = (netSales-10000) * TIER_TWO + 500); //commission rate for between $10,000 and $25,000
else if ((netSales > 25000) && (netSales < 50000))
return (commission = (netSales - 25000) * TIER_THREE + 2000); //commission rate for between $25,000 and $49,999
else
return (commission = (netSales -50000) * TIER_FOUR + 5750 );//commission rate for over $50,000
}
public override string ToString()
{
return employeeFirstName + " " + employeeLastName + "Base Salary:" + CalculateBasePay().ToString("C") + "Sales:" + Sales + "Commission :" + CalculateCommission().ToString("C") + "Gross Pay:" + CalculateGrossPay().ToString("C");
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Employee
{
class Program
{
//Here is List of type Employee pay
static List<EmployeePay> lstEmp = new List<EmployeePay>();
static void Main(string[] args)
{
string choice = "";
// Iterate till user inputs choice 3
do
{
WriteLine("1. Enter Employee ");
WriteLine("2. Display All Employee ");
WriteLine("3. Exit");
choice = ReadLine();
switch (choice)
{
case "1":
input();
break;
case "2":
Output();
break;
case "3":
return;
}
}
while (choice != "3");
}
static void input()
{
//Create an object of EmployeePay
EmployeePay firstEmployee = new EmployeePay();
firstEmployee.EmployeeFirstName = AskForName("Enter First Name");
firstEmployee.EmployeeLastName = AskForName("Enter Last Name");
firstEmployee.BasePay = AskForBasePay();
firstEmployee.Sales = AskForMonthlySales();
WriteLine();
WriteLine("Data saved successfully...Please enter 1 Enter Employee 2 Display All Employees 3 Exit ");
ReadLine();
// Add This object to List
lstEmp.Add(firstEmployee);
Clear();
}
static void Output()
{
// Display Output
WriteLine("OUTPUT: ");
foreach (EmployeePay ep in lstEmp)
{
WriteLine(ep.ToString());
}
}
static string AskForName(string str)
{
string name;
WriteLine(str);
name = ReadLine();
return name;
}
static double AskForBasePay()
{
string inValue;
double basePay=-1;
Write("Enter your annual base pay:");
inValue = ReadLine();
while(double.TryParse( inValue, out basePay) == false || basePay < 0)
{
WriteLine ("Invalid input");
Write("Please re-enter a dollar amount.");
inValue = ReadLine();
}
basePay = double.Parse(inValue);
return basePay;
}
static double AskForMonthlySales()
{
string inValue;
double monthlySales=-1;
WriteLine("Enter the monthly sales: ");
inValue = ReadLine();
while (double.TryParse(inValue, out monthlySales) == false || monthlySales < 12000) // change this number to the minimum limit of the value you want monthlySales to hold
{
WriteLine("Invalid input");
Write("Please re-enter a dollar amount.");
inValue = ReadLine();
}
monthlySales = double.Parse(inValue);
return monthlySales;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.