Skip to content
Snippets Groups Projects
Commit 1833d679 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Har fjernet det studentene ikke skal starte med.

parent b71bc5f9
Branches hal
No related tags found
No related merge requests found
package bike;
import java.util.ArrayList;
import java.util.List;
/*
* @startuml
* class Bike
......@@ -12,38 +9,7 @@ import java.util.List;
*/
public class Bike {
private GeoLocation location;
private Person renter;
public GeoLocation getLocation() {
return location;
}
void setLocation(final GeoLocation location) {
this.location = location;
}
public Person getRenter() {
return renter;
}
void setRenter(final Person renter) {
this.renter = renter;
}
// for computing rental price
private final List<RentalInfo> rentals = new ArrayList<RentalInfo>();
void addRentalInfo(final RentalInfo rentalInfo) {
rentals.add(rentalInfo);
}
List<RentalInfo> getRentalInfos() {
return new ArrayList<RentalInfo>(rentals);
}
// TODO: del 1, assosiasjoner
void clearRentalInfos() {
rentals.clear();
}
// TODO: del 1, registrering av leie
}
package bike;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
/*
* @startuml
* class Bike
*
* class Location {
* double latitude
* double longitude
* }
* Bike --> Location: location
*
* class BikeRental
* BikeRental *-- "*" Location: places
* BikeRental *-- "*" Bike: allBikes
*
* class Person
* class RentalInfo {
* LocalDateTime startTime
* LocalDateTime endTime
* }
* Bike --> Person: renter
* Bike *--> "*" RentalInfo: rentals
*
* interface PricePolicy
* class DefaultPricePolicy
* DefaultPricePolicy -up-|> PricePolicy
* BikeRental --> PricePolicy: pricePolicy
* Person -left-> PricePolicy: pricePolicy
*
* @enduml
*/
public class BikeRental {
private final Collection<GeoLocation> places = new ArrayList<GeoLocation>();
private final Collection<Bike> allBikes = new ArrayList<Bike>();
void addPlace(final GeoLocation location) {
places.add(location);
}
void addBike(final Bike bike, final GeoLocation location) {
if (location != null) {
bike.setLocation(location);
}
allBikes.add(bike);
}
// TODO: del 1, stations and bikes
/**
* Counts the number of available bikes within a certain distance of a provided location.
......@@ -58,14 +14,7 @@ public class BikeRental {
* @return the number of available bikes within a certain distance of a provided location
*/
private int countAvailableBikesNearby(final GeoLocation location, final double distance) {
int count = 0;
for (final Bike bike : allBikes) {
if (bike.getRenter() == null && bike.getLocation().distance(location) <= distance) {
count++;
}
}
return count;
// return allBikes.stream().filter(bike -> bike.getRenter() == null && bike.getLocation().distance(location) <= distance).count();
// TODO: del 1
}
/**
......@@ -75,37 +24,21 @@ public class BikeRental {
* @return the closest station (location) within the provided (maximum) distance of the provided bike
*/
private GeoLocation getStationNearby(final Bike bike, final double maxDistance) {
GeoLocation closestPlace = null;
double minDistance = maxDistance;
for (final GeoLocation place : places) {
final double distance = bike.getLocation().distance(place);
if (distance < minDistance) {
closestPlace = place;
minDistance = distance;
}
}
return closestPlace;
// TODO: del 1
}
/**
* @return the bikes that currently are rented
*/
private Collection<Bike> getRentedBikes() {
final Collection<Bike> result = new ArrayList<Bike>();
for (final Bike bike : allBikes) {
if (bike.getRenter() != null) {
result.add(bike);
}
}
return result;
// return allBikes.stream().filter(bike -> bike.getRenter() != null).collect(Collectors.toList());
// TODO: del 1
}
/**
* @return the bikes that are close to a station (within 30m), but still are rented
*/
private Collection<Bike> getUnreturnedBikes() {
return getRentedBikes().stream().filter(bike -> getStationNearby(bike, 30.0) != null).collect(Collectors.toList());
// TODO: del 1
}
/**
......@@ -118,18 +51,7 @@ public class BikeRental {
* @throws (some subclass of) RuntimeException if the bike isn't available for rental
*/
public void rentBike(final Person person, final Bike bike, final LocalDateTime now, final LocalDateTime returnTime) {
checkNowIsBeforeReturnTime(now, returnTime);
if (bike.getRenter() != null) {
throw new IllegalArgumentException(bike + " is currently being rented");
}
bike.setRenter(person);
bike.addRentalInfo(new RentalInfo(now, returnTime));
}
private void checkNowIsBeforeReturnTime(final LocalDateTime now, final LocalDateTime returnTime) {
if (now.compareTo(returnTime) >= 0) {
throw new IllegalArgumentException("The start time, " + now + " is the same as or after the return time, " + returnTime);
}
// TODO: del 1
}
/**
......@@ -144,11 +66,7 @@ public class BikeRental {
* @throws (some subclass of) RuntimeException if person isn't currently renting the bike
*/
public void extendRental(final Person person, final Bike bike, final LocalDateTime now, final LocalDateTime returnTime) {
checkNowIsBeforeReturnTime(now, returnTime);
if (bike.getRenter() != person) {
throw new IllegalArgumentException(bike + " isn't currently being rented by " + person);
}
bike.addRentalInfo(new RentalInfo(now, returnTime));
// TODO: del 1
}
/**
......@@ -162,26 +80,6 @@ public class BikeRental {
* @throws (some subclass of) RuntimeException if person isn't near (within 30m of) a station
*/
public void returnBike(final Person person, final Bike bike, final LocalDateTime now) {
if (bike.getRenter() != person) {
throw new IllegalArgumentException(bike + " isn't currently being rented by " + person);
}
final GeoLocation place = getStationNearby(bike, 30.0);
if (place == null) {
throw new IllegalArgumentException(bike + " isn't near (enough) a bike place");
}
person.withdraw(computePrice(person, bike, now));
bike.setRenter(null);
bike.clearRentalInfos();
}
private PricePolicy pricePolicy;
private int computePrice(final Person person, final Bike bike, final LocalDateTime returnTime) {
PricePolicy pricePolicy = person.getPricePolicy();
if (pricePolicy == null) {
pricePolicy = this.pricePolicy;
}
final int price = pricePolicy.computePrice(person, bike, returnTime);
return price;
// TODO: del 1
}
}
package bike;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.util.converter.LocalTimeStringConverter;
......@@ -12,25 +7,18 @@ import javafx.util.converter.LocalTimeStringConverter;
public class BikeRentalController {
private final BikeRental bikeRental;
private final Person me;
private final Bike bike;
public BikeRentalController() {
bikeRental = new BikeRental();
final GeoLocation here = new GeoLocation(63, 10);
bikeRental.addPlace(here);
bike = new Bike();
bikeRental.addBike(bike, here);
me = new Person("Hallvard");
// TODO: initialise bikeRental
}
// hint: LocalTimeStringConverter has two useful method, fromString(String) and toString(LocalTime)
private final LocalTimeStringConverter localTimeStringConverter = new LocalTimeStringConverter();
@FXML
public void initialize() {
setFromTime(LocalTime.now());
setToTime(LocalTime.now().plus(1, ChronoUnit.HOURS));
Platform.runLater(() -> toInput.requestFocus());
// TODO: initialise ui stuff
}
@FXML
......@@ -39,43 +27,6 @@ public class BikeRentalController {
@FXML
private TextField toInput;
/**
* @return a LocalDataTime object corresponding to the from input field value
*/
private LocalTime getFromTime() {
return localTimeStringConverter.fromString(fromInput.getText());
}
/**
* Updates the from input field value according to the LocalDateTime argument
* @param time
*/
private void setFromTime(final LocalTime time) {
fromInput.setText(localTimeStringConverter.toString(time));
}
private LocalTime getToTime() {
return localTimeStringConverter.fromString(toInput.getText());
}
private void setToTime(final LocalTime time) {
toInput.setText(localTimeStringConverter.toString(time));
}
@FXML
public void plus1HourAction() {
setToTime(getToTime().plus(1, ChronoUnit.HOURS));
}
@FXML
public void minus1HourAction() {
setToTime(getToTime().minus(1, ChronoUnit.HOURS));
}
private LocalDateTime toLocalDateTime(final LocalTime time) {
return LocalDateTime.now().withHour(time.getHour()).withMinute(time.getMinute());
}
@FXML
public void calculatePriceAction() {
}
// TODO: handle button actions
// hint: use plus and minus methods of LocalTime class and ChronoUnit.HOURS
}
......@@ -24,7 +24,7 @@ public class GeoLocation {
* @return distance to other Location
*/
public double distance(final GeoLocation other) {
return distance(latitude, longitude, other.latitude, other.longitude);
// TODO
}
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
......
......@@ -2,35 +2,4 @@ package bike;
public class Person {
private final String name;
public Person(final String name) {
this.name = name;
}
public String getName() {
return name;
}
/**
* Withdraws the provided amount from this person's account.
* If the transaction couldn't be completed, i.e. no money was actually withdrawn,
* one of the indicated exceptions will be thrown.
* @param amount the amount to withdraw
* @throws IllegalArgumentException
* @throws IllegalStateException
*/
public void withdraw(final double amount) throws IllegalArgumentException, IllegalStateException {
// ...
}
private PricePolicy pricePolicy;
public PricePolicy getPricePolicy() {
return pricePolicy;
}
public void setPricePolicy(final PricePolicy pricePolicy) {
this.pricePolicy = pricePolicy;
}
}
package bike;
import java.time.LocalDateTime;
public interface PricePolicy {
public int computePrice(Person person, Bike bike, LocalDateTime returnTime);
}
package bike;
import java.time.LocalDateTime;
public class RentalInfo {
private final LocalDateTime startTime;
private final LocalDateTime endTime;
public RentalInfo(final LocalDateTime startTime, final LocalDateTime endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public LocalDateTime getStartTime() {
return startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
}
......@@ -62,7 +62,7 @@ Under ser du et utkast til en mobil-app for sykkelutleietjenesten, basert på FX
<img src="bike-rental-app.png" width="500"/>
Prototypen er laget vha. FXML og noe Java, og nå er det din jobb å fullføre den!
Prototypen er laget vha. FXML, se [BikeRental.fxml](BikeRental.fxml), og noe Java, se bl.a. [BikeRentalApp.java](BikeRentalApp.java), og nå er det din jobb å fullføre den!
- Legg først til de Java-elementene (klasser, felt og metoder) som må finnes i koden som implementerer oppførselen (logikken) til appen
- Skriv koden som er nødvendig for å vise frem prisen, når tilsvarende knapp trykkes!
......@@ -20,24 +20,11 @@ public class EverySecondMetaIteratorTest {
@Test
public void testThatIdentifiesACaseWhereTheCodeFails() {
final EverySecondMetaIterator metaIterator = new EverySecondMetaIterator(Arrays.asList("1", "2", "3").iterator());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertEquals("1", metaIterator.next());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertEquals("3", metaIterator.next());
Assert.assertFalse(metaIterator.hasNext());
// TODO
}
@Test
public void testThatIdentifiesTheFundamentalBug() {
final EverySecondMetaIterator metaIterator = new EverySecondMetaIterator(Arrays.asList("1", "2", "3", "4").iterator());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertEquals("1", metaIterator.next());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertTrue(metaIterator.hasNext());
Assert.assertEquals("3", metaIterator.next());
Assert.assertFalse(metaIterator.hasNext());
Assert.assertFalse(metaIterator.hasNext());
// TODO
}
}
......@@ -19,13 +19,7 @@ public class LambdaUtilities {
* @return the copy, without strings that does not satisfy the predicate
*/
public static List<String> copyIf(final List<String> strings, final Predicate<String> pred) {
final List<String> copy = new ArrayList<>();
for (final String string : strings) {
if (pred.test(string)) {
copy.add(string);
}
}
return copy;
// TODO
}
/**
......@@ -35,11 +29,8 @@ public class LambdaUtilities {
* @param supplier
* @return the Collection that has been filled
*/
public static Collection<Integer> add(final Collection<Integer> col, int n, final Supplier<Integer> supplier) {
while (n-- > 0) {
col.add(supplier.get());
}
return col;
public static Collection<Integer> add(final Collection<Integer> col, final int n, final Supplier<Integer> supplier) {
// TODO
}
/**
......@@ -51,7 +42,7 @@ public class LambdaUtilities {
* @param maxExclusive
*/
public static Collection<Integer> addRandoms(final Collection<Integer> col, final int n, final int minInclusive, final int maxExclusive) {
return add(col, n, () -> (int) (minInclusive + Math.random() * (maxExclusive - minInclusive)));
// TODO
}
/**
......@@ -61,9 +52,7 @@ public class LambdaUtilities {
*/
@SuppressWarnings("unchecked")
public static void forEach(final Consumer<Object> cons, final Iterable<Object>... elements) {
for (int i = 0; i < elements.length; i++) {
elements[i].forEach(cons);
}
// TODO
}
/**
......@@ -74,26 +63,24 @@ public class LambdaUtilities {
* @return the sum of the result of applying op to consecutive elements of nums1 and nums2
*/
public static double zip(final BinaryOperator<Double> op, final List<Double> nums1, final Collection<Double> nums2) {
final Iterator<Double> it1 = nums1.iterator(), it2 = nums2.iterator();
double result = 0;
while (it1.hasNext() && it2.hasNext()) {
result += op.apply(it1.next(), it2.next());
}
return result;
// TODO
}
@SuppressWarnings("unchecked")
public static void main(final String[] args) {
// Should print [lambda, er, kult]
System.out.println(copyIf(Arrays.asList("lambda", "er", "ikke", "kult"), s -> ! s.equals("ikke")));
// Should print [3, 2, 1]
final Iterator<Integer> it = Arrays.asList(3, 2, 1).iterator();
System.out.println(add(new ArrayList<>(), 3, () -> it.next()));
// Should print
// 3
// 2
// 1
forEach(System.out::println, Arrays.asList("3", "2"), Arrays.asList("1"));
// Should print 13.0 (i.e. the sum of the products)
System.out.println(zip((n1, n2) -> n1 * n2, Arrays.asList(1.0, 2.5), Arrays.asList(3.0, 4.0)));
}
......
......@@ -6,32 +6,13 @@ import java.util.Iterator;
public class StringBooleanMetaIterator implements Iterator<String> {
private final Iterator<String> strings;
private final Iterator<Boolean> booleans;
// TODO: necessary field declarations
public StringBooleanMetaIterator(final Iterator<String> strings, final Iterator<Boolean> booleans) {
this.strings = strings;
this.booleans = booleans;
nextBoolean();
// TODO: necessary initialisation
}
@Override
public boolean hasNext() {
return strings.hasNext();
}
private void nextBoolean() {
while (booleans.hasNext() && (! booleans.next())) {
strings.next();
}
}
@Override
public String next() {
final String next = strings.next();
nextBoolean();
return next;
}
// TODO: necessary methods
public static void main(final String[] args) {
final Collection<String> strings = Arrays.asList("meta-iteratorer", "er", "ikke", "kult");
......
......@@ -4,23 +4,11 @@ import java.util.Iterator;
public class StringBuilderCharacterIterator implements Iterator<Character> {
private final StringBuilder stringBuilder;
// TODO: necessary field declarations
public StringBuilderCharacterIterator(final StringBuilder stringBuilder) {
this.stringBuilder = stringBuilder;
// TODO: necessary initialisation
}
private int pos = 0;
@Override
public boolean hasNext() {
return pos < stringBuilder.length();
}
@Override
public Character next() {
final char c = stringBuilder.charAt(pos);
pos++;
return c;
}
// TODO: necessary methods
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment