Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java DB Query Assume that a bicycle has the following attributes: 1. Type (mount

ID: 3873134 • Letter: J

Question

Java DB Query Assume that a bicycle has the following attributes: 1. Type (mountain bike, race bike or street bike) 2. Number of gears (4-10, in increments of 1) 3. Wheel base (36- 60 inches, in increments of 6 inches) 4. Height (1, 2,3,4) 5. Color (steel, red, blue, black) 6. 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 Linked List data structure, but cannot use existing linked list classes in Java. You have to use try-catch constructs for error handling.

Explanation / Answer

import java.io.*;

import java.util.*;

public class ShowBikes{

public static class Bicycle{

String type;

int gears;

int wheelbase;

int height;

String color;

String material;

Bicycle(String type,int gears,int wheelbase,int height,String color, String material){

this.type=type;

this.gears=gears;

this.wheelbase=wheelbase;

this.height=height;

this.color=color;

this.material=material;

}

}

public static void main(String args[]){

ArrayList ar = new ArrayList();

ArrayList result = new ArrayList();

BufferedReader br = null;

FileReader fr = null;

try {

fr = new FileReader("bikes.txt");

br = new BufferedReader(fr);

String line;

while ((line = br.readLine()) != null) {

StringTokenizer stk = new StringTokenizer(line,",");

String type=stk.nextToken();

int gears = Integer.parseInt(stk.nextToken());

int wheelbase = Integer.parseInt(stk.nextToken());

int height = Integer.parseInt(stk.nextToken());

String color=stk.nextToken();

String material=stk.nextToken();

Bicycle b = new Bicycle(type,gears,wheelbase,height,color,material);

ar.add(b);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (br != null)

br.close();

if (fr != null)

fr.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

for(int i=0;i<args.length-1;i=i+2){

String type="";

int gears = 0;

int wheelbase = 0;

int height = 0;

String color="";

String material="";

if(args[i].equals("-type"))type=args[i+1];

if(args[i].equals("-gear"))gears=Integer.parseInt(args[i+1]);

if(args[i].equals("-wheelbase"))wheelbase=Integer.parseInt(args[i+1]);

if(args[i].equals("-height"))height=Integer.parseInt(args[i+1]);

if(args[i].equals("-color"))color=args[i+1];

if(args[i].equals("-material"))material=args[i+1];

for (i = 0; i < ar.size(); i++) {

Bicycle b = (Bicycle)ar.get(i);

if(!type.equals("") && type.equals(b.type))result.add(b);

else result.remove(b);

if(gears!=0 && gears==b.gears)result.add(b);

else result.remove(b);

if(wheelbase!=0 && wheelbase==b.wheelbase)result.add(b);

else result.remove(b);

if(height!=0 && height==b.height)result.add(b);

else result.remove(b);

if(!color.equals("") && color.equals(b.color))result.add(b);

else result.remove(b);

if(!material.equals("") && material.equals(b.material))result.add(b);

else result.remove(b);

}

if(args[args.length-1].equals("-gear")){

Collections.sort(result, new Comparator<Bicycle>(){

public int compare(Bicycle b1, Bicycle b2){

if(b1.gears == b2.gears)

return 0;

return b1.gears < b2.gears ? -1 : 1;

}

});

}

if(args[args.length-1].equals("-wheelbase")){

Collections.sort(result, new Comparator<Bicycle>(){

public int compare(Bicycle b1, Bicycle b2){

if(b1.wheelbase == b2.wheelbase)

return 0;

return b1.wheelbase < b2.wheelbase ? -1 : 1;

}

});

}

if(args[args.length-1].equals("-height")){

Collections.sort(result, new Comparator<Bicycle>(){

public int compare(Bicycle b1, Bicycle b2){

if(b1.height == b2.height)

return 0;

return b1.height < b2.height ? -1 : 1;

}

});

}

for (i = 0; i < result.size(); i++) {

Bicycle b = (Bicycle)result.get(i);

System.out.println(b.type+"," + b.gears+"," + b.wheelbase+"," + b.height + "," + b.color + "," + b.material);

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote