一些验证方法有问题

Having trouble with some validating methods

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

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

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

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

以下问题供参考。

任何帮助将不胜感激

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

private boolean isValidModel(字符串模型) private String fixModel(String model) // 将 return 模型,删除无效字符

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

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);


}

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

自行车class:

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
}

希望此回答对您有所帮助。