Skip to content

Commit

Permalink
ICollection bei Show,IEquatable fuer Show,Season,Episode
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiktubik committed Jul 7, 2013
1 parent 2e81e6b commit 6207311
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 46 deletions.
14 changes: 13 additions & 1 deletion TVLib/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TVLib
{
public class Episode : IComparable
public class Episode : IComparable,IEquatable<Episode>
{
public int Number { get; set; }
public string Name { get; set; }
Expand All @@ -23,5 +23,17 @@ public int CompareTo(object obj)
Episode otherEpisode = obj as Episode;
return this.Number.CompareTo(otherEpisode.Number);
}

public bool Equals(Episode other)
{
if (this.Name == other.Name && this.Number == this.Number)
{
return true;
}
else
{
return false;
}
}
}
}
40 changes: 39 additions & 1 deletion TVLib/Season.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TVLib
{
public class Season : ICollection<Episode>
public class Season : ICollection<Episode>,IEquatable<Season>
{
public int Number { get; set; }
private Dictionary<int, Episode> Episodes { get; set; }
Expand Down Expand Up @@ -49,6 +49,20 @@ public Episode GetEpisodeByNumber(int number)
}
}

public bool Remove(int number)
{
//Find the Episode to remove
if (this.Episodes.ContainsKey(number))
{
this.Episodes.Remove(number);
return true;
}
else
{
return false;
}
}

#region ICollection Members
public void Add(Episode item)
{
Expand Down Expand Up @@ -126,5 +140,29 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
return this.Episodes.GetEnumerator();
}
#endregion

#region IEqualable Members
public bool Equals(Season other)
{
if (other.Number == this.Number)
{
foreach (var otherElement in this)
{
foreach (var thisItem in other)
{
if (otherElement.Equals(thisItem))
{
return true;
}
}
}
return false;
}
else
{
return false;
}
}
#endregion
}
}
151 changes: 130 additions & 21 deletions TVLib/Show.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,172 @@

namespace TVLib
{
public class Show
public class Show : ICollection<Season>,IEquatable<Show>
{
public string Name { get; set; }
public Dictionary<int,Season> Seasons { get; set; }
private Dictionary<int,Season> Seasons { get; set; }
// For IsReadOnly
private bool isRO = false;

/// <summary>
/// Adds a new season to the Show
/// </summary>
public void AddSeason(Season season)
public Show(string name, Dictionary<int, Season> seasons) : this(name)
{
if (season == null)
if (seasons == null)
{
throw new ArgumentNullException();
}
else if (Seasons.ContainsKey(season.Number) == true)
else if (seasons.Count <= 0)
{
throw new ArgumentException("Season " + season.Number + " schon vorhanden!");
throw new ArgumentException("Keine Liste mit 0 Elementen erlaubt!");
}
else
{
this.Seasons.Add(season.Number, season);
this.Seasons = seasons;
}
}

public Show(string name)
{
this.Name = name;
this.Seasons = new Dictionary<int, Season>();
}

public void RemoveSeason(int number)
public Season GetSeasonByNumber(int number)
{
if (this.Seasons.ContainsKey(number) == false)
if (this.Seasons.ContainsKey(number))
{
throw new ArgumentOutOfRangeException("Keine Season " + number + " vorhanden");
return this.Seasons[number];
}
else
{
throw new ArgumentOutOfRangeException("Season nicht vorhanden");
}
}

public bool Remove(int number)
{
//Find the Episode to remove
if (this.Seasons.ContainsKey(number))
{
this.Seasons.Remove(number);
return true;
}
else
{
return false;
}
}

public Show(string name, Dictionary<int, Season> seasons) : this(name)
#region ICollection Member

public void Add(Season item)
{
if (seasons == null)
if (item == null)
{
throw new ArgumentNullException();
}
else if (seasons.Count <= 0)
else if (!this.Contains(item))
{
throw new ArgumentException("Keine Liste mit 0 Elementen erlaubt!");
this.Seasons.Add(item.Number, item);
}
else
{
this.Seasons = seasons;
throw new ArgumentException("Staffel schon vorhanden");
}
}

public Show(string name)
public void Clear()
{
this.Name = name;
this.Seasons = new Dictionary<int, Season>();
Seasons.Clear();
}

public bool Contains(Season item)
{
if (this.Seasons.ContainsKey(item.Number) == true )
{
return true;
}
else
{
return false;
}
}

public void CopyTo(Season[] array, int arrayIndex)
{
int i = 0;
foreach (var item in this.Seasons)
{
array[i] = (Season)item.Value;
i++;
}
}

public int Count
{
get
{
return Seasons.Count;
}
}

public bool IsReadOnly
{
get { return isRO; }
}

public bool Remove(Season item)
{
//Find the Episode to remove
if (this.Seasons.ContainsKey(item.Number))
{
this.Seasons.Remove(item.Number);
return true;
}
else
{
return false;
}
}

public IEnumerator<Season> GetEnumerator()
{
foreach (KeyValuePair<int, Season> pair in this.Seasons)
{
yield return pair.Value;
}
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.Seasons.GetEnumerator();
}

#endregion

#region IEquatable
public bool Equals(Show other)
{
if (this.Name == other.Name)
{
foreach (var thisSeason in this)
{
foreach (var otherSeason in other)
{
if (otherSeason.Equals(thisSeason))
{
return true;
}
}
}
//Sonst false
return false;
}
else
{
return false;
}
}

#endregion
}
}
29 changes: 29 additions & 0 deletions TVLib/Tests/SeasonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,34 @@ public void CopyToTest()
Assert.That(liste[1].Name, Is.EqualTo("Second"));
Assert.That(liste[2].Name, Is.EqualTo("Third"));
}

[Test]
public void EqualsTrueTest()
{
Season s01 = new Season(1);
s01.Add(new Episode(1, "Pilot"));
s01.Add(new Episode(2, "Second"));

Season s02 = new Season(1);
s02.Add(new Episode(1, "Pilot"));
s02.Add(new Episode(2, "Second"));

Assert.That(s01.Equals(s02));
}

[Test]
public void EqualsFalseTest()
{
Season s01 = new Season(1);
s01.Add(new Episode(1, "Pilot"));
s01.Add(new Episode(2, "Second"));

Season s02 = new Season(1);
s02.Add(new Episode(1, "Pilot"));
s02.Add(new Episode(2, "Bla"));

Assert.That(s02.Equals(s01));
}

}
}
Loading

0 comments on commit 6207311

Please sign in to comment.