Write a class representing any Vegetable described by a color passed in the constructor. Write a class Carrot derived from Vegetable, which in the constructor assumes the price per kilogram. Write Buy method (which takes the number of kilograms as the parameter) in such a way that it is possible to write the code: Vegetable r = new Carrot (15, "red"); // 15 PLN / kg, color: red double toPay = r.Buy (0.5); // Returns the price of 0.5 kg of the carrot
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
abstract class Vegetable
{
private string color;
public Vegetable() { }
public Vegetable(string color)
{
this.color = color;
}
public string Color
{
get { return color; }
set { color = value; }
}
public abstract double Buy(double numberKilograms);
}
class Carrot: Vegetable
{
private double pricePerKilogram;
public Carrot() { }
public Carrot(double pricePerKilogram, string color)
: base(color)
{
this.pricePerKilogram = pricePerKilogram;
}
public override double Buy(double numberKilograms)
{
return numberKilograms * pricePerKilogram;
}
}
class Program
{
static void Main(string[] args){
Vegetable r = new Carrot(15, "red"); // 15 PLN / kg, color: red
double toPay = r.Buy(0.5); // Returns the price of 0.5 kg of the carrot
Console.Write("The price of 0.5 kg of the carrot: {0}",toPay);
Console.ReadLine();
}
}
}
Comments
Leave a comment