Why getRoadtaxPrice() function didn't get typeVehicle input value?

Viewed 19

I am still beginner in programming. This is my first year. I have some problem with my code. I hope you guys can help me with it.

This is my main (VehicleApp.java)

public static void main(String[] args) {
    // TODO code application logic here
    int acc, brake;
    String type, powerTrain;
    
    Scanner sc = new Scanner(System.in);
    Vehicle vehicle = new Vehicle("Car", "Manual", "Electric");               
           
    //input
    
    System.out.println("Type of vehicle? [Car, SUV, MPV, Lorry]: ");
    type = sc.nextLine();
    vehicle.setType(type);
    
    if(type.equals("Lorry")){
        System.out.println("Enter Accelerate: ");
        acc = sc.nextInt();
        vehicle.autoAccelerate(acc);
    
        System.out.println("Enter Brake: ");
        brake = sc.nextInt();
        vehicle.autobrake(brake);
        
        //output
        System.out.println("===========================================\n");
        System.out.println(vehicle.getPowertrain()+" "+vehicle.getType()+".");
        System.out.println("\nAccelerate:\nCurrent Speed = "+vehicle.autoAccelerate(acc)+" kmh");      
        System.out.println("\nBrake:\nCurrent Speed = "+vehicle.autobrake(brake)+" kmh");
        System.out.println("\nPrice: RM "+vehicle.getRoadtaxPrice());
    }
    else{
        System.out.println("Type of powertrain? [Gasoline, Hybrid, Electric]: ");
        powerTrain = sc.nextLine();
        vehicle.setPowertrain(powerTrain);
    
        System.out.println("Enter Accelerate: ");
        acc = sc.nextInt();
        vehicle.autoAccelerate(acc);
    
        System.out.println("Enter Brake: ");
        brake = sc.nextInt();
        vehicle.autobrake(brake);
    
        //output
        System.out.println("===========================================\n");
        System.out.println(vehicle.getPowertrain()+" "+vehicle.getType()+".");
        System.out.println("\nAccelerate:\nCurrent Speed = "+vehicle.autoAccelerate(acc)+" kmh");      
        System.out.println("\nBrake:\nCurrent Speed = "+vehicle.autobrake(brake)+" kmh");
        System.out.println("\nPrice: RM "+vehicle.getRoadtaxPrice());
    }
}

This is my class (Vehicle.java)

public class Vehicle {
private String typeVehicle, transmissionVehicle, powertrainVehicle;
private int speedVehicle;

public Vehicle(String type, String transmission, String powertrain){
    this.typeVehicle = type;
    this.transmissionVehicle = transmission;
    this.powertrainVehicle = powertrain;
    speedVehicle = 60;
}
        
public int accelerate(){
    return speedVehicle+=10;
}

public int brake(){
    return speedVehicle-=5;
}

//Getter
public String getType(){
    return typeVehicle;
}

//Setter
public void setType(String type){
    //type = this.typeVehicle;
    this.typeVehicle = type;
}
 
//Lab3 Start
public int autoAccelerate(int accelerate){
    for(int i = 0; i < accelerate; i++){
        speedVehicle += 10;
    }
    return speedVehicle;
}

public int autobrake(int brake){
    for(int i = 0; i < brake; i++){
        speedVehicle -= 5;
        
        if(speedVehicle <= 0){
            speedVehicle = 0;
        }
    }
    return speedVehicle;
}

public double getRoadtaxPrice(){
    double price = 0;
    
    if (typeVehicle == "Car"){
                  
        if (powertrainVehicle == "Gasoline"){
            price = 200;
        }
        else if (powertrainVehicle == "Hybrid"){
            price = 100;
        }
        else if (powertrainVehicle == "Electric"){
            price = 50;
        }
        else{
            price = 0;
        } 
        
    }
    
    else if (typeVehicle == "SUV" || typeVehicle == "MPV"){
                    
        if (powertrainVehicle == "Gasoline"){
            price = 300;
        }
        else if (powertrainVehicle == "Hybrid"){
            price = 150;
        }
        else if (powertrainVehicle == "Electric"){
            price = 100;
        }
        else{
            price = 0;
        }                
        
    }
    
    else if (typeVehicle == "Lorry"){
        
        powertrainVehicle = "All";
        price = 400;
                    
    }
     
    return price;
}
//Lab3 End
      
//Getter
public String getTransmission(){
    return transmissionVehicle;
}

//Setter
public void setTransmission(String transmission){
    transmission = this.transmissionVehicle;
}

//Getter
public String getPowertrain(){
    return powertrainVehicle;
}

//Setter
public void setPowertrain(String powertrain){
    powertrain = this.powertrainVehicle;
}

public void output(){
    System.out.println("Type of vehicle: "+getType());
}

}

My input:

Type of vehicle? [Car, SUV, MPV, Lorry]: 
MPV
Type of powertrain? [Gasoline, Hybrid, Electric]: 
Hybrid
Enter Accelerate: 
2
Enter Brake: 
2

My output:

===========================================

Electric MPV.

Accelerate:
Current Speed = 90 kmh

Brake:
Current Speed = 80 kmh

Price: RM 0.0

The output should be like this:

===========================================

Hybrid MPV.

Accelerate:
Current Speed = 90 kmh

Brake:
Current Speed = 80 kmh

Price: RM 150.0

There are problems with the Price and Powertrain. Please help me! Thank you.

1 Answers

My first thought is that vehicle.typeVehicle or vehicle.powertrainVehicle have an extra space at the end of them you aren't accounting for. But we can't know that just from your code. I'd suggest you learn to step through your code using your IDE's debugger. Then watch how it goes through the code as well as exactly what values each variable and property are getting.


That was going to be my comment, then I looked at the code a little harder and found the mistake. And you have more than one. Stepping through your code with an IDE's debugger should help you solve this kind of problem, which is why I'm leaving that part in.

Your setPowertrain and setTransmission methods have the same bug. You are setting the parameter to the value of the existing property, instead of the other way around. You are doing it correctly in your constructor and setType, though.

public void setPowertrain(String powertrain){
    // powertrain = this.powertrainVehicle;
    this.powertrainVehicle = powertrain;
}

public void setTransmission(String transmission){
    // transmission = this.transmissionVehicle;
    this.transmissionVehicle = transmission;
}

Everyone misses things at times, which is why having a 2nd set of eyes on code is so great. There's even a semi-official idea of "rubber ducking", where people keep something like a rubber duck at their desk. Then whenever they run into a problem, they start explaining it to the duck as if they were showing someone else the problem. This helps puts your brain into a different mode and you can hopefully see your problems/issues/bugs/blocks/whatever with sort of a new set of eyes.

Related