Advertisement
Levi0227

2. félév 1. ZH gyak

Mar 19th, 2024
432
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.83 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.         }
  15.     }
  16. }
  17.  
  18. ---------------------------------------------------------------------
  19. Osztályok
  20. ---------------------------------------------------------------------
  21.  
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27.  
  28. namespace ConsoleApp1
  29. {
  30.     public interface IDeliverable
  31.     {
  32.         int Weight { get; set; }
  33.         string Address { get; set; }
  34.         double CalculatePrice(bool fromLocker);
  35.  
  36.     }
  37. }
  38.  
  39. ---------------------------------------------------------------------
  40.  
  41. using System;
  42. using System.Collections.Generic;
  43. using System.Linq;
  44. using System.Text;
  45. using System.Threading.Tasks;
  46.  
  47. namespace ConsoleApp1
  48. {
  49.     public class Envelope : IDeliverable
  50.     {
  51.         public int Weight { get; set; }
  52.         public string Address { get; set; }
  53.         private string desciption;
  54.  
  55.         public Envelope(int weight, string address, string desciption)
  56.         {
  57.             this.Weight = weight;
  58.             this.Address = address;
  59.             this.desciption = desciption;
  60.         }
  61.  
  62.         public double CalculatePrice(bool fromLocker)
  63.         {
  64.             if (this.Weight <= 50)
  65.             {
  66.                 return 200;
  67.             }
  68.  
  69.             if (51 <= this.Weight && this.Weight <= 500)
  70.             {
  71.                 return 400;
  72.             }
  73.  
  74.             if (this.Weight >= 501 && this.Weight <= 2000)
  75.             {
  76.                 return 1000;
  77.             }
  78.  
  79.             throw new OverweightException();
  80.         }
  81.  
  82.         public override string ToString()
  83.         {
  84.             return $"Címzett: {this.Address} / Leírás: {this.desciption} / Tömeg:{this.Weight} g";
  85.         }
  86.     }
  87. }
  88.  
  89. ---------------------------------------------------------------------
  90.  
  91. using System;
  92. using System.Collections.Generic;
  93. using System.Linq;
  94. using System.Runtime.Serialization;
  95. using System.Text;
  96. using System.Threading.Tasks;
  97.  
  98. namespace ConsoleApp1
  99. {
  100.     public class OverweightException : Exception
  101.     {
  102.         public OverweightException() { }
  103.  
  104.         public OverweightException(string message) : base(message)
  105.         {
  106.         }
  107.  
  108.         public OverweightException(string message, Exception innerException) : base(message, innerException)
  109.         {
  110.         }
  111.  
  112.         protected OverweightException(SerializationInfo info, StreamingContext context) : base(info, context)
  113.         {
  114.         }
  115.     }
  116. }
  117.  
  118. ---------------------------------------------------------------------
  119.  
  120. using System;
  121. using System.Collections.Generic;
  122. using System.Linq;
  123. using System.Text;
  124. using System.Threading.Tasks;
  125.  
  126. namespace ConsoleApp1
  127. {
  128.     public abstract class Parcel : IDeliverable, IComparable
  129.     {
  130.         public enum OrientationType
  131.         {
  132.             Arbitrary, Horizontal, Vertical
  133.         }
  134.  
  135.  
  136.         protected Parcel(int weight, string address)
  137.         {
  138.             this.orientationType = OrientationType.Arbitrary;
  139.             Weight = weight;
  140.             Address = address;
  141.         }
  142.  
  143.         protected Parcel(OrientationType orientationType, int weight, string address)
  144.         {
  145.             this.orientationType = orientationType;
  146.             Weight = weight;
  147.             Address = address;
  148.         }
  149.  
  150.         protected OrientationType orientationType;
  151.         public int Weight { get; set; }
  152.         public string Address { get; set; }
  153.         public OrientationType Orientation { get { return this.orientationType; } }
  154.  
  155.         public abstract double CalculatePrice(bool fromLocker);
  156.  
  157.         public override string ToString()
  158.         {
  159.             return $"Címzett: {this.Address} / Tömeg:{this.Weight} g";
  160.         }
  161.  
  162.         public int CompareTo(object obj)
  163.         {
  164.             if (obj is IDeliverable)
  165.             {
  166.                 IDeliverable deliverable = obj as IDeliverable;
  167.  
  168.                 if (deliverable.Weight == this.Weight)
  169.                 {
  170.                     return 0;
  171.                 }
  172.  
  173.                 if (deliverable.Weight < this.Weight)
  174.                 {
  175.                     return -1;
  176.                 }
  177.  
  178.                 if (deliverable.Weight > this.Weight)
  179.                 {
  180.                     return 1;
  181.                 }
  182.             }
  183.  
  184.             return 0;
  185.         }
  186.     }
  187. }
  188.  
  189. ---------------------------------------------------------------------
  190.  
  191. using System;
  192. using System.Collections.Generic;
  193. using System.Diagnostics;
  194. using System.Linq;
  195. using System.Text;
  196. using System.Threading.Tasks;
  197.  
  198. namespace ConsoleApp1
  199. {
  200.     public class NormalParcel : Parcel
  201.     {
  202.         private static Random random = new Random();
  203.         private const int BASE_PRICE = 500;
  204.  
  205.         public NormalParcel(int weight, string address) : base(weight, address)
  206.         {
  207.             this.orientationType = (OrientationType)random.Next(0,3);
  208.         }
  209.  
  210.         public override double CalculatePrice(bool fromLocker)
  211.         {
  212.             int price = BASE_PRICE + this.Weight;
  213.  
  214.             return (fromLocker) ? price - 250 : price;
  215.         }
  216.     }
  217. }
  218.  
  219. ---------------------------------------------------------------------
  220.  
  221. using System;
  222. using System.Collections.Generic;
  223. using System.Linq;
  224. using System.Text;
  225. using System.Threading.Tasks;
  226.  
  227. namespace ConsoleApp1
  228. {
  229.     public class FragileParcel : Parcel
  230.     {
  231.         private const int BASE_PRICE = 1000;
  232.         public FragileParcel(OrientationType orientationType, int weight, string address) : base(orientationType, weight, address)
  233.         {
  234.             if (orientationType == OrientationType.Arbitrary)
  235.             {
  236.                 throw new IncorrectOrientationException(this, "Nem megfelelő orientáció!");
  237.             }
  238.         }
  239.  
  240.         public override double CalculatePrice(bool fromLocker)
  241.         {
  242.             if (fromLocker)
  243.             {
  244.                 throw new DeliveryException("Törékeny csomagot nem lehet auómatából feladni!");
  245.             }
  246.  
  247.             return BASE_PRICE + (this.Weight) * 2;
  248.         }
  249.     }
  250. }
  251.  
  252. ---------------------------------------------------------------------
  253.  
  254. using System;
  255. using System.Collections.Generic;
  256. using System.Linq;
  257. using System.Runtime.Serialization;
  258. using System.Text;
  259. using System.Threading.Tasks;
  260.  
  261. namespace ConsoleApp1
  262. {
  263.     public class DeliveryException : Exception
  264.     {
  265.         public DeliveryException()
  266.         {
  267.         }
  268.  
  269.         public DeliveryException(string message) : base(message)
  270.         {
  271.         }
  272.  
  273.         public DeliveryException(string message, Exception innerException) : base(message, innerException)
  274.         {
  275.         }
  276.  
  277.         protected DeliveryException(SerializationInfo info, StreamingContext context) : base(info, context)
  278.         {
  279.         }
  280.     }
  281. }
  282.  
  283. ---------------------------------------------------------------------
  284.  
  285. using System;
  286. using System.Collections.Generic;
  287. using System.Linq;
  288. using System.Runtime.Serialization;
  289. using System.Text;
  290. using System.Threading.Tasks;
  291.  
  292. namespace ConsoleApp1
  293. {
  294.     public class IncorrectOrientationException : DeliveryException
  295.     {
  296.         public Parcel Parcel { get; set; }
  297.  
  298.         public IncorrectOrientationException()
  299.         {
  300.         }
  301.  
  302.         public IncorrectOrientationException(Parcel parcel, string message) : base(message)
  303.         {
  304.         }
  305.  
  306.         public IncorrectOrientationException(string message) : base(message)
  307.         {
  308.         }
  309.  
  310.         public IncorrectOrientationException(string message, Exception innerException) : base(message, innerException)
  311.         {
  312.         }
  313.  
  314.         protected IncorrectOrientationException(SerializationInfo info, StreamingContext context) : base(info, context)
  315.         {
  316.         }
  317.     }
  318. }
  319.  
  320. ---------------------------------------------------------------------
  321.  
  322. using System;
  323. using System.Collections.Generic;
  324. using System.Linq;
  325. using System.Text;
  326. using System.Threading.Tasks;
  327.  
  328. namespace ConsoleApp1
  329. {
  330.     public class Courier
  331.     {
  332.         private int sumOfWeight;
  333.         public IDeliverable[] deliverables;
  334.  
  335.         public int SumOfWeight { get { return this.sumOfWeight; } }
  336.  
  337.         public Courier(int deliverablesSize)
  338.         {
  339.             this.deliverables = new IDeliverable[deliverablesSize];
  340.         }
  341.  
  342.         public void PickUpItem(IDeliverable item)
  343.         {
  344.             int i = 0;
  345.             while (i < this.deliverables.Length && this.deliverables[i] != null)
  346.             {
  347.                 ++i;
  348.             }
  349.  
  350.             if (i < this.deliverables.Length)
  351.             {
  352.                 this.deliverables[i] = item;
  353.                 this.sumOfWeight += item.Weight;
  354.                 return;
  355.             }
  356.  
  357.             throw new DeliveryException();
  358.         }
  359.  
  360.         public IDeliverable[] FragilesSorted()
  361.         {
  362.             IDeliverable[] sortedResult = new IDeliverable[this.deliverables.Length];
  363.  
  364.             for (int i = 0; i < this.deliverables.Length; ++i)
  365.             {
  366.                 if (this.deliverables[i] is FragileParcel)
  367.                 {
  368.                     sortedResult[i] = this.deliverables[i];
  369.                 }
  370.             }
  371.  
  372.             Array.Sort(sortedResult);
  373.            
  374.             return sortedResult;
  375.         }
  376.     }
  377. }
  378.  
  379. ---------------------------------------------------------------------
  380.  
  381. using ConsoleApp1;
  382. using NUnit.Framework;
  383. using System.Data;
  384.  
  385. namespace TestProject1
  386. {
  387.     public class Tests
  388.     {
  389.         [TestCase(true)]
  390.         [TestCase(false)]
  391.         public void TestEnvelopeCalculatePrice(bool fromLocker)
  392.         {
  393.             Envelope envelope = new Envelope(1500, "Parlament", "Ajándék Vikinek!");
  394.  
  395.             Assert.That(envelope.CalculatePrice(fromLocker), Is.EqualTo(1000));
  396.         }
  397.        
  398.         [Test]
  399.         public void TestEnvelopeOverweight()
  400.         {
  401.             Envelope envelope = new Envelope(2300, "OE NIK", "");
  402.  
  403.             Assert.That(() => envelope.CalculatePrice(true), Throws.TypeOf<OverweightException>());
  404.         }
  405.  
  406.         [Test]
  407.         public void TestFragileParcelFromLocker()
  408.         {
  409.             FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Budapest");
  410.  
  411.             Assert.That(() => fragileParcel.CalculatePrice(true), Throws.TypeOf<DeliveryException>());
  412.         }
  413.        
  414.         [Test]
  415.         public void TestFragileParcelInvalidOrientation()
  416.         {
  417.             Assert.That(() => new FragileParcel(Parcel.OrientationType.Arbitrary, 3000, "Budapest"), Throws.TypeOf(typeof(IncorrectOrientationException)));
  418.         }
  419.  
  420.         [Test]
  421.         public void TestCourierPickUpItem()
  422.         {
  423.             FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Óbuda");
  424.  
  425.             Courier courier = new Courier(1);
  426.             courier.PickUpItem(fragileParcel);
  427.  
  428.             Assert.That(courier.SumOfWeight, Is.EqualTo(3000));
  429.         }
  430.        
  431.         [Test]
  432.         public void TestCourierFragilesSorted()
  433.         {
  434.             FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 4000, "Óbuda");
  435.  
  436.             NormalParcel normalParcel = new NormalParcel(1200, "Otthon");
  437.  
  438.             FragileParcel fragileParcel2 = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Óbuda");
  439.  
  440.             Courier courier = new Courier(5);
  441.             courier.PickUpItem(fragileParcel);
  442.             courier.PickUpItem(normalParcel);
  443.             courier.PickUpItem(fragileParcel2);
  444.  
  445.             IDeliverable[] expected = new IDeliverable[] { null, null, null, fragileParcel, fragileParcel2 };
  446.  
  447.             Assert.That(courier.FragilesSorted(), Is.EquivalentTo(expected));
  448.         }
  449.     }
  450. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement