diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/libraries/junit.xml b/.idea/libraries/junit.xml new file mode 100644 index 000000000..e0e3044a8 --- /dev/null +++ b/.idea/libraries/junit.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..7d53ab542 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..93173df29 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 000000000..2b63946d5 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/READMEoldLab.md b/READMEoldLab.md new file mode 100644 index 000000000..da0814311 --- /dev/null +++ b/READMEoldLab.md @@ -0,0 +1,3 @@ +# laboration-2 + +Se Canvas för instruktioner. diff --git a/Refaktoriseringsplan b/Refaktoriseringsplan new file mode 100644 index 000000000..fe1704c95 --- /dev/null +++ b/Refaktoriseringsplan @@ -0,0 +1,6 @@ +Dela upp vehicle till olika klasser och ett interface +Formaterar vehicle så att den i praktiken blir en ny vehicle-klass +Ändrar mycket subklassning till komposition +Ändar vilka klasser som är relaterade till varandra, ex. är inte Car och Truck relaterade till Vehicle + +nice \ No newline at end of file diff --git a/Saab95.java b/Saab95.java deleted file mode 100644 index e032a0244..000000000 --- a/Saab95.java +++ /dev/null @@ -1,79 +0,0 @@ -import java.awt.*; - -public class Saab95{ - - public boolean turboOn; - public int nrDoors; // Number of doors on the car - public double enginePower; // Engine power of the car - public double currentSpeed; // The current speed of the car - public Color color; // Color of the car - public String modelName; // The car model name - - public Saab95(){ - nrDoors = 2; - color = Color.red; - enginePower = 125; - turboOn = false; - modelName = "Saab95"; - stopEngine(); - } - - public int getNrDoors(){ - return nrDoors; - } - public double getEnginePower(){ - return enginePower; - } - - public double getCurrentSpeed(){ - return currentSpeed; - } - - public Color getColor(){ - return color; - } - - public void setColor(Color clr){ - color = clr; - } - - public void startEngine(){ - currentSpeed = 0.1; - } - - public void stopEngine(){ - currentSpeed = 0; - } - - public void setTurboOn(){ - turboOn = true; - } - - public void setTurboOff(){ - turboOn = false; - } - - public double speedFactor(){ - double turbo = 1; - if(turboOn) turbo = 1.3; - return enginePower * 0.01 * turbo; - } - - public void incrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() + speedFactor() * amount; - } - - public void decrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() - speedFactor() * amount; - } - - // TODO fix this method according to lab pm - public void gas(double amount){ - incrementSpeed(amount); - } - - // TODO fix this method according to lab pm - public void brake(double amount){ - decrementSpeed(amount); - } -} diff --git a/TDA553-lab1.iml b/TDA553-lab1.iml new file mode 100644 index 000000000..ae37f7eb7 --- /dev/null +++ b/TDA553-lab1.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UML-diagram 1.png b/UML-diagram 1.png new file mode 100644 index 000000000..9060633ec Binary files /dev/null and b/UML-diagram 1.png differ diff --git a/UML-diagram 2.png b/UML-diagram 2.png new file mode 100644 index 000000000..f138b4d71 Binary files /dev/null and b/UML-diagram 2.png differ diff --git a/Volvo240.java b/Volvo240.java deleted file mode 100644 index 283eacd9c..000000000 --- a/Volvo240.java +++ /dev/null @@ -1,68 +0,0 @@ -import java.awt.*; - -public class Volvo240{ - - public final static double trimFactor = 1.25; - public int nrDoors; // Number of doors on the car - public double enginePower; // Engine power of the car - public double currentSpeed; // The current speed of the car - public Color color; // Color of the car - public String modelName; // The car model name - - public Volvo240(){ - nrDoors = 4; - color = Color.black; - enginePower = 100; - modelName = "Volvo240"; - stopEngine(); - } - - public int getNrDoors(){ - return nrDoors; - } - public double getEnginePower(){ - return enginePower; - } - - public double getCurrentSpeed(){ - return currentSpeed; - } - - public Color getColor(){ - return color; - } - - public void setColor(Color clr){ - color = clr; - } - - public void startEngine(){ - currentSpeed = 0.1; - } - - public void stopEngine(){ - currentSpeed = 0; - } - - public double speedFactor(){ - return enginePower * 0.01 * trimFactor; - } - - public void incrementSpeed(double amount){ - currentSpeed = Math.min(getCurrentSpeed() + speedFactor() * amount,enginePower); - } - - public void decrementSpeed(double amount){ - currentSpeed = Math.max(getCurrentSpeed() - speedFactor() * amount,0); - } - - // TODO fix this method according to lab pm - public void gas(double amount){ - incrementSpeed(amount); - } - - // TODO fix this method according to lab pm - public void brake(double amount){ - decrementSpeed(amount); - } -} diff --git a/out/production/TDA553-lab1/.idea/.gitignore b/out/production/TDA553-lab1/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/out/production/TDA553-lab1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/TDA553-lab1/.idea/libraries/junit.xml b/out/production/TDA553-lab1/.idea/libraries/junit.xml new file mode 100644 index 000000000..e0e3044a8 --- /dev/null +++ b/out/production/TDA553-lab1/.idea/libraries/junit.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/TDA553-lab1/.idea/misc.xml b/out/production/TDA553-lab1/.idea/misc.xml new file mode 100644 index 000000000..7d53ab542 --- /dev/null +++ b/out/production/TDA553-lab1/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/out/production/TDA553-lab1/.idea/modules.xml b/out/production/TDA553-lab1/.idea/modules.xml new file mode 100644 index 000000000..93173df29 --- /dev/null +++ b/out/production/TDA553-lab1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/TDA553-lab1/.idea/uiDesigner.xml b/out/production/TDA553-lab1/.idea/uiDesigner.xml new file mode 100644 index 000000000..2b63946d5 --- /dev/null +++ b/out/production/TDA553-lab1/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/TDA553-lab1/.idea/vcs.xml b/out/production/TDA553-lab1/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/out/production/TDA553-lab1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/TDA553-lab1/Car.class b/out/production/TDA553-lab1/Car.class new file mode 100644 index 000000000..fff21ecb4 Binary files /dev/null and b/out/production/TDA553-lab1/Car.class differ diff --git a/out/production/TDA553-lab1/Movable.class b/out/production/TDA553-lab1/Movable.class new file mode 100644 index 000000000..22ef37250 Binary files /dev/null and b/out/production/TDA553-lab1/Movable.class differ diff --git a/out/production/TDA553-lab1/README.md b/out/production/TDA553-lab1/README.md new file mode 100644 index 000000000..38ae33214 --- /dev/null +++ b/out/production/TDA553-lab1/README.md @@ -0,0 +1,4 @@ +# oopd-gu-chalmers Lab 1 +Lab assignment 1 in the course Object-oriented Programming and Design, GU/Chalmers + +See Canvas for instructions. diff --git a/out/production/TDA553-lab1/TDA553-lab1.iml b/out/production/TDA553-lab1/TDA553-lab1.iml new file mode 100644 index 000000000..ae37f7eb7 --- /dev/null +++ b/out/production/TDA553-lab1/TDA553-lab1.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/test/TDA553-lab1/CarTest$TestSuite.class b/out/test/TDA553-lab1/CarTest$TestSuite.class new file mode 100644 index 000000000..0eadd46be Binary files /dev/null and b/out/test/TDA553-lab1/CarTest$TestSuite.class differ diff --git a/out/test/TDA553-lab1/CarTest.class b/out/test/TDA553-lab1/CarTest.class new file mode 100644 index 000000000..77b4ea3c9 Binary files /dev/null and b/out/test/TDA553-lab1/CarTest.class differ diff --git a/out/test/TDA553-lab1/TestSaab95Brake.class b/out/test/TDA553-lab1/TestSaab95Brake.class new file mode 100644 index 000000000..b0451a69e Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95Brake.class differ diff --git a/out/test/TDA553-lab1/TestSaab95Gas.class b/out/test/TDA553-lab1/TestSaab95Gas.class new file mode 100644 index 000000000..de5f9bb57 Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95Gas.class differ diff --git a/out/test/TDA553-lab1/TestSaab95LeftTurn.class b/out/test/TDA553-lab1/TestSaab95LeftTurn.class new file mode 100644 index 000000000..97fa13b6e Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95LeftTurn.class differ diff --git a/out/test/TDA553-lab1/TestSaab95Move.class b/out/test/TDA553-lab1/TestSaab95Move.class new file mode 100644 index 000000000..e531b1282 Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95Move.class differ diff --git a/out/test/TDA553-lab1/TestSaab95RightTurn.class b/out/test/TDA553-lab1/TestSaab95RightTurn.class new file mode 100644 index 000000000..6fa73092b Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95RightTurn.class differ diff --git a/out/test/TDA553-lab1/TestSaab95SpeedFactor.class b/out/test/TDA553-lab1/TestSaab95SpeedFactor.class new file mode 100644 index 000000000..b2b6436eb Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95SpeedFactor.class differ diff --git a/out/test/TDA553-lab1/TestSaab95StartEngine.class b/out/test/TDA553-lab1/TestSaab95StartEngine.class new file mode 100644 index 000000000..753a9c8fc Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95StartEngine.class differ diff --git a/out/test/TDA553-lab1/TestSaab95StopEngine.class b/out/test/TDA553-lab1/TestSaab95StopEngine.class new file mode 100644 index 000000000..9e76c2e30 Binary files /dev/null and b/out/test/TDA553-lab1/TestSaab95StopEngine.class differ diff --git a/out/test/TDA553-lab1/TestVolvo240LeftTurn.class b/out/test/TDA553-lab1/TestVolvo240LeftTurn.class new file mode 100644 index 000000000..46dd37b78 Binary files /dev/null and b/out/test/TDA553-lab1/TestVolvo240LeftTurn.class differ diff --git a/out/test/TDA553-lab1/TestVolvo240Move.class b/out/test/TDA553-lab1/TestVolvo240Move.class new file mode 100644 index 000000000..20c8e16f3 Binary files /dev/null and b/out/test/TDA553-lab1/TestVolvo240Move.class differ diff --git a/out/test/TDA553-lab1/TestVolvo240RightTurn.class b/out/test/TDA553-lab1/TestVolvo240RightTurn.class new file mode 100644 index 000000000..51128bb20 Binary files /dev/null and b/out/test/TDA553-lab1/TestVolvo240RightTurn.class differ diff --git a/src/Application.java b/src/Application.java new file mode 100644 index 000000000..f4463b4f5 --- /dev/null +++ b/src/Application.java @@ -0,0 +1,55 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Objects; +import java.util.Stack; + +public class Application extends JFrame{ + public static void main(String[] args) { + int X = 800; + int Y = 800; + Vehicle[] vehicles = { + new Vehicle(new Volvo240()), + new Vehicle(new Saab95()), + new Vehicle<>(new Scania()) + }; + + ArrayList images = new ArrayList<>(); + for(Vehicle vehicle : vehicles) { + try { + images.add(ImageIO.read(Objects.requireNonNull(DrawPanel.class.getResourceAsStream("pics/" + vehicle.getName() + ".jpg")))); + } + catch (IOException e) { + } + } + + Stack vehicleStack = new Stack<>(); + int vehicleIndex = 0; + for(Vehicle vehicle: vehicles) { + double[] position = {0, 160 * vehicleIndex}; + + vehicle.setPosition(position); + vehicleStack.push(vehicle); + vehicleIndex++; + } + + VehicleController vc = new VehicleController(vehicleStack); + JFrame frame = vc.createFrameWithButtons(X, Y); + + + HashMap vehicleBufferedImageHashMap = new HashMap<>(); + for (int i = 0; i < vehicles.length; i++) { + vehicleBufferedImageHashMap.put(vehicles[i], images.get(i)); + } + + VehicleView vv = new VehicleView(frame, "Car Sim 1.0", vehicleBufferedImageHashMap, X, Y); + + vc.addListener(vv); + + // Start the timer + vc.startTimer(); + } +} diff --git a/src/Back.java b/src/Back.java new file mode 100644 index 000000000..4410bd06d --- /dev/null +++ b/src/Back.java @@ -0,0 +1,4 @@ +public interface Back { + void raiseBack(); + void lowerBack(); +} diff --git a/src/Car.java b/src/Car.java new file mode 100644 index 000000000..a240ea573 --- /dev/null +++ b/src/Car.java @@ -0,0 +1,78 @@ +import java.awt.*; + +public abstract class Car implements Engine, Movable, HasName { + private final Movement movement = new Movement(); + + protected int nrDoors; + protected Color color; + protected String modelName; + + public int getNrDoors() {return nrDoors;} + public void setNrDoors(int nrDoors) {this.nrDoors = nrDoors;} + public Color getColor() {return color;} + public void setColor(Color color) {this.color = color;} + public String getModelName() {return modelName;} + public void setModelName(String modelName) {this.modelName = modelName;} + + + + protected double enginePower; + + public String getName() { + return this.modelName; + } + + public double getEnginePower() {return enginePower;} + + public double[] getPosition() {return movement.getPosition();} + + public double[] getDirection() { + return movement.getDirection(); + } + + public double getCurrentSpeed() { + return movement.getCurrentSpeed(); + } + + public void setPosition(double[] position) { + movement.setPosition(position); + } + + public void setDirection(double[] direction) { + movement.setDirection(direction); + } + + public void setCurrentSpeed(double currentSpeed) { + movement.setCurrentSpeed(currentSpeed); + } + + public double speedFactor() { + return 1; + } + + public void move(){movement.move();} + + public void turnLeft(){movement.turnLeft();} + + public void turnRight(){movement.turnRight();} + + public void gas(double amount) { + if ((amount >= 0) && (amount <= 1)) { + this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, enginePower)); + } + } + + public void brake(double amount) { + if ((amount >= 0) && (amount <= 1)) { + this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); + } + } + + public void startEngine(){ + this.setCurrentSpeed(0.1); + } + + public void stopEngine(){ + this.setCurrentSpeed(0); + } +} diff --git a/src/CarStack.java b/src/CarStack.java new file mode 100644 index 000000000..e637fb9d7 --- /dev/null +++ b/src/CarStack.java @@ -0,0 +1,54 @@ +import java.util.Iterator; +import java.util.Stack; + +public class CarStack implements Iterable{ + private final int maxCars; + private final int maxDoors; + private final Stack cars = new Stack<>(); + + public CarStack(int maxCars, int maxDoors){ + this.maxCars = maxCars; + this.maxDoors = maxDoors; + } + + public void addCar(Car car) { + if((cars.size() < maxCars) && (car.getNrDoors() <= maxDoors) && !cars.contains(car)) { + cars.push(car); + } + } + + public Car removeCar() { + if (!cars.isEmpty()) return cars.pop(); + return null; + } + + public int getNrOfLoadedCars() { + return cars.size(); + } + + public boolean isCarLoaded(Car car) { + return cars.contains(car); + } + + public Iterator iterator() { + return cars.iterator(); + } + + public void move() { + for (Car car: cars) { + car.move(); + } + } + + public void turnLeft() { + for (Car car: cars) { + car.turnLeft(); + } + } + + public void turnRight() { + for(Car car: cars) { + car.turnRight(); + } + } +} diff --git a/src/DrawPanel.java b/src/DrawPanel.java new file mode 100644 index 000000000..43beaf330 --- /dev/null +++ b/src/DrawPanel.java @@ -0,0 +1,36 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import javax.swing.*; + +// This panel represent the animated part of the view with the vehicle vehicles. + +public class DrawPanel extends JPanel { + HashMap vehicleImages; + + // Initializes the panel and reads the images + public DrawPanel(int x, int y, HashMap vehicleImages) { + this.setDoubleBuffered(true); + this.setPreferredSize(new Dimension(x, y)); + this.setBackground(Color.green); + this.vehicleImages = vehicleImages; + } + + // This method is called each time the panel updates/refreshes/repaints itself + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + vehicleImages.forEach((vehicle, image) -> { + Point point = new Point((int) vehicle.getPosition()[0], (int) vehicle.getPosition()[1]); + g.drawImage(image, point.x, point.y, null); // see javadoc for more info on the parameters + }); + } + + public void removeVehicle(Vehicle vehicle) { + vehicleImages.remove(vehicle); + } + + public void addVehicle(Vehicle vehicle, BufferedImage image) { + vehicleImages.put(vehicle, image); + } +} \ No newline at end of file diff --git a/src/Engine.java b/src/Engine.java new file mode 100644 index 000000000..d5673a188 --- /dev/null +++ b/src/Engine.java @@ -0,0 +1,6 @@ +public interface Engine { + void startEngine(); + void stopEngine(); + void gas(double amount); + void brake(double amount); +} diff --git a/src/HasName.java b/src/HasName.java new file mode 100644 index 000000000..d2eab2925 --- /dev/null +++ b/src/HasName.java @@ -0,0 +1,3 @@ +public interface HasName { + String getName(); +} diff --git a/src/MercedesCarTransport.java b/src/MercedesCarTransport.java new file mode 100644 index 000000000..ee0b0c3a6 --- /dev/null +++ b/src/MercedesCarTransport.java @@ -0,0 +1,52 @@ +import java.util.Arrays; + +public class MercedesCarTransport{ + private final Truck truck = new Truck(); + private final CarStack cars; + + public MercedesCarTransport(int maxCars, int maxDoors) { + truck.setModelName("Mercedes"); + cars = new CarStack(maxCars, maxDoors); + } + + public void addCar(Car car) { + if (!truck.getBackIsOpen() && Arrays.equals(car.getPosition(), new double[]{this.truck.getPosition()[0] - this.truck.getDirection()[0], + this.truck.getPosition()[1] - this.truck.getDirection()[1]})) { + car.setPosition(truck.getPosition()); + car.setDirection(truck.getDirection()); + cars.addCar(car); + } + } + + public Car removeCar() { + Car car = cars.removeCar(); + car.setPosition(new double[]{this.truck.getPosition()[0] - this.truck.getDirection()[0], + this.truck.getPosition()[1] - this.truck.getDirection()[1]}); + return car; + } + + public void raiseBack() { + truck.setBackIsOpen(false); + } + + public void lowerBack() { + if (truck.getCurrentSpeed()==0) { + truck.setBackIsOpen(true); + } + } + + public void move() { + truck.move(); + cars.move(); + } + + public void turnLeft() { + truck.turnLeft(); + cars.turnLeft(); + } + + public void turnRight() { + truck.turnRight(); + cars.turnRight(); + } +} diff --git a/src/ModelControl.java b/src/ModelControl.java new file mode 100644 index 000000000..c10c70be7 --- /dev/null +++ b/src/ModelControl.java @@ -0,0 +1,128 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Stack; + +public class ModelControl { + private final Stack vehicles; + private final int delay = 50; + final Timer timer = new Timer(delay, new ModelControl.TimerListener()); + ArrayList movementListeners = new ArrayList<>(); + + public ModelControl(Stack vehicles) { + this.vehicles = vehicles; + } + + public void addListener(MovementListener listener) { + movementListeners.add(listener); + } + + void addVehicle() { + if (vehicles.size() < 10) { + Vehicle newVehicle = new Vehicle<>(new Volvo240()); + newVehicle.setPosition(new double[]{160 * (int) (vehicles.size() / 3), 160 * (int) (vehicles.size() % 3)}); + vehicles.push(newVehicle); + for (MovementListener movementListener : movementListeners) { + movementListener.addVehicle(newVehicle); + } + } + } + + void removeVehicle() { + Vehicle vehicleToBeRemoved = vehicles.pop(); + if (vehicleToBeRemoved != null) { + for (MovementListener movementListener : movementListeners) { + movementListener.removeVehicle(vehicleToBeRemoved); + } + } + } + + void gas(int amount) { + double gas = ((double) amount) / 100; + for (Vehicle vehicle : vehicles) { + vehicle.gas(gas); + } + } + + void brake(int amount) { + double brake = ((double) amount) / 100; + for (Vehicle vehicle : vehicles) { + vehicle.brake(brake); + } + } + + void setTurboOn() { + for (Vehicle vehicle : vehicles) { + if (vehicle.vehicle instanceof Turbo) + ((Turbo) vehicle.vehicle).setTurboOn(); + } + } + + void setTurboOff() { + for (Vehicle vehicle : vehicles) { + if (vehicle.vehicle instanceof Turbo) { + ((Turbo) vehicle.vehicle).setTurboOff(); + } + } + } + + void liftBed() { + for (Vehicle vehicle : vehicles) { + if (vehicle.vehicle instanceof Back) { + ((Back) vehicle.vehicle).raiseBack(); + } + } + } + + void lowerBed() { + for (Vehicle vehicle : vehicles) { + if (vehicle.vehicle instanceof Back) { + ((Back) vehicle.vehicle).lowerBack(); + } + } + } + + void startEngine() { + for (Vehicle vehicle : vehicles) { + if (vehicle.getCurrentSpeed() == 0) { + vehicle.startEngine(); + } + } + } + + void stopEngine() { + for (Vehicle vehicle : vehicles) { + vehicle.stopEngine(); + } + } + + private void turnVehicleAround(Vehicle vehicle) { + vehicle.turnLeft(); + vehicle.turnLeft(); + } + + private class TimerListener implements ActionListener { + public void actionPerformed(ActionEvent e) { + for (Vehicle vehicle : vehicles) { + vehicle.move(); + + if (vehicle.getPosition()[1] > (800 - 240 - 60)) { + turnVehicleAround(vehicle); + } + if (vehicle.getPosition()[1] < (0)) { + turnVehicleAround(vehicle); + } + if (vehicle.getPosition()[0] > (800 - 100)) { + turnVehicleAround(vehicle); + } + if (vehicle.getPosition()[0] < (0)) { + turnVehicleAround(vehicle); + } + for (MovementListener movementListener : movementListeners) { + movementListener.updateMovement(vehicle); + } + } + } + } +} \ No newline at end of file diff --git a/src/Movable.java b/src/Movable.java new file mode 100644 index 000000000..0878bf5b5 --- /dev/null +++ b/src/Movable.java @@ -0,0 +1,9 @@ +interface Movable { + void setPosition(double[] position); + double[] getPosition(); + void setCurrentSpeed(double currentSpeed); + double getCurrentSpeed(); + void move(); + void turnLeft(); + void turnRight(); +} diff --git a/src/Movement.java b/src/Movement.java new file mode 100644 index 000000000..f17e5afb0 --- /dev/null +++ b/src/Movement.java @@ -0,0 +1,55 @@ +public class Movement implements Movable { + private double[] position; + private double[] direction; + private double currentSpeed = 0; + public double[] getPosition() { + return position; + } + public double[] getDirection() { + return direction; + } + + public double getCurrentSpeed() { + return currentSpeed; + } + public void setPosition(double[] position) { + this.position = position; + } + public void setDirection(double[] direction) { + this.direction = direction; + } + public void setCurrentSpeed(double currentSpeed) { + this.currentSpeed = currentSpeed; + } + public Movement() { + position = new double[]{0, 0}; + direction = new double[]{1, 0}; + } + + public Movement(double[] position, double[] direction) { + this.position = position; + this.direction = direction; + } + + public void move() { + //Ändrar x eller y baserat på riktning + position[0] = position[0] + currentSpeed * direction[0]; + position[1] = position[1] + currentSpeed * direction[1]; + } + + public void turnLeft() { + //Roterar den 90 grader åt vänster + double x = direction[0]; + double y = direction[1]; + direction[0] = -y; + direction[1] = x; + } + + public void turnRight() { + //Roterar den 90 grader åt höger + double x = direction[0]; + double y = direction[1]; + direction[0] = y; + direction[1] = -x; + } +} \ No newline at end of file diff --git a/src/MovementListener.java b/src/MovementListener.java new file mode 100644 index 000000000..a57814f43 --- /dev/null +++ b/src/MovementListener.java @@ -0,0 +1,5 @@ +public interface MovementListener { + void updateMovement(Vehicle vehicle); + void removeVehicle(Vehicle vehicle); + void addVehicle(Vehicle vehicle); +} diff --git a/src/Saab95.java b/src/Saab95.java new file mode 100644 index 000000000..35d3a62b3 --- /dev/null +++ b/src/Saab95.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class Saab95 extends Car implements Turbo{ + + protected boolean turboOn; + + public Saab95(){ + nrDoors = 2; + color = Color.red; + enginePower = 125; + turboOn = false; + modelName = "Saab95"; + stopEngine(); + } + + public void setTurboOn(){ + turboOn = true; + System.out.println("Turbo on!"); + } + + public void setTurboOff(){ + turboOn = false; + System.out.println("Turbo off!"); + } + + + public double speedFactor(){ + double turbo = 1; + if(turboOn) turbo = 1.3; + return enginePower * 0.01 * turbo; + } +} diff --git a/src/Scania.java b/src/Scania.java new file mode 100644 index 000000000..3fac20e35 --- /dev/null +++ b/src/Scania.java @@ -0,0 +1,86 @@ +import java.awt.*; + +public class Scania implements Movable, Engine, HasName, Back{ + + private double tilt = 0; + private final Truck truck = new Truck(); + private final String modelName = "Scania"; + public Scania(){ + } + + public double getTilt() { + return tilt; + } + + public void raiseBack() { + raiseBack(10); + } + + public void lowerBack() { + lowerBack(10); + System.out.println("Back lowered!"); + } + + public void raiseBack(double amount) { + if ((amount >= 0) && (truck.getCurrentSpeed() == 0)) { + tilt = Math.min(tilt + amount, 70); + truck.setBackIsOpen(true); + System.out.println("Beck raised!"); + } + } + + public void lowerBack(double amount) { + if (amount >= 0) { + tilt = Math.max(tilt - amount, 0); + if (tilt == 0) { + truck.setBackIsOpen(false); + } + } + } + + public void setPosition(double[] position) { + truck.setPosition(position); + } + + public double[] getPosition() { + return truck.getPosition(); + } + + public void setCurrentSpeed(double currentSpeed) { + truck.setCurrentSpeed(currentSpeed); + } + + public double getCurrentSpeed() { + return truck.getCurrentSpeed(); + } + + public void move() { + truck.move(); + } + + public void turnLeft() {truck.turnLeft();} + + public void turnRight() { + truck.turnRight(); + } + + public void startEngine() { + truck.startEngine(); + } + + public void stopEngine() { + truck.stopEngine(); + } + + public void gas(double amount) { + truck.gas(amount); + } + + public void brake(double amount) { + truck.brake(amount); + } + + public String getName() { + return this.modelName; + } +} diff --git a/src/Truck.java b/src/Truck.java new file mode 100644 index 000000000..2259a4ee3 --- /dev/null +++ b/src/Truck.java @@ -0,0 +1,84 @@ +import java.awt.*; + +public class Truck implements Engine{ + private final Movement movement = new Movement(); + + protected int nrDoors; + protected Color color; + protected String modelName; + + public int getNrDoors() {return nrDoors;} + public void setNrDoors(int nrDoors) {this.nrDoors = nrDoors;} + public Color getColor() {return color;} + public void setColor(Color color) {this.color = color;} + public String getModelName() {return modelName;} + public void setModelName(String modelName) {this.modelName = modelName;} + + + + private final double enginePower = 100; + + public double getEnginePower() {return enginePower;} + + private boolean backIsOpen = false; + + public boolean getBackIsOpen(){return backIsOpen;} + + public void setBackIsOpen(boolean backIsOpen){this.backIsOpen = backIsOpen;} + + public double[] getPosition() {return movement.getPosition();} + + public double[] getDirection() { + return movement.getDirection(); + } + + public double getCurrentSpeed() { + return movement.getCurrentSpeed(); + } + + public void setPosition(double[] position) { + movement.setPosition(position); + } + + public void setDirection(double[] direction) { + movement.setDirection(direction); + } + + public void setCurrentSpeed(double currentSpeed) { + movement.setCurrentSpeed(currentSpeed); + } + + public double speedFactor() { + return 1; + } + + public void move(){movement.move();} + + public void turnLeft(){movement.turnLeft();} + + public void turnRight(){movement.turnRight();} + + public void gas(double amount) { + if (!backIsOpen) { + if ((amount >= 0) && (amount <= 1)) { + this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + this.speedFactor() * amount, enginePower)); + } + } + } + + public void brake(double amount) { + if ((amount >= 0) && (amount <= 1)) { + this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); + } + } + + public void startEngine(){ + if (!backIsOpen) { + this.setCurrentSpeed(0.1); + } + } + + public void stopEngine(){ + this.setCurrentSpeed(0); + } +} \ No newline at end of file diff --git a/src/Turbo.java b/src/Turbo.java new file mode 100644 index 000000000..237189982 --- /dev/null +++ b/src/Turbo.java @@ -0,0 +1,4 @@ +public interface Turbo { + void setTurboOn(); + void setTurboOff(); +} diff --git a/src/Vehicle.java b/src/Vehicle.java new file mode 100644 index 000000000..bbf49b0b5 --- /dev/null +++ b/src/Vehicle.java @@ -0,0 +1,43 @@ +public class Vehicle { + public T vehicle; + public Vehicle(T vehicle) { + this.vehicle = vehicle; + } + + public void setPosition(double[] position) { + vehicle.setPosition(position); + } + public double[] getPosition() { + return vehicle.getPosition(); + } + public void setCurrentSpeed(double currentSpeed) { + vehicle.setCurrentSpeed(currentSpeed); + } + public double getCurrentSpeed() { + return vehicle.getCurrentSpeed(); + } + public void move() { + vehicle.move(); + } + public void turnLeft() { + vehicle.turnLeft(); + } + public void turnRight() { + vehicle.turnRight(); + } + public void startEngine() { + vehicle.startEngine(); + } + public void stopEngine() { + vehicle.stopEngine(); + } + public void gas(double amount) { + vehicle.gas(amount); + } + public void brake(double amount) { + vehicle.brake(amount); + } + public String getName() { + return vehicle.getName(); + } +} diff --git a/src/VehicleController.java b/src/VehicleController.java new file mode 100644 index 000000000..f1d69b080 --- /dev/null +++ b/src/VehicleController.java @@ -0,0 +1,101 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Stack; + +/* +* This class represents the Controller part in the MVC pattern. +* Its responsibilities are to listen to the View and responds in an appropriate manner by +* modifying the model state and the updating the view. + */ + +public class VehicleController { + JPanel controlPanel = new JPanel(); + ModelControl mc; + + JPanel gasPanel = new JPanel(); + JSpinner gasSpinner = new JSpinner(); + int gasAmount = 0; + JLabel gasLabel = new JLabel("Amount of gas"); + JButton gasButton = new JButton("Gas"); + JButton brakeButton = new JButton("Brake"); + JButton turboOnButton = new JButton("Saab Turbo on"); + JButton turboOffButton = new JButton("Saab Turbo off"); + JButton liftBedButton = new JButton("Scania Lift Bed"); + JButton lowerBedButton = new JButton("Lower Lift Bed"); + JButton addCarButton = new JButton("Add car"); + JButton removeCarButton = new JButton("Remove Car"); + + JButton startButton = new JButton("Start all vehicles"); + JButton stopButton = new JButton("Stop all vehicles"); + // member fields: + + + + // The frame that represents this instance View of the MVC pattern + + public VehicleController(Stack vehicles) { + mc = new ModelControl(vehicles); + } + + void addListener(MovementListener listener) { + mc.addListener(listener); + } + + public JFrame createFrameWithButtons(int X, int Y) { + JFrame frame = new JFrame(); + SpinnerModel spinnerModel = + new SpinnerNumberModel(0, //initial value + 0, //min + 100, //max + 1);//step + gasSpinner = new JSpinner(spinnerModel); + gasSpinner.addChangeListener(e -> gasAmount = (int) ((JSpinner)e.getSource()).getValue()); + + gasPanel.setLayout(new BorderLayout()); + gasPanel.add(gasLabel, BorderLayout.PAGE_START); + gasPanel.add(gasSpinner, BorderLayout.PAGE_END); + + frame.add(gasPanel); + + controlPanel.setLayout(new GridLayout(2,6)); + + controlPanel.add(gasButton, 0); + controlPanel.add(turboOnButton, 1); + controlPanel.add(liftBedButton, 2); + controlPanel.add(addCarButton, 3); + controlPanel.add(brakeButton, 4); + controlPanel.add(lowerBedButton, 5); + controlPanel.add(turboOffButton, 6); + controlPanel.add(removeCarButton, 7); + controlPanel.setPreferredSize(new Dimension((X/2)+4, 200)); + frame.add(controlPanel); + controlPanel.setBackground(Color.CYAN); + + startButton.setBackground(Color.blue); + startButton.setForeground(Color.green); + startButton.setPreferredSize(new Dimension(X/5-15,200)); + frame.add(startButton); + + stopButton.setBackground(Color.red); + stopButton.setForeground(Color.black); + stopButton.setPreferredSize(new Dimension(X/5-15,200)); + frame.add(stopButton); + + gasButton.addActionListener(e -> mc.gas(gasAmount)); + brakeButton.addActionListener(e -> mc.brake(gasAmount)); + turboOnButton.addActionListener(e -> mc.setTurboOn()); + turboOffButton.addActionListener(e -> mc.setTurboOff()); + liftBedButton.addActionListener(e -> mc.liftBed()); + lowerBedButton.addActionListener(e -> mc.lowerBed()); + startButton.addActionListener(e -> mc.startEngine()); + stopButton.addActionListener(e -> mc.stopEngine()); + addCarButton.addActionListener(e -> mc.addVehicle()); + removeCarButton.addActionListener(e -> mc.removeVehicle()); + return frame; + } + + void startTimer() { + mc.timer.start(); + } + +} diff --git a/src/VehicleView.java b/src/VehicleView.java new file mode 100644 index 000000000..7bc0d9a3e --- /dev/null +++ b/src/VehicleView.java @@ -0,0 +1,74 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.HashMap; +import java.util.Objects; + +/** + * This class represents the full view of the MVC pattern of your vehicle simulator. + * It initializes with being center on the screen and attaching its controller in its state. + * It communicates with the Controller by calling methods of it when an action fires of in + * each of its components. + **/ + +public class VehicleView implements MovementListener{ + private final JFrame frame; + int X; + int Y; + + DrawPanel drawPanel; + + // Constructor + public VehicleView(JFrame frame, String frameName, HashMap vehicleImages, int X, int Y){ + this.frame = frame; + this.X = X; + this.Y = Y; + this.drawPanel = new DrawPanel(X, Y-240, vehicleImages); + initComponents(frameName); + } + + // Sets everything in place and fits everything + private void initComponents(String title) { + + this.frame.setTitle(title); + this.frame.setPreferredSize(new Dimension(X,Y)); + this.frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); + + this.frame.add(drawPanel); + + // Get the computer screen resolution + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + // Center the frame + this.frame.setLocation(dim.width/2-this.frame.getSize().width/2, dim.height/2-this.frame.getSize().height/2); + this.frame.pack(); + // Make the frame visible + this.frame.setVisible(true); + // Make sure the frame exits when "x" is pressed + this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + public void updateMovement(Vehicle vehicle) { + //drawPanel.moveit(vehicle); + frame.repaint(); + } + + @Override + public void removeVehicle(Vehicle vehicle) { + drawPanel.removeVehicle(vehicle); + frame.repaint(); + } + + @Override + public void addVehicle(Vehicle vehicle) { + try { + BufferedImage image = ImageIO.read(Objects.requireNonNull(DrawPanel.class.getResourceAsStream("pics/" + vehicle.getName() + ".jpg"))); + drawPanel.addVehicle(vehicle, image); + } + catch (IOException e) { + System.out.println("Could not find picture " + vehicle.getName() + ".jpg"); + } + frame.repaint(); + } +} \ No newline at end of file diff --git a/src/Volvo240.java b/src/Volvo240.java new file mode 100644 index 000000000..2d3848c01 --- /dev/null +++ b/src/Volvo240.java @@ -0,0 +1,18 @@ +import java.awt.*; + +public class Volvo240 extends Car{ + + protected final static double trimFactor = 1.25; + + public Volvo240() { + nrDoors = 4; + color = Color.black; + enginePower = 100; + modelName = "Volvo240"; + stopEngine(); + } + + public double speedFactor() { + return enginePower * 0.01 * trimFactor; + } +} diff --git a/src/Workshop.java b/src/Workshop.java new file mode 100644 index 000000000..9d806df2f --- /dev/null +++ b/src/Workshop.java @@ -0,0 +1,21 @@ +import java.util.Vector; + + + +public class Workshop { + private final int maxCars; + public Vector cars = new Vector<>(); + + public Workshop(int maxCars) { + this.maxCars = maxCars; + } + + public void addCar(CarBrand car) { + if (cars.size() < maxCars && !cars.contains(car)) + cars.add(car); + } + + public void removeCar(CarBrand car) { + cars.remove(car); + } +} diff --git a/src/pics/Saab95.jpg b/src/pics/Saab95.jpg new file mode 100644 index 000000000..b3bf59254 Binary files /dev/null and b/src/pics/Saab95.jpg differ diff --git a/src/pics/Scania.jpg b/src/pics/Scania.jpg new file mode 100644 index 000000000..4bc73cb9a Binary files /dev/null and b/src/pics/Scania.jpg differ diff --git a/src/pics/Volvo240.jpg b/src/pics/Volvo240.jpg new file mode 100644 index 000000000..be464046a Binary files /dev/null and b/src/pics/Volvo240.jpg differ diff --git a/test/CarTest.java b/test/CarTest.java new file mode 100644 index 000000000..f6a026636 --- /dev/null +++ b/test/CarTest.java @@ -0,0 +1,15 @@ +import org.junit.*; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +public class CarTest { + @Test + public void getNrDoorsTest(){ + + } + + @RunWith(Suite.class) + @Suite.SuiteClasses({TestSaab95SpeedFactor.class,TestSaab95StartEngine.class}) + public static class TestSuite { + } +} diff --git a/test/TestCarStackAddCar.java b/test/TestCarStackAddCar.java new file mode 100644 index 000000000..fdaa265ae --- /dev/null +++ b/test/TestCarStackAddCar.java @@ -0,0 +1,57 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestCarStackAddCar { + private CarStack cars; + private Volvo240 volvo; + private Saab95 saab1; + private Saab95 saab2; + private Saab95 saab3; + private MercedesCarTransport transport; + + @Before + public void init() { + cars = new CarStack(2, 2); + volvo = new Volvo240(); + saab1 = new Saab95(); + saab2 = new Saab95(); + saab3 = new Saab95(); + } + + @Test + public void testLoadedCarsIncreaseWhenAddingCar() { + int oldNr = cars.getNrOfLoadedCars(); + cars.addCar(saab1); + assertTrue(oldNr < cars.getNrOfLoadedCars()); + } + + @Test + public void testAddCar() { + cars.addCar(saab1); + assertEquals(1, cars.getNrOfLoadedCars()); + } + + @Test + public void testAddSameCarTwice() { + cars.addCar(saab1); + cars.addCar(saab1); + assertEquals(1, cars.getNrOfLoadedCars()); + } + + @Test + public void testAddTooManyCars() { + cars.addCar(saab1); + cars.addCar(saab2); + cars.addCar(saab3); + assertEquals(2, cars.getNrOfLoadedCars()); + } + + @Test + public void testAddTooBigCar() { + cars.addCar(volvo); + assertEquals(0, cars.getNrOfLoadedCars()); + } +} \ No newline at end of file diff --git a/test/TestCarStackRemoveCar.java b/test/TestCarStackRemoveCar.java new file mode 100644 index 000000000..7093180f2 --- /dev/null +++ b/test/TestCarStackRemoveCar.java @@ -0,0 +1,44 @@ +import org.junit.Before; +import org.junit.Test; + +import java.util.Objects; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestCarStackRemoveCar { + private CarStack cars; + private Saab95 saab; + private Volvo240 volvo; + + @Before + public void init() { + cars = new CarStack(2, 4); + saab = new Saab95(); + volvo = new Volvo240(); + + cars.addCar(saab); + cars.addCar(volvo); + } + + @Test + public void testRemoveCarDecreasesNrOfCars() { + int oldNr = cars.getNrOfLoadedCars(); + cars.removeCar(); + assertTrue(oldNr > cars.getNrOfLoadedCars()); + } + + @Test + public void testRemoveCarReturnsSameCar() { + Car tempCar = cars.removeCar(); + assertEquals(volvo.getClass(), tempCar.getClass()); + } + + @Test + public void testRemoveTooManyCars() { + cars.removeCar(); + cars.removeCar(); + Car tempCar = cars.removeCar(); + assertEquals(null, tempCar); + } +} diff --git a/test/TestSaab95Brake.java b/test/TestSaab95Brake.java new file mode 100644 index 000000000..c766d0319 --- /dev/null +++ b/test/TestSaab95Brake.java @@ -0,0 +1,38 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSaab95Brake { + private Saab95 saab; + private double oldSpeed; + + @Before + public void init() { + saab = new Saab95(); + saab.currentSpeed = saab.enginePower; + oldSpeed = saab.getCurrentSpeed(); + } + + @Test + public void testIfBrakeWhenAmountBelowZero() { + saab.brake(-1); + assertEquals(oldSpeed, saab.getCurrentSpeed(), 0.01); + } + + @Test + public void testIfBrakeWhenAmountAboveOne() { + saab.brake(1.1); + assertEquals(oldSpeed, saab.getCurrentSpeed(), 0.01); + } + + @Test + public void testIfBrakeWhenBetweenZeroAndOne() { + for (double i = 10; i > 1; i--) { + oldSpeed = saab.getCurrentSpeed(); + saab.brake(i / 10); + assertTrue(oldSpeed > saab.getCurrentSpeed()); + } + } +} \ No newline at end of file diff --git a/test/TestSaab95DecrementSpeed.java b/test/TestSaab95DecrementSpeed.java new file mode 100644 index 000000000..f2e8ab092 --- /dev/null +++ b/test/TestSaab95DecrementSpeed.java @@ -0,0 +1,34 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSaab95DecrementSpeed { + private Saab95 saab; + + @Before + public void init() { + saab = new Saab95(); + saab.currentSpeed = saab.enginePower; + } + + @Test + public void testSpeedDecrements() { + double oldSpeed; + oldSpeed = saab.getCurrentSpeed(); + saab.decrementSpeed(1); + assertTrue(oldSpeed > saab.getCurrentSpeed()); + } + + @Test + public void testSpeedDecrementAmount() { + for (int i = 0; i < 10; i++) { + double oldSpeed; + oldSpeed = saab.getCurrentSpeed(); + saab.decrementSpeed(i); + assertEquals(oldSpeed - saab.speedFactor()*i, saab.getCurrentSpeed(), 0.01); + } + } +} + diff --git a/test/TestSaab95Gas.java b/test/TestSaab95Gas.java new file mode 100644 index 000000000..07aa60057 --- /dev/null +++ b/test/TestSaab95Gas.java @@ -0,0 +1,37 @@ +import org.junit.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSaab95Gas { + private Saab95 saab; + private double oldSpeed; + + @Before + public void init() { + saab = new Saab95(); + oldSpeed = saab.getCurrentSpeed(); + } + + @Test + public void testIfGasWhenAmountBelowZero() { + saab.gas(-1); + assertEquals(oldSpeed, saab.getCurrentSpeed(), 0.01); + } + + @Test + public void testIfGasWhenAmountAboveOne() { + saab.gas(1.1); + assertEquals(oldSpeed, saab.getCurrentSpeed(), 0.01); + } + + + @Test + public void testIfGasWhenBetweenZeroAndOne() { + for (double i = 1; i < 10; i++) { + oldSpeed = saab.getCurrentSpeed(); + saab.gas(i / 10); + assertTrue(oldSpeed < saab.getCurrentSpeed()); + } + } +} \ No newline at end of file diff --git a/test/TestSaab95IncrementSpeed.java b/test/TestSaab95IncrementSpeed.java new file mode 100644 index 000000000..e4f2e3a9a --- /dev/null +++ b/test/TestSaab95IncrementSpeed.java @@ -0,0 +1,32 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSaab95IncrementSpeed { + private Saab95 saab; + + @Before + public void init() { + saab = new Saab95(); + } + + @Test + public void testSpeedIncrements() { + double oldSpeed; + oldSpeed = saab.getCurrentSpeed(); + saab.incrementSpeed(1); + assertTrue(oldSpeed < saab.getCurrentSpeed()); + } + + @Test + public void testSpeedIncrementAmount() { + for (int i = 0; i < 10; i++) { + double oldSpeed; + oldSpeed = saab.getCurrentSpeed(); + saab.incrementSpeed(i); + assertEquals(oldSpeed + saab.speedFactor()*i, saab.getCurrentSpeed(), 0.01); + } + } +} diff --git a/test/TestSaab95LeftTurn.java b/test/TestSaab95LeftTurn.java new file mode 100644 index 000000000..3c27080e6 --- /dev/null +++ b/test/TestSaab95LeftTurn.java @@ -0,0 +1,75 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestSaab95LeftTurn { + private Saab95 saab; + private final double[] start = {0, 1}; + private final double[] newDirection = {-1, 0}; + + @Before + public void init() { + saab = new Saab95(); + } + + @Test + public void testInitialXDirection() { + assertEquals(start[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testInitialYDirection() { + assertEquals(start[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterOneLeftTurn() { + saab.turnLeft(); + assertEquals(newDirection[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterOneLeftTurn() { + saab.turnLeft(); + assertEquals(newDirection[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterFullCounterClockwiseRotation() { + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + assertEquals(start[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterFullCounterClockwiseRotation() { + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + assertEquals(start[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterFullCounterClockwiseRotationPlusOneTurn() { + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + assertEquals(newDirection[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterFullCounterClockwiseRotationPlusOneTurn() { + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + saab.turnLeft(); + assertEquals(newDirection[1], saab.getDirection()[1], 0.01); + } +} diff --git a/test/TestSaab95Move.java b/test/TestSaab95Move.java new file mode 100644 index 000000000..8bbb1bad4 --- /dev/null +++ b/test/TestSaab95Move.java @@ -0,0 +1,38 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestSaab95Move { + private Saab95 saab; + + private final double[] start = {0, 0}; + + @Before + public void init() { + saab = new Saab95(); + saab.startEngine(); + } + + @Test + public void testStartXPosition() { + assertEquals(start[0], saab.getPosition()[0], 0.01); + } + + @Test + public void testStartYPosition() { + assertEquals(start[1], saab.getPosition()[1], 0.01); + } + + @Test + public void testXPositionAfterOneMove() { + saab.move(); + assertEquals(0, saab.getPosition()[0], 0.01); + } + + @Test + public void testYPositionAfterOneMove() { + saab.move(); + assertEquals(saab.getCurrentSpeed(), saab.getPosition()[1], 0.01); + } +} \ No newline at end of file diff --git a/test/TestSaab95RightTurn.java b/test/TestSaab95RightTurn.java new file mode 100644 index 000000000..721087499 --- /dev/null +++ b/test/TestSaab95RightTurn.java @@ -0,0 +1,75 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestSaab95RightTurn { + private Saab95 saab; + private final double[] start = {0, 1}; + private final double[] newDirection = {1, 0}; + + @Before + public void init() { + saab = new Saab95(); + } + + @Test + public void testInitialXDirection() { + assertEquals(start[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testInitialYDirection() { + assertEquals(start[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterOneRightTurn() { + saab.turnRight(); + assertEquals(newDirection[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterOneRightTurn() { + saab.turnRight(); + assertEquals(newDirection[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterFullClockwiseRotation() { + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + assertEquals(start[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterFullClockwiseRotation() { + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + assertEquals(start[1], saab.getDirection()[1], 0.01); + } + + @Test + public void testXDirectionAfterFullClockwiseRotationPlusOneTurn() { + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + assertEquals(newDirection[0], saab.getDirection()[0], 0.01); + } + + @Test + public void testYDirectionAfterFullClockwiseRotationPlusOneTurn() { + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + saab.turnRight(); + assertEquals(newDirection[1], saab.getDirection()[1], 0.01); + } +} \ No newline at end of file diff --git a/test/TestSaab95SpeedFactor.java b/test/TestSaab95SpeedFactor.java new file mode 100644 index 000000000..fb4fffb2f --- /dev/null +++ b/test/TestSaab95SpeedFactor.java @@ -0,0 +1,21 @@ +import org.junit.*; +import static org.junit.Assert.assertEquals; + +public class TestSaab95SpeedFactor { + private Saab95 saab; + @Before + public void init() { + saab = new Saab95(); + } + + @Test + public void testInitialSpeedFactor() { //Kanske bara för när turbo off??? + assertEquals(saab.getEnginePower() * 0.01 * 1, saab.speedFactor(), 0.01); + } + + @Test + public void testSpeedFactorWhenTurboOn() { + saab.setTurboOn(); + assertEquals(saab.getEnginePower() * 0.01 * 1.3, saab.speedFactor(), 0.01); + } +} diff --git a/test/TestSaab95StartEngine.java b/test/TestSaab95StartEngine.java new file mode 100644 index 000000000..52920a52a --- /dev/null +++ b/test/TestSaab95StartEngine.java @@ -0,0 +1,31 @@ +import org.junit.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSaab95StartEngine { + private Saab95 saab; + + @Before + public void init() { + saab = new Saab95(); + } + + @Test + public void testInitialSpeed() { + assertEquals(0, saab.getCurrentSpeed(), 0.01); + } + + @Test + public void testStartEngineIncreasesSpeed() { + double speed = saab.getCurrentSpeed(); + saab.startEngine(); + assertTrue(speed < saab.getCurrentSpeed()); + } + + @Test + public void testStartEngineSpeedValue() { + saab.startEngine(); + assertEquals(0.1, saab.getCurrentSpeed(), 0.01); + } +} diff --git a/test/TestSaab95StopEngine.java b/test/TestSaab95StopEngine.java new file mode 100644 index 000000000..ab25bf375 --- /dev/null +++ b/test/TestSaab95StopEngine.java @@ -0,0 +1,17 @@ +import org.junit.*; + +public class TestSaab95StopEngine { + private Saab95 saab; + + @Before + public void init() { + saab = new Saab95(); + saab.startEngine(); + } + + @Test + public void testStopEngineValue(){ + saab.stopEngine(); + Assert.assertEquals(0, saab.getCurrentSpeed(), 0.01); + } +} diff --git a/test/TestSaab95Turbo.java b/test/TestSaab95Turbo.java new file mode 100644 index 000000000..f9658f08a --- /dev/null +++ b/test/TestSaab95Turbo.java @@ -0,0 +1,25 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TestSaab95Turbo { + private Saab95 saab; + + @Before + public void init() { + saab = new Saab95(); + saab.setTurboOn(); + } + + @Test + public void testTurboOn() { + assertTrue(saab.turboOn); + } + + @Test + public void testTurboOff() { + saab.setTurboOff(); + assertFalse(saab.turboOn); + } +} diff --git a/test/TestScaniaLowerBack.java b/test/TestScaniaLowerBack.java new file mode 100644 index 000000000..169754515 --- /dev/null +++ b/test/TestScaniaLowerBack.java @@ -0,0 +1,41 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestScaniaLowerBack { + private Scania scania; + private double oldTilt; + + @Before + public void init() { + scania = new Scania(); + scania.raiseBack(70); + oldTilt = scania.getTilt(); + } + + @Test + public void testLowerBackLowers() { + scania.lowerBack(10); + assertTrue(oldTilt > scania.getTilt()); + } + + @Test + public void testLowerBackAmount() { + scania.lowerBack(10); + assertEquals(60, scania.getTilt(), 0.01); + } + + @Test + public void testNegativeLowerAmount() { + scania.lowerBack(-10); + assertEquals(oldTilt, scania.getTilt(), 0.01); + } + + @Test + public void testExcessiveTilt() { + scania.lowerBack(80); + assertEquals(0, scania.getTilt(), 0.01); + } +} diff --git a/test/TestScaniaRaiseBack.java b/test/TestScaniaRaiseBack.java new file mode 100644 index 000000000..b52edc594 --- /dev/null +++ b/test/TestScaniaRaiseBack.java @@ -0,0 +1,40 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestScaniaRaiseBack { + private Scania scania; + private double oldTilt; + + @Before + public void init() { + scania = new Scania(); + oldTilt = scania.getTilt(); + } + + @Test + public void testRaiseBackRaises() { + scania.raiseBack(10); + assertTrue(oldTilt < scania.getTilt()); + } + + @Test + public void testRaiseBackAmount() { + scania.raiseBack(10); + assertEquals(10, scania.getTilt(), 0.01); + } + + @Test + public void testNegativeRaiseAmount() { + scania.raiseBack(-10); + assertEquals(oldTilt, scania.getTilt(), 0.01); + } + + @Test + public void testExcessiveRaise() { + scania.raiseBack(80); + assertEquals(70, scania.getTilt(), 0.01); + } +} \ No newline at end of file diff --git a/test/TestVolvo240LeftTurn.java b/test/TestVolvo240LeftTurn.java new file mode 100644 index 000000000..cdc05bbb5 --- /dev/null +++ b/test/TestVolvo240LeftTurn.java @@ -0,0 +1,75 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestVolvo240LeftTurn { + private Volvo240 volvo; + private final double[] start = {0, 1}; + private final double[] newDirection = {-1, 0}; + + @Before + public void init() { + volvo = new Volvo240(); + } + + @Test + public void checkInitialXDirection() { + assertEquals(start[0], volvo.getDirection()[0], 0.01); + } + + @Test + public void checkInitialYDirection() { + assertEquals(start[1], volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterOneLeftTurn() { + volvo.turnLeft(); + assertEquals(newDirection[0], volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterOneLeftTurn() { + volvo.turnLeft(); + assertEquals(newDirection[1], volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterFullCounterClockwiseRotation() { + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + assertEquals(start[0], volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterFullCounterClockwiseRotation() { + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + assertEquals(start[1], volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterFullCounterClockwiseRotationPlusOneTurn() { + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + assertEquals(newDirection[0], volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterFullCounterClockwiseRotationPlusOneTurn() { + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + volvo.turnLeft(); + assertEquals(newDirection[1], volvo.getDirection()[1], 0.01); + } +} diff --git a/test/TestVolvo240Move.java b/test/TestVolvo240Move.java new file mode 100644 index 000000000..bcfa1cbe1 --- /dev/null +++ b/test/TestVolvo240Move.java @@ -0,0 +1,39 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestVolvo240Move { + private Volvo240 volvo; + + private final double[] start = {0, 0}; + + @Before + public void init() { + volvo = new Volvo240(); + volvo.startEngine(); + } + + @Test + public void checkStartXPosition() { + assertEquals(start[0], volvo.getPosition()[0], 0.01); + } + + @Test + public void checkStartYPosition() { + assertEquals(start[1], volvo.getPosition()[1], 0.01); + } + + @Test + public void checkXPositionAfterOneMove() { + volvo.move(); + assertEquals(0, volvo.getPosition()[0], 0.01); + } + + @Test + public void checkYPositionAfterOneMove() { + volvo.move(); + assertEquals(volvo.getCurrentSpeed(), volvo.getPosition()[1], 0.01); + } +} + diff --git a/test/TestVolvo240RightTurn.java b/test/TestVolvo240RightTurn.java new file mode 100644 index 000000000..95c426a3c --- /dev/null +++ b/test/TestVolvo240RightTurn.java @@ -0,0 +1,75 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestVolvo240RightTurn { + private Volvo240 Volvo; + private final double[] start = {0, 1}; + private final double[] newDirection = {1, 0}; + + @Before + public void init() { + Volvo = new Volvo240(); + } + + @Test + public void checkInitialXDirection() { + assertEquals(start[0], Volvo.getDirection()[0], 0.01); + } + + @Test + public void checkInitialYDirection() { + assertEquals(start[1], Volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterOneRightTurn() { + Volvo.turnRight(); + assertEquals(newDirection[0], Volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterOneRightTurn() { + Volvo.turnRight(); + assertEquals(newDirection[1], Volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterFullClockwiseRotation() { + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + assertEquals(start[0], Volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterFullClockwiseRotation() { + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + assertEquals(start[1], Volvo.getDirection()[1], 0.01); + } + + @Test + public void checkXDirectionAfterFullClockwiseRotationPlusOneTurn() { + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + assertEquals(newDirection[0], Volvo.getDirection()[0], 0.01); + } + + @Test + public void checkYDirectionAfterFullClockwiseRotationPlusOneTurn() { + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + Volvo.turnRight(); + assertEquals(newDirection[1], Volvo.getDirection()[1], 0.01); + } +} \ No newline at end of file diff --git a/test/TestVolvo240SpeedFactor.java b/test/TestVolvo240SpeedFactor.java new file mode 100644 index 000000000..20b90f5b2 --- /dev/null +++ b/test/TestVolvo240SpeedFactor.java @@ -0,0 +1,13 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestVolvo240SpeedFactor { + private Volvo240 volvo; + + @Test + public void testSpeedFactor() { + volvo = new Volvo240(); + assertEquals(volvo.enginePower* 0.01 * volvo.trimFactor, volvo.speedFactor(), 0.01); + } +} diff --git a/test/TestWorkshopAdd.java b/test/TestWorkshopAdd.java new file mode 100644 index 000000000..edd320d54 --- /dev/null +++ b/test/TestWorkshopAdd.java @@ -0,0 +1,48 @@ +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestWorkshopAdd { + //private Workshop workshop; + private final Volvo240 p = new Volvo240(); + private final Volvo240 r = new Volvo240(); + private final Saab95 q = new Saab95(); + + private final Scania s = new Scania(); + + + + @Test + public void testIfEmptyWhenInitialized(){ + Workshop workshop = new Workshop<>(1); + assertTrue(workshop.cars.isEmpty()); + } + @Test + public void testIfCanAddWhenNotFull(){ + Workshop workshop = new Workshop<>(1); + workshop.addCar(p); + assertEquals(1, workshop.cars.size(),0.01); + } + @Test + public void testIfCannotAddWhenFull() { + Workshop workshop = new Workshop<>(1); + workshop.addCar(p); + workshop.addCar(r); + assertEquals(1, workshop.cars.size(),0.01); + } + @Test //Ska ta bort detta test maybe, är detta compile time error? + public void testIfCannotAddWrongBrand() { + Workshop workshop = new Workshop<>(2); + workshop.addCar(p); + //workshop.addCar(q); + assertEquals(1, workshop.cars.size(),0.01); + } + @Test + public void testIfCanAddAnyCarWhenNoBrand(){ + Workshop workshop = new Workshop<>(2); + workshop.addCar(p); + workshop.addCar(q); + //workshop.addCar(s); + assertEquals(2, workshop.cars.size(),0.01); + } +} diff --git a/test/TestWorkshopRemove.java b/test/TestWorkshopRemove.java new file mode 100644 index 000000000..cc7fa3c59 --- /dev/null +++ b/test/TestWorkshopRemove.java @@ -0,0 +1,24 @@ +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +public class TestWorkshopRemove { + private final Volvo240 p = new Volvo240(); + private final Volvo240 r = new Volvo240(); + private final Saab95 q = new Saab95(); + + private Workshop workshop; + + @Before + public void init() { + workshop = new Workshop<>(2); + workshop.addCar(r); + workshop.addCar(p); + } + + @Test + public void testIfYouCanRemove() { + workshop.removeCar(p); + assertEquals(1,workshop.cars.size(),0.01); + } +} diff --git a/uml-2.md b/uml-2.md new file mode 100644 index 000000000..5e99cbbfb --- /dev/null +++ b/uml-2.md @@ -0,0 +1,187 @@ +```puml +@startuml +abstract class "Car" { + - movement : Movement + + + startEngine() : void + + stopEngine() : void +} +skinparam classAttributeIconSize 0 +class "CarStack" { + - maxCars : int + - maxDoors : int + + + addCar(car : Car) : void + + removeCar() : Car + + getNrOfLoadedCars() : int + + isCarLoaded(car : Car) : boolean +} + +class "DrawPanel" { + ~ vehicles : ArrayList + ~ images : ArrayList + ~ vehiclePoints : ArrayList + + ~ moveit(x : int, y : int, index : int) : void + # paintComponent(g : Graphics) : void +} + +interface "Engine" { + + startEngine() : void + + stopEngine() : void + + gas() : void + + brake() : void +} + +class "MercedesCarTransport" { + - cars : Carstack + - truck : Truck + + + addCar(car : Car) : void + + removeCar() : Car + + raiseBack() : void + + lowerBack() : void + + move() : void + + turnLeft() : void + + turnRight() : void +} + +interface "Movable" { + + move() : void + + turnLeft() : void + + turnRight() : void +} + +class "Movement" { + + position : double[] + + direction : double[] + + + move() : void + + turnLeft() : void + + turnRight() : void +} + +class "Saab95" { + # turboOn : boolean + + + setTurboOn() : void + + setTurboOff() : void + + speedFactor() : double +} + +class "Scania" { + - tilt : double + - truck : Truck + + + raiseBack() : void + + raiseBack(amount : double) : void + + lowerBack() : void + + lowerBack(amount : double) : void +} + +class "Truck" { + # backIsClosed : boolean + - movement : Movement + + + speedFactor() : double + {abstract} + raiseBack() : void + {abstract} + lowerBack() : void + + startEngine() : void + + stopEngine() : void + + gas() : void +} +/' +class "Vehicle" { + # nrDoors : int + # enginePower : double + # currentSpeed : double + # color : Color + # modelName : String + + {abstract} + speedFactor() : double + # incrementSpeed(amount : double) : void + # decrementSpeed(amount : double) : void + + gas(amount : double) : void + + brake(amount : double) : void +} +'/ +class "Vehicle" { + + vehicle : T + + + move() : void + + turnLeft() : void + + turnRight() : void + + gas() : void + + break () : void + +} + +class "VehicleController" { + ~ frame : VehicleView + ~ vehicles : ArrayList + + {static} + main(args : String[]) : void + ~ gas(amount : int) : void + ~ brake(amount : int) : void +} + +class "VehicleView" { + {static} - X : int + {static} - Y : int + ~ vehicleC : VehicleController + ~ DrawPanel : drawPanel + + - initComponents(String title) : void +} + +class "Volvo240" { + {static} # trimFactor : double + - car : Car + + + speedFactor() : double +} + +class "Workshop" { + - maxCars : int + + cars : Vector + + + addCar(car : CarBrand) : void + + removeCar(car : CarBrand) : void +} + +Car <|-- Saab95 +Car <|-- Volvo240 +Car <.. CarStack +Car <.. MercedesCarTransport +Car <.. "Workshop" + +CarStack <-- MercedesCarTransport + +DrawPanel <-- VehicleView + +Engine <|.. Car +Engine <|.. Truck +Engine <|.. Scania +Engine <|.. MercedesCarTransport +Engine <|.. "Vehicle" + +Movable <|.. CarStack +Movable <|.. Movement +Movable <|.. Scania +Movable <|.. MercedesCarTransport +Movable <.. "Vehicle" + +Movement <-- Car +Movement <-- Truck + +Truck <-- Scania +Truck <-- MercedesCarTransport + +"Vehicle" <-- VehicleController +"Vehicle" <-- VehicleView +"Vehicle" <-- DrawPanel + +VehicleController <-- VehicleView + +VehicleView <-- VehicleController +@enduml \ No newline at end of file diff --git a/uml-3 b/uml-3 new file mode 100644 index 000000000..e69de29bb diff --git a/uml-3.md b/uml-3.md new file mode 100644 index 000000000..b84d47901 --- /dev/null +++ b/uml-3.md @@ -0,0 +1,231 @@ +```puml +@startuml +skinparam classAttributeIconSize 0 +class "Application" { + {static} + main(args : String[]) : void +} + +interface "Back" { + + raiseBack() : void + + lowerBack() : void +} + +abstract class "Car" { + - movement : Movement + + + startEngine() : void + + stopEngine() : void +} + +class "CarStack" { + - maxCars : int + - maxDoors : int + + + addCar(car : Car) : void + + removeCar() : Car + + getNrOfLoadedCars() : int + + isCarLoaded(car : Car) : boolean +} + +class "DrawPanel" { + /' ~ vehicles : ArrayList '/ + ~ vehicleImages : HashMap + ~ vehiclePoints : HashMap + + ~ moveit(x : int, y : int, index : int) : void + # paintComponent(g : Graphics) : void + + addVehicle(Vehicle vehicle, BufferedImage image) : void + + removeVehicle(Vehicle vehicle) : void +} + +interface "Engine" { + + startEngine() : void + + stopEngine() : void + + gas() : void + + brake() : void +} + +interface "HasName" { + + getName() : String +} + +class "MercedesCarTransport" { + - cars : Carstack + - truck : Truck + + + addCar(car : Car) : void + + removeCar() : Car + + raiseBack() : void + + lowerBack() : void + + move() : void + + turnLeft() : void + + turnRight() : void +} + +interface "Movable" { + + move() : void + + turnLeft() : void + + turnRight() : void +} + +class "Movement" { + + position : double[] + + direction : double[] + + + move() : void + + turnLeft() : void + + turnRight() : void +} + +interface "MovementListener" { + + updateMovement() : void + + addVehicle() : void + + removeVehicle() : void +} + +class "Saab95" { + # turboOn : boolean + + + setTurboOn() : void + + setTurboOff() : void + + speedFactor() : double +} + +class "Scania" { + - tilt : double + - truck : Truck + - modelName : String + + + raiseBack() : void + + raiseBack(amount : double) : void + + lowerBack() : void + + lowerBack(amount : double) : void +} + +class "Truck" { + # backIsClosed : boolean + - movement : Movement + + + speedFactor() : double + {abstract} + raiseBack() : void + {abstract} + lowerBack() : void + + startEngine() : void + + stopEngine() : void + + gas() : void +} +/' +class "Vehicle" { + # nrDoors : int + # enginePower : double + # currentSpeed : double + # color : Color + # modelName : String + + {abstract} + speedFactor() : double + # incrementSpeed(amount : double) : void + # decrementSpeed(amount : double) : void + + gas(amount : double) : void + + brake(amount : double) : void +} +'/ +class "Vehicle" { + + vehicle : T + + + move() : void + + turnLeft() : void + + turnRight() : void + + gas() : void + + break () : void + +} + +class "VehicleController" { + ~ vehicles : Stack + - timer : Timer + ~ various buttons : JButton + + + ~ gas(amount : int) : void + ~ brake(amount : int) : void + + createFrameWithButtons() : JFrame +} + + + +class "VehicleView" { + {static} - X : int + {static} - Y : int + /' ~ vehicleC : VehicleController '/ + ~ frame : JFrame + ~ DrawPanel : drawPanel + + - initComponents(String title) : void + + updateMovement(double x, double y, Vehicle vehicles) : void + + addVehicle(Vehicle vehicle) : void + + removeVehicle(Vehicle vehicle) : void +} + +class "Volvo240" { + {static} # trimFactor : double + - car : Car + + + speedFactor() : double +} + +class "Workshop" { + - maxCars : int + + cars : Vector + + + addCar(car : CarBrand) : void + + removeCar(car : CarBrand) : void +} + +Back <|.. Scania + +Car <|-- Saab95 +Car <|-- Volvo240 +Car <.. CarStack +Car <.. MercedesCarTransport +Car <.. "Workshop" + +CarStack <-- MercedesCarTransport + +DrawPanel <-- VehicleView + +Engine <|.. Car +Engine <|.. Truck +Engine <|.. Scania +Engine <|.. MercedesCarTransport +Engine <.. "Vehicle" + +HasName <|.. Car +HasName <|.. Scania +HasName <.. "Vehicle" + +Movable <|.. CarStack +Movable <|.. MercedesCarTransport +Movable <|.. Movement +Movable <|.. Scania +Movable <.. "Vehicle" + +Movement <-- Car +Movement <-- Truck + +MovementListener <-- VehicleController +MovementListener <|.. VehicleView + +Truck <-- Scania +Truck <-- MercedesCarTransport + +"Vehicle" <-- VehicleController +/' "Vehicle" <-- DrawPanel '/ +"Vehicle" <-- Application +"Vehicle" <.. VehicleView + +VehicleController <-- Application +/' VehicleController <-- VehicleView '/ + +VehicleView <-- Application +/' VehicleView <-- VehicleController '/ + +@enduml \ No newline at end of file diff --git a/uml-klass.md b/uml-klass.md new file mode 100644 index 000000000..dc5d80eb4 --- /dev/null +++ b/uml-klass.md @@ -0,0 +1,146 @@ +```puml +@startuml +abstract class "Car" +skinparam classAttributeIconSize 0 +class "CarStack" { + - maxCars : int + - maxDoors : int + + + addCar(car : Car) : void + + removeCar() : Car + + getNrOfLoadedCars() : int + + isCarLoaded(car : Car) : boolean +} + +class "DrawPanel" { + ~ vehicles : ArrayList + ~ images : ArrayList + ~ vehiclePoints : ArrayList + + ~ moveit(x : int, y : int, index : int) : void + # paintComponent(g : Graphics) : void +} + +class "MercedesCarTransport" { + - cars : Carstack + + + addCar(car : Car) : void + + removeCar() : Car + + raiseBack() : void + + lowerBack : void + + move() : void + + turnLeft() : void + + turnRight() : void +} + +interface "Movable" { + ~ move() : void + ~ turnLeft() : void + ~ turnRight() : void +} + +class "Saab95" { + # turboOn : boolean + + + setTurboOn() : void + + setTurboOff() : void + + speedFactor() : double +} + +class "Scania" { + - tilt : double + + + raiseBack() : void + + raiseBack(amount : double) : void + + lowerBack() : void + + lowerBack(amount : double) : void +} + +abstract class "Truck" { + # backIsClosed : boolean + + + speedFactor() : double + {abstract} + raiseBack() : void + {abstract} + lowerBack() : void + + gas() : void +} + +abstract class "Vehicle" { + # position : double[] + # direction : double[] + # nrDoors : int + # enginePower : double + # currentSpeed : double + # color : Color + # modelName : String + + + startEngine() : void + + stopEngine() : void + {abstract} + speedFactor() : double + # incrementSpeed(amount : double) : void + # decrementSpeed(amount : double) : void + + gas(amount : double) : void + + brake(amount : double) : void + + move() : void + + turnLeft() : void + + turnRight() : void +} + +class "VehicleController" { + ~ frame : VehicleView + ~ vehicles : ArrayList + + {static} + main(args : String[]) : void + ~ gas(amount : int) : void + ~ brake(amount : int) : void +} + +class "VehicleView" { + {static} - X : int + {static} - Y : int + ~ vehicleC : VehicleController + ~ DrawPanel : drawPanel + + - initComponents(String title) : void +} + +class "Volvo240" { + {static} # trimFactor : double + + + speedFactor() : double +} + +class "Workshop" { + - maxCars : int + + cars : Vector + + + addCar(car : CarBrand) : void + + removeCar(car : CarBrand) : void +} + +Car <|-- Saab95 +Car <|-- Volvo240 +Car <.. CarStack +Car <.. MercedesCarTransport +Car <.. "Workshop" + +CarStack <-- MercedesCarTransport + +DrawPanel <-- VehicleView + +Movable <|.. CarStack +Movable <|.. Vehicle + +Truck <|-- Scania +Truck <|-- MercedesCarTransport + +Vehicle <|-- Car +Vehicle <|-- Truck +Vehicle <.. DrawPanel +Vehicle <.. VehicleView +Vehicle <.. VehicleController + +VehicleController <-- VehicleView + +VehicleView <-- VehicleController +@enduml \ No newline at end of file