oppgave: train
This commit is contained in:
@@ -9,7 +9,7 @@ package oving7.train;
|
||||
*/
|
||||
public class CargoCar extends TrainCar {
|
||||
|
||||
// TODO: Add fields here
|
||||
private int cargoWeight;
|
||||
|
||||
/**
|
||||
* Constructor for the cargo car.
|
||||
@@ -21,8 +21,8 @@ public class CargoCar extends TrainCar {
|
||||
* @see CargoCarTest#testWeight()
|
||||
*/
|
||||
public CargoCar(int deadWeight, int cargoWeight) {
|
||||
// TODO: Implement this constructor
|
||||
super(deadWeight);
|
||||
this.cargoWeight = cargoWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,8 +31,7 @@ public class CargoCar extends TrainCar {
|
||||
* @see CargoCarTest#testWeight()
|
||||
*/
|
||||
public int getCargoWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return cargoWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,19 +41,20 @@ public class CargoCar extends TrainCar {
|
||||
* @see CargoCarTest#testWeight()
|
||||
*/
|
||||
public void setCargoWeight(int cargoWeight) {
|
||||
// TODO: Implement this method
|
||||
if (cargoWeight < 0) {
|
||||
throw new IllegalArgumentException("cargo weight cannot be less than zero");
|
||||
}
|
||||
this.cargoWeight = cargoWeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return cargoWeight + super.getDeadWeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// TODO: Implement this method
|
||||
return null;
|
||||
return "CargoCar";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -9,7 +9,7 @@ package oving7.train;
|
||||
*/
|
||||
public class PassengerCar extends TrainCar {
|
||||
|
||||
// TODO: Add fields here
|
||||
private int passengerCount;
|
||||
|
||||
/**
|
||||
* Constructor for the passenger car.
|
||||
@@ -21,7 +21,7 @@ public class PassengerCar extends TrainCar {
|
||||
* @see PassengerCarTest#testWeight()
|
||||
*/
|
||||
public PassengerCar(int deadWeight, int passengerCount) {
|
||||
// TODO: Implement this constructor
|
||||
this.passengerCount = passengerCount;
|
||||
super(deadWeight);
|
||||
}
|
||||
|
||||
@@ -31,8 +31,7 @@ public class PassengerCar extends TrainCar {
|
||||
* @see PassengerCarTest#testWeight()
|
||||
*/
|
||||
public int getPassengerCount() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return passengerCount;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,22 +41,22 @@ public class PassengerCar extends TrainCar {
|
||||
* @see PassengerCarTest#testWeight()
|
||||
*/
|
||||
public void setPassengerCount(int passengerCount) {
|
||||
// TODO: Implement this method
|
||||
if (passengerCount < 0) {
|
||||
throw new IllegalArgumentException("passenger count cannot be less than zero");
|
||||
}
|
||||
this.passengerCount = passengerCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalWeight() {
|
||||
// To calculate the total weight of the passenger car, you can assume that an average
|
||||
// passenger weighs 80 kg
|
||||
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return super.getDeadWeight() + passengerCount * 80;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// TODO: Implement this method
|
||||
return null;
|
||||
return "PassengerCar";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package oving7.train;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* The class {@code Train} represents a train that consists of one or more train cars.
|
||||
*
|
||||
@@ -9,7 +13,7 @@ package oving7.train;
|
||||
*/
|
||||
public class Train {
|
||||
|
||||
// TODO: Add fields here
|
||||
private ArrayList<TrainCar> trainCars = new ArrayList<TrainCar>();
|
||||
|
||||
/**
|
||||
* @param trainCar the train car to check for
|
||||
@@ -18,8 +22,7 @@ public class Train {
|
||||
* @see TrainTest#testAddCarToTrain()
|
||||
*/
|
||||
public boolean contains(TrainCar trainCar) {
|
||||
// TODO: Implement this method
|
||||
return false;
|
||||
return trainCars.contains(trainCar);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,7 +34,10 @@ public class Train {
|
||||
* @see TrainTest#testAddCarToTrain()
|
||||
*/
|
||||
public void addTrainCar(TrainCar trainCar) {
|
||||
// TODO: Implement this method
|
||||
if (trainCar == null) {
|
||||
throw new IllegalArgumentException("train car cannot be null");
|
||||
}
|
||||
trainCars.add(trainCar);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,8 +47,9 @@ public class Train {
|
||||
* @see TrainTest#testTotalTrainWeight()
|
||||
*/
|
||||
public int getTotalWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return trainCars.stream()
|
||||
.mapToInt(car -> car.getTotalWeight())
|
||||
.sum();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,8 +58,11 @@ public class Train {
|
||||
* @see TrainTest#testPassengerCount()
|
||||
*/
|
||||
public int getPassengerCount() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return trainCars.stream()
|
||||
.filter(car -> car instanceof PassengerCar)
|
||||
.map(car -> (PassengerCar) car)
|
||||
.mapToInt(car -> car.getPassengerCount())
|
||||
.sum();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +71,11 @@ public class Train {
|
||||
* @see TrainTest#testCargoWeight()
|
||||
*/
|
||||
public int getCargoWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return trainCars.stream()
|
||||
.filter(car -> car instanceof CargoCar)
|
||||
.map(car -> (CargoCar) car)
|
||||
.mapToInt(car -> car.getCargoWeight())
|
||||
.sum();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +84,9 @@ public class Train {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return null;
|
||||
return trainCars.stream()
|
||||
.map(car -> car.toString())
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
// TODO: Write a main method to test the class
|
||||
|
||||
@@ -5,7 +5,7 @@ package oving7.train;
|
||||
*/
|
||||
public class TrainCar {
|
||||
|
||||
// TODO: Add fields here
|
||||
private int deadWeight;
|
||||
|
||||
/**
|
||||
* Constructor for a train car.
|
||||
@@ -16,7 +16,10 @@ public class TrainCar {
|
||||
* @see TrainCarTest#testDeadWeight()
|
||||
*/
|
||||
public TrainCar(int deadWeight) {
|
||||
// TODO: Implement this constructor
|
||||
if (deadWeight < 0) {
|
||||
throw new IllegalArgumentException("dead weight cannot be less than zero");
|
||||
}
|
||||
this.deadWeight = deadWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +30,10 @@ public class TrainCar {
|
||||
* @see TrainCarTest#testDeadWeight()
|
||||
*/
|
||||
public void setDeadWeight(int deadWeight) {
|
||||
// TODO: Implement this method
|
||||
if (deadWeight < 0) {
|
||||
throw new IllegalArgumentException("dead weight cannot be less than zero");
|
||||
}
|
||||
this.deadWeight = deadWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,8 +43,7 @@ public class TrainCar {
|
||||
* @see TrainCarTest#testDeadWeight()
|
||||
*/
|
||||
public int getDeadWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return deadWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,8 +54,7 @@ public class TrainCar {
|
||||
* @see TrainCarTest#testDeadWeight()
|
||||
*/
|
||||
public int getTotalWeight() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
return deadWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +65,7 @@ public class TrainCar {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
// TODO: Implement this method
|
||||
return null;
|
||||
return "TrainCar";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user