I had to do a test which I passed mainly to the MCQ part but my coding part is not working and since our instructor is not too keen on giving the feedback I would love to learn about my mistakes and improve from that. The explaining comments are in the code.
In the class Program I get two errors (technically three but one is the same as the other). In the line with "Console.WriteLine("Commission is: ", item.CalculateComission);" I get: "CS1061 'double' does not contain a definition for 'CalculateComission' and no extension method 'CalculateComission' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference?)"
In the line with "GetCheapestResidance(apartments);" I get: "CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<Test2.Apartment>' to 'System.Collections.Generic.List<Test2.Residence>'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2
{
abstract class Residence
{
//properties Bedrooms, Bathrooms, Price
public int Bedrooms { get; set; }
public int Bathrooms { get; set; }
public double Price { get; set; }
//A no-argument constructor that creates a default Residence
public Residence()
{
}
//A constructor that creates a Residence with the specified bedrooms, bathrooms, price
public Residence(int bedrooms, int bathrooms, double price)
{
Bedrooms = bedrooms;
Bathrooms = bathrooms;
Price = price;
}
//CalculateCommission() (public): abstract method with return type: double
public abstract double CalculateCommission();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Test2
{
class Apartment : Residence
{
//Properties: Unit and Floor. Both are integer type
public int Unit { get; set; }
public int Floor { get; set; }
//A no-argument constructor
public Apartment() { }
//A constructor that creates a apartment object with the specified bedrooms, bathrooms, price, Unit and Floor
public Apartment(int unit, int floor, int bedrooms, int bathrooms, double price) : base(bedrooms, bathrooms, price)
{
Unit = unit;
Floor = floor;
}
//Override CalculateCommission() method. The commission rate for an apartment is set up at 3%
public override double CalculateCommission()
{
return Price * 0.03;
}
//toString() that returns the information about the current object
public override string ToString()
{
return $" Bedrooms: {Bedrooms}\n" +
$"Bathrooms: {Bathrooms}\n" +
$"Floor: {Floor}\n" +
$"Unit: {Unit}\n" +
$"Price: {Price}";
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Test2
{
class House : Residence
{
//Propeties: Stories and Basement.Stories is integer type, Basement is Boolean type,
//indicating whether the house has basement
public int Stories { get; set; }
public bool Basement { get; set; }
//A no-arg constructor
public House() { }
//A constructor that creates a house object with the specified bedrooms,
//bathrooms, price, stories, basement
public House(int stories, bool basement, int bedrooms, int bathrooms, double price) : base(bedrooms, bathrooms, price)
{
Stories = stories;
Basement = basement;
}
//toString() that returns the information about the apartment
public override string ToString()
{
return $" Bedrooms: {Bedrooms}\n" +
$"Bathrooms: {Bathrooms}\n" +
$"Stories: {Stories}\n" +
$"Basement: {Basement}\n" +
$"Price: {Price}";
}
//Override CalculateCommission() method. The commission rate for a house is set up at 3.5%
public override double CalculateCommission()
{
return Price * 0.035;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace Test2
{
class Program
{
public static void GetCheapestResidance(List<Residence> list)
{
//finds the cheapest residence in the list (either in the houses list or apartments list )
//depending on the parameter that you will pass.
var item = list.Min(x => x.Price);
//then display the information about the cheapest residence using ToString()
Console.WriteLine("The cheapest is: ", item.ToString());
//and display the commission
Console.WriteLine("Commission is: ", item.CalculateComission);
}
static void Main(string[] args)
{
List<House> houses = new List<House>();
List<Apartment> apartments = new List<Apartment>();
//Create three house objects and three apartment object with data of your choice.
//Then add them to the the above lists.
Residence ap1 = new Apartment(101, 1, 1, 1, 1100);
Residence ap2 = new Apartment(102, 2, 2, 1, 1500);
Residence ap3 = new Apartment(103, 3, 3, 2, 1900);
Residence hs1 = new House(1, false, 2, 1, 1500);
Residence hs2 = new House(1, true, 3, 1, 1900);
Residence hs3 = new House(2, true, 4, 1, 2300);
apartments.Add((Apartment)ap1);
apartments.Add((Apartment)ap2);
apartments.Add((Apartment)ap3);
houses.Add((House)hs1);
houses.Add((House)hs2);
houses.Add((House)hs3);
//Invoke GetCheapestResidance method by passing either houses list OR department list
GetCheapestResidance(apartments);
GetCheapestResidance(houses);
Console.ReadKey();
}
}
}