Write a program named SalespersonDemo that instantiates objects using classes na
ID: 665782 • Letter: W
Question
Write a program named SalespersonDemo that instantiates objects using classes named RealEstateSalesperson and GirlScout. Demonstrate that each object can use a SalesSpeech() method appropriately. Also, use a MakeSale() method two or three times with each object and display the final contents of each object’s data fields. First, create an abstract class named Salesperson. Fields include first and last names; the Salesperson constructor requires both these values. Include properties for the fields. Include a method that returns a string that holds the Salesperson’s full name—the first and last names separated by a space. Then perform the following tasks:
• Create two child classes of Salesperson: RealEstateSalesperson and GirlScout. The RealEstateSalesperson class contains fields for total value sold in dollars and total commission earned (both of which are initialized to 0), and a commission rate field required by the class constructor. The GirlScout class includes a field to hold the number of boxes of cookies sold, which is initialized to 0. Include properties for every field.
• Create an interface named ISellable that contains two methods: SalesSpeech() and MakeSale(). In each RealEstateSalesperson and GirlScout class, implement SalesSpeech() to display an appropriate one- or two-sentence sales speech that the objects of the class could use. In the RealEstateSalesperson class, implement the MakeSale() method to accept an integer dollar value for a house, add the value to the RealEstateSalesperson’s total value sold, and compute the total commission earned. In the GirlScout class, implement the MakeSale() method to accept an integer representing the number of boxes of cookies sold and add it to the total field. (Farrell 499-500)
Explanation / Answer
First solution :
using System;
abstract class Salesperson
{
protected string firstName;
protected string lastName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string GetFullName()
{
return firstName + " " + lastName;
}
protected Salesperson(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
interface ISellable
{
void SalesSpeech();
void MakeSale(int value);
}
class RealEstateSalesperson : Salesperson, ISellable
{
public static readonly double CommissionRate;
static RealEstateSalesperson()
{
CommissionRate = 2.0; // say
}
int totalValueSold = 0;
double totalCommissionEarned = 0.0;
public int TotalValueSold
{
get { return totalValueSold; }
set { totalValueSold = value; }
}
public double TotalCommissionEarned
{
get { return totalCommissionEarned; }
set { totalCommissionEarned = value; }
}
public RealEstateSalesperson(string firstName, string lastName)
: base(firstName, lastName)
{
}
public void SalesSpeech()
{
Console.WriteLine("I'm {0}, I sell houses", GetFullName());
}
public void MakeSale(int houseValue)
{
totalValueSold += houseValue;
totalCommissionEarned += houseValue * CommissionRate / 100.0;
}
}
class GirlScout : Salesperson, ISellable
{
int boxesOfCookiesSold = 0;
public int BoxesOfCookiesSold
{
get { return boxesOfCookiesSold; }
set { boxesOfCookiesSold = value; }
}
public GirlScout(string firstName, string lastName)
: base(firstName, lastName)
{
}
public void SalesSpeech()
{
Console.WriteLine("I'm {0}, I sell boxes of cookies", GetFullName());
}
public void MakeSale(int boxesSold)
{
boxesOfCookiesSold += boxesSold;
}
}
class SalespersonDemo
{
static void Main()
{
RealEstateSalesperson resp = new RealEstateSalesperson("John", "Doe");
resp.SalesSpeech();
resp.MakeSale(200000);
resp.MakeSale(250000);
resp.MakeSale(300000);
Console.WriteLine("Total value of houses sold = ${0:N0}", resp.TotalValueSold);
Console.WriteLine("Total commission earned @ {0}% = ${1:N2}", RealEstateSalesperson.CommissionRate, resp.TotalCommissionEarned);
Console.WriteLine();
GirlScout gs = new GirlScout("Jane", "Doe");
gs.SalesSpeech();
gs.MakeSale(10);
gs.MakeSale(15);
gs.MakeSale(20);
Console.WriteLine("Total number of boxes of cookies sold = {0:N0}", gs.BoxesOfCookiesSold);
Console.ReadKey();
}
}
Another method :
namespace SalesPersonDemo
{
public interface ISell
{
string SalesSpeach();
double MakeSale();
}
abstract class Salesperson
{
private string firstName;
private string lastName;
public Salesperson(string first, string last)
{
FirstName = first;
LastName = last;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public abstract string FullName();
}
class RealEstateSalesperson : Salesperson
{
private double houseVal;
private double totalValDollars = 0;
private double totalCommission = 0;
public RealEstateSalesperson(double commissionRate)
{
CommissionRate = commissionRate;
}
public int TotalVal
{
get { return totalValDollars; }
set { totalValDollars = value; }
}
public int TotalCommission
{
get { return totalCommission; }
set { totalCommission = value; }
}
public int CommissionRate
{
get { return commissionRate; }
set { commissionRate = value; }
}
public override string SalesSpeach()
{
return "We must remind ourselves that value, not just price has to be a driving force behind our success. ";
}
public override double MakeSale()
{
double total;
total = CommissionRate * TotalVal +TotalVal;
return total;
}
public override double FullName()
{
return FirstName + " " + LastName;
}
}
class GirlScout : Salesperson
{
private int numOfboxes = 0;
public int NumOfBoxes
{
get { return numOfboxes; }
set { numOfboxes = value; }
}
public override string SalesSpeach()
{
return "Selling Girl Scout Cookies is an important component of the Girl Scout Leadership Experience for girls.";
}
public override double FullName()
{
return FirstName + " " + LastName;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.