Dears,
Will it be possible to check what am I doing wrong for the second validation?
Thanks,
Sanjay Chauhan
package com.codegym.task.task29.task2909.car;
import java.util.Date;
public abstract class Car {
static public final int TRUCK = 0;
static public final int SEDAN = 1;
static public final int CABRIOLET = 2;
double fuel;
public double summerFuelConsumption;
public double winterFuelConsumption;
public double winterWarmingUp;
private int type;
private boolean driverAvailable;
private int numberOfPassengers;
protected Car(int type, int numberOfPassengers) {
this.type = type;
this.numberOfPassengers = numberOfPassengers;
}
//3. You need to add a static Car create(int type, int numberOfPassengers) factory method to the Car class and implement it.
public static Car create(int type, int numberOfPassengers){
switch (type) {
case 0:
return new Truck(numberOfPassengers);
case 1:
return new Sedan(numberOfPassengers);
default:
return new Cabriolet(numberOfPassengers);
}
}
//1. You need to change the Car class's fill(double numberOfGallons) method so that it throws an Exception if there is an error.
public void fill(double numberOfGallons) throws Exception{
if (numberOfGallons < 0)
throw new Exception();
fuel += numberOfGallons;
}
public double getTripConsumption(Date date, int length, Date summerStart, Date summerEnd) {
if (!isSummer(date,summerStart,summerEnd))
return getWinterConsumption(length);
else
return getSummerConsumption(length);
}
//Rewrite the getNumberOfPassengersThatCanBeCarried() method by combining the conditional
//expressions (use the canPassengersBeCarried() method)
//You need to change the getNumberOfPassengersThatCanBeCarried() method by combining conditional statements (use the canPassengersBeCarried() method).
//You are incorrectly calculating the number of passengers that can be carried.
public int getNumberOfPassengersThatCanBeCarried() {
if (canPassengersBeCarried())
return numberOfPassengers;
return 0;
}
public boolean isDriverAvailable() {
return driverAvailable;
}
public void setDriverAvailable(boolean driverAvailable) {
this.driverAvailable = driverAvailable;
}
// Combine duplicate conditional fragments. Rewrite the
//startMoving() method so that it does not have duplicate function calls.
//In the startMoving() method, if there are passengers in the car, then the driver and passengers must fasten their seat belts.
public void startMoving() {
fastenDriverBelt();
if (canPassengersBeCarried()) {
fastenPassengerBelts();
}
}
public void fastenPassengerBelts() {
}
public void fastenDriverBelt() {
}
//12.3. Replace a magic number with a symbolic constant. Replace the magic numbers in the
//getMaxSpeed() method with constants: MAX_TRUCK_SPEED,
//MAX_SEDAN_SPEED, and MAX_CABRIOLET_SPEED.
public abstract int getMaxSpeed();
protected Car(){
}
// 2. In the Car class, you need to add and implement a public boolean isSummer(Date date, Date summerStart, Date summerEnd) method.
public boolean isSummer(Date date, Date summerStart, Date summerEnd){
return (date.compareTo(summerStart)>=0 && summerEnd.compareTo(date)>=0);
}
// 3. In the Car class, you need to add and implement a public double getWinterConsumption(int length) method.
public double getWinterConsumption(int length){ return length * winterFuelConsumption + winterWarmingUp; }
//4. In the Car class, you need to add and implement a public double getSummerConsumption(int length) method.
public double getSummerConsumption(int length){ return summerFuelConsumption * length ;}
// In the Car class, add a private method that indicates whether passengers can be carried:
//boolean canPassengersBeCarried(). The method should return true if
//the driver is available (isDriverAvailable) and there is fuel.
private boolean canPassengersBeCarried(){
return (fuel >0 && isDriverAvailable() );
}
}