From 38287f8c0b83a45d3aad48f5af40270a6c600b33 Mon Sep 17 00:00:00 2001 From: peet Date: Thu, 6 Jun 2024 17:27:35 +0200 Subject: [PATCH] initial commit --- bin/uebung07/quantities/plain/Length.class | Bin 0 -> 2704 bytes .../quantities/plain/LengthUnit.class | Bin 0 -> 643 bytes .../plain/PlainQuantitiesDemo.class | Bin 0 -> 1554 bytes bin/uebung07/quantities/plain/Quantity.class | Bin 0 -> 1730 bytes bin/uebung07/quantities/plain/Unit.class | Bin 0 -> 532 bytes src/uebung07/quantities/plain/Length.java | 46 ++++++++++ src/uebung07/quantities/plain/LengthUnit.java | 14 +++ .../quantities/plain/PlainQuantitiesDemo.java | 60 +++++++++++++ src/uebung07/quantities/plain/Quantity.java | 28 ++++++ src/uebung07/quantities/plain/Unit.java | 16 ++++ test/oop/ch05/generic/mensa/MensaTest.java | 80 ++++++++++++++++++ 11 files changed, 244 insertions(+) create mode 100644 bin/uebung07/quantities/plain/Length.class create mode 100644 bin/uebung07/quantities/plain/LengthUnit.class create mode 100644 bin/uebung07/quantities/plain/PlainQuantitiesDemo.class create mode 100644 bin/uebung07/quantities/plain/Quantity.class create mode 100644 bin/uebung07/quantities/plain/Unit.class create mode 100644 src/uebung07/quantities/plain/Length.java create mode 100644 src/uebung07/quantities/plain/LengthUnit.java create mode 100644 src/uebung07/quantities/plain/PlainQuantitiesDemo.java create mode 100644 src/uebung07/quantities/plain/Quantity.java create mode 100644 src/uebung07/quantities/plain/Unit.java create mode 100644 test/oop/ch05/generic/mensa/MensaTest.java diff --git a/bin/uebung07/quantities/plain/Length.class b/bin/uebung07/quantities/plain/Length.class new file mode 100644 index 0000000000000000000000000000000000000000..d013456760c52c71b2ddbd6a56f8bcdc11e03449 GIT binary patch literal 2704 zcmds(ZBNrs6vxl4W8KPh3L+{fcSB$UWxg;mamK{3@xl~T7$m+Tciqi)wNJLUi+nN@ zP4vBA#&2Le?Pyp+%Zd+)@lAVsd(Q7X-2G4Y?Z>At0Pq|hsGty_0q+KO|Ka1t`+(U} zNWuMvYcgRsbZ+@1!4U^gZM%(iouK<^c^8LW}xbK)l-lK+Nxx!>pI5u@Xr)zS{e?qll zXTWKX8z%G6$YsVm*5|Z(TK{HLf0gze?o->5)MC;YP>F8*gUby$?iLrjdB%SfNLa4p zNqYL08q5x%T~6an|BmG$HyuOB~ah(EIz>`BWb7IY44??hRhQT&p<2g zx3^yIwlWw-z@t~~y`#)7f3BJgq^t%1LLFHP)QM)q;j znLoeeXjsJ_sdD)bRUCCJE*!fSHLn0Ggv@5>SO($7?B8$dKQICaX!}m!8GKuW+MXVL zWH-Z6ph6wR5;jDI4FZL@ZUUtp;rlosI`|Fnp#mrp{8xaDhbhS8s)%b93V1Ez(<6MF z!@Kqwv^O)Qk8tHHJ}GbwuX(f?c>muw#d!Ac?2RfAKNCN1=FvKcm`o0)YoTlx+D zkYGXI{V4HnSc#y0n0xM>Ip@yYGe3WQ`wpOoM@=j-?38L$n(W|N{JE6IYp>NTE^?_& zER@OoL;-kEjaV||354_VRJAAo+&JY~iv1($KAzBa;=d4>n zhvCbaacXAsOEu=HofbNmUR%QpXGgi3KIdU;I8l78(p);?6f*rJGsU+T26g8KZS%2J zGj6QsQ|Z$Q_aswSg-ZS3vOi{Y|IvpbBCNO3#sA;pQRj+hKj23QpD<|*-47cd>8`#1!MG!V?T&b~DV_4(uIWkhV tafjkvQaz2z literal 0 HcmV?d00001 diff --git a/bin/uebung07/quantities/plain/PlainQuantitiesDemo.class b/bin/uebung07/quantities/plain/PlainQuantitiesDemo.class new file mode 100644 index 0000000000000000000000000000000000000000..3df06d16434f6ef9cdc607168225f0a12659589a GIT binary patch literal 1554 zcmc&!U279T6g`usO|w>;Mq4YstZ!*Sw{L}31Zh_)=F8>-#0Qz(4(XKLnRGK7=+E-0 zprG&mDDiGunwJKyAo#F%_TGE;%-Ola`SttT4*+lRtb&rj%Ungd8SU){m$@`fJFOFnJ)6%V%eSRi9pc&D^Z8EVMN6C!N75W3$%#_Mjg!fJ2qoSi7r*(3^u`!alT4 z781=*f=R}NwZeXQ`lU+`dlWpGjA<|+%z-g~mdVD@^`CA?tIN3||7<|qzw}VUX5ndD z0-KHdyhgz5**uHYJ6+h^_B|&0s&FE_0J$X@F;Jp6a!HC6$^u#it;@UDSp7O{dlZW{ y@CXkmuH67=eMniAs_RrKKHeqD0_S~4?NhyejVC`TD`C#?e8y0ss=(7(Z~G67OWno* literal 0 HcmV?d00001 diff --git a/bin/uebung07/quantities/plain/Quantity.class b/bin/uebung07/quantities/plain/Quantity.class new file mode 100644 index 0000000000000000000000000000000000000000..503cd515e5f4fc3223b8962982f7cd894f7704bf GIT binary patch literal 1730 zcmah}+foxj5IvKGEW|}jRKy!1q9g&0cho2fiFk`zRx}pn(fr?g^9 z!z#TB6>Az&0*BtqRar8nwNQH5e6L!rK-a1?JtZKj0x8cjs2Zy64)=x@Wr6fvgTkc1 zNWQwCb)iAzian^Myta$Vf*Q(!P|_p`lx#uVz^4g}2;P?bottl3K22 zx1`ySZNooD%9OikIE=qX^v%@CS6@)>v~*M>OqS8BJvf0=8cqrf?wziV(?|gpenqg8yI*wsLpxd?UZriXH1dikjwf}7hwdD*(a6!ZQEiDS~bc`Y| zpap$;IWKS_AM+2}SndFs<9rl#jA2~h*s@$w729e__pRYBQgU3*3F|k?{_SAX0gWx6 zx7*8-<1rJDAMxL}8aspd<+<#4e=viqxUS*amgK9p$7cJ<-w-&Ouf{orcR|r6bX>$O zf!iHOy^96iy-u9P`DLb+i&;lmvFUZ&Yq!*l;m=|3=0IKYD@L}{w(UA@Tdq7;?xH>C zWN;r-8Xj;g|JT&8R_!HK3L(QDgj2NUH)mSlW1Ml zj<45NR0s2Y3KBt7q-oX-SC#wHnC!+w7Dx;UzM&_UrP>u!I*xKQJQg^=Bg;;_H9X}T z+uM}m6(*TF%dtpubQ8$>n}Jf+-wXV9A;sO{kIey|W@(k;x3BmedU5nCg^OR2{X$g& zhxwliuDSN;+~Y4Dx*nl^D1h(7QS|dffFO(rBtKA+0r+O4Fcb9m?|O^tjIfUEI*u=W zq9P2E4le^>J(5FUZdMFNAd>`1&?i&;hBISCu(u;*1BCw2>L0aCkU4iV&SE&iDhC8v zl}$n$IQIhu;PW<8{V`J62vYE|T#A4iEUsVZfw8WiNX{mUvt6Oc_1vm7wFi+lWb^RB{Q6*D~f&3l9=c!uMD0TB|FIsgCw literal 0 HcmV?d00001 diff --git a/bin/uebung07/quantities/plain/Unit.class b/bin/uebung07/quantities/plain/Unit.class new file mode 100644 index 0000000000000000000000000000000000000000..ddea055836c24bc881b7707a5c0a4bdb11e63ac1 GIT binary patch literal 532 zcmaJ-%SyvQ6g`uskJQ-MTI&m>?$QdzoeM<}OIJk~)q=}O9O{&GqG{rHDOdy-et;h( z-kBC$6x__*$GPX6d%u4^zW`ifzYL4vAQxe-#wVx2bIw&JGbv`lQ_Q6b?v%{Ruo*Tc z{DlWGSL5I&oQNo6uoa&QhQ{EpHOx|}#%B!Gkk3S)M_H0GupUF{ToTme{2J5D-=B zuTgeSD4Tw-`vJFmG^fCyqyA=qoAfqJn{G|Rjy?d5HmTk=^d7d*qKpWP3^S1yvPfVX hJ4=~>e%&i}-{HMdU_q0YOtEGN-34Gt$gpcrd%pqvW8(k- literal 0 HcmV?d00001 diff --git a/src/uebung07/quantities/plain/Length.java b/src/uebung07/quantities/plain/Length.java new file mode 100644 index 0000000..b8a2b23 --- /dev/null +++ b/src/uebung07/quantities/plain/Length.java @@ -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); + } +} diff --git a/src/uebung07/quantities/plain/LengthUnit.java b/src/uebung07/quantities/plain/LengthUnit.java new file mode 100644 index 0000000..c5a8e04 --- /dev/null +++ b/src/uebung07/quantities/plain/LengthUnit.java @@ -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); +} diff --git a/src/uebung07/quantities/plain/PlainQuantitiesDemo.java b/src/uebung07/quantities/plain/PlainQuantitiesDemo.java new file mode 100644 index 0000000..bfa6924 --- /dev/null +++ b/src/uebung07/quantities/plain/PlainQuantitiesDemo.java @@ -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))); + } +} diff --git a/src/uebung07/quantities/plain/Quantity.java b/src/uebung07/quantities/plain/Quantity.java new file mode 100644 index 0000000..b2d6e70 --- /dev/null +++ b/src/uebung07/quantities/plain/Quantity.java @@ -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); + } +} diff --git a/src/uebung07/quantities/plain/Unit.java b/src/uebung07/quantities/plain/Unit.java new file mode 100644 index 0000000..efaa8d7 --- /dev/null +++ b/src/uebung07/quantities/plain/Unit.java @@ -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; + } +} diff --git a/test/oop/ch05/generic/mensa/MensaTest.java b/test/oop/ch05/generic/mensa/MensaTest.java new file mode 100644 index 0000000..b3a3e06 --- /dev/null +++ b/test/oop/ch05/generic/mensa/MensaTest.java @@ -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()); + } +}