initial commit
This commit is contained in:
parent
d9eaae7b7a
commit
38287f8c0b
BIN
bin/uebung07/quantities/plain/Length.class
Normal file
BIN
bin/uebung07/quantities/plain/Length.class
Normal file
Binary file not shown.
BIN
bin/uebung07/quantities/plain/LengthUnit.class
Normal file
BIN
bin/uebung07/quantities/plain/LengthUnit.class
Normal file
Binary file not shown.
BIN
bin/uebung07/quantities/plain/PlainQuantitiesDemo.class
Normal file
BIN
bin/uebung07/quantities/plain/PlainQuantitiesDemo.class
Normal file
Binary file not shown.
BIN
bin/uebung07/quantities/plain/Quantity.class
Normal file
BIN
bin/uebung07/quantities/plain/Quantity.class
Normal file
Binary file not shown.
BIN
bin/uebung07/quantities/plain/Unit.class
Normal file
BIN
bin/uebung07/quantities/plain/Unit.class
Normal file
Binary file not shown.
46
src/uebung07/quantities/plain/Length.java
Normal file
46
src/uebung07/quantities/plain/Length.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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 Length extends Quantity {
|
||||||
|
private final LengthUnit unit;
|
||||||
|
|
||||||
|
public Length(double value, LengthUnit unit) {
|
||||||
|
super(value, unit);
|
||||||
|
this.unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Length plus(Length other) {
|
||||||
|
return new Length(value + other.getBaseValue() / unit.baseFactor, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Length minus(Length other) {
|
||||||
|
return new Length(value - other.getBaseValue() / unit.baseFactor, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Length mult(double f) {
|
||||||
|
return new Length(value * f, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Length div(double f) {
|
||||||
|
return new Length(value / f, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Length to(LengthUnit unit) {
|
||||||
|
return new Length(getBaseValue() / unit.baseFactor, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double div(Length other) {
|
||||||
|
return getBaseValue() / other.getBaseValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Velocity div(Time t) {
|
||||||
|
return new Velocity(this.value(METER) / t.value(SECOND), METER_PER_SECOND);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Time div(Velocity v) {
|
||||||
|
return new Time(this.value(METER) / v.value(METER_PER_SECOND), SECOND);
|
||||||
|
}
|
||||||
|
}
|
14
src/uebung07/quantities/plain/LengthUnit.java
Normal file
14
src/uebung07/quantities/plain/LengthUnit.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package quantities.plain;
|
||||||
|
|
||||||
|
public class LengthUnit extends Unit {
|
||||||
|
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);
|
||||||
|
}
|
60
src/uebung07/quantities/plain/PlainQuantitiesDemo.java
Normal file
60
src/uebung07/quantities/plain/PlainQuantitiesDemo.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package uebung07.quantities.plain;
|
||||||
|
|
||||||
|
import static uebung07.quantities.plain.LengthUnit.KILOMETER;
|
||||||
|
import static uebung07.quantities.plain.LengthUnit.MILE;
|
||||||
|
import static uebung07.quantities.plain.LengthUnit.MILLIMETER;
|
||||||
|
import static uebung07.quantities.plain.LengthUnit.PARSEC;
|
||||||
|
import static uebung07.quantities.plain.TimeUnit.HOUR;
|
||||||
|
import static uebung07.quantities.plain.TimeUnit.MINUTE;
|
||||||
|
import static uebung07.quantities.plain.TimeUnit.SECOND;
|
||||||
|
import static uebung07.quantities.plain.VelocityUnit.KMH;
|
||||||
|
import static uebung07.quantities.plain.VelocityUnit.METER_PER_SECOND;
|
||||||
|
import static uebung07.quantities.plain.VelocityUnit.MPH;
|
||||||
|
|
||||||
|
public class PlainQuantitiesDemo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final Length l1 = new Length(1, KILOMETER);
|
||||||
|
final Length l2 = new Length(1200, MILLIMETER);
|
||||||
|
final Length l3 = new Length(1, MILE);
|
||||||
|
|
||||||
|
System.out.println(l1);
|
||||||
|
System.out.println(l2);
|
||||||
|
System.out.println(l1 + " + " + l2 + " = " + l1.plus(l2));
|
||||||
|
System.out.println(l1 + " + " + l2 + " (in mm) = " + l1.plus(l2).to(MILLIMETER));
|
||||||
|
|
||||||
|
System.out.println(l3 + " / " + l1 + " = " + l3.div(l1));
|
||||||
|
|
||||||
|
final Time t1 = new Time(100, SECOND);
|
||||||
|
final Time t2 = new Time(5, HOUR);
|
||||||
|
|
||||||
|
System.out.println(t1);
|
||||||
|
System.out.println(t2);
|
||||||
|
System.out.println(t1.plus(t2));
|
||||||
|
System.out.println(t1.plus(t2).to(MINUTE));
|
||||||
|
|
||||||
|
final Velocity v1 = new Velocity(12, KMH);
|
||||||
|
final Velocity v2 = new Velocity(100, METER_PER_SECOND);
|
||||||
|
|
||||||
|
System.out.println(v1);
|
||||||
|
System.out.println(v2);
|
||||||
|
System.out.println(v2.to(KMH));
|
||||||
|
System.out.println(v1.plus(v2));
|
||||||
|
|
||||||
|
final Length l4 = new Length(300, KILOMETER).to(PARSEC);
|
||||||
|
final Time t3 = new Time(2, HOUR);
|
||||||
|
final Velocity v3 = l4.div(t3);
|
||||||
|
System.out.println(l4 + " / " + l3 + " = " + v3);
|
||||||
|
|
||||||
|
System.out.println(v1 + " * " + t1 + " = " + v1.mult(t1).to(KILOMETER));
|
||||||
|
|
||||||
|
final Length l5 = v3.mult(t1.to(HOUR));
|
||||||
|
System.out.println(v3 + " * " + t1 + " = " + l5);
|
||||||
|
|
||||||
|
final Time t5 = l4.div(v2);
|
||||||
|
System.out.println(l4 + " / " + v2 + " = " + t5.to(MINUTE));
|
||||||
|
|
||||||
|
Velocity v5 = new Velocity(55, MPH);
|
||||||
|
System.out.println(v5 + " = " + v5.format("%4.1f %s", KMH));
|
||||||
|
System.out.println((v5.mult(new Time(30, MINUTE)).to(MILE)));
|
||||||
|
}
|
||||||
|
}
|
28
src/uebung07/quantities/plain/Quantity.java
Normal file
28
src/uebung07/quantities/plain/Quantity.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package uebung07.quantities.plain;
|
||||||
|
|
||||||
|
public abstract class Quantity {
|
||||||
|
public final double value;
|
||||||
|
public final Unit unit;
|
||||||
|
|
||||||
|
protected Quantity(double value, Unit unit) {
|
||||||
|
this.value = value;
|
||||||
|
this.unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBaseValue() {
|
||||||
|
return value * unit.baseFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double value(Unit unit) {
|
||||||
|
return getBaseValue() / unit.baseFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value + " " + unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String format(String fmt, Unit unit) {
|
||||||
|
return String.format(fmt, value(unit), unit);
|
||||||
|
}
|
||||||
|
}
|
16
src/uebung07/quantities/plain/Unit.java
Normal file
16
src/uebung07/quantities/plain/Unit.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package uebung07.quantities.plain;
|
||||||
|
|
||||||
|
public abstract class Unit {
|
||||||
|
public final String name;
|
||||||
|
public final double baseFactor;
|
||||||
|
|
||||||
|
public Unit(String name, double baseFactor) {
|
||||||
|
this.name = name;
|
||||||
|
this.baseFactor = baseFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
80
test/oop/ch05/generic/mensa/MensaTest.java
Normal file
80
test/oop/ch05/generic/mensa/MensaTest.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package oop.ch05.generic.mensa;
|
||||||
|
|
||||||
|
import oop.ch05.generic.secured.AuthorizationException;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
public class MensaTest {
|
||||||
|
private static final String CONF_ACCOUNT = "33-1298";
|
||||||
|
private static final String OTHER_ACCOUNT = "33-1299";
|
||||||
|
private AccountManagement accountMgt;
|
||||||
|
private VendingMachine vm1;
|
||||||
|
private VendingMachine vm2;
|
||||||
|
private VendingMachine tumVM;
|
||||||
|
private CashPoint unibwMensa;
|
||||||
|
|
||||||
|
private AccountCard conf;
|
||||||
|
private MensaCard frankSmith;
|
||||||
|
private CashCard hansMueller;
|
||||||
|
private CashCard peterSchmidt;
|
||||||
|
private CashCard thomasMayer;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
accountMgt = new AccountManagement(4711, "UniBw admin");
|
||||||
|
|
||||||
|
vm1 = new VendingMachine("left", 4711);
|
||||||
|
vm2 = new VendingMachine("right", 4711);
|
||||||
|
tumVM = new VendingMachine("TUM Mensa", 3141);
|
||||||
|
unibwMensa = new CashPoint("UniBw Mensa", 4711, accountMgt);
|
||||||
|
|
||||||
|
conf = new AccountCard("conference", CONF_ACCOUNT, 42);
|
||||||
|
frankSmith = new CountCard("Frank Smith", Color.gray);
|
||||||
|
hansMueller = new CashCard("Hans Müller", Color.green, 4711);
|
||||||
|
peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711);
|
||||||
|
thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPayment() throws AuthorizationException, RejectedException {
|
||||||
|
vm1.deposit(hansMueller, 10);
|
||||||
|
vm1.deposit(peterSchmidt, 5);
|
||||||
|
vm2.deposit(thomasMayer, 2);
|
||||||
|
assertThrows(AuthorizationException.class, () -> tumVM.deposit(hansMueller, 10));
|
||||||
|
|
||||||
|
assertEquals(15, vm1.getEuros());
|
||||||
|
assertEquals(2, vm2.getEuros());
|
||||||
|
assertEquals(0, tumVM.getEuros());
|
||||||
|
assertEquals(1000, hansMueller.getBalance());
|
||||||
|
assertEquals(500, peterSchmidt.getBalance());
|
||||||
|
assertEquals(200, thomasMayer.getBalance());
|
||||||
|
|
||||||
|
hansMueller.pass(unibwMensa);
|
||||||
|
assertEquals(733, hansMueller.getBalance());
|
||||||
|
assertEquals(1, unibwMensa.getCounter());
|
||||||
|
assertEquals(267, unibwMensa.getCents());
|
||||||
|
|
||||||
|
frankSmith.pass(unibwMensa);
|
||||||
|
assertEquals(0, accountMgt.getAmount(CONF_ACCOUNT));
|
||||||
|
assertThrows(RejectedException.class, () -> conf.pass(unibwMensa));
|
||||||
|
assertThrows(RejectedException.class, () -> thomasMayer.pass(unibwMensa));
|
||||||
|
hansMueller.pass(unibwMensa);
|
||||||
|
hansMueller.pass(unibwMensa);
|
||||||
|
assertEquals(199, hansMueller.getBalance());
|
||||||
|
|
||||||
|
assertThrows(RejectedException.class, () -> hansMueller.pass(unibwMensa));
|
||||||
|
|
||||||
|
accountMgt.deposit(CONF_ACCOUNT, 1000);
|
||||||
|
accountMgt.deposit(OTHER_ACCOUNT, 2000);
|
||||||
|
assertEquals(100000, accountMgt.getAmount(CONF_ACCOUNT));
|
||||||
|
assertEquals(200000, accountMgt.getAmount(OTHER_ACCOUNT));
|
||||||
|
|
||||||
|
conf.pass(unibwMensa);
|
||||||
|
assertEquals(99505, accountMgt.getAmount(CONF_ACCOUNT));
|
||||||
|
assertEquals(200000, accountMgt.getAmount(OTHER_ACCOUNT));
|
||||||
|
assertEquals(5, unibwMensa.getCounter());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user