IN JAVA: An application tracks the amount that account owed for tolling charges.
ID: 3855958 • Letter: I
Question
IN JAVA:
An application tracks the amount that account owed for tolling charges. These charges are accrued when crossing a bridge. There are 3 types of accounts: one for cars, one for trucks and one for emergency vehicles. Cars have a person owner with a first and last name, a license plate, and an account number. When a car crossed a bridge the charge is $5. Trucks have a corporation owner, a license plate, and an account number. When a truck crosses the bridge, it is charged $3 per axle. Emergency vehicles are also in the system, but cross the bridge for free. These emergency vehicles have a government owner, an account number and a license plate.
Make classes for all types of vehicles, with all properties mentioned, using inheritance when possible. Each class should have a method that can be called when the vehicle crosses the bridge. The method should keep track of how much the account owes.
In addition to the classes, write some code in a main method that creates some vehicles, post some tolls and prints out their account balance. Your main method code should verify that the method called for each crossing accurately keeps the account balance.
Explanation / Answer
Below is your code. If you find any problem , please comment on the answer. I'll resolve it.: -
Vehicle.java
public class Vehicle {
private String licenseNumber;
private String accountNumber;
private int amountOwn;
private String ownerType;
public Vehicle(String licenseNumber, String accountNumber, String ownerType) {
this.licenseNumber = licenseNumber;
this.accountNumber = accountNumber;
this.amountOwn = 0;
this.ownerType = ownerType;
}
public String getLicenseNumber() {
return licenseNumber;
}
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getOwnerType() {
return ownerType;
}
public void setOwnerType(String ownerType) {
this.ownerType = ownerType;
}
public int getAmountOwn() {
return amountOwn;
}
public void setAmountOwn(int amountOwn) {
this.amountOwn = amountOwn;
}
public void passToll() {
this.setAmountOwn(this.amountOwn);
}
@Override
public String toString() {
return "licenseNumber=" + licenseNumber + ", accountNumber=" + accountNumber + ", amountOwn="
+ amountOwn + ", ownerType=" + ownerType;
}
}
Car.java
public class Car extends Vehicle{
private String firstName;
private String lastName;
public Car(String licenseNumber, String accountNumber, String ownerType, String firstName, String lastName) {
super(licenseNumber, accountNumber, ownerType);
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void passToll() {
this.setAmountOwn(this.getAmountOwn()+5);
}
@Override
public String toString() {
return "Car [firstName=" + firstName + ", lastName=" + lastName + "]" +" "+super.toString();
}
}
Truck.java
public class Truck extends Vehicle{
private String cooperationName;
private int numOfAxles;
public Truck(String licenseNumber, String accountNumber, String ownerType, String cooperationName,int numOfAxles) {
super(licenseNumber, accountNumber, ownerType);
this.cooperationName = cooperationName;
this.numOfAxles = numOfAxles;
}
public String getCooperationName() {
return cooperationName;
}
public void setCooperationName(String cooperationName) {
this.cooperationName = cooperationName;
}
public int getNumOfAxles() {
return numOfAxles;
}
public void setNumOfAxles(int numOfAxles) {
this.numOfAxles = numOfAxles;
}
public void passToll() {
this.setAmountOwn(this.getAmountOwn()+(3*this.numOfAxles));
}
@Override
public String toString() {
return "Truck [cooperationName=" + cooperationName + ", numOfAxles=" + numOfAxles + "]" +" "+super.toString();
}
}
EmergencyVehicle.java
public class EmergencyVehicle extends Vehicle{
public EmergencyVehicle(String licenseNumber, String accountNumber, String ownerType) {
super(licenseNumber, accountNumber, ownerType);
}
public void passToll() {
this.setAmountOwn(0);
}
@Override
public String toString() {
return "EmergencyVehicle " +" "+super.toString();
}
}
Driver.java
public class Driver {
public static void main(String[] args) {
Car c1 = new Car("AB122343", "212121", "Personal", "Ravi", "Shastri");
Car c2 = new Car("AB122356", "212122", "Personal", "Dhoni", "Dravid");
Truck t1 = new Truck("TR45RC343", "212123", "Cooperation", "Maruthi Travels", 4);
EmergencyVehicle em = new EmergencyVehicle("EM34ED3333", "212124", "Government");
c1.passToll();
c1.passToll();
c1.passToll();
c2.passToll();
t1.passToll();
t1.passToll();
t1.passToll();
t1.passToll();
em.passToll();
em.passToll();
System.out.println("Details of the vehicle.");
System.out.println("Car 1: ");
System.out.println(c1);
System.out.println("Number of times toll passed : 3");
System.out.println();
System.out.println("Car 2: ");
System.out.println(c2);
System.out.println("Number of times toll passed : 1");
System.out.println();
System.out.println("Truck 1: ");
System.out.println(t1);
System.out.println("Number of times toll passed : 4");
System.out.println();
System.out.println("Emergency Vehicle #1: ");
System.out.println(em);
System.out.println("Number of times toll passed : 2");
}
}
Sample Output: -
Details of the vehicle.
Car 1:
Car [firstName=Ravi, lastName=Shastri]
licenseNumber=AB122343, accountNumber=212121, amountOwn=15, ownerType=Personal
Number of times toll passed : 3
Car 2:
Car [firstName=Dhoni, lastName=Dravid]
licenseNumber=AB122356, accountNumber=212122, amountOwn=5, ownerType=Personal
Number of times toll passed : 1
Truck 1:
Truck [cooperationName=Maruthi Travels, numOfAxles=4]
licenseNumber=TR45RC343, accountNumber=212123, amountOwn=48, ownerType=Cooperation
Number of times toll passed : 4
Emergency Vehicle #1:
EmergencyVehicle
licenseNumber=EM34ED3333, accountNumber=212124, amountOwn=0, ownerType=Government
Number of times toll passed : 2