uebung07
This commit is contained in:
parent
701e732eb6
commit
1b07a3b6ab
@ -58,6 +58,12 @@ public class CashPoint implements SecurityClient {
|
||||
|
||||
void charge(AccountCard accountCard) {
|
||||
final int price = getPrice(accountCard.color);
|
||||
// zahlen methode
|
||||
/*
|
||||
|
||||
zahlen bein acmgmt
|
||||
|
||||
*/
|
||||
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
|
||||
cents += price;
|
||||
}
|
||||
|
22
uebung07/src/quantities/generic/Length.java
Normal file
22
uebung07/src/quantities/generic/Length.java
Normal file
@ -0,0 +1,22 @@
|
||||
package quantities.generic;
|
||||
|
||||
import static quantities.generic.LengthUnit.METER;
|
||||
import static quantities.generic.TimeUnit.SECOND;
|
||||
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Length extends Quantity<Length>{
|
||||
|
||||
public Length(double value, Unit<Length> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Velocity div(Time t) {
|
||||
return METER_PER_SECOND.quantity(this.value(METER) / t.value(SECOND));
|
||||
}
|
||||
|
||||
public Time div(Velocity v) {
|
||||
return SECOND.quantity(this.value(METER) / v.value(METER_PER_SECOND));
|
||||
}
|
||||
|
||||
|
||||
}
|
19
uebung07/src/quantities/generic/LengthUnit.java
Normal file
19
uebung07/src/quantities/generic/LengthUnit.java
Normal file
@ -0,0 +1,19 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class LengthUnit extends Unit<Length> {
|
||||
public LengthUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final LengthUnit METER = new LengthUnit("m", 1);
|
||||
public static final LengthUnit MILLIMETER = new LengthUnit("mm", 0.001);
|
||||
public static final LengthUnit KILOMETER = new LengthUnit("km", 1000);
|
||||
public static final LengthUnit MILE = new LengthUnit("mi", 1609.344);
|
||||
public static final LengthUnit LIGHTYEAR = new LengthUnit("ly", 9460730472580800.0);
|
||||
public static final LengthUnit PARSEC = new LengthUnit("pc", 3.0856776e16);
|
||||
|
||||
@Override
|
||||
public Length quantity(double value) {
|
||||
return new Length(value, this);
|
||||
}
|
||||
}
|
55
uebung07/src/quantities/generic/Quantity.java
Normal file
55
uebung07/src/quantities/generic/Quantity.java
Normal file
@ -0,0 +1,55 @@
|
||||
package quantities.generic;
|
||||
|
||||
public abstract class Quantity<Q extends Quantity<Q>> {
|
||||
public final double value;
|
||||
public final Unit<Q> unit;
|
||||
|
||||
protected Quantity(double value, Unit<Q> unit) {
|
||||
this.value = value;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public double getBaseValue() {
|
||||
return value * unit.baseFactor;
|
||||
}
|
||||
|
||||
public double value(Unit<Q> unit) {
|
||||
return getBaseValue() / unit.baseFactor;
|
||||
}
|
||||
|
||||
//neue Methoden
|
||||
|
||||
public Q plus(Q other) {
|
||||
return unit.quantity(value + other.getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public Q minus(Q other) {
|
||||
return unit.quantity(value - other.getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public Q mult(double f) {
|
||||
return unit.quantity(value * f);
|
||||
}
|
||||
|
||||
public Q div(double f) {
|
||||
return unit.quantity(value / f);
|
||||
}
|
||||
|
||||
public Q to(Unit<Q> unit) {
|
||||
return unit.quantity(getBaseValue() / unit.baseFactor);
|
||||
}
|
||||
|
||||
public double div(Q other) {
|
||||
return getBaseValue() / other.getBaseValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value + " " + unit;
|
||||
}
|
||||
|
||||
public String format(String fmt, Unit unit) {
|
||||
return String.format(fmt, value(unit), unit);
|
||||
}
|
||||
}
|
11
uebung07/src/quantities/generic/Time.java
Normal file
11
uebung07/src/quantities/generic/Time.java
Normal file
@ -0,0 +1,11 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class Time extends Quantity<Time> {
|
||||
public Time(double value, Unit<Time> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Length mult(Velocity v) {
|
||||
return v.mult(this);
|
||||
}
|
||||
}
|
17
uebung07/src/quantities/generic/TimeUnit.java
Normal file
17
uebung07/src/quantities/generic/TimeUnit.java
Normal file
@ -0,0 +1,17 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class TimeUnit extends Unit<Time> {
|
||||
public TimeUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time quantity(double value) {
|
||||
return new Time(value, this);
|
||||
}
|
||||
|
||||
public static final TimeUnit SECOND = new TimeUnit("s", 1);
|
||||
public static final TimeUnit MINUTE = new TimeUnit("min", 60);
|
||||
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
|
||||
public static final TimeUnit DAY = new TimeUnit("d", 86400);
|
||||
}
|
18
uebung07/src/quantities/generic/Unit.java
Normal file
18
uebung07/src/quantities/generic/Unit.java
Normal file
@ -0,0 +1,18 @@
|
||||
package quantities.generic;
|
||||
|
||||
public abstract class Unit<T extends Quantity<T>> {
|
||||
public final String name;
|
||||
public final double baseFactor;
|
||||
|
||||
public Unit(String name, double baseFactor) {
|
||||
this.name = name;
|
||||
this.baseFactor = baseFactor;
|
||||
}
|
||||
|
||||
public abstract T quantity(double value);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
15
uebung07/src/quantities/generic/Velocity.java
Normal file
15
uebung07/src/quantities/generic/Velocity.java
Normal file
@ -0,0 +1,15 @@
|
||||
package quantities.generic;
|
||||
|
||||
import static quantities.generic.LengthUnit.METER;
|
||||
import static quantities.generic.TimeUnit.SECOND;
|
||||
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Velocity extends Quantity<Velocity> {
|
||||
public Velocity(double value, Unit<Velocity> unit) {
|
||||
super(value, unit);
|
||||
}
|
||||
|
||||
public Length mult(Time t) {
|
||||
return METER.quantity(this.value(METER_PER_SECOND) * t.value(SECOND));
|
||||
}
|
||||
}
|
16
uebung07/src/quantities/generic/VelocityUnit.java
Normal file
16
uebung07/src/quantities/generic/VelocityUnit.java
Normal file
@ -0,0 +1,16 @@
|
||||
package quantities.generic;
|
||||
|
||||
public class VelocityUnit extends Unit<Velocity> {
|
||||
public VelocityUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Velocity quantity(double value) {
|
||||
return new Velocity(value, this);
|
||||
}
|
||||
|
||||
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("m/s", 1);
|
||||
public static final VelocityUnit KMH = new VelocityUnit("km/h", 1.0 / 3.6);
|
||||
public static final VelocityUnit MPH = new VelocityUnit("mph", 0.44704);
|
||||
}
|
34
uebung07/src/quantities/plain/Time.java
Normal file
34
uebung07/src/quantities/plain/Time.java
Normal file
@ -0,0 +1,34 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class Time extends Quantity{
|
||||
private final TimeUnit unit;
|
||||
|
||||
public Time(double value, TimeUnit unit) {
|
||||
super(value, unit);
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public Time plus(Time other) {
|
||||
return new Time(value + other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Time minus(Time other) {
|
||||
return new Time(value - other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Time mult(double f) {
|
||||
return new Time(value * f, unit);
|
||||
}
|
||||
|
||||
public Time div(double f) {
|
||||
return new Time(value / f, unit);
|
||||
}
|
||||
|
||||
public Time to(TimeUnit unit) {
|
||||
return new Time(getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Length mult(Velocity v) {
|
||||
return v.mult(this);
|
||||
}
|
||||
}
|
13
uebung07/src/quantities/plain/TimeUnit.java
Normal file
13
uebung07/src/quantities/plain/TimeUnit.java
Normal file
@ -0,0 +1,13 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class TimeUnit extends Unit{
|
||||
|
||||
public TimeUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final TimeUnit SECOND = new TimeUnit("s", 1);
|
||||
public static final TimeUnit MINUTE = new TimeUnit("m", 60);
|
||||
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
|
||||
|
||||
}
|
38
uebung07/src/quantities/plain/Velocity.java
Normal file
38
uebung07/src/quantities/plain/Velocity.java
Normal file
@ -0,0 +1,38 @@
|
||||
package quantities.plain;
|
||||
|
||||
import static quantities.plain.LengthUnit.METER;
|
||||
import static quantities.plain.TimeUnit.SECOND;
|
||||
import static quantities.plain.VelocityUnit.METER_PER_SECOND;
|
||||
|
||||
public class Velocity extends Quantity {
|
||||
private final VelocityUnit unit;
|
||||
|
||||
public Velocity(double value, VelocityUnit unit) {
|
||||
super(value, unit);
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public Velocity plus(Velocity other) {
|
||||
return new Velocity(value + other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Velocity minus(Velocity other) {
|
||||
return new Velocity(value - other.getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Velocity mult(double f) {
|
||||
return new Velocity(value * f, unit);
|
||||
}
|
||||
|
||||
public Velocity div(double f) {
|
||||
return new Velocity(value / f, unit);
|
||||
}
|
||||
|
||||
public Velocity to(VelocityUnit unit) {
|
||||
return new Velocity(getBaseValue() / unit.baseFactor, unit);
|
||||
}
|
||||
|
||||
public Length mult(Time t) {
|
||||
return new Length(this.value(METER_PER_SECOND) * t.value(SECOND), METER);
|
||||
}
|
||||
}
|
13
uebung07/src/quantities/plain/VelocityUnit.java
Normal file
13
uebung07/src/quantities/plain/VelocityUnit.java
Normal file
@ -0,0 +1,13 @@
|
||||
package quantities.plain;
|
||||
|
||||
public class VelocityUnit extends Unit{
|
||||
|
||||
public VelocityUnit(String name, double baseFactor) {
|
||||
super(name, baseFactor);
|
||||
}
|
||||
|
||||
public static final VelocityUnit KMH = new VelocityUnit("kmh", 1000/3600); // 1/3.6
|
||||
public static final VelocityUnit MPH = new VelocityUnit("mph", 1609.344/3600);
|
||||
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("meter per second", 1);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user