public abstract class Vehicle { private String description;// make-model for car
ID: 3830002 • Letter: P
Question
public abstract class Vehicle {
private String description;// make-model for cars/suvs, length for trucks
private int mpg;// miles per gallon
private String num_value;// (num of seats, bench seats, of rooms of storage)
private String VIN;// unique vehicle identification number
// reservation and cost information. When a vehicle is reserved, both reserv and cost are
// assigned. When no reservation exists, both are equal to null.
private Reservation reserv;// stores name, rental period, insur option, credit card # (for private customers)
// stores name, rental period, insur option, acct # (for corporate customers)
// assigned PrivateCustReservation or CorporationReservation type object
private Cost cost; // Stores the rates in effect at the time reservation made, including daily_rate,
// weekly_rate, monthly_rate, per_mile_charge, and daily_insur_rate. Contains a
// computeCost method using this information for when a vehicle is returned
// and the actual time period used and the number of miles driven is known
// Note that these are the rates for a particular vehicle type, since “attached” to
// a particular vehicle..
public Vehicle(String des, int mpg, String num_value, String VIN) { //arg-constructor
description = des;
this.mpg = mpg;
this.num_value = num_value;
this.VIN = VIN;
reserv = null; // init as “no reservation”
cost = null;
}
public String getDescription() {
return description;
}
public int getMpg() {
return mpg;
}
public String getNum_value() {
return num_value;
}
public String getVIN() {//only getter needed
return VIN;
}
public Reservation getReservation() {
return reserv;
}
public void setReservation(Reservation resv) {
this.reserv = reserv;
}
public Cost getCost() {
return cost;
}
public void setCost(Cost cost) {
this.cost = cost;
}
public abstract String toString();
public boolean isReserved() {
if (reserv != null) {
return true;
} else {
return false;
}
}
//Calculates cost of a returned vehicle
public double calcCost(String time_unit, int num_time_units, int num_miles_driven) {
// Calculates cost of a returned vehicle. Thus passed the ACTUAL time period vehicle used and
// ACTUAL num miles driven. Uses its Reservation object to determine whether a company reservation
// or private customer. Also uses the Reservation object to determine, if a company reservation,
// whether the insurance is desired.
// It in turn calls the calcCost method of the Cost object, since its Cost object has the rates appropriate
// for this rental, which calculates and returns the total cost of the rental. The amount is then reduced
// if this a company reservation, therefore receiving a 15% discount.
// compute the total charge (before any discounts)
double total_chrg = cost.calcCost(time_unit, num_time_units, num_miles_driven,
reserv.insurance_selected());
// apply any applicable discounts (check if Reservation object of type CompanyReservation)
if (reserv instanceof CompanyReservation) {
total_chrg = total_chrg * (100 - Rates.company_discount);
}
return total_chrg;
}
}
complete java methods below
VVVVVVVVVVV
public class Vehicles {
private Vehicle[] vehicles;
private int current;
public Vehicles() {
current = 0;
vehicles = new Vehicle[50];
}
public void add(Vehicle veh) {
...
}
public void remove(String VIN) {
...
}
//iterartor methods
public VehiclesIterator getAllVehiclesItr() {
return new AllVehiclesIterator(0, vehicles);
}
public VehiclesIterator getAllUnreservedVehiclesItr() {
return new AllUnreservedIterator(0, vehicles);
}
public void reset() {
...
}
public boolean hasNext() {
if (current < (vehicles.length - 1)) {
if (vehicles[current + 1] != null) {
return true;
}
}
return false;
}
public Vehicle next() {
return vehicles[++current];
}
}
Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
public class Vehicles {
private Vehicle[] vehicles;
private int current;
public Vehicles() {
current = 0;
vehicles = new Vehicle[50];
}
public void add(Vehicle veh) {
vehicles[current] = veh;
current++;
}
public void remove(String VIN) {
if(current == 0){
return;
}
int i = 0;
for(i=0; i<current; i++){
if(vehicles[i].VIN.compareTo(VIN) == 0){
break;
}
if(i == current-1){
vehicles[i] == null;
return;
}
for(int j=i; j<current-1; j++){
vehicles[j] = vehicles[j+1];
}
vehicles[current-1] = null;
current--;
}
}
//iterartor methods
public VehiclesIterator getAllVehiclesItr() {
return new AllVehiclesIterator(0, vehicles);
}
public VehiclesIterator getAllUnreservedVehiclesItr() {
return new AllUnreservedIterator(0, vehicles);
}
public void reset() {
...
}
public boolean hasNext() {
if (current < (vehicles.length - 1)) {
if (vehicles[current + 1] != null) {
return true;
}
}
return false;
}
public Vehicle next() {
return vehicles[++current];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.