-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestellung.java
178 lines (134 loc) · 3.42 KB
/
Bestellung.java
1
/** Beispiel fuer Java-Datenstrukturen * "Bestellung" zur Vorlesung "Softwaretechnologie". * (c) Heinrich Hussmann, Uwe Aßmann, TU Dresden, 2013 * Diese Version des Beispiels "Bestellung" arbeitet mit * einer selbstdefinierten Implementierung von Collection. * Der Einfachheit halber wurden die Operationen entfernt, die * das spezifische List-Interface benutzen. * @Version 2.0 Generics */import java.util.Collection;import java.util.Iterator;import java.util.AbstractCollection;class MyCollection extends AbstractCollection implements Collection { private class Elem { private Object elem; private Elem next; public Elem (Object elem, Elem next) { this.elem = elem; this.next = next; } } private Elem start; private Elem end; private int size; public boolean add (Object o) { Elem e = new Elem(o,null); if (end != null) end.next = e; if (start == null) start = e; end = e; size++; return true; } public int size() { return size; } private class MyCollectionIterator implements Iterator { private Elem current; private Elem removeRef; public MyCollectionIterator() { current = start; } public boolean hasNext() { return current != null; } public Object next() { Object o = current.elem; current = current.next; return o; } public void remove() { throw new UnsupportedOperationException(); } } public Iterator iterator() { return new MyCollectionIterator(); }}class Artikel { private String name; private int preis; public Artikel (String name, int preis) { this.name = name; this.preis = preis; } public int preis() { return preis; } public String toString() { return name; }}class Bestellposition { private Artikel artikel; private int anzahl; private int preis; // Standard: Artikelpreis, aber Sonderpreise moeglich public Bestellposition (Artikel artikel, int anzahl) { this.artikel = artikel; this.anzahl = anzahl; this.preis = artikel.preis(); } public int einzelpreis() { return preis; } public void einzelpreis (int sonderpreis) { preis = sonderpreis; } public int positionspreis() { return preis*anzahl; } public String toString() { return anzahl+" x "+artikel+" Einzelpreis: "+preis+" Summe: "+positionspreis(); }}class Bestellung { private String kunde; private Collection liste; public Bestellung(String kunde) { this.kunde = kunde; liste = new MyCollection(); } public void neuePosition (Bestellposition b) { liste.add(b); } public int auftragssumme() { Iterator i = liste.iterator(); int s = 0; while (i.hasNext()) s += ((Bestellposition)i.next()).positionspreis(); return s; } public void print () { System.out.println("Bestellung fuer Kunde "+kunde); Iterator i = liste.iterator(); int pos = 0; while (i.hasNext()) { System.out.println(pos+". "+i.next()); pos++; } System.out.println("Auftragssumme: "+auftragssumme()); System.out.println(); } public static void main (String[] args) { Artikel tisch = new Artikel("Tisch",200); Artikel stuhl = new Artikel("Stuhl",100); Artikel schrank = new Artikel("Schrank",300); Bestellung4 b1 = new Bestellung("TUD"); b1.neuePosition(new Bestellposition(tisch,1)); b1.neuePosition(new Bestellposition(stuhl,4)); b1.neuePosition(new Bestellposition(schrank,2)); b1.print(); }}