Laden...

CD/DVD Bibliothek 2

Erstellt von R3turnz vor 8 Jahren Letzter Beitrag vor 8 Jahren 3.666 Views
Thema geschlossen
R
R3turnz Themenstarter:in
125 Beiträge seit 2016
vor 8 Jahren
CD/DVD Bibliothek 2

Hallo,
diesesmal mit dem 2ten Anlauf, und auch im richtigen Forum.
Diese Version funktioniert komplet.
Ich möchte diese aber nochmal hier posten bevor ich mich an Detrails mache: Linq,Und solche Geschichten wie : ReleaseYear in ReleaseDate umbennenen und das dann als Time und nicht als int..,Save(),Load()

Program.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CD_DVD_Libary
{
    class Program
    {
        static void Main(string[] args)
        {
            var appDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DVDCDLibary");
            var client = new CDDVDLibaryClient();
            client.Proccess(appDir);
        }
    }
}


CDDVDLibaryClient.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CD_DVD_Libary
{
    class CDDVDLibaryClient
    {
        private CDDVDLibary libary;
        Validator validator = new Validator();
        public void Proccess(string applicationDirectory)
        {
            Console.WriteLine("CDDVDLibary V.0.6\nIdea from Mycsharp.de\nhttp://www.mycsharp.de/wbb2/thread.php?threadid=43577");
            libary = new CDDVDLibary(applicationDirectory);
            if (Directory.Exists(applicationDirectory))
            {
                Console.WriteLine("Loaded Program Directory.");
            }
            else
            {
                Console.Write("Creating Program Directory...");
                Directory.CreateDirectory(applicationDirectory);
                Console.WriteLine("Done...");
            }
            bool stop = false;
            do
            {
               
                Console.WriteLine("Main Menu:Add/Remove/Print/Modify/Exit");
                string cmd = Console.ReadLine();
                if (cmd.ToUpper() == "ADD")
                {
                    Add();
                }
                else if (cmd.ToUpper() == "REMOVE")
                {
                    Remove();
                }
                else if (cmd.ToUpper() == "EXIT")
                {
                    stop = true;
                }
                else if (cmd.ToUpper() == "PRINT")
                {
                    Print();
                }
                else if (cmd.ToUpper() == "MODIFY") 
                {
                    Modify();
                }
                else
                {
                    Console.WriteLine("Try again...");
                }
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                Console.Clear();
            } while (!stop);
            Console.WriteLine("\nSaving...");
            libary.Save();
            Console.ReadKey();
        }
        private void Add()
        {
            IMedium medium;
            Console.Write("What shoud be added?\n0.DVD\n1.CD\nChoose:");
            string input = Console.ReadLine();
            if (input == "0")
            {
                medium = InputDVD();
            }
            else if (input == "1")
            {
                medium = InputCD();
            }
            else
            {
                Console.WriteLine("Can not validate your input.");
                medium = null;
            }
            libary.Mediums.Add(medium);
            Console.WriteLine("Added.");
        }
        private void Remove()
        {
            IMedium toRemove = GetMedium();
            if (toRemove != null)
                {
                libary.Mediums.Remove(toRemove);
                Console.WriteLine("Removed.");
            }
            }
        private void Modify()
        {
            IMedium medium = GetMedium();
            if (medium != null)
            {
                int indexOfMedium = libary.Mediums.IndexOf(medium);
                Console.WriteLine("Choosen Medium:");
                libary.PrintMedium(medium);
                if (medium is DVD)
                {
                  
                    Console.Write("Options:\n0.Modify Medium\n1.Add Actor\n2.Remove Actor\nChoose:");
                    string input = Console.ReadLine();
                    if (input == "0")
                    {

                        medium = InputDVD();
                        libary.Mediums.Add(medium);
                    }
                    else if (input == "1")
                    {
                        medium.Attributs.Add(InputActor());
                    }
                    else if (input == "2")
                    {
                        medium.Attributs.Remove(GetAttribut());
                    }
                }
                else
                {
                    Console.Write("Options:\n0.Modify Medium\n1.Add Track\n2.Remove Track\nChoose:");
                    string input = Console.ReadLine();
                    if (input == "0")
                    {

                        medium = InputDVD();
                        libary.Mediums.Add(medium);
                    }
                    else if (input == "1")
                    {
                        medium.Attributs.Add(InputTrack());
                    }
                    else if (input == "2")
                    {
                        medium.Attributs.Remove(GetAttribut());
                    }
                }
                libary.Mediums[indexOfMedium] = medium;
            }
            }
        private void Print()
        {
            Console.Write("Printing Options:\n0.Print mediums\n1.Print all\nChoose:");
            string input = Console.ReadLine();
            if (input == "0")
                libary.PrintMediums();

            else if (input == "1")
                libary.PrintAll();
            else
                Console.WriteLine("Try again.");
                    }
        private CD InputCD()
        {
            CD output = null;
            string name;
            int length;
            int releaseYear;
            try
            {
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Length:");
                length = int.Parse(Console.ReadLine());
                Console.Write("Release Year:");
                releaseYear = int.Parse(Console.ReadLine());
                output = new CD(name, length, releaseYear);
            }
            catch
            {
                Console.WriteLine("An Input Error occured.");
            }
            return output;
        }
        private DVD InputDVD()
        {
            DVD output = null;
            string name;
            int length;
            int releaseYear;
            try
            {
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Length:");
                length = int.Parse(Console.ReadLine());
                Console.Write("Release Year:");
                releaseYear = int.Parse(Console.ReadLine());
                output = new DVD(name, length, releaseYear);
            }
            catch
            {
                Console.WriteLine("An Input Error occured.");
            }
            return output;
        }
private Actor InputActor()
        {
            Actor output = null;
            try
            {
                string name;
                int age;
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Age:");
                age = int.Parse(Console.ReadLine());
                output = new Actor(name, age);
            }
        catch
            {
                Console.WriteLine("An Input Error occured");
            }
            return output;
        }
        private Track InputTrack()
        {
            Track output = null;
            try
            {
                string name;
                string artist;
                decimal length;
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Artist:");
                artist = Console.ReadLine();
                Console.Write("Length (Decimal):");
                length = decimal.Parse(Console.ReadLine());
                output = new Track(name, length, artist);
            }
            catch
            {
                Console.WriteLine("An Input Error occured.");
            }
            return output;
        }
        public IMedium GetMedium()
        {
            libary.PrintMediums();
            IMedium output;
            Console.Write("Choose:");
            string input = Console.ReadLine();
            
            if(validator.ValidateIndex(input, libary.Mediums.Count - 1))
            {
                int validatedinput = int.Parse(input);
                output = libary.Mediums[validatedinput];
            }
            else
            {
                output = null;
                Console.WriteLine("An validation error occured.");
            }
            return output;
        }
        public IAttribut GetAttribut()
        {
            IMedium medium = GetMedium();
            IAttribut output = null;
            if (medium != null)
            {
               if(medium.Attributs.Any())
                {
                    libary.PrintMedium(medium);
                    Console.Write("Choose:");
                    string index = Console.ReadLine();
                    if(validator.ValidateIndex(index, medium.Attributs.Count - 1))
                    {
                        int validatedindex = int.Parse(index);
                        output = medium.Attributs[validatedindex];
                    }
                    else
                    {
                        Console.WriteLine("An validation error occured");
                    }
                }
               else
                {
                    Console.WriteLine("The Medium has no Attributs.");
                }

            }
            return output;
        }
    }
}


CDDVDLibary:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class CDDVDLibary
    {
        private string _applicationDirectory;

        public string ApplicationDirectory
        {
            get { return _applicationDirectory; }
            set { _applicationDirectory = value; }
        }
        public List<IMedium> Mediums = new List<IMedium>();

        public CDDVDLibary(string applicationdirectory)
        {
            this.ApplicationDirectory = applicationdirectory;
        }
        public void PrintMediums()
        {
            for(int i = 0; i <= Mediums.Count - 1;i++)
            {
                Console.WriteLine(i+"."+Mediums[i].Name);
            }
        }
        public void PrintAll()
        {
            for (int i = 0; i <= Mediums.Count - 1; i++)
            {
                Console.WriteLine(Mediums[i].ToString());
                if(Mediums[i].Attributs.Any())
                    {
                    for (int a = 0; a <= Mediums[i].Attributs.Count - 1; a++)
                    {
                        Console.WriteLine(Mediums[i].Attributs[a].ToString());
                    }
                }
            }
        }
        public void PrintMedium(IMedium medium)
        {
            Console.WriteLine(medium.ToString());
            if (medium.Attributs.Any())
            {
                for (int a = 0; a <= medium.Attributs.Count; a++)
                {
                    medium.Attributs[a].ToString();
                }
            }
        }
        public void Save()
        {

        }
        public void Load()
        {

        }

    }
}


