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