简体   繁体   English

如何在另一个 class 中使用 Boolean 方法检查字符串参数?

[英]How to use a Boolean method from one class in another to check a string parameter?

I'm working on an a java assignment for school, it's about a phone calling card system, where there are various classes for each type of calling card, I am stuck on part where I use this supplied CallZone class to check if the zone put in the parameter is valid, I am not sure how to call its isValidZone method in the phonecard class, such as SuperNA10Card class.我正在为学校分配 java,它是关于电话卡系统的,每种类型的电话卡都有不同的课程,我被困在使用这个提供的 CallZone class 来检查区域是否放在参数是有效的,我不知道如何在电话卡class中调用它的isValidZone方法,比如SuperNA10Card class。

I am not sure if this the proper way to use the CallZone class to to check if the paramater zone is vaild.我不确定这是否是使用 CallZone class 检查参数区域是否有效的正确方法。

if(zone.equals("canada")){return true;} 
or
if(CallZone.isValidZone(zone) == true){return true}

here is the CallZone class:这是 CallZone class:

public final class CallZone {

     public static boolean isValidZone(String zone) {
         zone = zone.toLowerCase();
         return (zone.equals("canada") ||
             zone.equals("usa") ||
             zone.equals("europe") ||
             zone.equals("asia") ||
             zone.equals("anz") ||
             zone.equals("latinam") ||
             zone.equals("africa")
         );
     }
}

and here is one of the Phonecard classes, SuperNA10 class, using it to check if the zone is valid:这是电话卡类之一,SuperNA10 class,使用它来检查区域是否有效:

public class SuperNA10 extends PhoneCard{
    final double canMinRate = 0.05;
    final double usMinRate = 0.10;
    final double weeklyMainFee = 0.50;

    public SuperNA10(long no, int passwd){
      super(no, passwd,10.00) //invokes superclass class constructor sets no, passwd and balance to 10.00
    }

    public boolean allowed(String zone){
     if(CallZone.isValidZone(zone) ){
        return true;
      }else{
       return false;
     }


    }

}

I am not sure how to get the CallZone class to check if the parameter in the SuperNA10 class method allowed is valid, also sorry if my question isn't clear or cause confusion, it's my first time posting.我不确定如何获取 CallZone class 来检查 SuperNA10 class 方法中允许的参数是否有效,如果我的问题不清楚或引起混淆,也很抱歉,这是我第一次发布。

Since you have a specific number of call zones, you can use an enum like this:由于您有特定数量的呼叫区域,您可以使用这样的enum

public enum CallZone {

    CANADA, USA, EUROPE; //add any number of zones here

    public static boolean isValid(String zone) {
        return Stream.of(values())
                .anyMatch(s -> s.toString().equalsIgnoreCase(zone));
    }
}

You can also look at using CallZone.valueOf(zone) along with catching IllegalArgumentException , instead of using the Stream as above.您还可以查看使用CallZone.valueOf(zone)以及捕获IllegalArgumentException ,而不是像上面那样使用Stream But I prefer not to use exceptions for non exceptional scenarios.但我不喜欢在非异常情况下使用异常。

And then in your SuperNA10 class, you can use it like:然后在您的SuperNA10 class 中,您可以像这样使用它:

public boolean allowed(String zone) {
    return CallZone.isValid(zone);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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