lundi 14 novembre 2016

how can I change the if statements to boolean statements in java?

I am trying to create a prgram that calculates the cost of different Vehicles. The cost varies depending on the day (When Weekday--> cost= price ) and (When Weekend--> cost= price * (3/4) ). In order to achive this I used if statements but they dont work as expected.

day == "true" // when Weekday

day == "false" //when Weekend

I though of using boolean statements but I dont really know how to implemnt it in my program

P.S. I am new at java

Here is the Method with the cost of different Vehicles

// Vehicle Cost Method

public void VehicleCost() {


// Motorbike Cost
if ((Class == "Class 1") & (day == "true" )) { cost = 2.00; }       
if ((Class == "Class 1") & (day == "false")) { cost = 2.00 * WE; }      

// Car Cost
if ((Class == "Class 2") & (day == "true"   )) { cost = 3.20; }
if ((Class == "Class 2") & (day == "false"  )) { cost = 3.20 * WE; }

// Car with trailer Cost
if ((Class == "Class 3") & (day == "true"   )) { cost = 3.80; }
if ((Class == "Class 3")& (day == "false"  )) { cost = 3.80 * WE; } 

// Van Cost
if ((Class == "Class 4") & (day == "true"   )) { cost = 4.40; }
if ((Class == "Class 4") & (day == "false"  )) { cost = 4.40 * WE; }

// Small Lorry/Bus Cost
if ((Class == "Class 5") & (day == "true"   )) { cost = 8.00 ; }
if ((Class == "Class 5") & (day == "false"  )) { cost = 8.00 * (3/4); }

// Large lorry Cost
if ((Class == "Class 6") & (day == "true"   )) { cost = 12.00; }
if ((Class == "Class 6") & (day == "false"  )) { cost = 12.00 * WE; }




}

And this is the entire program

    class TunnelTollCharge {

    // ------------------- FIELDS ------------------------               

    private int wheels;
    private double length;
    private int axles;
    private double weight;      
    private String day;     


    private String Class;   
    private String type;
    private double cost;


    //Weekend Constant
    private static final double WE= 0.75;


    // ------------------ CONSTRUCTORS (inputs) -------------------

    /* Constructor */

        public TunnelTollCharge(int newWheels,double newLength,int newAxles, double newWeight, String newDay) {
            wheels = newWheels;
            length = newLength;
            axles = newAxles;
            weight = newWeight;
            day = newDay;
}

    // ------------------ METHODS ------------------------

  //Vehicle Class Method

 public void VehicleClass() {

 /* Class1 --> When (Wheels <4) */

        if ( wheels < 4 ) { Class = "Class 1"; }


    /* Class2 --> When (Wheels == 4) & (length <=15) && (axles = 2) */


        if (( wheels == 4 ) & (length <= 15 )) { Class = "Class 2"; }


    /* Class3 --> When (Wheels >= 4) & (length >15) & (axles >=3 & <= 6) & (weight <= 2)  */


        if ((wheels >= 4) & (length >15) & (axles >=3 & axles<= 6) & (weight <= 2)) { Class = "Class 3"; }


    /* Class4 --> When (Wheels == 4) & (length >15) & (axles =2) */


        if ((wheels == 4) & (length >15) & (axles ==2)) { Class = "Class 4"; }


    /* Class5 --> When (Wheels >= 4) & (length >15) & (axles >=3) & (weight >2 & <= 3.5) */


        if ((wheels >= 4) & (length >15) & (axles >=3) & (weight >2 & weight<= 3.5)) { Class = "Class 5"; }


    /* Class6 --> When (Wheels >= 4) & (length >15) & (axles >6) & (weight > 3.5) */


        if ((wheels >= 4) & (length >15) & (axles >6) & (weight > 3.5)) { Class = "Class 6"; }
    }


    // Vehicle Type Method

    public void VehicleType() {

    if (Class == "Class 1" ) { type = "Motorbike"; }        // Motorbike Type

    if (Class == "Class 2" ) { type = "Car"; }          // Car Type

    if (Class == "Class 3" ) { type = "Car with trailer"; }     // Car with trailer Type

    if (Class == "Class 4" ) { type = "Van"; }          // Van Type

    if (Class == "Class 5" ) { type = "Small Lorry/Bus"; }      // Small Lorry/Bus Type

    if (Class == "Class 6" ) { type = "Large lorry"; }      // Large lorry Type


    }   


// Vehicle Cost Method

    public void VehicleCost() {


    // Motorbike Cost
    if ((Class == "Class 1") & (day == "true" )) { cost = 2.00; }       
    if ((Class == "Class 1") & (day == "false")) { cost = 2.00 * WE; }      

    // Car Cost
    if ((Class == "Class 2") & (day == "true"   )) { cost = 3.20; }
    if ((Class == "Class 2") & (day == "false"  )) { cost = 3.20 * WE; }

    // Car with trailer Cost
    if ((Class == "Class 3") & (day == "true"   )) { cost = 3.80; }
    if ((Class == "Class 3")& (day == "false"  )) { cost = 3.80 * WE; } 

    // Van Cost
    if ((Class == "Class 4") & (day == "true"   )) { cost = 4.40; }
    if ((Class == "Class 4") & (day == "false"  )) { cost = 4.40 * WE; }

    // Small Lorry/Bus Cost
    if ((Class == "Class 5") & (day == "true"   )) { cost = 8.00 ; }
    if ((Class == "Class 5") & (day == "false"  )) { cost = 8.00 * (3/4); }

    // Large lorry Cost
    if ((Class == "Class 6") & (day == "true"   )) { cost = 12.00; }
    if ((Class == "Class 6") & (day == "false"  )) { cost = 12.00 * WE; }




    }
    /* Output Class, Type and Cost */

    public String toString() {
        return("\nA Wheels: "+ wheels + ", length: " + length + ", axles:" + axles + ", weight:" + weight + ", day: " + day +
         "\nVehicle class = " + Class +
         "\nVehicle Type = " + type +
         "\nVehicle Cost = " + cost +
         "\n"

         );
        }
    }

Aucun commentaire:

Enregistrer un commentaire