Creating an ArrayList Application in Java

Problem:

Vehicle
- type : String
- name : String
-Price  :int
+ Vehicle(type : String, name : String, price :int)
 //setters getters
+ toString() : String

Garage
- vehicles : ArrayList<Vehicle>
+ Garage()
+ addVehicle(vehicle : Vehicle) : void
+ deleteVehicle(name : String) : void
+ getAllCars() : ArrayList<Vehicle>
+ getAllTrucks() : ArrayList<Vehicle>
+ getAllMotorcycles() : ArrayList<Vehicle>
+ hasVehicle(name : String) : Boolean
- requestOrder(name : String) : void
+ printVehicles() : void



You have to implement the previous two classes. The class Vehicle has thre, private variables, a constructor, setters and getters, and a toString method. The class Garage has an ArrayList of vehicles and a couple of methods that apply operations on the ArrayList.

- AddVehicle(vehicle : Vehicle): takes a vehicle as parameter and adds it  to vehicles
- deleteVehicle(name : String): deletes a vehicle from vehicles with the specified name
- getAllcars(): returns a ArrayList<Vehicle>that contains all the vehicle that have a type equal to car in vehicle
- getAllTrucks(): returns a ArrayList<Vehicle> that  contains all the vehicles that have a type equal to truck in vehicles
- getAllmotorcycles(): returns a ArrayList<Vehicle>that contains  all the vehicles that have a type  equal to motorcycle in vehicles
-hasVehicle(name : String): returns true if the vehicle with the specified name is found in vehicles else, it returns false and calls requestOrder(name)
-requestOrder(name:string): prints “Request an order for” + name
-printVehicles(): prints the elements in vehicles

In the tester class, you are required to create an object of type Garage and add to its list a number of vehicles.

You should retrieve all vehicles that are cars, and print them ,retrieve all vehicles that are trucks and print them ,and retrieve all vehicles that are mototcycles and print them.

You should test the deleteVehicle  method and ptint the vehicles list after deletion .
Finally, test the requestOrder method and verify that it is calling the requestOrder method if the vehicle is not found in the list.

Solution:

/**-----------------------Garage.java----------------------*/
import java.util.ArrayList;

public class Garage {
 
 private ArrayList<Vehicle> vehicles;
 
 public Garage() {
  vehicles = new ArrayList<Vehicle>();
 }
 
 public void addVehicle(Vehicle vehicle) {
  vehicles.add(vehicle);
 }
 
 public void deleteVehicle(String name) {
  for (int i = 0; i < vehicles.size(); i++)
   if (vehicles.get(i).getName().equals(name)) {
    vehicles.remove(i);
    return;
   }
 }
 
 public ArrayList<Vehicle> getAllCars() {
  ArrayList<Vehicle> cars = new ArrayList<Vehicle>();
  
  for (int i = 0; i < vehicles.size(); i++)
   if (vehicles.get(i).getType().equalsIgnoreCase("car"))
    cars.add(vehicles.get(i));
  
  return cars;
 }
 
 public ArrayList<Vehicle> getAllTrucks() {
  ArrayList<Vehicle> trucks = new ArrayList<Vehicle>();
  
  for (int i = 0; i < vehicles.size(); i++)
   if (vehicles.get(i).getType().equalsIgnoreCase("truck"))
    trucks.add(vehicles.get(i));
  
  return trucks;
 }
 
 public ArrayList<Vehicle> getAllMotorcycles() {
  ArrayList<Vehicle> motorcycles = new ArrayList<Vehicle>();
  
  for (int i = 0; i < vehicles.size(); i++)
   if (vehicles.get(i).getType().equalsIgnoreCase("motorcycle"))
    motorcycles.add(vehicles.get(i));
  
  return motorcycles;
 }
 
 public boolean hasVehicle(String name) {
  for (int i = 0; i < vehicles.size(); i++)
   if (vehicles.get(i).getName().equals(name))
    return true;
  
  requestOrder(name);
  return false;
 }
 
 public void printVehicles() {
  System.out.println(vehicles);
 }
 
 private void requestOrder(String name) {
  System.out.println("Requesting an order for "+name);
 }
}

/**-----------------------Vehicle.java----------------------*/
public class Vehicle {
 
 private String type;
 private String name;
 private int price;
 
 public Vehicle(String type, String name, int price) {
  this.type = type;
  this.name = name;
  this.price = price;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPrice() {
  return price;
 }

 public void setPrice(int price) {
  this.price = price;
 }

 @Override
 public String toString() {
  return "[type=" + type + ", name=" + name + ", price=" + price + "]";
 }
}

/**-----------------------GarageTester.java----------------------*/
public class GarageTester {

 public static void main(String args[]) {
  Vehicle car1 = new Vehicle("car", "Ferrari", 1000000);
  Vehicle car2 = new Vehicle("car", "Maserati", 1000000);
  Vehicle truck1 = new Vehicle("truck", "CAT", 20000);
  Vehicle truck2 = new Vehicle("truck", "chev", 30000);
  Vehicle moto1 = new Vehicle("motorcycle", "Suzuki", 10000);
  Vehicle moto2 = new Vehicle("motorcycle", "Ducati", 11000);
  
  Garage garage = new Garage();
  
  garage.addVehicle(car1);
  garage.addVehicle(car2);
  garage.addVehicle(truck1);
  garage.addVehicle(truck2);
  garage.addVehicle(moto1);
  garage.addVehicle(moto2);
  
  System.out.print("All Vehicles: ");
  garage.printVehicles();
  System.out.println("Cars: "+garage.getAllCars());
  System.out.println("Trucks: "+garage.getAllTrucks());
  System.out.println("Motorcycles: "+garage.getAllMotorcycles());
  garage.deleteVehicle("CAT");
  System.out.print("All Vehicles: ");
  garage.printVehicles();
  System.out.println(garage.hasVehicle("CAT"));
 }
}
 


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact