简体   繁体   中英

Using return values from methods on ArrayList

Yesterday I posted a question about ArrayList not printing the output from methods, it happened to be just a syntax error. Today, however, I encounter a similar problem with that error fixed. I would appreciate it if someone could tell me what's wrong with this code.

My problem is that the ArrayList prints [0.0, 0.0, 2.75] as opposed to the list of prices I expected.

    import java.util.ArrayList;

    public class TransitCalculatorTwo {
    int numberDays;
    int numberRides;
    int numberPlanSeven;
    int numberPlanThirty;
    double totalPriceSeven;
    double totalPriceThirty;
    double pricePerRideSeven;
    double pricePerRideThirty;
    double SinglePrice = 2.75;
    double sevenDayPrice = 33.0;
    double thirtyDayPrice = 127.00;


    public int numberPlanSeven(int numberDays) {
        int planSeven = (int) Math.ceil (numberDays / 7.0);
        return planSeven;
    }
    public int numberPlanThirty(int numberDays) {
        int planThirty = (int) Math.ceil (numberDays / 30.0);
        return planThirty;
    }

    public double totalPriceSeven (int numberPlanSeven, double sevenDayPrice) {
        double totalSeven = numberPlanSeven * sevenDayPrice;
        return totalSeven;
    }

    public double totalPriceThirty (int numberPlanThirty, double thirtyDayPrice) {
        double totalThirty = numberPlanThirty * thirtyDayPrice;
        return totalThirty;
    }

    public double pricePerRideSeven (double totalPriceSeven, int numberRides) {
        double ppRideSeven = totalPriceSeven / numberRides;
        return ppRideSeven;
    }
    public double pricePerRideThirty (double totalPriceThirty, int numberRides) {
        double ppRideThirty = totalPriceThirty / numberRides;
        return ppRideThirty;
    }

    public ArrayList<Double> comparePrice() {
        ArrayList<Double> prices = new ArrayList<Double>(); for (int i=0; i<1; i++) {
            prices.add(pricePerRideSeven);
            prices.add(pricePerRideThirty);
            prices.add(SinglePrice);
        }
        return prices;

    }

    public static void main (String[]args) {
        TransitCalculatorTwo customer = new TransitCalculatorTwo();

        customer.pricePerRideSeven(66.0, 50);
        customer.pricePerRideThirty(127.00, 50);

        System.out.println(customer.comparePrice());

    }
}

You are not setting variable values in the object you have constructed in main. You are setting values to the local variables and not the objects parameters.Your code must look like this:

public double pricePerRideSeven (double totalPriceSeven, int numberRides) {
    this.pricePerRideSeven = totalPriceSeven / numberRides;
    return this.pricePerRideSeven;
}

public double pricePerRideThirty (double totalPriceThirty, int numberRides) {
    this.pricePerRideThirty = totalPriceThirty / numberRides;
    return pricePerRideThirty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM