In C#: Write the following classes: 1. A CafeItem class with name and cost as pr
ID: 3906531 • Letter: I
Question
In C#:
Write the following classes:
1. A CafeItem class with name and cost as properties. The cost cannot be negative.
2. A Sandwich class that inherits from CafeItem.
3. A Cookie class that inherits from CafeItem. This class has an additional property: the number of cookies in the order. Override the get of the cost property: it returns the cost of the order (# of cookies * cost of one cookie). If the number of cookies is >= 3, give a 10% discount.
4. A Chocolate class that inherits from CafeItem. This class has an additional property: the number of pounds purchased in the order. Override the get of the cost property: it returns the cost of the order (# of pounds * cost of one pound).
You must use Properties for this homework. Make sure each class has a constructor and a ToString method that use its parent's methods where possible.
In main, create a list of CafeItems and add one instance of each of the children classes then print the cost for each instance as well as the total cost for all the items on the list.
Explanation / Answer
using System;
class CafeItem // base class
{
private string name;
public double cost;
public CafeItem(){}
public CafeItem(string name,double cost)// constructor
{
this.name = name;
if(cost > 0)
this.cost = cost;
else
this.cost = 0.0;
}
//properties
public string Name
{
set{ name = value; }
get{ return name;}
}
public virtual double Cost
{
set{ cost = value; }
get{ return cost;}
}
public override string ToString()
{
return " Name of Caffe: "+name;
}
public string getName()
{
return name;
}
}
class Sandwich : CafeItem // derived class
{
public Sandwich(){}
public Sandwich(string name,double cost): base(name,cost)// sending parameters to base class
{
}
public override string ToString()
{
return " Name of Caffe : "+getName()+" Sandwich ";
}
}
class Cookie : CafeItem
{
private int numCookies;
public Cookie(){}
public Cookie(string name,double cost,int numCookies): base(name,cost)
{
this.numCookies = numCookies;
}
public override double Cost
{
set{cost = value;}
get{if(numCookies < 3) return cost*numCookies; else return cost*numCookies - cost*numCookies*0.1;}
}
public int NumCookies
{
set{ numCookies = value; }
get{ return numCookies;}
}
public override string ToString()
{
return " Name of Caffe : "+getName()+" Number of Cookies Ordered : "+NumCookies;
}
}
class Chocolate : CafeItem
{
private int numPounds;
public Chocolate(){}
public Chocolate(string name,double cost,int numPounds): base(name,cost)
{
this.numPounds = numPounds;
}
public override double Cost
{
set{cost = value;}
get{ return cost*numPounds;}
}
public int NumPounds
{
set{ numPounds = value; }
get{ return numPounds;}
}
public override string ToString()
{
return " Name of Caffe : "+getName()+" Number of Pounds of Choclates ordered : "+NumPounds;
}
}
public class Test
{
public static void Main()
{
CafeItem[] ci = new CafeItem[4]; // array of 4 CaffeItems
ci[0] = new CafeItem("Aroma Mocha",12.6);
ci[1] = new Sandwich("Baristas",9.5);
ci[2] = new Cookie("Club Coffee",2.8,6);
ci[3] = new Chocolate("Expresso",3.6,5);
for(int i=0;i<4;i++)
Console.WriteLine(ci[i]+ " Cost : $"+ci[i].Cost);
}
}
Output:
Name of Caffe: Aroma Mocha Cost : $12.6
Name of Caffe : Baristas Sandwich Cost : $9.5
Name of Caffe : Club Coffee Number of Cookies Ordered : 6 Cost : $15.12
Name of Caffe : Expresso Number of Pounds of Choclates ordered : 5 Cost : $18
Do ask if any doubt. Please upvote.