IMedium.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    interface IMedium
    {
        
        string Name { get; set; }
        int Length { get; set; }
        int ReleaseYear { get; set; }
        List<IAttribut> Attributs { get; set; }
    }
}


DVD.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class DVD : IMedium
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _length;

        public int Length
        {
            get { return _length; }
            set { _length = value; }
        }

        private int _releaseYear;

        public int ReleaseYear
        {
            get { return _releaseYear; }
            set { _releaseYear = value; }
        }
        private List<IAttribut> _attributs = new List<IAttribut>();
        public List<IAttribut> Attributs
        {
            get { return _attributs; }
            set { _attributs = value; }
        }
        public DVD(string name,int length,int releaseYear)
        {
            this.Name = name;
            this.Length = length;
            this.ReleaseYear = releaseYear;
        }
        public override string ToString()
        {
            return string.Format("DVD Name={0} Length={1} ReleaseYear={2}",Name,Length,ReleaseYear);
        }

    }
}


CD.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class CD : IMedium
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _length;

        public int Length
        {
            get { return _length; }
            set { _length = value; }
        }

        private int _releaseYear;

        public int ReleaseYear
        {
            get { return _releaseYear; }
            set { _releaseYear = value; }
        }

        private List<IAttribut> _attributs = new List<IAttribut>();
        public List<IAttribut> Attributs
        {
            get { return _attributs; }
            set { _attributs = value; }
        }

        public CD(string name,int length,int realeaseYear)
        {
            this.Name = name;
            this.Length = length;
            this.ReleaseYear = realeaseYear;
        }
        public override string ToString()
        {
            return string.Format("CD Name={0} Length={1} ReleaseYear={2}", Name, Length, ReleaseYear);
        }


    }
}

IAttribut.cs:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    interface IAttribut
    {
        string Name { get; set; }



    }
}

Track.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    public class Track : IAttribut
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private decimal _length;

        public decimal Length
        {
            get { return _length; }
            set { _length = value; }
        }
        private string _artist;

        public string Artist
        {
            get { return _artist; }
            set { _artist = value; }
        }

        public Track(string name,decimal length,string artist)
        {
            this.Name = name;
            this.Length = length;
            this.Artist = artist;

        }
        public override string ToString()
        {
            return string.Format("Track Name={0} Artist={1} Length={2}",Name,Artist,Length);
        }
    }
}


Actor.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
   public class Actor : IAttribut
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _age;

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public Actor(string name,int age)
            {
            this.Name = name;
            this.Age = age;
        }
    
    public override string ToString()
        {
            return string.Format("Actor Name={0} Age={1}",Name,Age);
        }



    }
}


Validator.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class Validator
    {
        public bool ValidateIndex(string value,int arrayLength)
        {
            int output = 0;
            if (!int.TryParse(value,out output) )
            {
                return false;
            }

            if(output <= arrayLength && output >= 0)
            {
                return true;
            }
            return false;
        }

    }
}


Passt diesesmal meine OOP?
Wo kann ich etwas am Stil ändern oder eleganter lösen?

LG

S
322 Beiträge seit 2007
vor 8 Jahren

Hallo,

ich würde Dir raten eine abstrakte Klasse "Medium" zu implementieren.
siehe https://msdn.microsoft.com/de-de/library/sf985hc5.aspx

z.B:


    internal abstract class Medium : IMedium
    {
        public string Name { get; set; }

        public int Length { get; set; }

        public int ReleaseYear { get; set; }

        public IList<IAttribut> Attributs { get; }

        public Medium(string name, int length, int releaseYear)
        {
            Attributs = new List<IAttribut>();
            Name = name;
            Length = length;
            ReleaseYear = releaseYear;
        }
    }

CD/DVD-Klasse wäre dann viel schlanker:


       namespace CD_DVD_Libary
{
    internal class CD : Medium
    {
        public CD(string name, int length, int realeaseYear) : base(name, length, realeaseYear)
        {
        }

