I have a school project where I am supposed to make a car rental application in BlueJ using Java. For one part, I have 2 arrays, one for the price and one for the name of the car. I have to print the name of the car in descending order of the price. I have managed to sort the price array in descending order using bubble sort but I am not able to figure out how to print the name of the car when I sorted the price array. Please help.
String carModel[] = {"A", "B", "C"}; //Names of cars
int costPerDay[] = {100, 75, 250}; //Rental cost per day
for(int x = 0; x < costPerDay.length-1; x++) { //Sort the Cost Per Day Array in descending order using bubble sort
for(int j = x + 1; j < costPerDay.length; j++) {
if(costPerDay[x] < costPerDay[j]) {
int t = costPerDay[x];
costPerDay[x] = costPerDay[j];
costPerDay[j] = t;
}
}
}
This is the code snippet. I need to print the names of the cars in the descending order of their corresponding cost.
Thanks in advance!
String carModel[] = {"A", "B", "C"}; //Names of cars
int costPerDay[] = {100, 75, 250}; //Rental cost per day
for(int x = 0; x < costPerDay.length-1; x++){ //Sort the Cost Per Day Array in descending order using bubble sort
for(int j = x + 1; j < costPerDay.length; j++){
if(costPerDay[x] < costPerDay[j]){
int t = costPerDay[x];
String s = carModel[x];
costPerDay[x] = costPerDay[j];
costPerDay[j] = t;
carModel[x] = carModel[j];
carModel[j] = s;
}
}
}
for(int x = 0; x < carModel.length; x++){
System.out.println(carModel[i]);
}