static void Main(string[] args) { //initialise the static variable Property.propertiesForSale = 0; //initialise a list of properties for use in the non-static //DisplayAllproperties method List <Property> propertiesList = new List <Property>(); //instantiate house objects and give values to their fields using add method, //incrementing the static variable each time a house is added and adding each //Property to the list //a constructor is now being used, so the field values for each object are //passed as parameters to the constructor House house1 = new House("23 Railway Cuttings, East Cheam", 1, 150000, 10); propertiesList.Add(house1); House house2 = new House("4 Privet Drive, Little Whinging, Surrey", 4, 750000, 200); propertiesList.Add(house2); Flat flat1 = new Flat("Flat 368, Nelson Mandela House, Dockside Estate, Peckham", 3, 250000, "Lewisham Council"); propertiesList.Add(flat1); Flat flat2 = new Flat("Upstairs Flat, 11 Mafeking Parade, Hammersmith", 2, 50000, "Mr Harrison"); propertiesList.Add(flat2); //display the details of the properties Property.DisplayAllProperties(propertiesList); //sell a Property house1.SellHouse(135000); flat1.SellFlat(270000); //display the details of all the properties Property.DisplayAllProperties(propertiesList); //display details of the houses House.DisplayAllHouses(propertiesList); //display the details of the flats Flat.DisplayAllFlats(propertiesList); }
public static void DisplayAllFlats(List <Property> propertiesList) { int totalFlatsSoldValue = 0; int totalFlatsAvailableValue = 0; Console.WriteLine(); Console.WriteLine("Flats on our books"); Console.WriteLine(); foreach (Property property in propertiesList) { //display flat details if (property is Flat) { Console.WriteLine("Address: {0} \n No. bedrooms: {1}\n Price: £{2:N0}.", property.address, property.numberOfBedrooms, property.price); //:N0 formats the number //property must be explicitly cast back to a flat to access its fields from the Flat class Flat flat = property as Flat; Console.WriteLine(" Name of freeholder: {0}", flat.freeholder); if (property.sold) { Console.WriteLine("This flat has been sold."); totalFlatsSoldValue += property.price; } else { Console.WriteLine("This flat is available to buy."); totalFlatsAvailableValue += property.price; } Console.WriteLine(); Console.WriteLine(); } } //display summary data Console.WriteLine("The total number of flats on our books is: " + numberOfFlatsForSale); Console.WriteLine(); Console.WriteLine("The total value of flats sold is: £{0:N0}.", totalFlatsSoldValue); Console.WriteLine("The total value of flats available is: £{0:N0}.", totalFlatsAvailableValue); Console.WriteLine(); }