Czoko_Smieciarka/Trunk/MonoGameView/DataModels/Interfaces/TrashCans/ATrashCan.cs
2019-05-14 19:55:00 +02:00

86 lines
2.5 KiB
C#

using System;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
{
public abstract class ATrashCan : ICloneable
{
protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume)
{
this.MaxVolume = maxVolume;
this.TypeOfGarbage = typeOfGarbage;
Garbage = new BasicGarbage((TypeOfGarbage)typeOfGarbage, 0);
}
protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, AGarbage garbage)
{
this.MaxVolume = maxVolume;
this.TypeOfGarbage = typeOfGarbage;
this.Garbage = garbage;
}
public ITypeOfGarbage TypeOfGarbage { get; }
public int MaxVolume { get;}
public AGarbage Garbage { get; private set; }
public double FillPercent
{
get { return ((double)Garbage.Volume) / this.MaxVolume; }
}
public virtual bool AddGarbage(AGarbage garbage)
{
//if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType)
// throw new Exception("You cannot add up different type garbage!");
if (FillPercent == 1) return false;
if (this.Garbage.Volume + garbage.Volume > this.MaxVolume)
{
this.Garbage.Weight = this.MaxVolume;
garbage.Weight = (this.MaxVolume - this.Garbage.Volume);
return true;
} else
{
var newGarbage = this.Garbage + garbage;
if (newGarbage.Volume > this.MaxVolume)
return false;
this.Garbage = newGarbage;
return true;
}
}
public virtual object Clone()
{
throw new NotImplementedException();
}
public virtual AGarbage TakeGarbage(double maxVolume)
{
if (this.Garbage.Weight == 0)
return null;
if (this.Garbage.Volume > maxVolume)
{
var result = (AGarbage)this.Garbage.Clone();
result.Weight = maxVolume;
this.Garbage.Weight = this.Garbage.Weight - maxVolume;
return result;
} else
{
var result = (AGarbage)this.Garbage.Clone();
this.Garbage.Weight = 0;
return result;
}
}
}
}