【问题标题】:Having trouble with some validating methods某些验证方法遇到问题
【发布时间】:2019-11-03 18:37:33
【问题描述】:

我在这个家庭作业上遇到了一些麻烦

首先,他们希望我创建一个方法来检查属性 type 是否是以下三种类型之一:mountainfixie跨国。我该怎么做?

其次,如果包含特殊字符,他们希望我创建一个修复字符串 model 的方法。我创建了方法 private boolean isValidModel(String model) 来检查,但不确定我的修复方法是否正确。

最后,我该如何创建如下所述的自行车类的多个实例?

下面的问题供参考。

任何帮助将不胜感激

编写一个名为 Bike 的类,它具有三个私有属性:model、type、year - 为这些属性提供 getter 和 setter。不允许在属性中存储无效数据——年份应该是正数,模型应该只包含字母、数字和空格,类型应该是以下之一:山、固定、越野。提供私有方法来验证模型并在更改模型时使用它,还提供从模型中删除任何无效字符的方法,以便您可以将新字符串保存到模型属性中。

私有布尔isValidModel(字符串模型) private String fixModel(String model) // 将返回模型,移除无效字符

还提供公共方法 display,它显示有关自行车的信息: 公共无效显示() 编写一个主程序来创建 Bike 类的多个实例,尝试为属性设置无效数据并在每次更改后调用 display。

public class Bike
{

// Instance field
private String model;
private String type;
private int year;





public Bike(String model, String type, int year )
{
     model = unknown;
     type = unknown;
     year = unknown;
}

//getters
public String getModel()
{
     return model;
}

public String getType()
{
     return type;
}


public int getYear()
{
     return year;
}

//setters
public void setModel( String model )
{

     model = N/A;
}

public void setType( String type )
{

     type = N/A;
}

public void setYear( int year )
{
  if(year < 1970)
     year = 1970;
}










private boolean isValidModel(String model){

int len = model.length();
      for (int i = 0; i < len; i++) {

         if ((Character.isLetterOrDigit(model.charAt(i)) == false) && model.charAt(i)!=' ') {
            return false;
         }
      }
      return true;

}


private String fixModel(String model){


model= model.replaceAll("[^A-Za-z0-9]","");



}






public void display(){

System.out.println("Year: "+year+" Model: "+model+"Type: "+type);


}

【问题讨论】:

    标签: java class object constructor getter-setter


    【解决方案1】:

    您可以使用不同的方法检查模型是否有效,我确实选择使用 RegEx。如果您仍想使用您的解决方案,您必须将“&&”(AND)符号更改为“||” (OR) 符号。

    自行车类:

    public class Bike {
    
        private String model;
        private String type;
        private int year;
    
    
        private boolean isValidModel(String model) {
            // Making a pattern that checks for the requirements
            Pattern pattern = Pattern.compile("[ A-Za-z0-9]*");
            return pattern.matcher(model).matches();
        }
    
        private String fixModel(String model) {
            // Replacing all the bad characters
            return model.replaceAll("[^ A-Za-z0-9]", "");
        }
    
        public String getModel() {
            return model;
        }
    
        public void setModel(String model) {
            // Check if the Model is valid
            if (this.isValidModel(model))
                this.model = model; // If so store the model
            else
                this.model = this.fixModel(model); // Store the fixed model
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            // Check if the type contains one of the hard coded types
            if (type.equals("mountain") || type.equals("fixie") || type.equals("cross-country"))
                this.type = type;
        }
    
        public int getYear() {
            return year;
        }
    
        public void setYear(int year) {
            if (year > 0) // Check if the year is positive
                this.year = year;
        }
    
        public void display() {
            // Displaying the stored information
            System.out.println("Year:" + year + " Model:" + model + " Type:" + type);
        }
    }
    

    主要方法:

    public static void main(String[] args) {
        Bike bike1 = new Bike(); // Creating the first Bike instance
        bike1.setModel("This Is Valid"); // Valid model
        bike1.setYear(10); // Valid year
        bike1.setType("cross-country"); // Valid type
    
        Bike bike2 = new Bike(); // Creating the second Bike instance
        bike2.setModel("This-Is-Invalid"); // Invalid model
        bike2.setYear(-1); // Invalid year
        bike2.setType("Invalid Type"); // Invalid type
    
        bike1.display(); // Expected Year:10 Model:This Is Valid Type:cross-country
        bike2.display(); // Expected Year:0 Model:ThisIsInvalid Type:null
    }
    

    希望这个答案对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多