        public override string ToString()
        {
            return string.Format("CD Name={0} Length={1} ReleaseYear={2}", Name, Length, ReleaseYear);
        }
    }
}


namespace CD_DVD_Libary
{
    internal class DVD : Medium
    {
        public DVD(string name, int length, int releaseYear) : base(name, length, releaseYear)
        {
        }

        public override string ToString()
        {
            return string.Format("DVD Name={0} Length={1} ReleaseYear={2}", Name, Length, ReleaseYear);
        }
    }
}

Ich sehe im Moment noch nicht den Sinn und Zweck die CD/DVD-Klasse extra zu implementieren die identisch sind außer der Klassen-Name.
Man könnte in der Medium Klasse einfach noch einen "Typ" als Enum definieren (mit CD, DVD als EnumValues).

Sonst wird sicherlich noch einiges von den anderen kommen. (z.b. Trennung Logik und UI (Console)).

Gruß
Steffen

C
1.214 Beiträge seit 2006
vor 8 Jahren

Was mir gleich auffällt, es heißt Attribute und Attributes.

Ansonsten gibts in dem Code kaum Anwendungslogik, das meiste ist Logik für die Ein- und Ausgabe auf der Konsole. Und wenn du das ganze weiterentwickelst, kommt wahrscheinlich noch mehr Konsolencode dazu. Da muss ich mich schon fragen, wozu? Warum nicht gleich WPF oder WinForms? Dann wärst du nämlich so flexibel, dass du deine DVD Verwaltung sinnvoll beliebig erweitern könntest. Mit der Konsolenversion wirst du zwangsläufig bald soweit sein, dass du sagst, das macht eh keinen Sinn mehr.

1.040 Beiträge seit 2007
vor 8 Jahren

Moin,

ein paar Punkte, die mir aufgefallen sind:

  1. Properties, die einfach nur gesetzt und ausgelesen werden, können in einer Zeile geschrieben werden.
    Das Feld wird dann nicht mehr benötigt.

ALT:
private string _name;
public string Name
{
	get { return _name; }
	set { _name = value; }
}

NEU:
public string Name { get; set; }

2. Die Klassen CD und DVD mit einer abstrakten Basisklasse versehen (wie schon von steffen_dec angemerkt)

  1. In der Funktion 'ValidateIndex' gibt es den Parameter 'arrayLength'.
    An den Stellen, wo du die Funktion nutzt, übergibst du aber immer '.Count - 1' - das ist eine Fehlerquelle!
    Du solltest stattdessen lieber den Count übergeben (so wie es der Parameter auch aussagt) und in der Funktion stattdessen nur auf '<' statt auf '≤' prüfen.

  2. In den for-Schleifen nutzt du oft 'i ≤ Mediums.Count - 1'.
    Besser: 'i < Mediums.Count'.

  3. Ggf. foreach statt for-Schleifen verwenden.

  4. Das 'ApplicationDirectory' nutzt du aktuell nicht (ggf. später?!)

  5. Im folgenden Code-Block ist die Prüfung auf 'Any()' überflüssig, SOFERN du 4. umgesetzt hast.

if(Mediums[i].Attributs.Any())
{
	for (int a = 0; a <= Mediums[i].Attributs.Count - 1; a++)
	{
		Console.WriteLine(Mediums[i].Attributs[a].ToString());
	}
}
  1. In der Funktion 'PrintAll' kannst du in der ersten Schleife die Funktion 'PrintMedium' mit dem entsprechenden Medium rufen.
    Spart dir redundanten Code.

  2. In der Funktion 'PrintMedium' fehlt das 'Console.WriteLine'.
    Zudem knallt die Schleife (da 'i ≤ Count').

  3. InputActor/InputTrack/InputCD/InputDVD:
    Die Variablen dann deklarieren, wenn du sie brauchst.


ALT:
string name;
...
name = Console.ReadLine();

NEU:
string name = Console.ReadLine();

  1. In der 'Add'-Methode fügst du bei falscher Eingabe ein leeres Medium hinzu...

  2. Die Modify-Methode:

  • du verwendest in beiden Fällen 'InputDVD'
  • das Entfernen vom Actor/Track ist ungünstig gelöst. Bei der Auswahl des entsprechenden Attributs lässt du vorher nochmal ein Medium auswählen, obwohl ja schon eins ausgewählt ist...
  • Generell verstehe ich das Modifizieren der Medien nicht.
    Du legst ein NEUES Medium an und fügst es zur Liste hinzu.
    Abschließend weist du das neue Medium nochmal anstelle des alten Mediums zu.
    Es ist somit 2x in der Liste, das alte existiert in der Liste gar nicht mehr.
  1. Du hast immer noch "if-else if"-Konstrukte im Code - das geht einfacher... 😉

14. Libary müsste korrekterweise Library heißen

R
R3turnz Themenstarter:in
125 Beiträge seit 2016
vor 8 Jahren

Hallo,
ich habe versucht alle vorgeschalgenen Punkte umzusetzen, eine Speicher-Funktion wird folgen, konnte man vieleicht an den leeren Save and Load Methoden sehen 😃 (Wenn ich XML beherrsche werde ich diese Funktion einbauen).
Aktuell besteht das Problem das wenn ich ein Medium erstellen möchte eine StackOverflow Exception geworfen wird. (Im Gleichheits Operator). Ich kann gerade wirklich nicht verstehen wieso hier eine Endlos-Schleife sein sollte.
Außerdem wollte ich in der Attributes Eigenschaft nur das Attribut hinzufügen wenn es dem aktuellen Typ entspricht. (Also keine Lieder einem Film hinzufügen etc.)
Dies kann ich aber nicht, da value ja eine Liste ist. Wie löst man soetwas?
Die Fragen zur Implemtierung von Equals bzw. IEquatable in den Attributen habe ich ja schon in dem anderen Post gestellt.

Also hier das Projekt:
Program.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CD_DVD_Libary
{
    class Program
    {
        static void Main(string[] args)
        {
            var appDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DVDCDLibary");
            var client = new CDDVDLibaryClient();
            client.Proccess(appDir);
        }
    }
}


CDDVDLibaryClient.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CD_DVD_Libary
{
    class CDDVDLibaryClient
    {
        private CDDVDLibary _libary;
        Validator _validator = new Validator();
        public void Proccess(string applicationDirectory)
        {
            Console.WriteLine("CDDVDLibary V.0.6\nIdea from Mycsharp.de\nhttp://www.mycsharp.de/wbb2/thread.php?threadid=43577");
            _libary = new CDDVDLibary(applicationDirectory);
            if (Directory.Exists(applicationDirectory))
            {
                Console.WriteLine("Loaded Program Directory.");
            }
            else
            {
                Console.Write("Creating Program Directory...");
                Directory.CreateDirectory(applicationDirectory);
                Console.WriteLine("Done...");
            }
            bool stop = false;
            do
            {
               
                Console.WriteLine("Main Menu:Add/Remove/Print/Modify/Queries/Exit");
                string cmd = Console.ReadLine();
                if (cmd.ToUpper() == "ADD")
                {
                    Add();
                }
                else if (cmd.ToUpper() == "REMOVE")
                {
                    Remove();
                }
                else if (cmd.ToUpper() == "EXIT")
                {
                    stop = true;
                }
                else if (cmd.ToUpper() == "PRINT")
                {
                    Print();
                }
                else if (cmd.ToUpper() == "MODIFY") 
                {
                    Modify();
                }
                else if(cmd.ToUpper() == "QUERIES")
                {
                    Queries();
                }
                    
                else
                {
                    Console.WriteLine("Try again...");
                }
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                Console.Clear();
            } while (!stop);
            Console.WriteLine("\nSaving...");
            _libary.Save();
            Console.ReadKey();
        }
        private void Add()
        {
            Medium medium;
            Console.Write("What shoud be added?\n0.DVD\n1.CD\nChoose:");
            string input = Console.ReadLine();
            if (input == "0")
            {
                medium = InputMedium(MediumType.DVD);
            }
            else if (input == "1")
            {
                medium = InputMedium(MediumType.CD);
            }
            else
            {
                Console.WriteLine("Can not validate your input.");
                medium = null;
            }

            if (medium != null)
            {
                Console.WriteLine("Added.");
                _libary.Mediums.Add(medium);

            }
                
        }
        private void Remove()
        {
            Medium toRemove = GetMedium();
            if (toRemove != null)
                {
                _libary.Mediums.Remove(toRemove);
                Console.WriteLine("Removed.");
            }
            }
        private void Modify()
        {
            Medium medium = GetMedium();
            if (medium != null)
            {
                int indexOfMedium = _libary.Mediums.IndexOf(medium);
                Console.WriteLine("Choosen Medium:");
                _libary.PrintMedium(medium);
                if (medium.MediumType == MediumType.DVD)
                {
                  
                    Console.Write("Options:\n0.Modify Medium\n1.Add Actor\n2.Remove Actor\nChoose:");
                    string input = Console.ReadLine();
                    if (input == "0")
                    {
                        medium = InputMedium(MediumType.DVD);
                    }
                    else if (input == "1")
                    {
                        Actor temp = InputActor();
                        if (temp != null)
                        medium.Attributes.Add(temp);

                    }
                    else if (input == "2")
                    {
                        IAttribute temp = GetAttribut(medium);
                        if (medium.Attributes.Contains(temp))
                        medium.Attributes.Remove(temp);
                    }
                }
                else if(medium.MediumType == MediumType.CD)
                {
                    Console.Write("Options:\n0.Modify Medium\n1.Add Track\n2.Remove Track\nChoose:");
                    string input = Console.ReadLine();
                    if (input == "0")
                    {
                        medium = InputMedium(MediumType.CD);
                    }
                    else if (input == "1")
                    {
                        Track track = InputTrack();
                        if(track != null)
                        medium.Attributes.Add(track);
                    }
                    else if (input == "2")
                    {
                        IAttribute temp = GetAttribut(medium);
                        if (medium.Attributes.Contains(temp))
                            medium.Attributes.Remove(temp);
                    }
                }
                if (medium != null)
                {
                    _libary.Mediums[indexOfMedium] = medium;
                    Console.WriteLine("Modified.");
                }
            }
            }
        private void Print()
        {
            Console.Write("Printing Options:\n0.Print mediums\n1.Print all\n2.Print single medium\nChoose:");
            string input = Console.ReadLine();
            if (input == "0")
                _libary.PrintMediums();

            else if (input == "1")
                _libary.PrintAll();
            else if (input == "2")
            {
                Medium toPrint = GetMedium();
                if(toPrint != null)
                _libary.PrintMedium(toPrint);
            }
            else
                Console.WriteLine("Try again.");
                    }

        private void Queries()
        {
            Console.Write("Queries:\n0.Compare for identical Mediums\nChoose:");
            string input = Console.ReadLine();
            if (input == "0")
            {
                CompareMediums();
            }
            }
        private void CompareMediums()
        {
        }
    
        private Medium InputMedium(MediumType mediumType)
        {
            Medium output = null;
            
            try
            {
                Console.Write("Name:");
                string name = Console.ReadLine();
                Console.Write("Length:");
               int length = int.Parse(Console.ReadLine());
                Console.Write("Release Year:");
               int releaseYear = int.Parse(Console.ReadLine());
                Console.Write("Release Month:");
                int releaseMonth = int.Parse(Console.ReadLine());
                
                output = new Medium(name, length,new DateTime(releaseYear,releaseMonth,1),mediumType);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return output;
        }
private Actor InputActor()
        {
            Actor output = null;
            try
            {
                string name;
                int age;
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Age:");
                age = int.Parse(Console.ReadLine());
                output = new Actor(name, age);
            }
        catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return output;
        }
        private Track InputTrack()
        {
            Track output = null;
            try
            {
                string name;
                string artist;
                decimal length;
                Console.Write("Name:");
                name = Console.ReadLine();
                Console.Write("Artist:");
                artist = Console.ReadLine();
                Console.Write("Length (Decimal):");
                length = decimal.Parse(Console.ReadLine());
                output = new Track(name, length, artist);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return output;
        }
        public Medium GetMedium()
        {
            _libary.PrintMediums();
            Medium output;
            Console.Write("Choose:");
            string input = Console.ReadLine();
            
            if(_validator.ValidateIndex(input, _libary.Mediums.Count))
            {
                int validatedinput = int.Parse(input);
                output = _libary.Mediums[validatedinput];
            }
            else
            {
                output = null;
                Console.WriteLine("An validation error occured.");
            }
            return output;
        }
        public IAttribute GetAttribut(Medium parent)
        {
            
            IAttribute output = null;
           
                if (parent != null && parent.Attributes.Any())
                {
                    _libary.PrintMedium(parent);
                    Console.Write("Choose:");
                    string index = Console.ReadLine();
                    if (_validator.ValidateIndex(index, parent.Attributes.Count))
                    {
                        int validatedindex = int.Parse(index);
                        output = parent[validatedindex];
                    }
                    else
                    {
                        Console.WriteLine("An validation error occured");
                    }
                }
                else
                {
                    Console.WriteLine("The Medium has no Attributs.");
                }

            
            return output;
        }
    }
}


CDDVDLibary.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class CDDVDLibary
    {
        private string _applicationDirectory;

        public string ApplicationDirectory
        {
            get { return _applicationDirectory; }
            set { _applicationDirectory = value; }
        }
        public List<Medium> Mediums = new List<Medium>();

        public Medium this[int index]
        {
            get
            {
                return Mediums[index];
            }
            set
            {
                Mediums[index] = value;
            }
        }
        public CDDVDLibary(string applicationdirectory)
        {
            this.ApplicationDirectory = applicationdirectory;
        }
        public void PrintMediums()
        {
            for(int i = 0; i < Mediums.Count;i++)
            {
                Console.WriteLine(i+"."+Mediums[i].Name);
            }
        }
        public void PrintAll()
        {
                for (int i = 0; i < Mediums.Count; i++)
                {
                    Console.WriteLine(Mediums[i].ToString());
                    
                        for (int a = 0; a < Mediums[i].Attributes.Count; a++)
                        {
                            Console.WriteLine(Mediums[i].Attributes[a].ToString());
                            Console.WriteLine();
                        }
            }
            }
        public void PrintMedium(Medium medium)
        {
            Console.WriteLine(medium.ToString());
            
                for (int a = 0; a < medium.Attributes.Count; a++)
                {
                    Console.WriteLine(medium.Attributes[a].ToString());
                }
            
        }
        public void Save()
        {

        }
        public void Load()
        {

        }

    }
}


Medium.cs:


using System;   
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    public enum MediumType {DVD, CD}
      
    sealed class Medium : IEquatable<Medium>
    {
        public static bool operator ==(Medium medium1,Medium medium2)
        {
            if (medium1 == null|| medium2 == null)
            {
                return Object.Equals(medium1, medium2);
            }           
            return medium1.Equals(medium2) ? true : false;
        }
        public static bool operator !=(Medium medium1,Medium medium2)
        {
            return !(medium1.Equals(medium2));
        }
        public bool Equals(Medium medium)
        {
            if (medium == null) return false;
            if (this.Name != medium.Name)
                return false;
            if (this.Length != medium.Length)
                return false;
            if (this.ReleaseTime != medium.ReleaseTime)
                return false;
            return new HashSet<IAttribute>(Attributes).SetEquals(medium.Attributes);

        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Medium medium = obj as Medium;
            if (medium == null) return false;
            else return Equals(medium);
        }
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
        
        public IAttribute this[int index]
        {
            get
            {
                return Attributes[index];
            }
                set
            {
                Attributes[index] = value;
            }
        }
       
        public Medium(string name,int length,DateTime releaseTime,MediumType mediumtype)
        {
            this.Name = name;
            this.Length = length;
            this.ReleaseTime = releaseTime;
            this._mediumType = mediumtype;
        }

        private MediumType _mediumType;

        public MediumType MediumType
        {
            get { return _mediumType; }
        }
      
        public List<IAttribute> Attributes { get; set; }
      
        
        
        private string _name;

        public string Name
        {
            get { return _name; }
            set { if(value != null && value != "")
                {
                    _name = value;
                }
            else
                {
                    throw new Exception("A Medium must have a name!");
                }
            }
        }

        private int _length;

        public int Length
        {
            get { return _length; }
            set {
                if (value < 0)
                    throw new Exception("The length of the film must be over 0!");
                else
                    _length = value;
            }
        }

        private DateTime _releaseTime;

        public DateTime ReleaseTime
        {
            get { return _releaseTime; }
            set
            {
                if (DateTime.Compare(value,DateTime.Now) <= 0)
                {
                    _releaseTime = value;
                }
else
                {
                    throw new Exception("The release time can not be in the future!");
                }
            }
        }

  
        
    }
}


IAttribute.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    interface IAttribute
    {
        string Name { get; set; }


    }
}


Track.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    public class Track : IAttribute,IEquatable<Track>
    {
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
        public static bool operator ==(Track track1,Track track2)
        {
            if (track1 == null || track2 == null)
                return Object.Equals(track1, track2);
            return track1.Equals(track2);
        }
        public static bool operator !=(Track track1,Track track2)
        {
            return  track1 == track2 ? false : true ;
        }
            public override bool Equals(object obj)
        {
            if (obj == null) return false;
            IAttribute attribut = obj as IAttribute;
            if (attribut == null) return false;
            return Equals(attribut);
        }
        public bool Equals(Track other)
        {
            if (other == null) return false;
            if (other.Name != this.Name) return false;
            if (other.Artist != this.Artist) return false;
            if (other.Length != this.Length) return false;
            return true;

        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value != "")
                    _name = value;
                else
                    throw new Exception("An track needs a name!");
            }
        }

        private decimal _length;

        public decimal Length
        {
            get { return _length; }
            set
            {
                if (value > 0)
                    _length = value;
                else
                    throw new Exception("The length can not be under 0 minutes.");

            }
        }
        private string _artist;

        public string Artist
        {
            get { return _artist; }
            set
            {
                if (value != null && value != "")
                    _artist = value;
                else
                    throw new Exception("A artist needs a name!");
            }
        }

        public Track(string name,decimal length,string artist)
        {
            this.Name = name;
            this.Length = length;
            this.Artist = artist;

        }
        public override string ToString()
        {
            return string.Format("Track Name={0} Artist={1} Length={2}",Name,Artist,Length);
        }
    }
}


Actor.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
   public class Actor : IAttribute,IEquatable<Actor>
    {
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Actor actor1 = obj as Actor;
            if (actor1 == null) return false;
            return Equals(actor1);
           
        }
        public static bool operator ==(Actor actor1,Actor actor2)
        {
            if (actor1 == null || actor2 == null) return Object.Equals(actor1, actor2);
            return actor1.Equals(actor2);
        }
        public static bool operator !=(Actor actor1,Actor actor2)
        {
            return actor1 == actor2 ? false : true;
        }
        public bool Equals(Actor other)
        {
            if (other == null) return false;
            if (this.Name != other.Name) return false;
            if (this.Age != other.Age) return false;
            return true;
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value != "")
                    _name = value;
                else
                    throw new Exception("An actor needs an name!");
            }
        }
        private int _age;

        public int Age
        {
            get { return _age; }
            set
            {
                if (value < 0)
                    throw new Exception("A actor can't be jounger than 0!");
                else if (value > 100)
                    throw new Exception("The actor isn't older than 100, is he?");
                else
                    _age = value;
            }
        }

        public Actor(string name,int age)
            {
            this.Name = name;
            this.Age = age;
        }
    
    public override string ToString()
        {
            return string.Format("Actor Name={0} Age={1}",Name,Age);
        }



    }
}


Validator.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CD_DVD_Libary
{
    class Validator
    {
        public bool ValidateIndex(string value,int arrayLength)
        {
            int output = 0;
            if (!int.TryParse(value,out output) )
            {
                return false;
            }

            if(output < arrayLength && output > 0)
            {
                return true;
            }
            return false;
        }

    }
}


LG

5.658 Beiträge seit 2006
vor 8 Jahren

Hi R3turnz,

das Forum ist nicht dazu da, hier endlosen Quelltext zu posten, damit wir ihn für dich debuggen. Das Code-Review-Forum ist da keine Ausnahme. Du kannst selber den Debugger verwenden, um den Fehler einzugrenzen, und dann bei Bedarf konkrete Fragen in dem entsprechenden Forum zu posten. Das gleiche betrifft deine Fragen bezüglich der Attribut-Typen und der Implementierung der IEquatable-Schnittstelle. Soetwas gehört ins Grundlagen-Forum.

Ich möchte dich daher nochmals bitten, dich an [Hinweis] Wie poste ich richtig? und die Code-Review Regeln zu halten.

Christian

Weeks of programming can save you hours of planning

Thema geschlossen