diff --git a/refactoring-master/README.md b/refactoring-master/README.md new file mode 100644 index 0000000..25035bb --- /dev/null +++ b/refactoring-master/README.md @@ -0,0 +1,53 @@ +# Refactoring - autorendifirma hinnakalkulaator + +Ülesandeks on refaktoreerida programmikood selliselt, et see vastaks clean code standarditele, oleks testitud ja väikestes osades. + +# Töö käik + +Ülesande lahendamiseks toimige järgnevalt: +1. Fork'ige githubis see repositoorium enda kontole +2. kloonige `git clone URL` abil see repo enda arvutisse +3. Tehke kõik vajalikud muudatused ja commitige +4. Tehke githubis originaalrepo jaoks pull request +5. Hinde (2p) saavad tudengid, kes: + -- praktikumis kaasa töötasid + -- kelle tiim tegi pull requesti korraliku koodiga + +# Analüütiku/projektijuhi roll + +Kui tiimis on analüütik, kes ei programmeeri, siis tuleb tal hinde saamiseks koostada testiplaan, kus on kirjeldatud kõik juhtumid, mida tuleb testida. Näiteks: + +Ärinõue: autot saab rentida alates 18. eluaastast +Vajalikud testid: +1. 17-aastane ei saa autot rentida +-- sisendite kombinatsioon: vanus: 17, [...] +-- oodatav väljund: Exception +2. 18-aastane saab autot rentida +-- sisendite kombinatsioon: vanus: 18, [...] +-- oodatav väljund: hind 18-aastasele XXX EUR + +NB! Oodatavad väljundid sõltuvad ka teie programmi struktuurist. Kui originaalprogramm väljastab Exceptioni lõppkasutajale, siis teie võite vanusekontrolli viia eraldi meetodiks, mis tagastab vaid TRUE/FALSE + +Testiplaan tuleb commitida MD (readme) formaadis failina reposse. + +# Ärinõuded, millest lähtuda + + +**Taust** + +Rendiautod jagunevad klassidesse 1-5, kus 1 on kõige odavam kategooria ja 5 kõige kallim. + +**Ärireeglid autorendi ühe ööpäeva hinna arvutamiseks** + +Alla 18 aastane ei saa autot rentida +18-21-aastane saab rentida ainult klassi 1 kuuluvaid autosid + +Maksimaalne rendi hind on 1000 eurot päevas. + +Minimaalne rendi hind võrdub juhi vanusega. + +Klassides 4 ja 5 on hind 50% kallim kui juht on 25-aastane või noorem (välja arvatud madalhooajal). + +Rentida ei saa isikud, kellel on juhiluba olnud alla aasta. Kui juhiluba on olnud alla kolme aasta, siis on rendi hind 30% suurem. + +Kui isik on põhjustanud viimase aasta jooksul liiklusõnnestusi ja isik on alla 30 aasta vana, siis lisandub rendi ööpäevahinnale 15 eurot. \ No newline at end of file diff --git a/refactoring-master/bin/rental/RentalPriceCalculator.class b/refactoring-master/bin/rental/RentalPriceCalculator.class new file mode 100644 index 0000000..629d6c3 Binary files /dev/null and b/refactoring-master/bin/rental/RentalPriceCalculator.class differ diff --git a/refactoring-master/bin/rental/RentalPriceCalculatorTest.class b/refactoring-master/bin/rental/RentalPriceCalculatorTest.class new file mode 100644 index 0000000..209ab7b Binary files /dev/null and b/refactoring-master/bin/rental/RentalPriceCalculatorTest.class differ diff --git a/refactoring-master/src/rental/RentalPriceCalculator.java b/refactoring-master/src/rental/RentalPriceCalculator.java new file mode 100644 index 0000000..d72830d --- /dev/null +++ b/refactoring-master/src/rental/RentalPriceCalculator.java @@ -0,0 +1,82 @@ +package rental; + + +public class RentalPriceCalculator { + + public static final int minimumLegalAge = 18; + public static final int fullLegalAge = 21; + public static final int maxRentalPrice = 1000; + public static final int firstRiskGroupAge = 25; + public static final int secondRiskGroupAge = 30; + + + public double price(int ageOfDriver, int hadLicenseForYears, int carSizeClass, boolean hasCausedAccidentsLastYear, boolean hasParticipatedInAccidentsLastYear, boolean ifHighSeason) { + + checkIfDriverLegalAge(ageOfDriver); + + checkIfDriverIsSuitableForClass(ageOfDriver, carSizeClass); + + double rentalprice = ageOfDriver; + + rentalprice = adaptPriceOnSeasonCarClassDriversAge(ageOfDriver, carSizeClass, ifHighSeason, rentalprice); + + checkIfLicenseUnderYear(hadLicenseForYears); + + rentalprice = adaptPriceOnDriversPeriod(hadLicenseForYears, rentalprice); + + rentalprice = adaptPriceOnDriversAccidentsAndAge(ageOfDriver, hasCausedAccidentsLastYear, rentalprice); + + if (rentalprice > maxRentalPrice) { + return maxRentalPrice; + } + + return rentalprice; + } + + + void checkIfDriverLegalAge(int ageOfDriver) { + if (ageOfDriver < minimumLegalAge) { + throw new IllegalArgumentException("Driver too young - cannot quote the price"); + } + } + + + void checkIfDriverIsSuitableForClass(int ageOfDriver, int carSizeClass) { + if (ageOfDriver <= fullLegalAge && carSizeClass > 2) { + throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); + } + } + + + double adaptPriceOnSeasonCarClassDriversAge(int ageOfDriver, int carSizeClass, boolean ifHighSeason, + double rentalprice) { + if (carSizeClass >= 4 && ageOfDriver <= firstRiskGroupAge && !ifHighSeason) { + rentalprice = rentalprice * 2; + } + return rentalprice; + } + + + double adaptPriceOnDriversAccidentsAndAge(int ageOfDriver, boolean hasCausedAccidentsLastYear, + double rentalprice) { + if (hasCausedAccidentsLastYear && ageOfDriver < secondRiskGroupAge) { + rentalprice += 15; + } + return rentalprice; + } + + + void checkIfLicenseUnderYear(int hadLicenseForYears) { + if (hadLicenseForYears < 1) { + throw new IllegalArgumentException("Driver must hold driving hadLicenseForYears at least for one year. Can not rent a car!"); + } + } + + + double adaptPriceOnDriversPeriod(int hadLicenseForYears, double rentalprice) { + if (hadLicenseForYears < 3) { + rentalprice = rentalprice * 1.3; + } + return rentalprice; + } +} \ No newline at end of file diff --git a/refactoring-master/test/rental/RentalPriceCalculatorTest.java b/refactoring-master/test/rental/RentalPriceCalculatorTest.java new file mode 100644 index 0000000..a837794 --- /dev/null +++ b/refactoring-master/test/rental/RentalPriceCalculatorTest.java @@ -0,0 +1,42 @@ +package rental; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class RentalPriceCalculatorTest { + private RentalPriceCalculator calculator; + @Before + public void beforeEachTest() { + // this method is called before each test + calculator = new RentalPriceCalculator(); + } + + @Test + public void priceNormalFor3YearsOfExperience() { + assertEquals(100, + calculator + .adaptPriceOnDriversPeriod(3, 100), 0.0001); + + + } + @Test + public void priceNormalFor11YearsOfExperience() { + assertEquals(100, + calculator + .adaptPriceOnDriversPeriod(11, 100), 0.0001); + + + } + @Test + public void priceHigherForUnexperiencedDriver() { + assertEquals(130, + calculator + .adaptPriceOnDriversPeriod(1, 100), 0.0001); + + + } + + +}