I’ve a beginner at java programming and I’ve been tasked at creating a database. The database is a bike store with a super class Bike and sub classes mountain bike, road bike. I also have a class called bikeSystem that manages all the subclasses. So far, i’ve taken the approach of setting up a a constructor in the bike system class, and encapsulating with getter and setter methods. Then tried to create a for loop that sets the size of the database based on the attribute variable for bikesystem “bikes” that specifies the size of the database based on whatever value i change it to. Along with an array list that will create an array of size based on the size of the database.
However, this approach has not worked and i keep receiving errors along the lines of:
BikeSystem.java:18: error: constructor BikeSystem in class BikeSystem cannot be applied to given types;
BikeSystem noOfBikes = new BikeSystem();
As well as a bunch of errors relating to non-static and static contexts. Any help or insight into this problem, or possible solutions is really much appreciated, thank you for your time.
import java.util.ArrayList;
public class BikeSystem {
private int bikes;
public BikeSystem(int noOfBikes) {
bikes = noOfBikes;
}
// Getter
public int getBikes() {
return bikes;
}
//Setter
public void setBikes(int newBikes) {
this.bikes = 9;
}
public static void main(String[] args) {
BikeSystem noOfBikes = new BikeSystem();
for (int i = 0; i = noOfBikes; i++) {
ArrayList<Bike> Bike = new ArrayList<Bike>();
}
}
Advertisement
Answer
I made a Bike
class with a color propriety,a BykeSystem
class where you can add lists of bikes,and the test
class where you make the lists.I made it whithout a for loop.You can create then MountainBike
class for example that extends Bike
,add new propriety,like wheight,make a list of them in test class and add it to the BikeSystem
list:
BikeSystem
class :
import java.util.ArrayList;
import java.util.List;
public class BikeSystem {
//here you have to add attributes of bikeSystem
private ArrayList<Bike> bikes;
public BikeSystem(ArrayList<Bike> bikes) {
this.bikes = bikes;
}
public ArrayList<Bike> getBikes() {
return bikes;
}
public void setBikes(ArrayList<Bike> bikes) {
this.bikes = bikes;
}
@Override
public String toString() {
return "BikeSystem [bikes=" + bikes + "]";
}
}
Test
class:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Bike> Bikes = new ArrayList<Bike>();
Bikes.add(new Bike("red"));
Bikes.add(new Bike("blue"));
Bikes.add(new Bike("black"));
Bikes.add(new Bike("white"));
ArrayList<Bike> Bikes1 = new ArrayList<Bike>();
Bikes.add(new Bike("red"));
Bikes.add(new Bike("blue"));
Bikes.add(new Bike("black"));
Bikes.add(new Bike("white"));
ArrayList<BikeSystem> BikeSys = new ArrayList<BikeSystem>();
BikeSys.add(new BikeSystem(Bikes));
BikeSys.add(new BikeSystem(Bikes1));
}
}
Bike
class:
public class Bike {
//here you have to add attributes of bike
private String color;
public Bike(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Bike [color=" + color + "]";
}
}