Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Interface
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- //Lodgings lodgings = new Lodgings(60, 2, 1000000);
- //Console.WriteLine(lodgings.TotalValue());
- //Console.WriteLine(lodgings.IsBooked);
- //lodgings.Book(4);
- //Console.WriteLine(lodgings.IsBooked);
- //Console.WriteLine(lodgings.Book(2));
- //Console.WriteLine(lodgings.GetCost(1));
- //Console.WriteLine(lodgings.MoveIn(6));
- //Console.WriteLine(lodgings.InhabitantsCount);
- //Console.WriteLine(lodgings.GetCost(1));
- //Console.WriteLine(lodgings);
- //Console.WriteLine(lodgings.MoveIn(25));
- //Console.WriteLine(lodgings.MoveIn(11));
- //Console.WriteLine(lodgings.MoveIn(10));
- }
- }
- }
- --------------------------------------------------------
- Osztályok
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal interface IRealEstate
- {
- int TotalValue();
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal interface IRent
- {
- bool IsBooked { get; }
- int GetCost(int months);
- bool Book(int months);
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- abstract class Flat : IRealEstate
- {
- protected double area;
- protected int roomsCount;
- protected int inhabitantsCount;
- protected int unitPrice;
- public int InhabitantsCount => inhabitantsCount;
- protected Flat(double area, int roomsCount, int inhabitantsCount, int unitPrice)
- {
- this.area = area;
- this.roomsCount = roomsCount;
- this.inhabitantsCount = inhabitantsCount;
- this.unitPrice = unitPrice;
- }
- public abstract bool MoveIn(int newInhabitants);
- public int TotalValue() => (int)(area * unitPrice);
- public override string ToString()
- {
- return $"area: {area}, rooms: {roomsCount}, inhabitants: {inhabitantsCount}, unit price: {unitPrice}";
- }
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal class Lodgings : Flat, IRent
- {
- int bookedMonths;
- public bool IsBooked => bookedMonths > 0;
- public Lodgings(double area, int roomsCount, int unitPrice)
- : base(area, roomsCount, 0, unitPrice)
- {
- bookedMonths = 0;
- }
- public bool Book(int months)
- {
- if (IsBooked)
- {
- return false;
- }
- bookedMonths = months;
- return true;
- }
- public int GetCost(int months)
- {
- return (inhabitantsCount != 0 ? (int)((double)TotalValue() / 240 / inhabitantsCount) : 0) * months;
- }
- public override bool MoveIn(int newInhabitants)
- {
- if (!IsBooked) return false;
- int updatedInhabitantsCount = inhabitantsCount + newInhabitants;
- if (updatedInhabitantsCount > roomsCount * 8 || area < updatedInhabitantsCount * 2)
- {
- return false;
- }
- inhabitantsCount = updatedInhabitantsCount;
- return true;
- }
- public override string ToString()
- {
- return $"{base.ToString()}, booked months: {bookedMonths}";
- }
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal class FamilyApartment : Flat
- {
- int childrenCount;
- public FamilyApartment(double area, int roomsCount, int unitPrice)
- : base(area, roomsCount, 0, unitPrice)
- {
- childrenCount = 0;
- }
- public override bool MoveIn(int newInhabitants)
- {
- int adultsCount = inhabitantsCount - childrenCount;
- int updatedAdultsCount = adultsCount + newInhabitants;
- if (roomsCount * 2 < (updatedAdultsCount + 2 * childrenCount) || (updatedAdultsCount + 2 * childrenCount) * 10 < area)
- {
- return false;
- }
- inhabitantsCount += newInhabitants;
- return true;
- }
- public bool CildIsBorn()
- {
- if (inhabitantsCount - childrenCount < 2)
- {
- return false;
- }
- childrenCount++;
- inhabitantsCount++;
- return true;
- }
- public override string ToString()
- {
- return $"{base.ToString()}, children: {childrenCount}";
- }
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal class Garage : IRealEstate, IRent
- {
- double area;
- int unitPrice;
- bool isHeated;
- int months;
- bool isOccupied;
- public bool IsBooked => months > 0 || isOccupied;
- public Garage(double area, int unitPrice, bool isHeated)
- {
- this.area = area;
- this.unitPrice = unitPrice;
- this.isHeated = isHeated;
- }
- public bool Book(int months)
- {
- if (IsBooked)
- {
- return false;
- }
- this.months = months;
- return true;
- }
- public int GetCost(int months) => (int)((double)TotalValue() / 120 * (isHeated ? 1.5 : 1.0)) * months;
- public int TotalValue() => (int)(area * unitPrice);
- public void updatedOccupied() => isOccupied = !isOccupied;
- public override string ToString()
- {
- return $"area: {area}, unit price: {unitPrice}, is headted: {isHeated}, months: {months}, is occupied {isOccupied}";
- }
- }
- }
- --------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Interface
- {
- internal class ApartmentHouse
- {
- int maxFlats;
- int maxGarage;
- int flatsCount;
- int garagesCount;
- public IRealEstate[] RealEstates { get; private set; }
- public int InhabitantsCount
- {
- get
- {
- int total = 0;
- foreach (IRealEstate realEstate in RealEstates)
- {
- total += (realEstate as Flat)?.InhabitantsCount ?? 0;
- //if (realEstate is Flat)
- //{
- // count += (realEstate as Flat).InhabitantsCount;
- //}
- }
- return total;
- }
- }
- public ApartmentHouse(int maxFlats, int maxGarage)
- {
- this.maxFlats = maxFlats;
- this.maxGarage = maxGarage;
- RealEstates = new IRealEstate[maxFlats + maxGarage];
- flatsCount = 0;
- garagesCount = 0;
- }
- public bool AddFlat(Flat newFlat)
- {
- if (flatsCount == maxFlats)
- {
- return false;
- }
- RealEstates[flatsCount + garagesCount] = newFlat;
- flatsCount++;
- return true;
- }
- public bool AddGarage(Garage newGarage)
- {
- if (garagesCount == maxGarage)
- {
- return false;
- }
- RealEstates[flatsCount + garagesCount] = newGarage;
- garagesCount++;
- return true;
- }
- public int TotalValue()
- {
- int total = 0;
- foreach (IRealEstate realEstate in RealEstates)
- {
- if ((realEstate is Flat flat && flat.InhabitantsCount > 0) || (realEstate is Garage garage && garage.IsBooked))
- {
- total += realEstate.TotalValue();
- }
- }
- return total;
- }
- static public ApartmentHouse LoadFormFile(string fileName)
- {
- ApartmentHouse apartmentHouse = new ApartmentHouse(10, 10);
- using (StreamReader sr = new StreamReader(fileName))
- {
- while (!sr.EndOfStream)
- {
- string line = sr.ReadLine();
- string[] items = line.Split(' ');
- switch (items[0])
- {
- case "Alberlet": apartmentHouse.AddFlat(new Lodgings(double.Parse(items[1]), int.Parse(items[2]), int.Parse(items[3]))); break;
- case "CsaladiApartman": apartmentHouse.AddFlat(new FamilyApartment(double.Parse(items[1]), int.Parse(items[2]), int.Parse(items[3]))); break;
- case "Garazs": apartmentHouse.AddGarage(new Garage(double.Parse(items[1]), int.Parse(items[2]), items[3] == "futott")); break;
- }
- }
- }
- return apartmentHouse;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement