Last active
August 29, 2015 13:57
-
-
Save d8ta/9414247 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 1310601027, fhs36118 | |
| * Daniel Raudschus | |
| * ue02 bsp01 | |
| * */ | |
| using System; | |
| using System.Collections; | |
| class Article | |
| { | |
| private int Artikelnummer; | |
| private string Name; | |
| private double Preis; | |
| // implementation of auto-increment: | |
| private static int actualArtikelnummer; | |
| // There is only a single instance of this variable | |
| // for the whole class, no matter how many Article-instances | |
| // are created. | |
| public Article() {} | |
| public Article(string Name, double Preis) | |
| { | |
| this.Name = Name; | |
| this.Preis = Preis; | |
| Artikelnummer = actualArtikelnummer; | |
| actualArtikelnummer++; | |
| } | |
| // NOTE: the following Getter and Setter-Methods can be | |
| // (auto-)created in a much shorter way: see "C# properties", e.g. | |
| // http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx or | |
| // http://msdn.microsoft.com/en-us/library/bb384054.aspx | |
| public int GetNR() | |
| { | |
| return Artikelnummer; | |
| } | |
| public double GetPrice() | |
| { | |
| return Preis; | |
| } | |
| public void SetPrice(double Preis) | |
| { | |
| this.Preis = Preis; | |
| } | |
| public string GetName() | |
| { | |
| return Name; | |
| } | |
| public void SetName(string Name) | |
| { | |
| this.Name = Name; | |
| } | |
| public virtual void Print() //virtual because it gets overridden! | |
| { | |
| Console.WriteLine("----------------------------------"); | |
| Console.WriteLine("Artikelnummer: " + Artikelnummer); | |
| Console.WriteLine(Name); | |
| Console.WriteLine("Preis: {0:0.00} Euro", Preis); | |
| } | |
| // returns new Price | |
| public double withTax(float p) | |
| { | |
| return (Preis / 100 * p) + Preis; | |
| } | |
| // adds Tax to price | |
| public void addTax(float p) | |
| { | |
| Preis = (Preis * p/100) + Preis; | |
| } | |
| public virtual void edit() | |
| { | |
| string answer; | |
| Console.WriteLine("----------------------------------"); | |
| Console.WriteLine("Editieren des Artikels mit Nr.: " + Artikelnummer); | |
| Console.WriteLine("Wollen Sie den Namen 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| // NOTE: the following lines of code are duplicated many times. | |
| // This should be avoided, e.g. by using a helper function for | |
| // checking the input answer. | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Namen ein!"); | |
| Name = Console.ReadLine(); | |
| } | |
| Console.WriteLine("Wollen Sie den Preis 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Preis ein!"); | |
| bool OK = false; | |
| while (!OK) | |
| { | |
| answer = Console.ReadLine(); | |
| OK = double.TryParse(answer, out Preis); | |
| if (!OK) Console.WriteLine("Geben Sie einen g黮tigen Preis ein! (#.00))"); | |
| } | |
| } | |
| } | |
| } | |
| class Book : Article | |
| { | |
| private string Autor; | |
| private string Verlag; | |
| public Book(string Name, double Preis, string Autor, string Verlag) : | |
| base(Name,Preis) //call constructor from base class (Article!) | |
| { | |
| this.Autor = Autor; | |
| this.Verlag = Verlag; | |
| } | |
| public override void Print() | |
| { | |
| base.Print(); // call Print() from base class (Article!) | |
| Console.WriteLine("Autor: " + Autor); | |
| Console.WriteLine("Verlag; " + Verlag); | |
| } | |
| public string GetAutor() | |
| { | |
| return Autor; | |
| } | |
| public string GetVerlag() | |
| { | |
| return Verlag; | |
| } | |
| public override void edit() | |
| { | |
| string answer; | |
| base.edit(); | |
| Console.WriteLine("Wollen Sie den Autor 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Autor ein!"); | |
| Autor = Console.ReadLine(); | |
| } | |
| Console.WriteLine("Wollen Sie den Verlag 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Verlag ein!"); | |
| Verlag = Console.ReadLine(); | |
| } | |
| } | |
| } | |
| class DVD : Article | |
| { | |
| private string Publisher; | |
| private string Regisseur; | |
| public DVD(string Name, double Preis, string Publisher, string Regisseur) : | |
| base(Name, Preis) | |
| { | |
| this.Publisher = Publisher; | |
| this.Regisseur = Regisseur; | |
| } | |
| public override void Print() | |
| { | |
| base.Print(); | |
| Console.WriteLine("Publisher: " + Publisher); | |
| Console.WriteLine("Regisseur: " + Regisseur); | |
| } | |
| public string GetPublisher() | |
| { | |
| return Publisher; | |
| } | |
| public string GetRegisseur() | |
| { | |
| return Regisseur; | |
| } | |
| public override void edit() | |
| { | |
| string answer; | |
| base.edit(); | |
| Console.WriteLine("Wollen Sie den Publisher 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Publisher ein!"); | |
| Publisher = Console.ReadLine(); | |
| } | |
| Console.WriteLine("Wollen Sie den Regisseur 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Regisseur ein!"); | |
| Regisseur = Console.ReadLine(); | |
| } | |
| } | |
| } | |
| class PC : Article | |
| { | |
| private string Hersteller; | |
| private string Betriebssystem; | |
| public PC(string Name, double Preis, string Hersteller, string Betriebssystem): base(Name, Preis) | |
| { | |
| this.Hersteller = Hersteller; | |
| this.Betriebssystem = Betriebssystem; | |
| } | |
| public override void Print() | |
| { | |
| base.Print(); | |
| Console.WriteLine("Hersteller: " + Hersteller); | |
| Console.WriteLine("Betriebssystem: " + Betriebssystem); | |
| } | |
| public string GetHersteller() | |
| { | |
| return Hersteller; | |
| } | |
| public string GetBetriebssystem() | |
| { | |
| return Betriebssystem; | |
| } | |
| public override void edit() | |
| { | |
| string answer; | |
| base.edit(); | |
| Console.WriteLine("Wollen Sie den Hersteller 鋘dern!"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie den neuen Hersteller ein!"); | |
| Hersteller = Console.ReadLine(); | |
| } | |
| Console.WriteLine("Wollen Sie das Betriebssystem 鋘dern?"); | |
| answer = Console.ReadLine(); | |
| if ( | |
| (answer == "Ja") || | |
| (answer == "ja") || | |
| (answer == "j") || | |
| (answer == "JA") | |
| ) | |
| { | |
| Console.WriteLine("Geben Sie das neue Betriebssystem ein!"); | |
| Betriebssystem = Console.ReadLine(); | |
| } | |
| } | |
| } | |
| class Store | |
| { | |
| // array for storing all kinds of different articles: | |
| private ArrayList articles; | |
| public Store() | |
| { | |
| articles = new ArrayList(); | |
| } | |
| public void addArticle(Article a) | |
| { | |
| articles.Add (a); | |
| } | |
| public Article findArticle(int artNr) | |
| { | |
| // NOTE: in the original version, "i < articles.Length" | |
| // checked, which leads to a null-reference exception, if | |
| // the array is not full!!! | |
| for (int i = 0; i < articles.Count; i++) | |
| { | |
| if (((Article)articles[i]).GetNR() == artNr) | |
| return (Article)articles[i]; | |
| } | |
| return null; | |
| } | |
| public void printAll() | |
| { | |
| //NOTE: see above | |
| for (int i = 0; i < articles.Count; i++) | |
| ((Article)articles[i]).Print(); | |
| } | |
| public void addTaxes(float p) | |
| { | |
| //NOTE: see above | |
| for (int i = 0; i < articles.Count; i++) | |
| { | |
| ((Article)articles[i]).addTax(p); // delegate the taxation to the article | |
| // NOTE: the original code does the following instead... | |
| /* | |
| double price = articles[i].GetPrice(); | |
| articles[i].SetPrice(price + (price / 100 * p)); | |
| */ | |
| //... which is code duplication and should be avoided. | |
| } | |
| } | |
| public double computeTotalPrice() | |
| { | |
| double totalprice = 0; | |
| //NOTE: see above | |
| for (int i = 0; i < articles.Count; i++) | |
| totalprice += ((Article)articles[i]).GetPrice(); | |
| return totalprice; | |
| } | |
| // Aufgabe 1. Artikel/Store | |
| // a) Zählermethoden | |
| public uint countBooks() | |
| { | |
| uint numOfBooks = 0; | |
| for (int i = 0; i < articles.Count; i++) { | |
| if (articles[i].GetType() == typeof(Book)) { | |
| numOfBooks++; | |
| } | |
| } | |
| return numOfBooks; | |
| } | |
| public uint countDVDs() | |
| { | |
| uint numOfDVD = 0; | |
| for (int i = 0; i < articles.Count; i++) { | |
| if (articles[i].GetType() == typeof(DVD)) { | |
| numOfDVD++; | |
| } | |
| } | |
| return numOfDVD; | |
| } | |
| public uint countPC() | |
| { | |
| uint numOfPC = 0; | |
| for (int i = 0; i < articles.Count; i++) { | |
| if (articles[i].GetType() == typeof(PC)) { | |
| numOfPC++; | |
| } | |
| } | |
| return numOfPC; | |
| } | |
| public uint countArticle() | |
| { | |
| uint numOfArticles = 0; | |
| for (int i = 0; i < articles.Count; i++) { | |
| if (articles[i].GetType() == typeof(Article)) { | |
| numOfArticles++; | |
| } | |
| } | |
| return numOfArticles; | |
| } | |
| // b) Statistic | |
| public void printStatistics() | |
| { | |
| int gesamtartikel = articles.Count; | |
| // Ausgabe für verschiedene Zähler | |
| Console.WriteLine ("\n-----------------------------------------------------------------------------" + | |
| "\nAnzahl Bücher: " + ((double) 100 / gesamtartikel * countBooks() + " %")); | |
| Console.WriteLine ("\n-----------------------------------------------------------------------------" + | |
| "\nAnzahl DVDs: " + ((double) 100 / gesamtartikel * countDVDs() + " %")); | |
| Console.WriteLine ("\n-----------------------------------------------------------------------------" + | |
| "\nAnzahl PC: " + ((double) 100 / gesamtartikel * countPC() + " %")); | |
| Console.WriteLine ("\n-----------------------------------------------------------------------------" + | |
| "\nAnzahl Artikel: " + ((double) 100 / gesamtartikel * countArticle() + " %")); | |
| } | |
| // Ende | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Article eins = new Article("Irgendwas", 10.50); | |
| Article zwei = new Article("NochWasAnderes", 20); | |
| Book book = new Book("Buch", 10.50, "Autor", "Verlag"); | |
| DVD DvD = new DVD("Film", 20.99, "Herr Publisher", "Mr. Regisseur"); | |
| //Note: this is also possible: | |
| Article Computer = new PC("PC", 100.50, "Dell", "Windows 8"); | |
| // eins.Print(); | |
| // zwei.Print(); | |
| // book.Print(); | |
| // DvD.Print(); | |
| // Computer.Print(); | |
| // | |
| // eins.edit(); | |
| // book.edit(); | |
| // DvD.edit(); | |
| // Computer.edit(); | |
| Store store = new Store(); | |
| store.addArticle(eins); | |
| store.addArticle(book); | |
| store.addArticle(Computer); | |
| store.addArticle(zwei); | |
| Console.WriteLine( | |
| "Gesamtpreis der gespeicherten Artikel: " + | |
| store.computeTotalPrice() | |
| ); | |
| Console.WriteLine(); | |
| store.printAll(); | |
| Console.WriteLine("---------------------------"); | |
| // NOTE: the original code does not check whether | |
| // the searched article exists! | |
| Article searchedArticle = store.findArticle(2); | |
| if (searchedArticle != null) { | |
| Console.WriteLine("Artikel gefunden!"); | |
| searchedArticle.Print(); | |
| } | |
| else { | |
| Console.WriteLine("Artikel nicht gefunden!"); | |
| } | |
| Console.WriteLine("------------------------"); | |
| Console.WriteLine("Brutto-Preis von Artikel 0: " + eins.withTax(20)); | |
| Console.WriteLine( | |
| "Gesamtpreis der gespeicherten Artikel: " + | |
| store.computeTotalPrice() | |
| ); | |
| Console.WriteLine("20% Steuer wird nun 黚erall aufgeschlagen."); | |
| store.addTaxes(20); | |
| Console.WriteLine( | |
| "Gesamtpreis nach Steueraufschlag: " + | |
| store.computeTotalPrice() | |
| ); | |
| // Aufrufe für Aufgabe b) | |
| store.printStatistics (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment