Advertisement
Levi0227

2. félév 3. óra (otthon)

Mar 4th, 2024
2,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.98 KB | Source Code | 0 0
  1. namespace Interface
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //Lodgings lodgings = new Lodgings(60, 2, 1000000);
  8.  
  9.             //Console.WriteLine(lodgings.TotalValue());
  10.             //Console.WriteLine(lodgings.IsBooked);
  11.             //lodgings.Book(4);
  12.             //Console.WriteLine(lodgings.IsBooked);
  13.             //Console.WriteLine(lodgings.Book(2));
  14.             //Console.WriteLine(lodgings.GetCost(1));
  15.             //Console.WriteLine(lodgings.MoveIn(6));
  16.             //Console.WriteLine(lodgings.InhabitantsCount);
  17.             //Console.WriteLine(lodgings.GetCost(1));
  18.             //Console.WriteLine(lodgings);
  19.             //Console.WriteLine(lodgings.MoveIn(25));
  20.             //Console.WriteLine(lodgings.MoveIn(11));
  21.             //Console.WriteLine(lodgings.MoveIn(10));
  22.         }
  23.     }
  24. }
  25.  
  26. --------------------------------------------------------
  27. Osztályok
  28. --------------------------------------------------------
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Text;
  34. using System.Threading.Tasks;
  35.  
  36. namespace Interface
  37. {
  38.     internal interface IRealEstate
  39.     {
  40.         int TotalValue();
  41.     }
  42. }
  43.  
  44. --------------------------------------------------------
  45.  
  46. using System;
  47. using System.Collections.Generic;
  48. using System.Linq;
  49. using System.Text;
  50. using System.Threading.Tasks;
  51.  
  52. namespace Interface
  53. {
  54.     internal interface IRent
  55.     {
  56.         bool IsBooked { get; }
  57.         int GetCost(int months);
  58.         bool Book(int months);
  59.     }
  60. }
  61.  
  62. --------------------------------------------------------
  63.  
  64. using System;
  65. using System.Collections.Generic;
  66. using System.Linq;
  67. using System.Text;
  68. using System.Threading.Tasks;
  69.  
  70. namespace Interface
  71. {
  72.     abstract class Flat : IRealEstate
  73.     {
  74.         protected double area;
  75.         protected int roomsCount;
  76.         protected int inhabitantsCount;
  77.         protected int unitPrice;
  78.  
  79.         public int InhabitantsCount => inhabitantsCount;
  80.         protected Flat(double area, int roomsCount, int inhabitantsCount, int unitPrice)
  81.         {
  82.             this.area = area;
  83.             this.roomsCount = roomsCount;
  84.             this.inhabitantsCount = inhabitantsCount;
  85.             this.unitPrice = unitPrice;
  86.         }
  87.  
  88.         public abstract bool MoveIn(int newInhabitants);
  89.  
  90.         public int TotalValue() => (int)(area * unitPrice);
  91.  
  92.         public override string ToString()
  93.         {
  94.             return $"area: {area}, rooms: {roomsCount}, inhabitants: {inhabitantsCount}, unit price: {unitPrice}";
  95.         }
  96.     }
  97. }
  98.  
  99. --------------------------------------------------------
  100.  
  101. using System;
  102. using System.Collections.Generic;
  103. using System.Linq;
  104. using System.Text;
  105. using System.Threading.Tasks;
  106.  
  107. namespace Interface
  108. {
  109.     internal class Lodgings : Flat, IRent
  110.     {
  111.         int bookedMonths;
  112.  
  113.         public bool IsBooked => bookedMonths > 0;
  114.  
  115.         public Lodgings(double area, int roomsCount, int unitPrice)
  116.             : base(area, roomsCount, 0, unitPrice)
  117.         {
  118.             bookedMonths = 0;
  119.         }
  120.  
  121.         public bool Book(int months)
  122.         {
  123.             if (IsBooked)
  124.             {
  125.                 return false;                
  126.             }
  127.  
  128.             bookedMonths = months;
  129.             return true;
  130.         }
  131.  
  132.         public int GetCost(int months)
  133.         {
  134.             return (inhabitantsCount != 0 ? (int)((double)TotalValue() / 240 / inhabitantsCount) : 0) * months;
  135.         }
  136.  
  137.         public override bool MoveIn(int newInhabitants)
  138.         {
  139.             if (!IsBooked) return false;
  140.  
  141.             int updatedInhabitantsCount = inhabitantsCount + newInhabitants;
  142.             if (updatedInhabitantsCount > roomsCount * 8 || area < updatedInhabitantsCount * 2)
  143.             {
  144.                 return false;                
  145.             }
  146.  
  147.             inhabitantsCount = updatedInhabitantsCount;
  148.             return true;
  149.         }
  150.  
  151.         public override string ToString()
  152.         {
  153.             return $"{base.ToString()}, booked months: {bookedMonths}";
  154.         }
  155.     }
  156. }
  157.  
  158. --------------------------------------------------------
  159.  
  160. using System;
  161. using System.Collections.Generic;
  162. using System.Globalization;
  163. using System.Linq;
  164. using System.Text;
  165. using System.Threading.Tasks;
  166.  
  167. namespace Interface
  168. {
  169.     internal class FamilyApartment : Flat
  170.     {
  171.         int childrenCount;
  172.         public FamilyApartment(double area, int roomsCount, int unitPrice)
  173.             : base(area, roomsCount, 0, unitPrice)
  174.         {
  175.             childrenCount = 0;
  176.         }
  177.  
  178.         public override bool MoveIn(int newInhabitants)
  179.         {
  180.             int adultsCount = inhabitantsCount - childrenCount;
  181.             int updatedAdultsCount = adultsCount + newInhabitants;
  182.  
  183.             if (roomsCount * 2 < (updatedAdultsCount + 2 * childrenCount) || (updatedAdultsCount + 2 * childrenCount) * 10 < area)
  184.             {
  185.                 return false;
  186.             }
  187.  
  188.             inhabitantsCount += newInhabitants;
  189.             return true;
  190.         }
  191.  
  192.         public bool CildIsBorn()
  193.         {
  194.             if (inhabitantsCount - childrenCount < 2)
  195.             {
  196.                 return false;
  197.             }
  198.             childrenCount++;
  199.             inhabitantsCount++;
  200.             return true;
  201.         }
  202.  
  203.         public override string ToString()
  204.         {
  205.             return $"{base.ToString()}, children: {childrenCount}";
  206.         }
  207.     }
  208. }
  209.  
  210. --------------------------------------------------------
  211.  
  212. using System;
  213. using System.Collections.Generic;
  214. using System.Linq;
  215. using System.Text;
  216. using System.Threading.Tasks;
  217.  
  218. namespace Interface
  219. {
  220.     internal class Garage : IRealEstate, IRent
  221.     {
  222.         double area;
  223.         int unitPrice;
  224.         bool isHeated;
  225.         int months;
  226.         bool isOccupied;
  227.  
  228.         public bool IsBooked => months > 0 || isOccupied;
  229.  
  230.         public Garage(double area, int unitPrice, bool isHeated)
  231.         {
  232.             this.area = area;
  233.             this.unitPrice = unitPrice;
  234.             this.isHeated = isHeated;
  235.         }
  236.  
  237.         public bool Book(int months)
  238.         {
  239.             if (IsBooked)
  240.             {
  241.                 return false;
  242.             }
  243.  
  244.             this.months = months;
  245.             return true;
  246.         }
  247.  
  248.         public int GetCost(int months) => (int)((double)TotalValue() / 120 * (isHeated ? 1.5 : 1.0)) * months;
  249.  
  250.         public int TotalValue() => (int)(area * unitPrice);
  251.  
  252.         public void updatedOccupied() => isOccupied = !isOccupied;
  253.  
  254.         public override string ToString()
  255.         {
  256.             return $"area: {area}, unit price: {unitPrice}, is headted: {isHeated}, months: {months}, is occupied {isOccupied}";
  257.         }
  258.     }
  259. }
  260.  
  261. --------------------------------------------------------
  262.  
  263. using System;
  264. using System.Collections.Generic;
  265. using System.Linq;
  266. using System.Text;
  267. using System.Threading.Tasks;
  268.  
  269. namespace Interface
  270. {
  271.     internal class ApartmentHouse
  272.     {
  273.         int maxFlats;
  274.         int maxGarage;
  275.         int flatsCount;
  276.         int garagesCount;
  277.        
  278.         public IRealEstate[] RealEstates { get; private set; }
  279.         public int InhabitantsCount
  280.         {
  281.             get
  282.             {  
  283.                 int total = 0;
  284.  
  285.                 foreach (IRealEstate realEstate in RealEstates)
  286.                 {
  287.                     total += (realEstate as Flat)?.InhabitantsCount ?? 0;
  288.  
  289.                     //if (realEstate is Flat)
  290.                     //{
  291.                     //    count += (realEstate as Flat).InhabitantsCount;
  292.                     //}
  293.                 }
  294.  
  295.                 return total;
  296.             }
  297.         }
  298.  
  299.         public ApartmentHouse(int maxFlats, int maxGarage)
  300.         {
  301.             this.maxFlats = maxFlats;
  302.             this.maxGarage = maxGarage;
  303.             RealEstates = new IRealEstate[maxFlats + maxGarage];
  304.             flatsCount = 0;
  305.             garagesCount = 0;
  306.         }
  307.  
  308.         public bool AddFlat(Flat newFlat)
  309.         {
  310.             if (flatsCount == maxFlats)
  311.             {
  312.                 return false;
  313.             }
  314.  
  315.             RealEstates[flatsCount + garagesCount] = newFlat;
  316.             flatsCount++;
  317.             return true;
  318.         }
  319.  
  320.         public bool AddGarage(Garage newGarage)
  321.         {
  322.             if (garagesCount == maxGarage)
  323.             {
  324.                 return false;
  325.             }
  326.  
  327.             RealEstates[flatsCount + garagesCount] = newGarage;
  328.             garagesCount++;
  329.             return true;
  330.         }
  331.  
  332.         public int TotalValue()
  333.         {
  334.             int total = 0;
  335.  
  336.             foreach (IRealEstate realEstate in RealEstates)
  337.             {
  338.                 if ((realEstate is Flat flat && flat.InhabitantsCount > 0) || (realEstate is Garage garage && garage.IsBooked))
  339.                 {
  340.                     total += realEstate.TotalValue();
  341.                 }
  342.             }
  343.            
  344.             return total;
  345.         }
  346.  
  347.         static public ApartmentHouse LoadFormFile(string fileName)
  348.         {
  349.             ApartmentHouse apartmentHouse = new ApartmentHouse(10, 10);
  350.  
  351.             using (StreamReader sr = new StreamReader(fileName))
  352.             {
  353.                 while (!sr.EndOfStream)
  354.                 {
  355.                     string line = sr.ReadLine();
  356.                     string[] items = line.Split(' ');
  357.                     switch (items[0])
  358.                     {
  359.                         case "Alberlet": apartmentHouse.AddFlat(new Lodgings(double.Parse(items[1]), int.Parse(items[2]), int.Parse(items[3]))); break;
  360.                         case "CsaladiApartman": apartmentHouse.AddFlat(new FamilyApartment(double.Parse(items[1]), int.Parse(items[2]), int.Parse(items[3]))); break;
  361.                         case "Garazs": apartmentHouse.AddGarage(new Garage(double.Parse(items[1]), int.Parse(items[2]), items[3] == "futott")); break;
  362.                     }
  363.                 }
  364.             }
  365.  
  366.             return apartmentHouse;
  367.         }
  368.     }
  369. }
  370.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement