Write a class file called Vehicle Write another class Truck which is a Vehicle W
ID: 3801561 • Letter: W
Question
Write a class file called Vehicle
Write another class Truck which is a Vehicle
Write another class Car which is a Vehicle
Example Dialog:
Welcome to the fleet manager
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
1
Enter the manufacturer's name
Nissan
Enter the number of cylinders
6
Enter the owner's name
JJ
Enter the car's gas mileage
29
Enter the number of passengers
5
The Fleet currently
Manufacturer's Name: Nissan
Number Of Cylinders: 6
Owner's Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer's name
Chevy
Enter the number of cylinders
8
Enter the owner's name
Eddie
Enter the truck's load capacity
1
Enter the truck's towing capacity
2
The Fleet currently
Manufacturer's Name: Nissan
Number Of Cylinders: 6
Owner's Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer's Name: Chevy
Number Of Cylinders: 8
Owner's Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
1
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
3
Enter the manufacturer's name
Ford
Enter the number of cylinders
6
Enter the owner's name
Bob
The Fleet currently
Manufacturer's Name: Nissan
Number Of Cylinders: 6
Owner's Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer's Name: Chevy
Number Of Cylinders: 8
Owner's Name: Eddie
Towing Capacity: 2.0
Load Capacity: 1.0
Manufacturer's Name: Ford
Number Of Cylinders: 6
Owner's Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
2
Enter 1: if it is a car
Enter 2: if it is a truck
Enter 3: if it is unclassified
2
Enter the manufacturer's name
Chevy
Enter the number of cylinders
8
Enter the owner's name
Eddie
Enter the truck's load capacity
1
Enter the truck's towing capacity
2
The Fleet currently
Manufacturer's Name: Nissan
Number Of Cylinders: 6
Owner's Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer's Name: Ford
Number Of Cylinders: 6
Owner's Name: Bob
Enter 1: to add a Vehicle
Enter 2: to remove a Vehicle
Enter 9 to quit
9
The Fleet currently
Manufacturer's Name: Nissan
Number Of Cylinders: 6
Owner's Name: JJ
Gas Mileage: 29.0
Number of Passengers: 5
Manufacturer's Name: Ford
Number Of Cylinders: 6
Owner's Name: Bob
Goodbye
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.march;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Objects;
/**
*
* @author Sam
*/
public class VehicleMain {
public static void main(String[] args) throws IOException {
ArrayList<Car> cars = new ArrayList<>();
ArrayList<Truck> trucks = new ArrayList<>();
ArrayList<Vehicle> vehicles = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("Welcome to the fleet manager "
+ "Enter 1: to add a Vehicle "
+ "Enter 2: to remove a Vehicle "
+ "Enter 9 to quit");
switch (br.read()) {
case '1':
System.out.println("Enter 1: if it is a car "
+ "Enter 2: if it is a truck "
+ "Enter 3: if it is unclassified");
switch (br.read()) {
case '1':
/*
Input statements goes here
*/
cars.add(new Car(/*params here*/));
System.out.print("Cars added");
break;
case '2':
/*
Input statements goes here
*/
trucks.add(new Truck(/*params here*/));
System.out.print("Truck added");
break;
case '3':
/*
Input statements goes here
*/
vehicles.add(new Vehicle(/*params here*/));
System.out.print("Vehicle added");
break;
}
cars.stream().forEach((c) -> {
System.out.println(c.toString());
});
trucks.stream().forEach((t) -> {
System.out.println(t.toString());
});
vehicles.stream().forEach((v) -> {
System.out.println(v.toString());
});
break;
case '2':
System.out.println("Enter 1: if it is a car "
+ "Enter 2: if it is a truck "
+ "Enter 3: if it is unclassified");
switch (br.read()){
case '1':
/*
Input statements goes here
*/
cars.remove(new Car(/*params here*/));
break;
case '2':
/*
Input statements goes here
*/
trucks.remove(new Truck(/*params here*/));
break;
case '3':
/*
Input statements goes here
*/
vehicles.remove(new Vehicle(/*params here*/));
break;
}
cars.stream().forEach((c) -> {
System.out.println(c.toString());
});
trucks.stream().forEach((t) -> {
System.out.println(t.toString());
});
vehicles.stream().forEach((v) -> {
System.out.println(v.toString());
});
break;
case '9':
cars.stream().forEach((c) -> {
System.out.println(c.toString());
});
trucks.stream().forEach((t) -> {
System.out.println(t.toString());
});
vehicles.stream().forEach((v) -> {
System.out.println(v.toString());
});
System.out.println("Good bye!");
return;
}
}
}
}
class Vehicle {
private String mfgName;
private int cylCount;
private String owner;
Vehicle() {
mfgName = null;
owner = null;
cylCount = 0;
}
public Vehicle(String mfgName, int cylCount, String owner) {
this.mfgName = mfgName;
this.cylCount = cylCount;
this.owner = owner;
}
public String getMfgName() {
return mfgName;
}
public void setMfgName(String mfgName) {
this.mfgName = mfgName;
}
public int getCylCount() {
return cylCount;
}
public void setCylCount(int cylCount) {
if (cylCount > 0) {
this.cylCount = cylCount;
} else {
System.out.println("Invalid Cyl Count. Not updated.");
}
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
@Override
public int hashCode() {
int hash = 5;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Vehicle other = (Vehicle) obj;
if (!Objects.equals(this.mfgName, other.mfgName)) {
return false;
}
if (this.cylCount != other.cylCount) {
return false;
}
if (!Objects.equals(this.owner, other.owner)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Manufacturer's Name: " + mfgName
+ " Number Of Cylinders: " + cylCount
+ " Owner's Name: " + owner;
}
}
class Truck extends Vehicle {
double loadCap;
double towCap;
public Truck() {
super();
loadCap = 0;
towCap = 0;
}
public Truck(double loadCap, double towCap, String mfgName, int cylCount, String owner) {
super(mfgName, cylCount, owner);
if (loadCap > 0) {
this.loadCap = loadCap;
} else {
this.loadCap = 0;
}
if (towCap > 0) {
this.towCap = towCap;
} else {
this.towCap = 0;
}
}
public double getLoadCap() {
return loadCap;
}
public void setLoadCap(double loadCap) {
if (loadCap > 0) {
this.loadCap = loadCap;
} else {
this.loadCap = 0;
}
}
public double getTowCap() {
return towCap;
}
public void setTowCap(double towCap) {
if (towCap > 0) {
this.towCap = towCap;
} else {
this.towCap = 0;
}
}
@Override
public int hashCode() {
int hash = 5;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Truck other = (Truck) obj;
if (Double.doubleToLongBits(this.loadCap) != Double.doubleToLongBits(other.loadCap)) {
return false;
}
if (Double.doubleToLongBits(this.towCap) != Double.doubleToLongBits(other.towCap)) {
return false;
}
if (!Objects.equals(this.getMfgName(), other.getMfgName())) {
return false;
}
if (this.getCylCount() != other.getCylCount()) {
return false;
}
if (!Objects.equals(this.getOwner(), other.getOwner())) {
return false;
}
return true;
}
@Override
public String toString() {
return super.toString()
+ " Towing Capacity: " + towCap
+ " Load Capacity: " + loadCap;
}
}
class Car extends Vehicle {
double milage;
int passengerCap;
public Car() {
super();
milage = 0;
passengerCap = 0;
}
public Car(double milage, int passengerCap, String mfgName, int cylCount, String owner) {
super(mfgName, cylCount, owner);
if (milage > 0) {
this.milage = milage;
} else {
this.milage = 0;
}
if (passengerCap > 0) {
this.passengerCap = passengerCap;
} else {
this.passengerCap = 0;
}
}
public double getMilage() {
return milage;
}
public void setMilage(double milage) {
if (milage > 0) {
this.milage = milage;
} else {
this.milage = 0;
}
}
public int getPassengerCap() {
return passengerCap;
}
public void setPassengerCap(int passengerCap) {
if (passengerCap > 0) {
this.passengerCap = passengerCap;
} else {
this.passengerCap = 0;
}
}
@Override
public int hashCode() {
int hash = 3;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Car other = (Car) obj;
if (Double.doubleToLongBits(this.milage) != Double.doubleToLongBits(other.milage)) {
return false;
}
if (this.passengerCap != other.passengerCap) {
return false;
}
if (!Objects.equals(this.getMfgName(), other.getMfgName())) {
return false;
}
if (this.getCylCount() != other.getCylCount()) {
return false;
}
if (!Objects.equals(this.getOwner(), other.getOwner())) {
return false;
}
return true;
}
@Override
public String toString() {
return super.toString()
+ " Gas Mileage: " + milage
+ " Number of Passengers: " + passengerCap;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.