Alternative for 5.2
parent
cfedacde33
commit
9dcf307707
@ -0,0 +1,62 @@
|
|||||||
|
package _5._2.alternative.bill;
|
||||||
|
|
||||||
|
import _5._2.alternative.car.CarComponent;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* final damit keiner ein BillItem nach draußen gibt
|
||||||
|
* und es später wieder als Argument nimmt.
|
||||||
|
*/
|
||||||
|
public final class Bill {
|
||||||
|
private List<BillItem> items = new LinkedList<>();
|
||||||
|
|
||||||
|
public double getTotalPrice() {
|
||||||
|
double sum = 0.0;
|
||||||
|
for(BillItem item : this.items) {
|
||||||
|
sum += item.getPrice();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(CarComponent item, double price) {
|
||||||
|
return this.items.add(new BillItem(item, price));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
double total = 0.0;
|
||||||
|
for(BillItem item : this.items) {
|
||||||
|
sb.append(item.toString()).append('\n');
|
||||||
|
total += item.getPrice();
|
||||||
|
}
|
||||||
|
sb.append("Total: ").append(total);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
//inner class und private constructor damit eine Instanz nur als Teil einer Rechnung existieren kann.
|
||||||
|
public class BillItem {
|
||||||
|
private final CarComponent item;
|
||||||
|
private final double price;
|
||||||
|
|
||||||
|
private BillItem(CarComponent item, double price) {
|
||||||
|
//assert item != null;
|
||||||
|
this.item = item;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return this.price;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if(this.item.getComponents().isEmpty()) {
|
||||||
|
return this.item.getName() + ": " + this.price;
|
||||||
|
}
|
||||||
|
return this.item.getName() + this.item.getComponents() + ": " + this.price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package _5._2.alternative.bill;
|
||||||
|
|
||||||
|
import _5._2.alternative.car.Motor;
|
||||||
|
import _5._2.alternative.car.Seat;
|
||||||
|
import _5._2.alternative.car.Wheel;
|
||||||
|
|
||||||
|
public class BillExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Bill bill = new Bill();
|
||||||
|
bill.add(new Motor("Rolls Royce (Motor)"), 100000.0);
|
||||||
|
bill.add(new Seat(), 2000.0);
|
||||||
|
bill.add(new Wheel(), 1000.0);
|
||||||
|
bill.add(new Wheel(), 1000.0);
|
||||||
|
bill.add(new Wheel(), 1000.0);
|
||||||
|
bill.add(new Wheel(), 1000.0);
|
||||||
|
System.out.println(bill);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
package _5._2.alternative.car;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Car implements CarComponent {
|
||||||
|
private String name;
|
||||||
|
private final Set<CarComponent> components = new HashSet<>();
|
||||||
|
|
||||||
|
public Car(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<CarComponent> getComponents() {
|
||||||
|
return this.components;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if(this.components.isEmpty()) {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
return this.name + ": " + this.components;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract static class CarPart implements CarComponent {
|
||||||
|
private String name;
|
||||||
|
private final Set<CarComponent> components = new HashSet<>();
|
||||||
|
|
||||||
|
public CarPart(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<CarComponent> getComponents() {
|
||||||
|
return this.components;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if(this.components.isEmpty()) {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
return this.name + ": " + this.components;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package _5._2.alternative.car;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ein Interface hat keinen state.
|
||||||
|
* Es kann also auch keine Anforderungen an seinen state stellen.
|
||||||
|
* Um alle geforderten Multiplizitäten sicherzustellen müsste man
|
||||||
|
* sich daher auf jeden der dies implementiert verlassen,
|
||||||
|
* dass er sich an die Vereinbarung hällt.
|
||||||
|
*/
|
||||||
|
public interface CarComponent {
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
Collection<CarComponent> getComponents();
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package _5._2.alternative.car;
|
||||||
|
|
||||||
|
public class Motor extends Car.CarPart {
|
||||||
|
public Motor() {
|
||||||
|
this("Motor");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Motor(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package _5._2.alternative.car;
|
||||||
|
|
||||||
|
public class Seat extends Car.CarPart {
|
||||||
|
public Seat() {
|
||||||
|
this("Seat");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Seat(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package _5._2.alternative.car;
|
||||||
|
|
||||||
|
public class Wheel extends Car.CarPart {
|
||||||
|
public Wheel() {
|
||||||
|
this("Wheel");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Wheel(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue