简体   繁体   中英

method that takes no arguments and returns double

I need to write a public method costMultiplier() that takes no arguments and returns a double. The method should return 1.2 for postcodes beginning “WC1A” or “EC1A”, and 1.0 otherwise.

This is what I have so far but I have added an argument. I am not sure how to answer the question without the argument and how would I test ir accurately. Many thanks

public double costMultiplier(final String postCode) {
    double multiplier = 1.0d; `

    String pc = postCode.trim().toUpperCase(); 

    if (pc.startsWith("WC1A") || pc.startsWith("EC1A")) {
        multiplier = 1.2; 
    }

    return multiplier;
}

or I did this but need to include a String???

`public static double costMultiplier(){

if(postCode.contains("WC")

return 1.2; } else if(postCode.contains("EC") { return 1.0;}

Perhaps you can create a PostCode class like:

public class PostCode {

  private final String code;

  public PostCode(String code) {
    this.code = code.trim().toUpperCase();
  }

  public double costMultiplier() {
    if (code.startsWith("WC1A") || code.startsWith("EC1A")) {
        return 1.2; 
    }
    return 1.0
  }

}

then you can do:

new PostCode("WC1A").costMultiplier()

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