/**-----------------------------PROGRAM TEST/TEST DEALER/MAIN METHOD--------------------------------**/
public class testdealerchanging {
public static void main(String[] args) {
CarDealerShip dealer = new CarDealerShip();
dealer.addNewCar("GM Buick Century", 2004, 20000,"Silver");
dealer.addUsedCar("Toyota Corolla", 1999, 9000, true);
dealer.addNewCar("Honda Civic", 2004, 15000, "Green");
dealer.addNewCar("BMW 320i", 2004, 35000, "Black");
dealer.addUsedCar("Toyota Sienna", 2000, 11000, false);
System.out.println(dealer.printReport());
System.out.println("****************************************");
System.out.println(dealer.printAllCarsWithSellingPriceBelow(10000));
System.out.println("****************************************");
**System.out.println(dealer.printAllCarsOfColor("Green"));
}**
}
/**-----------------------------DEALERSHIP--------------------------------**/
class CarDealerShip {
Car[] cars;
int newCarIndex[] = new int[3];
int count=0, countNewCar;
int priceLimit = 0;
String colorInput = "";
public CarDealerShip() {
cars = new Car[81];
count=0;
countNewCar=0;
}
public void addNewCar(String name, int date, int amount, String c) {
NewCar newcar = new NewCar(name, date, amount, c); //create newcar object
cars[count]=newcar;
newCarIndex[countNewCar]=count;
count++;
countNewCar++;
}
public void addUsedCar(String name, int date, int amount, boolean r) {
UsedCar usedcar = new UsedCar(name, date, amount, r);
cars[count]=usedcar;
count++;
}
public String printReport() {
String s=""; //prevent default null output
for (int i=0; i<count; i++)
s+="car " +(i+1)+ ": " + cars[i].toString()+"Selling Price:" +cars[i].getRprice()+"\n"; //sum of all loops
// s+=1+2 same as s=s+1+2
return s;
}
public String printAllCarsWithSellingPriceBelow (int price) {
String s="";
priceLimit=price;
for (int i=0; i<count; i++) {
if (cars[i].getPrice() < priceLimit)
s+="cars below " + priceLimit + ": " + cars[i].toString()+"Selling Price:" +cars[i].getRprice()+"\n";
}
return s;
}
public String printAllCarsOfColor(String color) {
String s="";
colorInput=color;
for (int i=0; i<count; i++) {
**if (i == newCarIndex[i]) {**
if (((NewCar)cars[i]).getColor() == "Green")
s+="cars with color " + "Green" + (i+1) + ": " + `enter code here`enter code here`cars[i].toString()+"Selling Price:" +cars[i].getPrice()+"\n";
}
}
return s;
}
}
I'm having trouble with my cardealership program on printing all green colors
I get the errors I placed bolded in my program, I don't understand why I'm getting these errors.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at CarDealerShip.printAllCarsOfColor(testdealerchanging.java:179)
at testdealerchanging.main(testdealerchanging.java:20)
Aucun commentaire:
Enregistrer un commentaire