我在做这份作业时遇到了一些麻烦
首先,他们希望我创建一个方法来检查属性 type 是否是以下三种类型之一:mountain、fixie、 越野。我该如何去做呢?
其次,他们希望我创建一个方法来修复字符串 model(如果包含特殊字符)。我创建了方法 private boolean isValidModel(String model) 来检查这一点,但不确定我的修复方法是否正确。
最后,我如何创建如下所述的自行车类的多个实例?
以下问题可供引用。
任何帮助将不胜感激
编写一个名为 Bike 的类,它具有三个私有(private)属性:model、type、year - 为属性提供 getter 和 setter。不允许在属性中存储无效数据 - 年份应为正数,型号应仅包含字母、数字和空格,类型应为以下类型之一:山地、固定、越野。提供私有(private)方法来验证模型并在更改模型时使用它,还提供从模型中删除任何无效字符的方法,以便您可以将新字符串保存到模型属性中。
私有(private) boolean 值isValidModel(字符串模型) private String fixModel(String model)//将返回模型,并删除无效字符
还提供公共(public)方法display,它显示有关自行车的信息: 公共(public)无效显示() 编写一个主程序来创建 Bike 类的多个实例,尝试为属性设置无效数据,并在每次更改后调用显示。
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)符号更改为“||” (或)符号。
自行车类别:
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
}
希望这个答案对您有所帮助。
关于java - 某些验证方法遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58683407/