In: Computer Science
Complete the // TODO sections in the EasyRental class.
You are comparing Strings in an object not integers.
Ex. If the input is:
brown red white blue black -1
the output is:
Enter the colors of the vehicles you would like to search for on the same line followed by a -1 There are no rentals in brown There are 7 compact Jeep's in red There are no rentals in white There are 5 economy Fiat's in blue There are 5 compact Scion's in black Done
These are the given classes with code:
CarRentals.java
import java.util.Scanner;
public class CarRentals {
   public static void main(String[] args) {
       Scanner scrn = new
Scanner(System.in);
       EasyRental ezRent = new
EasyRental();
      
       // this section passes the make,
the color, the size, and the number of vehicles in stock
       // to the addVehicle method which
adds the data to an arrayList in the EasyRental class
       ezRent.addVehicle("Ford", "orange",
"mid-size", 3);
       ezRent.addVehicle("Fiat", "blue",
"economy", 5);
       ezRent.addVehicle("Toyota", "grey",
"compact", 6);
       ezRent.addVehicle("Saab", "canary",
"mid-size", 2);
       ezRent.addVehicle("Nissan",
"silver", "economy", 1);
       ezRent.addVehicle("Jeep", "red",
"compact", 7);
       ezRent.addVehicle("Honda",
"indigo", "economy", 4);
       ezRent.addVehicle("Dodge", "tan",
"mid-size", 1);
       ezRent.addVehicle("Chevy",
"turquoise", "full-size", 2);
       ezRent.addVehicle("Scion", "black",
"compact", 5);
       // sort the data based on
color
       ezRent.sort();
          
       System.out.println("Enter the
colors of the vehicles you would like to search for on the same
line followed by a -1");
       String color = scrn.next();
       System.out.println();
       while(!color.equals("-1")) {
           int index =
ezRent.binarySearch(color);
           if(index == -1)
{
          
    System.out.println("There are no rentals in " +
color);
           }
           else {
          
   
System.out.println(ezRent.printVehicleInfo(index));
           }
           color =
scrn.next();
       }
       System.out.println();
       System.out.println("Done");
   }
}
EasyRental.java
import java.util.ArrayList;
public class EasyRental {
private ArrayList<Rentals> rents = new ArrayList<Rentals>();
   public void addVehicle(String make, String color,
String size, int amount) {
       rents.add(new Rentals(make, color,
size, amount));
}
   public String printVehicleInfo(int index) {
       return
rents.get(index).toString();
   }
// TODO 1. create an iterative sort method that sorts the rentals by color
   // TODO 2. create a Binary Search method below to
search for a vehicle by a
   // color, that should return the index where the
vehicle was found or -1
   public void printVehicles() {
       for (Rentals r : rents) {
          
System.out.println(r.toString());
       }
       System.out.println();
   }
}
Rentals.java
public class Rentals {
  
   private String make;
   private String color;
   private String size;
   private int count;
  
   public Rentals(String make, String color, String size,
int count) {
       this.make = make;
       this.color = color;
       this.size = size;
       this.count = count;
   }
  
   public String getMake() {
       return make;
   }
   public void setMake(String make) {
       this.make = make;
   }
   public String getColor() {
       return color;
   }
   public void setColor(String color) {
       this.color = color;
   }
   public String getSize() {
       return size;
   }
   public void setSize(String size) {
       this.size = size;
   }
   public int getCount() {
       return count;
   }
   public void setCount(int count) {
       this.count = count;
   }
   public String toString() {
       return "There are " + count + " " +
size + " " + make + "'s" + " in " + color ;
   }
  
}
Rentals.java
public class Rentals {
          
           private String make;
           private String color;
           private String size;
           private int count;
          
           public Rentals(String make, String color, String size, int count) {
               this.make = make;
               this.color = color;
               this.size = size;
               this.count = count;
           }
          
           public String getMake() {
               return make;
           }
           public void setMake(String make) {
               this.make = make;
           }
           public String getColor() {
               return color;
           }
           public void setColor(String color) {
               this.color = color;
           }
           public String getSize() {
               return size;
           }
           public void setSize(String size) {
               this.size = size;
           }
           public int getCount() {
               return count;
           }
           public void setCount(int count) {
               this.count = count;
           }
           public String toString() {
               return "There are " + count + " " + size + " " + make + "'s" + " in " + color ;
           }
          
        }
EasyRentals.java
import java.util.ArrayList;
import java.util.*;
public class EasyRental {
   private ArrayList<Rentals> rents = new ArrayList<Rentals>();
   public void addVehicle(String make, String color, String size, int amount) {
       rents.add(new Rentals(make, color, size, amount));
   }
   public String printVehicleInfo(int index) {
       return rents.get(index).toString();
   }
   
   // TODO 1. create an iterative sort method that sorts the rentals by color
   
   public void sort() {
           
           // using Collections.sort() with Comparator to caompare the colors
           Collections.sort(rents, new Comparator<Rentals>() {
           @Override
           public int compare(Rentals o1, Rentals o2) {
               String color1 = o1.getColor();
               String color2 = o2.getColor();
               return color1.compareTo(color2);
           }
           });
           
   }
   
   
   // TODO 2. create a Binary Search method below to search for a vehicle by a
   // color, that should return the index where the vehicle was found or -1
   
   int binarySearch(String color)
   {
           int p=0, q=rents.size()-1;
           while(p<=q){
               int mid = (p+q)/2;
               if(rents.get(mid).getColor().compareTo(color)==0)
                   return mid;
               if(rents.get(mid).getColor().compareTo(color)<0)
                   p = mid+1;
               else
                   q = mid-1;
           }
           return -1;
          
   }
  
   public void printVehicles() {
       for (Rentals r : rents) {
           System.out.println(r.toString());
       }
       System.out.println();
   }
}
CarRentals.java
import java.util.Scanner;
public class CarRentals {
   public static void main(String[] args) {
       Scanner scrn = new Scanner(System.in);
       EasyRental ezRent = new EasyRental();
      
       // this section passes the make, the color, the size, and the number of vehicles in stock
       // to the addVehicle method which adds the data to an arrayList in the EasyRental class
       ezRent.addVehicle("Ford", "orange", "mid-size", 3);
       ezRent.addVehicle("Fiat", "blue", "economy", 5);
       ezRent.addVehicle("Toyota", "grey", "compact", 6);
       ezRent.addVehicle("Saab", "canary", "mid-size", 2);
       ezRent.addVehicle("Nissan", "silver", "economy", 1);
       ezRent.addVehicle("Jeep", "red", "compact", 7);
       ezRent.addVehicle("Honda", "indigo", "economy", 4);
       ezRent.addVehicle("Dodge", "tan", "mid-size", 1);
       ezRent.addVehicle("Chevy", "turquoise", "full-size", 2);
       ezRent.addVehicle("Scion", "black", "compact", 5);
       // sort the data based on color
       ezRent.sort();
       /* Test Method to check whether sort is working fine or not*/
      // ezRent.printVehicles();
          
       System.out.println("Enter the colors of the vehicles you would like to search for on the same line followed by a -1");
       String color = scrn.next();
       System.out.println();
       while(!color.equals("-1")) {
           int index = ezRent.binarySearch(color);
           if(index == -1) {
               System.out.println("There are no rentals in " + color);
           }
           else {
               System.out.println(ezRent.printVehicleInfo(index));
           }
           color = scrn.next();
       }
       System.out.println();
       System.out.println("Done");
   }
}
output of the program:

End of solution