School Commit Init
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package Controller;
|
||||
|
||||
import Model.IVehicle;
|
||||
import Repository.IRepository;
|
||||
|
||||
public class Controller {
|
||||
private IRepository _repository;
|
||||
|
||||
public Controller(IRepository repository) {
|
||||
this._repository = repository;
|
||||
}
|
||||
|
||||
public void addVehicle(IVehicle vehicle) throws IllegalArgumentException {
|
||||
_repository.add(vehicle);
|
||||
}
|
||||
|
||||
public void removeVehicle(int index) throws IllegalArgumentException {
|
||||
_repository.remove(index);
|
||||
}
|
||||
|
||||
public IVehicle[] getAllVehicles() {
|
||||
return _repository.getAll();
|
||||
}
|
||||
|
||||
public IVehicle[] filterVehicles(String color){
|
||||
IVehicle[] vehicles = _repository.getAll();
|
||||
IVehicle[] filteredVehicles = new IVehicle[_repository.getSize()];
|
||||
int index = 0;
|
||||
for(IVehicle vehicle : vehicles) {
|
||||
if(vehicle.getColor().equals(color)) {
|
||||
filteredVehicles[index++] = vehicle;
|
||||
}
|
||||
}
|
||||
IVehicle[] copy = new IVehicle[index];
|
||||
for(int i = 0; i < index; i++) {
|
||||
copy[i] = filteredVehicles[i];
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package Model;
|
||||
|
||||
public class Bicycle implements IVehicle {
|
||||
|
||||
private String _brand;
|
||||
private String _color;
|
||||
|
||||
public Bicycle(String brand, String color) {
|
||||
this._brand = brand;
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return this._brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) throws IllegalArgumentException {
|
||||
if (brand == null || brand.length() < 3) {
|
||||
throw new IllegalArgumentException("Brand must be at least 3 characters long");
|
||||
}
|
||||
this._brand = brand;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
public void setColor(String color) throws IllegalArgumentException {
|
||||
if (color == null || color.length() < 3) {
|
||||
throw new IllegalArgumentException("Color must be at least 3 characters long");
|
||||
}
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bicycle{" +
|
||||
"brand='" + _brand + '\'' +
|
||||
", color='" + _color + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package Model;
|
||||
|
||||
public class Car implements IVehicle {
|
||||
|
||||
private String _brand;
|
||||
private String _color;
|
||||
|
||||
public Car(String brand, String color) {
|
||||
this._brand = brand;
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return this._brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) throws IllegalArgumentException {
|
||||
if (brand == null || brand.length() < 3) {
|
||||
throw new IllegalArgumentException("Brand must be at least 3 characters long");
|
||||
}
|
||||
this._brand = brand;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
public void setColor(String color) throws IllegalArgumentException {
|
||||
if (color == null || color.length() < 3) {
|
||||
throw new IllegalArgumentException("Color must be at least 3 characters long");
|
||||
}
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{" +
|
||||
"brand='" + _brand + '\'' +
|
||||
", color='" + _color + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package Model;
|
||||
|
||||
public interface IVehicle {
|
||||
String getBrand();
|
||||
void setBrand(String brand) throws IllegalArgumentException;
|
||||
String getColor();
|
||||
void setColor(String color) throws IllegalArgumentException;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package Model;
|
||||
|
||||
public class Motorcycle implements IVehicle{
|
||||
|
||||
private String _brand;
|
||||
private String _color;
|
||||
|
||||
public Motorcycle(String brand, String color) {
|
||||
this._brand = brand;
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return this._brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) throws IllegalArgumentException {
|
||||
if (brand == null || brand.length() < 3) {
|
||||
throw new IllegalArgumentException("Brand must be at least 3 characters long");
|
||||
}
|
||||
this._brand = brand;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
public void setColor(String color) throws IllegalArgumentException {
|
||||
if (color == null || color.length() < 3) {
|
||||
throw new IllegalArgumentException("Color must be at least 3 characters long");
|
||||
}
|
||||
this._color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Motorcycle{" +
|
||||
"brand='" + _brand + '\'' +
|
||||
", color='" + _color + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package Repository;
|
||||
|
||||
import Model.IVehicle;
|
||||
|
||||
public interface IRepository {
|
||||
public void add(IVehicle vehicle) throws IllegalArgumentException;
|
||||
public void remove(int index) throws IllegalArgumentException;
|
||||
public IVehicle[] getAll();
|
||||
public int getSize();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package Repository;
|
||||
|
||||
import Model.IVehicle;
|
||||
|
||||
public class VehicleRepo implements IRepository {
|
||||
|
||||
private IVehicle[] _vehicles;
|
||||
private Integer _capacity;
|
||||
private Integer _size;
|
||||
|
||||
public VehicleRepo(Integer capacity) throws IllegalArgumentException {
|
||||
if(capacity < 0) {
|
||||
throw new IllegalArgumentException("Capacity must be a positive number");
|
||||
}
|
||||
this._capacity = capacity;
|
||||
this._vehicles = new IVehicle[capacity];
|
||||
this._size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(IVehicle vehicle) throws IllegalArgumentException {
|
||||
if(_size == _capacity) {
|
||||
throw new IllegalArgumentException("Repository is full");
|
||||
}
|
||||
_vehicles[_size] = vehicle;
|
||||
_size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) throws IllegalArgumentException {
|
||||
if(_size == 0) {
|
||||
throw new IllegalArgumentException("Repository is empty");
|
||||
}
|
||||
if(index < 0 || index > _size) {
|
||||
throw new IllegalArgumentException("Index out of bounds");
|
||||
}
|
||||
for(int i = index; i < _size - 1; i++) {
|
||||
_vehicles[i] = _vehicles[i + 1];
|
||||
}
|
||||
_size--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IVehicle[] getAll() {
|
||||
IVehicle[] copy = new IVehicle[_size];
|
||||
for(int i = 0; i < _size; i++) {
|
||||
copy[i] = _vehicles[i];
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return _size;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package View;
|
||||
|
||||
import Controller.Controller;
|
||||
import Model.Car;
|
||||
import Model.IVehicle;
|
||||
import Model.Motorcycle;
|
||||
import Model.Bicycle;
|
||||
import Repository.IRepository;
|
||||
import Repository.VehicleRepo;
|
||||
|
||||
public class View {
|
||||
public static void main(String[] args) {
|
||||
IRepository repo;
|
||||
try{
|
||||
repo = new VehicleRepo(-5);
|
||||
}
|
||||
catch(IllegalArgumentException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
}
|
||||
Controller controller = new Controller(repo);
|
||||
Car car = new Car("BMW", "Red");
|
||||
Motorcycle motorcycle = new Motorcycle("Honda", "Black");
|
||||
Bicycle bicycle = new Bicycle("Trek", "Blue");
|
||||
Bicycle bicycle2 = new Bicycle("Trek", "Red");
|
||||
try{
|
||||
controller.addVehicle(car);
|
||||
controller.addVehicle(motorcycle);
|
||||
controller.addVehicle(bicycle);
|
||||
controller.addVehicle(bicycle2);
|
||||
}
|
||||
catch(IllegalArgumentException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
}
|
||||
IVehicle[] filteredVehicles = controller.filterVehicles("Red");
|
||||
for(IVehicle vehicle : filteredVehicles) {
|
||||
System.out.println(vehicle.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user