public abstract class Medium { private static long idNrCounter = 0; // ID-Nr. des nächsten Buches bzw. Anzahl bisher erzeugter Bücher // ‾‾‾‾‾‾ -> statisch, d. h. für alle Bücher gemeinsam nur einmal existent private int preis; private final String titel; private final long idNr; // ‾‾‾‾‾ -> final, d. h. nach Zuweisung (nur im Konstruktor möglich) nicht mehr änderbar public Medium(String titel, int price) { this.titel = titel; setPreis(price); idNr = idNrCounter; idNrCounter++; } // Getter-Methoden (für alle Attribute, die in DVD und Book vorkommen): public long getIdNr() { return idNr; } public String getTitel() { return titel; } public int getPreis() { return preis; } // Setter-Methoden (nur für veränderliche Attribute): public void setPreis(int preis) { if (preis < 0) // negativer Preis ist nicht erlaubt throw new IllegalArgumentException(); this.preis = preis; } // Weitere Methoden (die in DVD und Book gleich sind und zusammengefasst werden können): // b) public boolean equals(Object obj) { if (obj instanceof Medium) { Medium b = (Medium) obj; // obj zu Medium casten, dadurch b.idNr möglich return b.getIdNr() == this.getIdNr(); } else { // deckt auch obj == null ab return false; } } // c) public void increasePrice(float euro) { int preisNeu = (int) (preis + euro * 100); setPreis(preisNeu); // prüft neuen Preis auf Gültigkeit } }