(Please give all the solutions and steps, if you give the answer by picture plea
ID: 3751381 • Letter: #
Question
(Please give all the solutions and steps, if you give the answer by picture please post a clear picture. Please don't answer the question if you can't do all the questions, THANKS). Answer in Java. If I need to create any file please tell me.
Java DB Query Assume that a bicycle has the following attributes: Type (mountainbike, race-bike, or street bike) Number of gears (4-10, in increments of 1) 1. 2. 3. Wheel base (36 60 inches, in increments of 6 inches) 4. 5. 6. Height (1, 2, 3, 4) Color (steel, red, blue, black) Construction Material (carbon, steel, aluminium) Make a text file with a bicycle database. The file should have bikes with ALL possible combinations. You can generate this file randomly, by scripts or by hand, or you can use any data that you may have found on the internet. Write a java code (ShowBikes.java) to read the file and return a list of bikes when the code is executed in the following example format: Java ShowBikes-type mountain bike-gear 5-wheelbase The above call should list ALL mountain bikes in the database with 5 gears, and sort them in ascending order according to their wheelbase dimensions. I should be able to call the code with as many attributes as I want, and the last attribute should always be used to sort the list. Requirements: 1) 2) You have to use a ArrayList data structure in Java. You have to use try-catch constructs for error handling.Explanation / Answer
package com.practice.sample;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Bike {
private String type;
private int noOfGears;
private int wheelBase;
private int height;
private String color;
private String constructionMaterial;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getNoOfGears() {
return noOfGears;
}
public void setNoOfGears(int noOfGears) {
this.noOfGears = noOfGears;
}
public int getWheelBase() {
return wheelBase;
}
public void setWheelBase(int wheelBase) {
this.wheelBase = wheelBase;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getConstructionMaterial() {
return constructionMaterial;
}
public void setConstructionMaterial(String constructionMaterial) {
this.constructionMaterial = constructionMaterial;
}
@Override
public String toString() {
return "Bike [type=" + type + ", noOfGears=" + noOfGears + ", wheelBase=" + wheelBase + ", height=" + height
+ ", color=" + color + ", constructionMaterial=" + constructionMaterial + "]";
}
}
//Comparator to sort by type
class TypeComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getType().compareTo(o2.getType());
}
}
//Comparator to sort by No of Gears
class GearComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getNoOfGears() - o2.getNoOfGears();
}
}
//Comparator to sort by wheelbase
class WheelbaseComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getWheelBase() - o2.getWheelBase();
}
}
//Comparator to sort by height
class HeightComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getHeight() - o2.getHeight();
}
}
//Comparator to sort by Color
class ColorComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getColor().compareTo(o2.getColor());
}
}
//Comparator to sort by Material
class MaterialComparator implements Comparator<Bike> {
public int compare(Bike o1, Bike o2) {
return o1.getConstructionMaterial().compareTo(o2.getConstructionMaterial());
}
}
//Driver Class
public class ShowBikes {
static Map<String, String> criteriaMap = new HashMap<>();
// Reading the Data From File
private static List<Bike> getBikeDataFromFile() {
List<Bike> bikes = new ArrayList<>();
File file = new File("D:\eclipse-workspace\JavaPractice\src\com\practice\sample\BikeData.txt");
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
// to skip the first header line
br.readLine();
line = br.readLine();
while (line != null) {
// trimming the extra spaces
line.trim();
Bike bike = createBikeObjectWithBikeDataFromFile(line.split(","));
bikes.add(bike);
line = br.readLine();
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return bikes;
}
// creating bike data object from the current line read
private static Bike createBikeObjectWithBikeDataFromFile(String bikeData[]) {
Bike bike = new Bike();
// trimming the spaces if any
bike.setType(bikeData[0].trim());
bike.setNoOfGears(Integer.parseInt(bikeData[1].trim()));
bike.setWheelBase(Integer.parseInt(bikeData[2].trim()));
bike.setHeight(Integer.parseInt(bikeData[3].trim()));
bike.setColor(bikeData[4].trim());
bike.setConstructionMaterial(bikeData[5].trim());
return bike;
}
private static List<Bike> getFilteredBikesByCriteria(Map<String, String> criteriaMap, List<Bike> bikes) {
List<Bike> filteredBikes = new ArrayList<>();
for (Bike bike : bikes) {
boolean result = true;
if (criteriaMap.containsKey("type")) {
result = bike.getType().equalsIgnoreCase(criteriaMap.get("type"));
}
if (criteriaMap.containsKey("gear")) {
result = result && bike.getNoOfGears() == Integer.parseInt(criteriaMap.get("gear"));
}
if (criteriaMap.containsKey("wheelbase")) {
result = result && bike.getWheelBase() == Integer.parseInt(criteriaMap.get("wheelbase"));
}
if (criteriaMap.containsKey("height")) {
result = result && bike.getHeight() == Integer.parseInt(criteriaMap.get("height"));
}
if (criteriaMap.containsKey("color")) {
result = result && bike.getColor().equalsIgnoreCase(criteriaMap.get("color"));
}
if (criteriaMap.containsKey("material")) {
result = result && bike.getConstructionMaterial().equalsIgnoreCase(criteriaMap.get("material"));
}
if (result) {
filteredBikes.add(bike);
}
}
sortByCriteria(filteredBikes, criteriaMap.get("sortBy"));
return filteredBikes;
}
// Sort By The Criteria Passed
private static void sortByCriteria(List<Bike> bikes, String sortBy) {
if (sortBy.equals("type")) {
Collections.sort(bikes, new TypeComparator());
} else if (sortBy.equals("gear")) {
Collections.sort(bikes, new GearComparator());
} else if (sortBy.equals("wheelbase")) {
Collections.sort(bikes, new WheelbaseComparator());
} else if (sortBy.equals("height")) {
Collections.sort(bikes, new HeightComparator());
} else if (sortBy.equals("color")) {
Collections.sort(bikes, new ColorComparator());
} else if (sortBy.equals("material")) {
Collections.sort(bikes, new MaterialComparator());
}
}
// Removing the "-" from the argument.
private static String removeHyphenFromParameter(String arg) {
if (arg.contains("-")) {
return arg.substring(arg.indexOf("-") + 1);
}
return arg;
}
//driver method
public static void main(String args[]) {
// Adding arguments to a Map. Each argument has a value associated with it
// except the sort criteria so adding the sorting criteria explicitly.
// Iterting the jump by 2 as each pair will have one key and the other value.
// If no criteria is passed all the bikes will be returned.
//Assumption sort criteria will be passed if any other argument is being passed.
for (int i = 0; i < args.length - 1; i = i + 2) {
// Removing the "-" from the argument.
args[i] = removeHyphenFromParameter(args[i]);
criteriaMap.put(args[i], args[i + 1]);
}
// If no arguments are passed all the bikes will be returned sorted by type.
if (args.length == 0) {
criteriaMap.put("sortBy", "type");
} else {
criteriaMap.put("sortBy", removeHyphenFromParameter(args[args.length - 1]));
}
for (Bike bike : getFilteredBikesByCriteria(criteriaMap, getBikeDataFromFile())) {
System.out.println(bike);
}
}
}
File Content:
FileName: BikeData.txt
Content:
Type,NoOfGears,Wheelbase,Height,Color,ConstructionMaterial
mountain_bike,4,36,3,red,carbon
mountain_bike,5,48,4,blue,steel
mountain_bike,6,48,1,black,aluminium
race_bike,10,36,3,steel,carbon
race_bike,9,42,4,blue,steel
race_bike,8,48,1,black,aluminium
street_bike,4,36,3,red,carbon
street_bike,5,60,4,blue,steel
street_bike,6,48,1,black,aluminium
Sample Input: -color blue -wheelbase
Output:
Bike [type=race_bike, noOfGears=9, wheelBase=42, height=4, color=blue, constructionMaterial=steel]
Bike [type=mountain_bike, noOfGears=5, wheelBase=48, height=4, color=blue, constructionMaterial=steel]
Bike [type=street_bike, noOfGears=5, wheelBase=60, height=4, color=blue, constructionMaterial=steel]
Sample Input: -type mountain_bike -gear 5 -color
Output:
Bike [type=mountain_bike, noOfGears=5, wheelBase=48, height=4, color=blue, constructionMaterial=steel]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.