I am having a bit of trouble on this homework on a couple things
First, they want me to create a method that checks whether the property type is one of the three types : mountain, fixie, cross-country. How do I go about doing this?
Second, they want me to create a method that fixes the string model if contains special characters. I created the method private boolean isValidModel(String model) to check that but am not sure if my fixing method is correct.
Lastly, how do I go about creating several instances of the bike class as mentioned below?
For reference the question is below.
Any help will be greatly appreciated
Write a class called Bike, which has three private properties: model, type, year - provide getters and setters for the properties. Don’t allow invalid data to be stored in the properties – year should be positive number, model should contain only letters, digits and spaces and type should be one of the following: mountain, fixie, cross-country. Provide private methods to validate the model and use it upon changing the model, also provide method to remove any invalid characters from the model so that you could save the new String into the model property.
private boolean isValidModel(String model) private String fixModel(String model) // will return the model, with removed invalid characters
Also provide the public method display, which displays information about the bike: public void display() Write a main program to create several instances of the Bike class, try to set invalid data to the properties and invoke display after each change.
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);
}
You can check if the model is valid using different methods, I have did choose to use RegEx. If you still want to use your solution you have to change the '&&' (AND) symbol to an '||' (OR) symbol.
Bike 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);
}
}
The main method:
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
}
I hope you find this answer helpful.