Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- ---------------------------------------------------------------------
- Osztályok
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public interface IDeliverable
- {
- int Weight { get; set; }
- string Address { get; set; }
- double CalculatePrice(bool fromLocker);
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class Envelope : IDeliverable
- {
- public int Weight { get; set; }
- public string Address { get; set; }
- private string desciption;
- public Envelope(int weight, string address, string desciption)
- {
- this.Weight = weight;
- this.Address = address;
- this.desciption = desciption;
- }
- public double CalculatePrice(bool fromLocker)
- {
- if (this.Weight <= 50)
- {
- return 200;
- }
- if (51 <= this.Weight && this.Weight <= 500)
- {
- return 400;
- }
- if (this.Weight >= 501 && this.Weight <= 2000)
- {
- return 1000;
- }
- throw new OverweightException();
- }
- public override string ToString()
- {
- return $"Címzett: {this.Address} / Leírás: {this.desciption} / Tömeg:{this.Weight} g";
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class OverweightException : Exception
- {
- public OverweightException() { }
- public OverweightException(string message) : base(message)
- {
- }
- public OverweightException(string message, Exception innerException) : base(message, innerException)
- {
- }
- protected OverweightException(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public abstract class Parcel : IDeliverable, IComparable
- {
- public enum OrientationType
- {
- Arbitrary, Horizontal, Vertical
- }
- protected Parcel(int weight, string address)
- {
- this.orientationType = OrientationType.Arbitrary;
- Weight = weight;
- Address = address;
- }
- protected Parcel(OrientationType orientationType, int weight, string address)
- {
- this.orientationType = orientationType;
- Weight = weight;
- Address = address;
- }
- protected OrientationType orientationType;
- public int Weight { get; set; }
- public string Address { get; set; }
- public OrientationType Orientation { get { return this.orientationType; } }
- public abstract double CalculatePrice(bool fromLocker);
- public override string ToString()
- {
- return $"Címzett: {this.Address} / Tömeg:{this.Weight} g";
- }
- public int CompareTo(object obj)
- {
- if (obj is IDeliverable)
- {
- IDeliverable deliverable = obj as IDeliverable;
- if (deliverable.Weight == this.Weight)
- {
- return 0;
- }
- if (deliverable.Weight < this.Weight)
- {
- return -1;
- }
- if (deliverable.Weight > this.Weight)
- {
- return 1;
- }
- }
- return 0;
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class NormalParcel : Parcel
- {
- private static Random random = new Random();
- private const int BASE_PRICE = 500;
- public NormalParcel(int weight, string address) : base(weight, address)
- {
- this.orientationType = (OrientationType)random.Next(0,3);
- }
- public override double CalculatePrice(bool fromLocker)
- {
- int price = BASE_PRICE + this.Weight;
- return (fromLocker) ? price - 250 : price;
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class FragileParcel : Parcel
- {
- private const int BASE_PRICE = 1000;
- public FragileParcel(OrientationType orientationType, int weight, string address) : base(orientationType, weight, address)
- {
- if (orientationType == OrientationType.Arbitrary)
- {
- throw new IncorrectOrientationException(this, "Nem megfelelő orientáció!");
- }
- }
- public override double CalculatePrice(bool fromLocker)
- {
- if (fromLocker)
- {
- throw new DeliveryException("Törékeny csomagot nem lehet auómatából feladni!");
- }
- return BASE_PRICE + (this.Weight) * 2;
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class DeliveryException : Exception
- {
- public DeliveryException()
- {
- }
- public DeliveryException(string message) : base(message)
- {
- }
- public DeliveryException(string message, Exception innerException) : base(message, innerException)
- {
- }
- protected DeliveryException(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class IncorrectOrientationException : DeliveryException
- {
- public Parcel Parcel { get; set; }
- public IncorrectOrientationException()
- {
- }
- public IncorrectOrientationException(Parcel parcel, string message) : base(message)
- {
- }
- public IncorrectOrientationException(string message) : base(message)
- {
- }
- public IncorrectOrientationException(string message, Exception innerException) : base(message, innerException)
- {
- }
- protected IncorrectOrientationException(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- public class Courier
- {
- private int sumOfWeight;
- public IDeliverable[] deliverables;
- public int SumOfWeight { get { return this.sumOfWeight; } }
- public Courier(int deliverablesSize)
- {
- this.deliverables = new IDeliverable[deliverablesSize];
- }
- public void PickUpItem(IDeliverable item)
- {
- int i = 0;
- while (i < this.deliverables.Length && this.deliverables[i] != null)
- {
- ++i;
- }
- if (i < this.deliverables.Length)
- {
- this.deliverables[i] = item;
- this.sumOfWeight += item.Weight;
- return;
- }
- throw new DeliveryException();
- }
- public IDeliverable[] FragilesSorted()
- {
- IDeliverable[] sortedResult = new IDeliverable[this.deliverables.Length];
- for (int i = 0; i < this.deliverables.Length; ++i)
- {
- if (this.deliverables[i] is FragileParcel)
- {
- sortedResult[i] = this.deliverables[i];
- }
- }
- Array.Sort(sortedResult);
- return sortedResult;
- }
- }
- }
- ---------------------------------------------------------------------
- using ConsoleApp1;
- using NUnit.Framework;
- using System.Data;
- namespace TestProject1
- {
- public class Tests
- {
- [TestCase(true)]
- [TestCase(false)]
- public void TestEnvelopeCalculatePrice(bool fromLocker)
- {
- Envelope envelope = new Envelope(1500, "Parlament", "Ajándék Vikinek!");
- Assert.That(envelope.CalculatePrice(fromLocker), Is.EqualTo(1000));
- }
- [Test]
- public void TestEnvelopeOverweight()
- {
- Envelope envelope = new Envelope(2300, "OE NIK", "");
- Assert.That(() => envelope.CalculatePrice(true), Throws.TypeOf<OverweightException>());
- }
- [Test]
- public void TestFragileParcelFromLocker()
- {
- FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Budapest");
- Assert.That(() => fragileParcel.CalculatePrice(true), Throws.TypeOf<DeliveryException>());
- }
- [Test]
- public void TestFragileParcelInvalidOrientation()
- {
- Assert.That(() => new FragileParcel(Parcel.OrientationType.Arbitrary, 3000, "Budapest"), Throws.TypeOf(typeof(IncorrectOrientationException)));
- }
- [Test]
- public void TestCourierPickUpItem()
- {
- FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Óbuda");
- Courier courier = new Courier(1);
- courier.PickUpItem(fragileParcel);
- Assert.That(courier.SumOfWeight, Is.EqualTo(3000));
- }
- [Test]
- public void TestCourierFragilesSorted()
- {
- FragileParcel fragileParcel = new FragileParcel(Parcel.OrientationType.Horizontal, 4000, "Óbuda");
- NormalParcel normalParcel = new NormalParcel(1200, "Otthon");
- FragileParcel fragileParcel2 = new FragileParcel(Parcel.OrientationType.Horizontal, 3000, "Óbuda");
- Courier courier = new Courier(5);
- courier.PickUpItem(fragileParcel);
- courier.PickUpItem(normalParcel);
- courier.PickUpItem(fragileParcel2);
- IDeliverable[] expected = new IDeliverable[] { null, null, null, fragileParcel, fragileParcel2 };
- Assert.That(courier.FragilesSorted(), Is.EquivalentTo(expected));
- }
- }
- }
Advertisement
Advertisement