From b2b7093c1c62d5c40fc04f288af2a487665e520d Mon Sep 17 00:00:00 2001 From: felixtan Date: Thu, 18 Jan 2024 13:36:30 +0100 Subject: [PATCH 01/16] Test --- car.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 car.java diff --git a/car.java b/car.java new file mode 100644 index 000000000..04ded59f6 --- /dev/null +++ b/car.java @@ -0,0 +1,3 @@ +public class car { + +} From b2b81ac405fdb671aa138f28dfa8f7087bbd70ff Mon Sep 17 00:00:00 2001 From: felixtan Date: Thu, 18 Jan 2024 13:55:31 +0100 Subject: [PATCH 02/16] test1 --- .idea/.gitignore | 8 ++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ Saab95.java | 29 +---------------------------- Volvo240.java | 32 ++------------------------------ car.java | 43 +++++++++++++++++++++++++++++++++++++++++++ lab1.iml | 11 +++++++++++ 8 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 lab1.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..13566b81b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..ea71d5024 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..e7763c5d4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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/Saab95.java b/Saab95.java index e032a0244..a28e52588 100644 --- a/Saab95.java +++ b/Saab95.java @@ -1,9 +1,8 @@ import java.awt.*; -public class Saab95{ +public class Saab95 extends car{ 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 @@ -18,32 +17,6 @@ public 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; diff --git a/Volvo240.java b/Volvo240.java index 283eacd9c..7c7916351 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -1,49 +1,21 @@ import java.awt.*; -public class Volvo240{ +public class Volvo240 extends car{ 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; + int 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; } diff --git a/car.java b/car.java index 04ded59f6..fcebf3a94 100644 --- a/car.java +++ b/car.java @@ -1,3 +1,46 @@ +import java.awt.*; + public class car { + private final int nrDoors; // Number of doors on the car + private final double enginePower; // Engine power of the car + private final double currentSpeed; // The current speed of the car + private final Color color; // Color of the car + private final String modelName; // The car model name + + public car(int nrDoors, double enginePower, double currentSpeed, Color color, String modelName) { + this.nrDoors = nrDoors; + this.enginePower = enginePower; + this.currentSpeed = currentSpeed; + this.color = color; + this.modelName = modelName; + } + public car() {} + + + 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; + } } diff --git a/lab1.iml b/lab1.iml new file mode 100644 index 000000000..b107a2dd8 --- /dev/null +++ b/lab1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 343f946f13e357edf5243787758763fc4f463b28 Mon Sep 17 00:00:00 2001 From: felixtan Date: Thu, 18 Jan 2024 14:15:23 +0100 Subject: [PATCH 03/16] test2 --- Saab95.java | 16 ++++------------ Volvo240.java | 17 +++++------------ car.java | 9 +++------ 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/Saab95.java b/Saab95.java index a28e52588..e123dbeca 100644 --- a/Saab95.java +++ b/Saab95.java @@ -3,17 +3,9 @@ public class Saab95 extends car{ public boolean turboOn; - 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; + super(2,125, Color.red, "Saab95"); turboOn = false; - modelName = "Saab95"; stopEngine(); } @@ -29,15 +21,15 @@ public void setTurboOff(){ public double speedFactor(){ double turbo = 1; if(turboOn) turbo = 1.3; - return enginePower * 0.01 * turbo; + return getEnginePower() * 0.01 * turbo; } public void incrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() + speedFactor() * amount; + getCurrentSpeed() = getCurrentSpeed() + speedFactor() * amount; } public void decrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() - speedFactor() * amount; + getCurrentSpeed() = getCurrentSpeed() - speedFactor() * amount; } // TODO fix this method according to lab pm diff --git a/Volvo240.java b/Volvo240.java index 7c7916351..93d141301 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -3,29 +3,22 @@ public class Volvo240 extends car{ public final static double trimFactor = 1.25; - 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(){ - int nrDoors = 4; - color = Color.black; - enginePower = 100; - modelName = "Volvo240"; + super(4, 100, Color.black, "Volvo240"); stopEngine(); } public double speedFactor(){ - return enginePower * 0.01 * trimFactor; + return getEnginePower() * 0.01 * trimFactor; } public void incrementSpeed(double amount){ - currentSpeed = Math.min(getCurrentSpeed() + speedFactor() * amount,enginePower); + getCurrentSpeed() = Math.min(getCurrentSpeed() + speedFactor() * amount,getEnginePower()); } public void decrementSpeed(double amount){ - currentSpeed = Math.max(getCurrentSpeed() - speedFactor() * amount,0); + getCurrentSpeed() = Math.max(getCurrentSpeed() - speedFactor() * amount,0); } // TODO fix this method according to lab pm diff --git a/car.java b/car.java index fcebf3a94..e406ce86e 100644 --- a/car.java +++ b/car.java @@ -3,19 +3,16 @@ public class car { private final int nrDoors; // Number of doors on the car private final double enginePower; // Engine power of the car - private final double currentSpeed; // The current speed of the car - private final Color color; // Color of the car + public double currentSpeed; // The current speed of the car + private Color color; // Color of the car private final String modelName; // The car model name - public car(int nrDoors, double enginePower, double currentSpeed, Color color, String modelName) { + public car(int nrDoors, double enginePower, Color color, String modelName) { this.nrDoors = nrDoors; this.enginePower = enginePower; - this.currentSpeed = currentSpeed; this.color = color; this.modelName = modelName; } - public car() {} - public int getNrDoors(){ return nrDoors; From 235ce551e03789442d0aaef52594d86349887e60 Mon Sep 17 00:00:00 2001 From: felixtan Date: Thu, 18 Jan 2024 14:17:16 +0100 Subject: [PATCH 04/16] test3 --- Saab95.java | 4 ++-- Volvo240.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Saab95.java b/Saab95.java index e123dbeca..a744abe5f 100644 --- a/Saab95.java +++ b/Saab95.java @@ -25,11 +25,11 @@ public double speedFactor(){ } public void incrementSpeed(double amount){ - getCurrentSpeed() = getCurrentSpeed() + speedFactor() * amount; + currentSpeed = getCurrentSpeed() + speedFactor() * amount; } public void decrementSpeed(double amount){ - getCurrentSpeed() = getCurrentSpeed() - speedFactor() * amount; + currentSpeed = getCurrentSpeed() - speedFactor() * amount; } // TODO fix this method according to lab pm diff --git a/Volvo240.java b/Volvo240.java index 93d141301..bf7ecbb5e 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -14,11 +14,11 @@ public double speedFactor(){ } public void incrementSpeed(double amount){ - getCurrentSpeed() = Math.min(getCurrentSpeed() + speedFactor() * amount,getEnginePower()); + currentSpeed = Math.min(getCurrentSpeed() + speedFactor() * amount,getEnginePower()); } public void decrementSpeed(double amount){ - getCurrentSpeed() = Math.max(getCurrentSpeed() - speedFactor() * amount,0); + currentSpeed = Math.max(getCurrentSpeed() - speedFactor() * amount,0); } // TODO fix this method according to lab pm From 62fc4db6bc03f0cc380c4145a109b82543bbe898 Mon Sep 17 00:00:00 2001 From: felixtan Date: Thu, 18 Jan 2024 14:46:48 +0100 Subject: [PATCH 05/16] test3 --- Movable.java | 5 +++++ Saab95.java | 8 ++++---- Volvo240.java | 8 ++++---- car.java | 11 ++++++++++- out/production/lab1/.idea/.gitignore | 8 ++++++++ out/production/lab1/.idea/misc.xml | 6 ++++++ out/production/lab1/.idea/modules.xml | 8 ++++++++ out/production/lab1/.idea/vcs.xml | 6 ++++++ out/production/lab1/README.md | 4 ++++ out/production/lab1/lab1.iml | 11 +++++++++++ 10 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 Movable.java create mode 100644 out/production/lab1/.idea/.gitignore create mode 100644 out/production/lab1/.idea/misc.xml create mode 100644 out/production/lab1/.idea/modules.xml create mode 100644 out/production/lab1/.idea/vcs.xml create mode 100644 out/production/lab1/README.md create mode 100644 out/production/lab1/lab1.iml diff --git a/Movable.java b/Movable.java new file mode 100644 index 000000000..ca7ecf109 --- /dev/null +++ b/Movable.java @@ -0,0 +1,5 @@ +public interface Movable { + void move(); + void turnleft(); + void turnright(); +} diff --git a/Saab95.java b/Saab95.java index a744abe5f..9b5d728a2 100644 --- a/Saab95.java +++ b/Saab95.java @@ -2,7 +2,7 @@ public class Saab95 extends car{ - public boolean turboOn; + private boolean turboOn; public Saab95(){ super(2,125, Color.red, "Saab95"); turboOn = false; @@ -18,17 +18,17 @@ public void setTurboOff(){ turboOn = false; } - public double speedFactor(){ + private double speedFactor(){ double turbo = 1; if(turboOn) turbo = 1.3; return getEnginePower() * 0.01 * turbo; } - public void incrementSpeed(double amount){ + private void incrementSpeed(double amount){ currentSpeed = getCurrentSpeed() + speedFactor() * amount; } - public void decrementSpeed(double amount){ + private void decrementSpeed(double amount){ currentSpeed = getCurrentSpeed() - speedFactor() * amount; } diff --git a/Volvo240.java b/Volvo240.java index bf7ecbb5e..1d8bfb11e 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -2,22 +2,22 @@ public class Volvo240 extends car{ - public final static double trimFactor = 1.25; + private final static double trimFactor = 1.25; public Volvo240(){ super(4, 100, Color.black, "Volvo240"); stopEngine(); } - public double speedFactor(){ + private double speedFactor(){ return getEnginePower() * 0.01 * trimFactor; } - public void incrementSpeed(double amount){ + private void incrementSpeed(double amount){ currentSpeed = Math.min(getCurrentSpeed() + speedFactor() * amount,getEnginePower()); } - public void decrementSpeed(double amount){ + private void decrementSpeed(double amount){ currentSpeed = Math.max(getCurrentSpeed() - speedFactor() * amount,0); } diff --git a/car.java b/car.java index e406ce86e..e4940fca3 100644 --- a/car.java +++ b/car.java @@ -1,6 +1,6 @@ import java.awt.*; -public class car { +public class car implements Movable { private final int nrDoors; // Number of doors on the car private final double enginePower; // Engine power of the car public double currentSpeed; // The current speed of the car @@ -40,4 +40,13 @@ public void startEngine(){ public void stopEngine(){ currentSpeed = 0; } + public void move() { + + } + public void turnleft() { + + } + public void turnright() { + + } } diff --git a/out/production/lab1/.idea/.gitignore b/out/production/lab1/.idea/.gitignore new file mode 100644 index 000000000..13566b81b --- /dev/null +++ b/out/production/lab1/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/out/production/lab1/.idea/misc.xml b/out/production/lab1/.idea/misc.xml new file mode 100644 index 000000000..ea71d5024 --- /dev/null +++ b/out/production/lab1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab1/.idea/modules.xml b/out/production/lab1/.idea/modules.xml new file mode 100644 index 000000000..e7763c5d4 --- /dev/null +++ b/out/production/lab1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lab1/.idea/vcs.xml b/out/production/lab1/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/out/production/lab1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab1/README.md b/out/production/lab1/README.md new file mode 100644 index 000000000..38ae33214 --- /dev/null +++ b/out/production/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/lab1/lab1.iml b/out/production/lab1/lab1.iml new file mode 100644 index 000000000..b107a2dd8 --- /dev/null +++ b/out/production/lab1/lab1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 10b36c2f104115ab8ba7bcc12857d4540054c4a9 Mon Sep 17 00:00:00 2001 From: felixtan Date: Fri, 19 Jan 2024 14:06:08 +0100 Subject: [PATCH 06/16] test3 --- Saab95.java | 1 + Volvo240.java | 1 + car.java | 10 ++++++--- carTest.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ lab1.iml | 10 +++++++++ 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 carTest.java diff --git a/Saab95.java b/Saab95.java index 9b5d728a2..5d3df63e8 100644 --- a/Saab95.java +++ b/Saab95.java @@ -5,6 +5,7 @@ public class Saab95 extends car{ private boolean turboOn; public Saab95(){ super(2,125, Color.red, "Saab95"); + // använder super för att få tillgång till car konstruktionen för att slippa göra egna variabler turboOn = false; stopEngine(); } diff --git a/Volvo240.java b/Volvo240.java index 1d8bfb11e..0c8e2a060 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -7,6 +7,7 @@ public class Volvo240 extends car{ public Volvo240(){ super(4, 100, Color.black, "Volvo240"); stopEngine(); + // använder super för att få tillgång till car konstruktionen för att slippa göra egna variabler } private double speedFactor(){ diff --git a/car.java b/car.java index e4940fca3..4546450ba 100644 --- a/car.java +++ b/car.java @@ -3,15 +3,19 @@ public class car implements Movable { private final int nrDoors; // Number of doors on the car private final double enginePower; // Engine power of the car - public double currentSpeed; // The current speed of the car + public double currentSpeed; // The current speed of the car // behövs pågrund att den behöver uppdateras i volvo, Saab private Color color; // Color of the car private final String modelName; // The car model name + public int direction = 0; // 360 degrees + public double xPos; + public double yPos; public car(int nrDoors, double enginePower, Color color, String modelName) { this.nrDoors = nrDoors; this.enginePower = enginePower; this.color = color; this.modelName = modelName; + } public int getNrDoors(){ @@ -44,9 +48,9 @@ public void move() { } public void turnleft() { - + direction -= (90 + 360) % 360; } public void turnright() { - + direction += 90 % 360; } } diff --git a/carTest.java b/carTest.java new file mode 100644 index 000000000..ea2810f70 --- /dev/null +++ b/carTest.java @@ -0,0 +1,57 @@ +import org.junit.Test; + +import java.awt.*; + +import static org.junit.Assert.*; + +public class carTest { + public carTest() { + testcar = new car(4, 100, Color.black, "Volvo240"); + } + + car testcar; + @Test + public void getNrDoors() { + assertEquals(4, testcar.getNrDoors()); + } + + @Test + public void getEnginePower() { + assertEquals(100, testcar.getEnginePower(), 00001); + } + + @Test + public void getCurrentSpeed() { + assertEquals(testcar.currentSpeed, testcar.getCurrentSpeed()); + } + + @Test + public void getColor() { + assertEquals(Color.black, testcar.getColor()); + } + + @Test + public void setColor() { + + } + + @Test + public void startEngine() { + } + + @Test + public void stopEngine() { + } + + @Test + public void move() { + } + + @Test + public void turnleft() { + } + + @Test + public void turnright() { + } +} \ No newline at end of file diff --git a/lab1.iml b/lab1.iml index b107a2dd8..c02b99a42 100644 --- a/lab1.iml +++ b/lab1.iml @@ -7,5 +7,15 @@ + + + + + + + + + + \ No newline at end of file From c66a62be71d4342c7d9df0c031d5d0bf9a1b4c58 Mon Sep 17 00:00:00 2001 From: felixtan Date: Mon, 22 Jan 2024 15:14:10 +0100 Subject: [PATCH 07/16] test3 --- CarTest.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ carTest.java | 57 -------------------------------------------- 2 files changed, 67 insertions(+), 57 deletions(-) create mode 100644 CarTest.java delete mode 100644 carTest.java diff --git a/CarTest.java b/CarTest.java new file mode 100644 index 000000000..bcda329c2 --- /dev/null +++ b/CarTest.java @@ -0,0 +1,67 @@ +import org.junit.Test; + +import java.awt.*; + +import static org.junit.Assert.*; + +public class CarTest { + public CarTest() { + volvocar = new car(4, 100, Color.black, "Volvo240"); + sabcar = new car(2, 125, Color.red, "Saab95"); + } + + car volvocar; + car sabcar; + @Test + public void getNrDoors() { + assertEquals(4, volvocar.getNrDoors()); + assertEquals(2, sabcar.getNrDoors()); + } + + @Test + public void getEnginePower() { + assertEquals(100, volvocar.getEnginePower(), 0.0001); + } + + @Test + public void getCurrentSpeed() { + volvocar.startEngine(); + assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + } + + @Test + public void getColor() { + assertEquals(Color.black, volvocar.getColor()); + } + + @Test + public void setColor() { + volvocar.setColor(Color.black); + assertEquals(Color.black, volvocar.getColor()); + } + + @Test + public void startEngine() { + volvocar.startEngine(); + assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + } + + @Test + public void stopEngine() { + volvocar.stopEngine(); + assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); + } + + @Test + public void move() { + } + + @Test + public void turnleft() { + + } + + @Test + public void turnright() { + } +} \ No newline at end of file diff --git a/carTest.java b/carTest.java deleted file mode 100644 index ea2810f70..000000000 --- a/carTest.java +++ /dev/null @@ -1,57 +0,0 @@ -import org.junit.Test; - -import java.awt.*; - -import static org.junit.Assert.*; - -public class carTest { - public carTest() { - testcar = new car(4, 100, Color.black, "Volvo240"); - } - - car testcar; - @Test - public void getNrDoors() { - assertEquals(4, testcar.getNrDoors()); - } - - @Test - public void getEnginePower() { - assertEquals(100, testcar.getEnginePower(), 00001); - } - - @Test - public void getCurrentSpeed() { - assertEquals(testcar.currentSpeed, testcar.getCurrentSpeed()); - } - - @Test - public void getColor() { - assertEquals(Color.black, testcar.getColor()); - } - - @Test - public void setColor() { - - } - - @Test - public void startEngine() { - } - - @Test - public void stopEngine() { - } - - @Test - public void move() { - } - - @Test - public void turnleft() { - } - - @Test - public void turnright() { - } -} \ No newline at end of file From b97090f48de67b429ec5899f1a202daede3d0809 Mon Sep 17 00:00:00 2001 From: felixtan Date: Mon, 22 Jan 2024 15:25:27 +0100 Subject: [PATCH 08/16] test3 --- CarTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CarTest.java b/CarTest.java index bcda329c2..99c06f029 100644 --- a/CarTest.java +++ b/CarTest.java @@ -1,3 +1,5 @@ + +import org.junit.Before; import org.junit.Test; import java.awt.*; @@ -21,35 +23,46 @@ public void getNrDoors() { @Test public void getEnginePower() { assertEquals(100, volvocar.getEnginePower(), 0.0001); + assertEquals(125, sabcar.getEnginePower(), 0.0001); + } @Test public void getCurrentSpeed() { volvocar.startEngine(); assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + sabcar.startEngine(); + assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); } @Test public void getColor() { assertEquals(Color.black, volvocar.getColor()); + assertEquals(Color.red, sabcar.getColor()); } @Test public void setColor() { volvocar.setColor(Color.black); assertEquals(Color.black, volvocar.getColor()); + sabcar.setColor(Color.red); + assertEquals(Color.red, sabcar.getColor()); } @Test public void startEngine() { volvocar.startEngine(); assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + sabcar.startEngine(); + assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); } @Test public void stopEngine() { volvocar.stopEngine(); assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); + sabcar.stopEngine(); + assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); } @Test From fda54162fb211cb317efcf3a4029608d5096278a Mon Sep 17 00:00:00 2001 From: felixtan Date: Fri, 26 Jan 2024 14:46:59 +0100 Subject: [PATCH 09/16] test3 --- Car.java | 115 +++++++++++++++++++++++++++++++++++++++++ CarTest.java | 138 ++++++++++++++++++++++++++++++++++++++++++++++---- Saab95.java | 24 +++------ Volvo240.java | 26 ++++------ car.java | 56 -------------------- 5 files changed, 259 insertions(+), 100 deletions(-) create mode 100644 Car.java delete mode 100644 car.java diff --git a/Car.java b/Car.java new file mode 100644 index 000000000..c308b8e3f --- /dev/null +++ b/Car.java @@ -0,0 +1,115 @@ +import java.awt.*; + +public abstract class Car implements Movable { + private final int nrDoors; // Number of doors on the car + private final double enginePower; // Engine power of the car + private double currentSpeed; // The current speed of the car // behövs pågrund att den behöver uppdateras i volvo, Saab + private Color color; // Color of the car + private final String modelName; // The car model name + private int direction; // + private double xPos; + private double yPos; + + public Car(int nrDoors, double enginePower, Color color, String modelName) { + this.nrDoors = nrDoors; + this.enginePower = enginePower; + this.color = color; + this.modelName = modelName; + this.direction = 0; + this.yPos = 0.0; + this.xPos = 0.0; + } + + public int getNrDoors() { + return nrDoors; + } + + public void setCurrentSpeed(double amount) {currentSpeed = amount;} + 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 setDirection(int i) { + direction = i; + } + + public int getDirection() { + return direction; + } + + public void move() { + switch (direction) { + case 0: // north + yPos += currentSpeed; + break; + case 1: // east + xPos += currentSpeed; + break; + case 2: // south + yPos -= currentSpeed; + break; + case 3: // west + xPos -= currentSpeed; + break; + default: + System.out.println("unknown direction"); + } + } + + public void turnleft() { + direction -= (1 + 4) % 4; + } + + public void turnright() { + direction += 1 % 4; + } + + public void gas(double amount) { + if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) { + incrementSpeed(amount); + if (currentSpeed > getEnginePower()) { + currentSpeed = getEnginePower(); + System.out.println("max speed has reached"); + } + } + else + System.out.println("amount is not ok"); + } + + // TODO fix this method according to lab pm + public void brake(double amount) { + if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) { + decrementSpeed(amount); + if (currentSpeed < 0) { + currentSpeed = 0; + System.out.println("minimum speed has reached"); + } + } + else + throw new IllegalArgumentException("amount to high"); + } + + protected void decrementSpeed(double amount) {}; + protected void incrementSpeed(double amount) {}; + +} diff --git a/CarTest.java b/CarTest.java index 99c06f029..9dfdb1fcb 100644 --- a/CarTest.java +++ b/CarTest.java @@ -7,13 +7,14 @@ import static org.junit.Assert.*; public class CarTest { - public CarTest() { - volvocar = new car(4, 100, Color.black, "Volvo240"); - sabcar = new car(2, 125, Color.red, "Saab95"); + Volvo240 volvocar; + Saab95 sabcar; + @Before + public void setUp() { + volvocar = new Volvo240(); + sabcar = new Saab95(); } - car volvocar; - car sabcar; @Test public void getNrDoors() { assertEquals(4, volvocar.getNrDoors()); @@ -53,28 +54,143 @@ public void setColor() { public void startEngine() { volvocar.startEngine(); assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); - sabcar.startEngine(); - assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); } @Test public void stopEngine() { - volvocar.stopEngine(); - assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); sabcar.stopEngine(); assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); } @Test public void move() { + volvocar.startEngine(); + volvocar.move(); + assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + sabcar.startEngine(); + sabcar.move(); + assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); } @Test - public void turnleft() { + public void turnLeft() { + volvocar.setDirection(0); + if (volvocar.getDirection() == 0) { + volvocar.setDirection(4); + } + volvocar.turnleft(); + assertEquals(3, volvocar.getDirection()); } @Test - public void turnright() { + public void turnRight() { + volvocar.setDirection(4); + if (volvocar.getDirection() == 4) { + volvocar.setDirection(-1); + } + volvocar.turnright(); + assertEquals(0, volvocar.getDirection()); + } + @Test + public void gasVolvo() { + //amount is higher than 1 + volvocar.gas(1.01); + assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); + //checks if speed is increasing and speed dosent lower when you gas + double e = volvocar.getCurrentSpeed(); + volvocar.gas(0.2); + assertEquals(0.25, volvocar.getCurrentSpeed(), 0.0001); + assertTrue(volvocar.getCurrentSpeed() >= e); + //speed limit + volvocar.setCurrentSpeed(99.99); + volvocar.gas(1); + assertEquals(100, volvocar.getCurrentSpeed(), 0.0001); + } + @Test + public void gasSab() { + //amount is higher than 1 + sabcar.gas(1.01); + assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); + //checks if speed is increasing and speed dosent lower when you gas + double f = sabcar.getCurrentSpeed(); + sabcar.gas(0.2); + assertEquals(0.25, sabcar.getCurrentSpeed(), 0.0001); + assertTrue(sabcar.getCurrentSpeed() >= f); + //speed limit + sabcar.setCurrentSpeed(124.99); + sabcar.gas(1); + assertEquals(125, sabcar.getCurrentSpeed(), 0.0001); + } + + + @Test + public void brakeVolvo() { + //amount lower than 0 + volvocar.brake(-0.01); + assertEquals(0, volvocar.getCurrentSpeed(), 0.0001); + //check if speed is lower, and speed dosent get higher. + volvocar.setCurrentSpeed(1); + double e = volvocar.getCurrentSpeed(); + volvocar.brake(0.2); + assertEquals(0.75, volvocar.getCurrentSpeed(), 0.0001); + assertTrue(volvocar.getCurrentSpeed() <= e); + // speed cant be lower than 0 + volvocar.setCurrentSpeed(0.01); + volvocar.brake(0.9); + assertEquals(0, volvocar.getCurrentSpeed(), 0.0001); } + @Test + public void brakeSab() { + //amount lower than 0 + sabcar.brake(-0.01); + assertEquals(0, sabcar.getCurrentSpeed(), 0.0001); + //check if speed is lower, and speed dosent get higher. + sabcar.setCurrentSpeed(1); + double f = sabcar.getCurrentSpeed(); + sabcar.brake(0.2); + assertEquals(0.75, sabcar.getCurrentSpeed(), 0.0001); + assertTrue(sabcar.getCurrentSpeed() <= f); + // speed cant be lower than 0 + sabcar.setCurrentSpeed(0.01); + sabcar.brake(0.9); + assertEquals(0, sabcar.getCurrentSpeed(), 0.0001); + } + @Test + public void turboOn() { + sabcar.setTurboOn(); + assertTrue(sabcar.getTurbo()); + } + @Test + public void turboOff() { + sabcar.setTurboOff(); + assertFalse(sabcar.getTurbo()); + } + @Test + public void raiseFlak() { + Scania scania = new Scania(); + scania.raiseFlak(); + assertEquals(5, scania.getAngleFlak(), 0.0001); + + scania.angle = 69; + scania.raiseFlak(); + assertEquals(70, scania.getAngleFlak(),0.0001); + + scania.setDirection(40); + scania.raiseFlak(); + } + + @Test + public void lowerFlak() { + Scania scania = new Scania(); + scania.angle = 5; + scania.lowerFlak(); + assertEquals(0, scania.getAngleFlak(), 0.0001); + + scania.lowerFlak(); + assertEquals(0, scania.getAngleFlak(),0.0001); + + scania.setCurrentSpeed(40); + scania.lowerFlak(); + } } \ No newline at end of file diff --git a/Saab95.java b/Saab95.java index 5d3df63e8..318724190 100644 --- a/Saab95.java +++ b/Saab95.java @@ -1,6 +1,6 @@ import java.awt.*; -public class Saab95 extends car{ +public class Saab95 extends Car { private boolean turboOn; public Saab95(){ @@ -10,7 +10,7 @@ public Saab95(){ stopEngine(); } - + public boolean getTurbo() { return turboOn;} public void setTurboOn(){ turboOn = true; } @@ -24,22 +24,14 @@ private double speedFactor(){ if(turboOn) turbo = 1.3; return getEnginePower() * 0.01 * turbo; } + @Override + protected void incrementSpeed(double amount){ + this.setCurrentSpeed(getCurrentSpeed() + speedFactor() * amount); - private void incrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() + speedFactor() * amount; } - - private void decrementSpeed(double amount){ - currentSpeed = getCurrentSpeed() - speedFactor() * amount; - } - - // TODO fix this method according to lab pm - public void gas(double amount){ - incrementSpeed(amount); + @Override + protected void decrementSpeed(double amount){ + this.setCurrentSpeed(getCurrentSpeed() - speedFactor() * amount); } - // TODO fix this method according to lab pm - public void brake(double amount){ - decrementSpeed(amount); - } } diff --git a/Volvo240.java b/Volvo240.java index 0c8e2a060..d3846724b 100644 --- a/Volvo240.java +++ b/Volvo240.java @@ -1,34 +1,26 @@ import java.awt.*; -public class Volvo240 extends car{ +public class Volvo240 extends Car { private final static double trimFactor = 1.25; - public Volvo240(){ + public Volvo240() { super(4, 100, Color.black, "Volvo240"); stopEngine(); // använder super för att få tillgång till car konstruktionen för att slippa göra egna variabler } - private double speedFactor(){ + private double speedFactor() { return getEnginePower() * 0.01 * trimFactor; } - - private void incrementSpeed(double amount){ - currentSpeed = Math.min(getCurrentSpeed() + speedFactor() * amount,getEnginePower()); - } - - private void decrementSpeed(double amount){ - currentSpeed = Math.max(getCurrentSpeed() - speedFactor() * amount,0); + @Override + protected void incrementSpeed(double amount) { + this.setCurrentSpeed(Math.min(getCurrentSpeed() + speedFactor() * amount, getEnginePower())); } + @Override + protected void decrementSpeed(double amount) { + this.setCurrentSpeed(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/car.java b/car.java deleted file mode 100644 index 4546450ba..000000000 --- a/car.java +++ /dev/null @@ -1,56 +0,0 @@ -import java.awt.*; - -public class car implements Movable { - private final int nrDoors; // Number of doors on the car - private final double enginePower; // Engine power of the car - public double currentSpeed; // The current speed of the car // behövs pågrund att den behöver uppdateras i volvo, Saab - private Color color; // Color of the car - private final String modelName; // The car model name - public int direction = 0; // 360 degrees - public double xPos; - public double yPos; - - public car(int nrDoors, double enginePower, Color color, String modelName) { - this.nrDoors = nrDoors; - this.enginePower = enginePower; - this.color = color; - this.modelName = modelName; - - } - - 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 move() { - - } - public void turnleft() { - direction -= (90 + 360) % 360; - } - public void turnright() { - direction += 90 % 360; - } -} From c3dbd07068fa5791977e09eb3eec21596f6ab462 Mon Sep 17 00:00:00 2001 From: felixtan Date: Mon, 29 Jan 2024 15:42:14 +0100 Subject: [PATCH 10/16] test3 --- BilTransport.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++ Car.java | 14 +++++++++++-- CarTest.java | 46 +++++++++++++++++++++++++++++------------- Scania.java | 49 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 BilTransport.java create mode 100644 Scania.java diff --git a/BilTransport.java b/BilTransport.java new file mode 100644 index 000000000..ee02f6ad2 --- /dev/null +++ b/BilTransport.java @@ -0,0 +1,51 @@ +import java.awt.*; +import java.util.ArrayDeque; +import java.util.Deque; + +public class BilTransport extends Car{ + private boolean rampState; + private Deque flaket = new ArrayDeque<>(); + private int maxload = 4; + private final double maxReach = 100.0; + + public BilTransport(){ + super(2, 60, Color.blue, "BilTransport"); + this.rampState = false; + } + + public void raiseRamp() { + if(getCurrentSpeed() == 0.0) { + rampState = true; + } + else + throw new IllegalArgumentException("cant raise ramp car is moving"); + } + public void lowerRamp() { + if(getCurrentSpeed() == 0.0) { + rampState = false; + } + else + throw new IllegalArgumentException("cant lower ramp car is moving"); + } + + public void loadCar(Car car) { + double x = Math.abs(car.getxPos() - this.getxPos()); + double y = Math.abs(car.getyPos() - this.getyPos()); + if(!rampState && flaket.size() <= maxload + && x <= maxReach && y <= maxReach) { + flaket.push(car); + car.setxPos(this.getxPos()); + car.setyPos(this.getyPos()); + } + + else + throw new IllegalArgumentException("error"); + } + public void unLoadCar(Car car) { + if(!rampState) { + flaket.pop(); + } + } + + +} diff --git a/Car.java b/Car.java index c308b8e3f..a15969883 100644 --- a/Car.java +++ b/Car.java @@ -19,7 +19,18 @@ public Car(int nrDoors, double enginePower, Color color, String modelName) { this.yPos = 0.0; this.xPos = 0.0; } - + public double getxPos() { + return xPos; + } + public double getyPos() { + return yPos; + } + public void setxPos(double pos) { + xPos = pos; + } + public void setyPos(double pos) { + yPos = pos; + } public int getNrDoors() { return nrDoors; } @@ -111,5 +122,4 @@ public void brake(double amount) { protected void decrementSpeed(double amount) {}; protected void incrementSpeed(double amount) {}; - } diff --git a/CarTest.java b/CarTest.java index 9dfdb1fcb..adc58ed4a 100644 --- a/CarTest.java +++ b/CarTest.java @@ -126,9 +126,12 @@ public void gasSab() { @Test public void brakeVolvo() { - //amount lower than 0 - volvocar.brake(-0.01); - assertEquals(0, volvocar.getCurrentSpeed(), 0.0001); + try { + //amount lower than 0 + volvocar.brake(-0.01); + } catch (IllegalArgumentException e) { + System.out.println("amount to low"); + } //check if speed is lower, and speed dosent get higher. volvocar.setCurrentSpeed(1); double e = volvocar.getCurrentSpeed(); @@ -142,9 +145,12 @@ public void brakeVolvo() { } @Test public void brakeSab() { - //amount lower than 0 - sabcar.brake(-0.01); - assertEquals(0, sabcar.getCurrentSpeed(), 0.0001); + try { + //amount lower than 0 + sabcar.brake(-0.01); + } catch (IllegalArgumentException e) { + System.out.println("amount is to low"); + } //check if speed is lower, and speed dosent get higher. sabcar.setCurrentSpeed(1); double f = sabcar.getCurrentSpeed(); @@ -168,29 +174,41 @@ public void turboOff() { } @Test public void raiseFlak() { + //raise flak with 5 Scania scania = new Scania(); scania.raiseFlak(); assertEquals(5, scania.getAngleFlak(), 0.0001); - - scania.angle = 69; + // cant raise more than 70 + scania.setAngleFLak(69); scania.raiseFlak(); assertEquals(70, scania.getAngleFlak(),0.0001); - + //cant raise flak while car is moving scania.setDirection(40); - scania.raiseFlak(); + try { + scania.raiseFlak(); + } catch (IllegalArgumentException e) { + System.out.println("car is moving"); + } + } @Test public void lowerFlak() { + // lower flak with 5 Scania scania = new Scania(); - scania.angle = 5; + scania.setAngleFLak(5); scania.lowerFlak(); assertEquals(0, scania.getAngleFlak(), 0.0001); - + // cant lower more than 0 scania.lowerFlak(); assertEquals(0, scania.getAngleFlak(),0.0001); - + //cant lower flak while car is moving scania.setCurrentSpeed(40); - scania.lowerFlak(); + try { + scania.lowerFlak(); + } catch (IllegalArgumentException e) { + System.out.println("car is moving"); + } } + } \ No newline at end of file diff --git a/Scania.java b/Scania.java new file mode 100644 index 000000000..0822197d2 --- /dev/null +++ b/Scania.java @@ -0,0 +1,49 @@ +import java.awt.*; + +public class Scania extends Car { + private int angle; + private final double trimFactor = 0.8; + + public Scania() { + super(2, 80, Color.white, "Scania"); + this.angle = 0; + } + private double speedFactor() { + return getEnginePower() * 0.01 * trimFactor; + } + @Override + protected void incrementSpeed(double amount) { + this.setCurrentSpeed(Math.min(getCurrentSpeed() + speedFactor() * amount, getEnginePower())); + } + @Override + protected void decrementSpeed(double amount) { + this.setCurrentSpeed(Math.max(getCurrentSpeed() - speedFactor() * amount, 0)); + } + public int getAngleFlak() { + return angle; + } + public void setAngleFLak(int amount) {angle = amount;} + + public void lowerFlak() { + if (getCurrentSpeed() == 0) { + angle -= 5; + if (angle < 0) { + angle = 0; + } + } + else + throw new IllegalArgumentException("car is moving, can´t lower the flak!"); + } + + public void raiseFlak(){ + if(getCurrentSpeed() == 0) { + angle += 5; + if (angle > 70.0) { + angle = 70; + } + } + else + throw new IllegalArgumentException("car is moving, can´t raise the flak!"); + } +} + From af16df01b7d6dd618678aff5e8180d42a84d71f0 Mon Sep 17 00:00:00 2001 From: felixtan Date: Mon, 29 Jan 2024 15:48:57 +0100 Subject: [PATCH 11/16] test3 --- BilTransport.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BilTransport.java b/BilTransport.java index ee02f6ad2..0ca47ed22 100644 --- a/BilTransport.java +++ b/BilTransport.java @@ -6,7 +6,7 @@ public class BilTransport extends Car{ private boolean rampState; private Deque flaket = new ArrayDeque<>(); private int maxload = 4; - private final double maxReach = 100.0; + private final double maxReach = 10.0; public BilTransport(){ super(2, 60, Color.blue, "BilTransport"); @@ -42,7 +42,7 @@ public void loadCar(Car car) { throw new IllegalArgumentException("error"); } public void unLoadCar(Car car) { - if(!rampState) { + if(!rampState && !flaket.isEmpty()) { flaket.pop(); } } From 3ac0ff75b0685ea47cae0c7b3df02001994a0cd6 Mon Sep 17 00:00:00 2001 From: felixtan Date: Tue, 30 Jan 2024 13:58:36 +0100 Subject: [PATCH 12/16] test3 --- BilTransport.java | 14 +++----- Car.java | 14 ++++++-- CarRun.java | 24 ++++++++++++++ JPanel.java | 82 +++++++++++++++++++++++++++++++++++++++++++++++ KeyHandler.java | 46 ++++++++++++++++++++++++++ Verkstad.java | 28 ++++++++++++++++ 6 files changed, 196 insertions(+), 12 deletions(-) create mode 100644 CarRun.java create mode 100644 JPanel.java create mode 100644 KeyHandler.java create mode 100644 Verkstad.java diff --git a/BilTransport.java b/BilTransport.java index 0ca47ed22..803cfebc3 100644 --- a/BilTransport.java +++ b/BilTransport.java @@ -1,12 +1,11 @@ import java.awt.*; +import java.awt.geom.Point2D; import java.util.ArrayDeque; import java.util.Deque; public class BilTransport extends Car{ private boolean rampState; - private Deque flaket = new ArrayDeque<>(); - private int maxload = 4; - private final double maxReach = 10.0; + private int maxload = 5; public BilTransport(){ super(2, 60, Color.blue, "BilTransport"); @@ -29,15 +28,12 @@ public void lowerRamp() { } public void loadCar(Car car) { - double x = Math.abs(car.getxPos() - this.getxPos()); - double y = Math.abs(car.getyPos() - this.getyPos()); + Point2D.Double transportCoordinates = new Point2D.Double(car.getxPos(), car.getyPos()); if(!rampState && flaket.size() <= maxload - && x <= maxReach && y <= maxReach) { + && car.getCordination().distance(transportCoordinates) <= 5.0) { flaket.push(car); - car.setxPos(this.getxPos()); - car.setyPos(this.getyPos()); + car.getCordination().setLocation(transportCoordinates); } - else throw new IllegalArgumentException("error"); } diff --git a/Car.java b/Car.java index a15969883..2946dd842 100644 --- a/Car.java +++ b/Car.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.awt.geom.Point2D; public abstract class Car implements Movable { private final int nrDoors; // Number of doors on the car @@ -9,6 +10,7 @@ public abstract class Car implements Movable { private int direction; // private double xPos; private double yPos; + private Point2D.Double cordination = new Point2D.Double(xPos, yPos); public Car(int nrDoors, double enginePower, Color color, String modelName) { this.nrDoors = nrDoors; @@ -19,6 +21,9 @@ public Car(int nrDoors, double enginePower, Color color, String modelName) { this.yPos = 0.0; this.xPos = 0.0; } + + // Getters och setters + public String getModelName() {return modelName;} public double getxPos() { return xPos; } @@ -31,10 +36,10 @@ public void setxPos(double pos) { public void setyPos(double pos) { yPos = pos; } - public int getNrDoors() { - return nrDoors; + public Point2D.Double getCordination(){ + return cordination; } - + public int getNrDoors() {return nrDoors;} public void setCurrentSpeed(double amount) {currentSpeed = amount;} public double getEnginePower() { return enginePower; @@ -85,6 +90,9 @@ public void move() { default: System.out.println("unknown direction"); } + System.out.println("Current Position: (" + xPos + ", " + yPos + ")"); + System.out.println("current direction" + direction); + } public void turnleft() { diff --git a/CarRun.java b/CarRun.java new file mode 100644 index 000000000..9c8a2046c --- /dev/null +++ b/CarRun.java @@ -0,0 +1,24 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class CarRun { + + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.setLocationRelativeTo(null); + frame.setTitle("2d car"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setResizable(false); + + JPanel jpanel = new JPanel(); + frame.add(jpanel); + frame.pack(); + frame.setVisible(true); + + jpanel.startGameThread(); + } +} + + diff --git a/JPanel.java b/JPanel.java new file mode 100644 index 000000000..709932ce1 --- /dev/null +++ b/JPanel.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import java.awt.*; + +public class JPanel extends javax.swing.JPanel implements Runnable{ + private int width = 800; + private int height = 600; + int playerX = 400; + int playerY = 100; + int FPS = 60; + + Thread gameThread; + KeyHandler keyH = new KeyHandler(); + + public JPanel() { + this.setPreferredSize(new Dimension(width, height)); + this.setBackground(Color.WHITE); + this.setDoubleBuffered(true); + this.addKeyListener(keyH); + this.setFocusable(true); + } + + public void startGameThread() { + gameThread = new Thread(this); + gameThread.start(); + } + + BilTransport car = new BilTransport(); + @Override + public void run() { + double drawInterval = 1000000000/FPS; + double nextDrawTime = System.nanoTime() + drawInterval; + while(gameThread != null) { + + update(); + + repaint(); + + try { + double remainingTime = nextDrawTime - System.nanoTime(); + remainingTime= remainingTime/1000000; + if(remainingTime<0) { + remainingTime=0; + } + Thread.sleep((long) remainingTime); + nextDrawTime += drawInterval; + } + catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void update(){ + if(keyH.upPressed) { + // playerY += 10; + car.gas(0.3); + } + if(keyH.downPressed) { + car.brake(0.3); + //playerY -=10; + } + if(keyH.rightPressed) { + car.turnright(); + //playerX += 10; + } + if(keyH.leftPressed) { + car.turnleft(); + // playerX -= 10;; + } + car.move(); + } + + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D)g; + g2.setColor(Color.blue); + g2.fillRect((int) car.getxPos(), (int) car.getyPos(), 15, 15); + g2.dispose(); + + } + + +} diff --git a/KeyHandler.java b/KeyHandler.java new file mode 100644 index 000000000..cb7192a68 --- /dev/null +++ b/KeyHandler.java @@ -0,0 +1,46 @@ +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class KeyHandler implements KeyListener { +public boolean upPressed, downPressed, leftPressed, rightPressed; + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + int code = e.getKeyCode(); + + if(code == KeyEvent.VK_W) { + upPressed = true; + } + if(code == KeyEvent.VK_S) { + downPressed = true; + } + if(code == KeyEvent.VK_A) { + leftPressed = true; + } + if(code == KeyEvent.VK_D) { + rightPressed = true; + } + } + + @Override + public void keyReleased(KeyEvent e) { + int code = e.getKeyCode(); + + if(code == KeyEvent.VK_W) { + upPressed = false; + } + if(code == KeyEvent.VK_S) { + downPressed = false; + } + if(code == KeyEvent.VK_A) { + leftPressed = false; + } + if(code == KeyEvent.VK_D) { + rightPressed = false; + } + } +} diff --git a/Verkstad.java b/Verkstad.java new file mode 100644 index 000000000..f584f712c --- /dev/null +++ b/Verkstad.java @@ -0,0 +1,28 @@ +import java.awt.geom.Point2D; +import java.util.ArrayList; +import java.util.List; + +public class Verkstad { + private List verkstaden; + private Point2D.Double position; + private final int maxCapacity; + + + public Verkstad(final int maxCapacity, Point2D.Double position) { + this.verkstaden = new ArrayList<>(); + this.position = position; + this.maxCapacity = maxCapacity; + } + + private boolean hasONECarAllowedInVerkstad() { + return true; + } + + public void loadCar(BilTransport transport) { + if (verkstaden.size() <= maxCapacity) { + + verkstaden.add(car); + } else + throw new IllegalArgumentException("verkstaden is full"); + } +} From bc70c79b3bb2f5c8a61f23c6f662b2cecbfefe92 Mon Sep 17 00:00:00 2001 From: ArvidJaha <146183591+ArvidJaha@users.noreply.github.com> Date: Thu, 1 Feb 2024 23:07:37 +0100 Subject: [PATCH 13/16] Detta fungerar inte! --- .idea/misc.xml | 1 - BilTransport.java | 40 +++--- Car.java | 42 +++--- CarTest.java | 200 +++++++++++++++++++++++------ JPanel.java | 12 +- Ramp.java | 31 +++++ Saab95.java | 5 +- Scania.java | 54 +++----- Truck.java | 68 ++++++++++ Verkstad.java | 32 +++-- out/production/lab1/.idea/misc.xml | 1 - out/production/lab1/lab1.iml | 10 ++ 12 files changed, 353 insertions(+), 143 deletions(-) create mode 100644 Ramp.java create mode 100644 Truck.java diff --git a/.idea/misc.xml b/.idea/misc.xml index ea71d5024..7d53ab542 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/BilTransport.java b/BilTransport.java index 803cfebc3..32d76441e 100644 --- a/BilTransport.java +++ b/BilTransport.java @@ -1,47 +1,53 @@ +import org.junit.Test; + import java.awt.*; import java.awt.geom.Point2D; import java.util.ArrayDeque; import java.util.Deque; +import java.util.Queue; -public class BilTransport extends Car{ +public class BilTransport extends Truck { + private final Ramp ramp = new Ramp(); private boolean rampState; - private int maxload = 5; + private Deque flaket; + private final int MAXLOAD; public BilTransport(){ - super(2, 60, Color.blue, "BilTransport"); + this.flaket = new ArrayDeque<>(); this.rampState = false; + this.MAXLOAD = 5; + this.setAngle(0); } + @Override + public boolean getRampState() { return this.rampState; } + + @Override public void raiseRamp() { - if(getCurrentSpeed() == 0.0) { - rampState = true; - } - else - throw new IllegalArgumentException("cant raise ramp car is moving"); + ramp.raiseRamp(getCurrentSpeed(), rampState); } + public void lowerRamp() { - if(getCurrentSpeed() == 0.0) { + if(this.getCurrentSpeed() == 0) { + this.setAngle(70); rampState = false; } - else - throw new IllegalArgumentException("cant lower ramp car is moving"); + else throw new IllegalArgumentException("cant lower ramp car is moving"); } public void loadCar(Car car) { Point2D.Double transportCoordinates = new Point2D.Double(car.getxPos(), car.getyPos()); - if(!rampState && flaket.size() <= maxload + if(!rampState && flaket.size() <= MAXLOAD && car.getCordination().distance(transportCoordinates) <= 5.0) { flaket.push(car); car.getCordination().setLocation(transportCoordinates); } - else - throw new IllegalArgumentException("error"); + else throw new IllegalArgumentException("error"); } public void unLoadCar(Car car) { if(!rampState && !flaket.isEmpty()) { - flaket.pop(); + flaket.remove(car); } } - - + public Deque getFlak() { return flaket; } } diff --git a/Car.java b/Car.java index 2946dd842..def44c75d 100644 --- a/Car.java +++ b/Car.java @@ -10,10 +10,11 @@ public abstract class Car implements Movable { private int direction; // private double xPos; private double yPos; - private Point2D.Double cordination = new Point2D.Double(xPos, yPos); + private Point2D.Double cordination; public Car(int nrDoors, double enginePower, Color color, String modelName) { this.nrDoors = nrDoors; + this.cordination = new Point2D.Double(xPos, yPos); this.enginePower = enginePower; this.color = color; this.modelName = modelName; @@ -23,19 +24,14 @@ public Car(int nrDoors, double enginePower, Color color, String modelName) { } // Getters och setters - public String getModelName() {return modelName;} + public double setXPos(double d) { return xPos = d;} + public double setYPos(double d) { return yPos = d;} public double getxPos() { return xPos; } public double getyPos() { return yPos; } - public void setxPos(double pos) { - xPos = pos; - } - public void setyPos(double pos) { - yPos = pos; - } public Point2D.Double getCordination(){ return cordination; } @@ -89,43 +85,39 @@ public void move() { break; default: System.out.println("unknown direction"); + break; } System.out.println("Current Position: (" + xPos + ", " + yPos + ")"); - System.out.println("current direction" + direction); - + System.out.println("Current direction: " + direction); } public void turnleft() { - direction -= (1 + 4) % 4; + direction ++; + if (direction > 4) { + direction = 0; + } } public void turnright() { - direction += 1 % 4; + direction --; + if (direction < 0) { + direction = 4; + } } public void gas(double amount) { if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) { incrementSpeed(amount); - if (currentSpeed > getEnginePower()) { - currentSpeed = getEnginePower(); - System.out.println("max speed has reached"); - } + currentSpeed = Math.min(currentSpeed, getEnginePower()); } - else - System.out.println("amount is not ok"); + this.move(); } - // TODO fix this method according to lab pm public void brake(double amount) { if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) { decrementSpeed(amount); - if (currentSpeed < 0) { - currentSpeed = 0; - System.out.println("minimum speed has reached"); - } + currentSpeed = Math.max(currentSpeed, 0); } - else - throw new IllegalArgumentException("amount to high"); } protected void decrementSpeed(double amount) {}; diff --git a/CarTest.java b/CarTest.java index adc58ed4a..750517216 100644 --- a/CarTest.java +++ b/CarTest.java @@ -3,16 +3,26 @@ import org.junit.Test; import java.awt.*; +import java.sql.SQLOutput; +import java.util.ArrayList; import static org.junit.Assert.*; public class CarTest { Volvo240 volvocar; Saab95 sabcar; + BilTransport bilTransport; + Scania scania; + Verkstad verkstadMedVolvo; + Verkstad verkstadMedAllaTrucks; @Before public void setUp() { - volvocar = new Volvo240(); - sabcar = new Saab95(); + this.volvocar = new Volvo240(); + this.sabcar = new Saab95(); + this.scania = new Scania(); + this.bilTransport = new BilTransport(); + this.verkstadMedVolvo = new Verkstad<>(2, new ArrayList<>()); + this.verkstadMedAllaTrucks = new Verkstad<>(2, new ArrayList<>()); } @Test @@ -79,8 +89,15 @@ public void turnLeft() { volvocar.setDirection(4); } volvocar.turnleft(); + assertEquals(0, volvocar.getDirection()); + volvocar.turnleft(); + assertEquals(1, volvocar.getDirection()); + volvocar.turnleft(); + assertEquals(2, volvocar.getDirection()); + volvocar.turnleft(); assertEquals(3, volvocar.getDirection()); - + volvocar.turnleft(); + assertEquals(4, volvocar.getDirection()); } @Test @@ -90,14 +107,22 @@ public void turnRight() { volvocar.setDirection(-1); } volvocar.turnright(); - assertEquals(0, volvocar.getDirection()); + assertEquals(4, volvocar.getDirection()); + volvocar.turnright(); + assertEquals(3, volvocar.getDirection()); + volvocar.turnright(); + assertEquals(2, volvocar.getDirection()); + volvocar.turnright(); + assertEquals(1, volvocar.getDirection()); + } + @Test public void gasVolvo() { //amount is higher than 1 volvocar.gas(1.01); assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); - //checks if speed is increasing and speed dosent lower when you gas + //checks if speed is increasing and speed doesn't decrease when you gas double e = volvocar.getCurrentSpeed(); volvocar.gas(0.2); assertEquals(0.25, volvocar.getCurrentSpeed(), 0.0001); @@ -107,22 +132,21 @@ public void gasVolvo() { volvocar.gas(1); assertEquals(100, volvocar.getCurrentSpeed(), 0.0001); } - @Test - public void gasSab() { - //amount is higher than 1 - sabcar.gas(1.01); - assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); - //checks if speed is increasing and speed dosent lower when you gas - double f = sabcar.getCurrentSpeed(); - sabcar.gas(0.2); - assertEquals(0.25, sabcar.getCurrentSpeed(), 0.0001); - assertTrue(sabcar.getCurrentSpeed() >= f); - //speed limit - sabcar.setCurrentSpeed(124.99); - sabcar.gas(1); - assertEquals(125, sabcar.getCurrentSpeed(), 0.0001); - } - + @Test + public void gasSab() { + //amount is higher than 1 + sabcar.gas(1.01); + assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); + //checks if speed is increasing and speed doesn't decrease when you gas + double f = sabcar.getCurrentSpeed(); + sabcar.gas(0.2); + assertEquals(0.25, sabcar.getCurrentSpeed(), 0.0001); + assertTrue(sabcar.getCurrentSpeed() >= f); + //speed limit + sabcar.setCurrentSpeed(124.99); + sabcar.gas(1); + assertEquals(125, sabcar.getCurrentSpeed(), 0.0001); + } @Test public void brakeVolvo() { @@ -173,42 +197,142 @@ public void turboOff() { assertFalse(sabcar.getTurbo()); } @Test - public void raiseFlak() { + public void raiseRamp() { //raise flak with 5 - Scania scania = new Scania(); - scania.raiseFlak(); - assertEquals(5, scania.getAngleFlak(), 0.0001); + scania.raiseRamp(); + assertEquals(5, scania.getAngle(), 0.0001); // cant raise more than 70 - scania.setAngleFLak(69); - scania.raiseFlak(); - assertEquals(70, scania.getAngleFlak(),0.0001); + scania.setAngle(69); + scania.raiseRamp(); + assertEquals(70, scania.getAngle(),0.0001); //cant raise flak while car is moving scania.setDirection(40); try { - scania.raiseFlak(); + scania.raiseRamp(); } catch (IllegalArgumentException e) { System.out.println("car is moving"); } + // Same tests with BilTransport + bilTransport.raiseRamp(); + assertTrue(bilTransport.getRampState()); } @Test - public void lowerFlak() { + public void lowerRamp() { // lower flak with 5 - Scania scania = new Scania(); - scania.setAngleFLak(5); - scania.lowerFlak(); - assertEquals(0, scania.getAngleFlak(), 0.0001); + scania.setAngle(5); + scania.lowerRamp(); + assertEquals(0, scania.getAngle(), 0.0001); // cant lower more than 0 - scania.lowerFlak(); - assertEquals(0, scania.getAngleFlak(),0.0001); + scania.lowerRamp(); + assertEquals(0, scania.getAngle(),0.0001); //cant lower flak while car is moving scania.setCurrentSpeed(40); try { - scania.lowerFlak(); + scania.lowerRamp(); } catch (IllegalArgumentException e) { - System.out.println("car is moving"); + System.out.println("Scania is moving"); } + + // Same tests with BilTransport + bilTransport.lowerRamp(); + assertFalse(bilTransport.getRampState()); + } + + @Test + public void loadOn() { + // Kollar ifall den läggs till i verkstaden. + verkstadMedVolvo.loadOn(volvocar); + //verkstadMedVolvo.loadOn(sabcar); + assertEquals(1, verkstadMedVolvo.getCapacity()); + + // Kollar om det fungerar att lägga till olika typer. + verkstadMedAllaTrucks.loadOn(scania); + verkstadMedAllaTrucks.loadOn(bilTransport); + //verkstadMedAllaTrucks.loadOn(volvocar); + assertEquals(2, verkstadMedAllaTrucks.getCapacity()); + } + + @Test + public void unLoadCar() { + verkstadMedVolvo.loadOn(volvocar); + verkstadMedAllaTrucks.loadOn(scania); + + // Kollar om det går med en Volvo-verkstad + verkstadMedVolvo.unLoadCar(volvocar); + //verkstadMedVolvo.unLoadCar(sabcar); + assertEquals(0, verkstadMedVolvo.getCapacity()); + + // Kollar om det går för en verkstad med alla typer av lastbilar. + verkstadMedAllaTrucks.unLoadCar(scania); + assertEquals(0, verkstadMedAllaTrucks.getCapacity()); + } + + @Test + public void loadCar() { + bilTransport.lowerRamp(); + bilTransport.loadCar(volvocar); + bilTransport.loadCar(sabcar); + assertEquals(2, bilTransport.getFlak().size()); + + //more than MAX_LOAD + bilTransport.loadCar(volvocar); + bilTransport.loadCar(volvocar); + bilTransport.loadCar(sabcar); + try { + bilTransport.loadCar(volvocar); + } catch (IllegalArgumentException e) { + System.out.println("reached full capacity"); } + } + @Test + public void unloadCar(){ + bilTransport.lowerRamp(); + bilTransport.loadCar(volvocar); + bilTransport.loadCar(sabcar); + bilTransport.unLoadCar(volvocar); + assertEquals(0, bilTransport.getFlak().size()); + } + + @Test + public void getAngleFlak(){ + assertEquals(0, scania.getAngle()); + } + + @Test + public void setAngleFLak(){ + scania.setAngle(20); + assertEquals(20, scania.getAngle()); + } + + @Test + public void truckGas(){ + scania.gas(0.2); + // flak is 50 cant gas + bilTransport.setAngle(50); + try { + bilTransport.gas(0.2); + } catch (IllegalArgumentException e) { + System.out.println("flak is not 0 cant gas"); + } + } + + @Test + public void decrementSpeed(){ + scania.setCurrentSpeed(50); + scania.decrementSpeed(10); + assertEquals(46.0, scania.getCurrentSpeed(), 0.0001); + } + @Test + public void getRampState () { + scania.rampState = true; + assertTrue(scania.getRampState()); + } + + @Test + public void moveTruck() { + + } } \ No newline at end of file diff --git a/JPanel.java b/JPanel.java index 709932ce1..85f244d38 100644 --- a/JPanel.java +++ b/JPanel.java @@ -1,9 +1,10 @@ import javax.swing.*; import java.awt.*; -public class JPanel extends javax.swing.JPanel implements Runnable{ +public class JPanel extends javax.swing.JPanel implements Runnable { private int width = 800; private int height = 600; + private BilTransport car; int playerX = 400; int playerY = 100; int FPS = 60; @@ -20,11 +21,10 @@ public JPanel() { } public void startGameThread() { - gameThread = new Thread(this); - gameThread.start(); + gameThread = new Thread(this); + gameThread.start(); } - BilTransport car = new BilTransport(); @Override public void run() { double drawInterval = 1000000000/FPS; @@ -32,7 +32,6 @@ public void run() { while(gameThread != null) { update(); - repaint(); try { @@ -52,8 +51,8 @@ public void run() { public void update(){ if(keyH.upPressed) { - // playerY += 10; car.gas(0.3); + // playerY += 10; } if(keyH.downPressed) { car.brake(0.3); @@ -75,7 +74,6 @@ public void paintComponent(Graphics g) { g2.setColor(Color.blue); g2.fillRect((int) car.getxPos(), (int) car.getyPos(), 15, 15); g2.dispose(); - } diff --git a/Ramp.java b/Ramp.java new file mode 100644 index 000000000..a179258c3 --- /dev/null +++ b/Ramp.java @@ -0,0 +1,31 @@ +import java.lang.annotation.Documented; + +public class Ramp implements IRamp{ + + public void lowerRamp(double speed, double angle) { + if (speed == 0) { + angle -= 10; + if (angle < 0) { + angle = 0; + } + } + else throw new IllegalArgumentException("Scania is moving, can't lower the flak!"); + } + + public void raiseRamp(double speed, double angle) { + if(speed == 0) { + angle += 10; + if (angle > 70) { + angle = 70; + } + } + else throw new IllegalArgumentException("Scania is moving, can't raise the flak!"); + } + + public void raiseRamp(double speed, boolean rampState) { + if(speed == 0) { + rampState = true; + } + else throw new IllegalArgumentException("cant raise ramp car is moving"); + } +} diff --git a/Saab95.java b/Saab95.java index 318724190..158f8d034 100644 --- a/Saab95.java +++ b/Saab95.java @@ -25,10 +25,7 @@ private double speedFactor(){ return getEnginePower() * 0.01 * turbo; } @Override - protected void incrementSpeed(double amount){ - this.setCurrentSpeed(getCurrentSpeed() + speedFactor() * amount); - - } + protected void incrementSpeed(double amount){ this.setCurrentSpeed(getCurrentSpeed() + speedFactor() * amount); } @Override protected void decrementSpeed(double amount){ this.setCurrentSpeed(getCurrentSpeed() - speedFactor() * amount); diff --git a/Scania.java b/Scania.java index 0822197d2..fdb9ca606 100644 --- a/Scania.java +++ b/Scania.java @@ -1,49 +1,31 @@ -import java.awt.*; - -public class Scania extends Car { +public class Scania extends Truck { + private final Ramp ramp = new Ramp(); private int angle; - private final double trimFactor = 0.8; public Scania() { - super(2, 80, Color.white, "Scania"); this.angle = 0; } - private double speedFactor() { - return getEnginePower() * 0.01 * trimFactor; - } - @Override - protected void incrementSpeed(double amount) { - this.setCurrentSpeed(Math.min(getCurrentSpeed() + speedFactor() * amount, getEnginePower())); - } - @Override - protected void decrementSpeed(double amount) { - this.setCurrentSpeed(Math.max(getCurrentSpeed() - speedFactor() * amount, 0)); - } - public int getAngleFlak() { + public int getAngle() { return angle; } - public void setAngleFLak(int amount) {angle = amount;} + public void setAngle(int amount) {angle = amount;} - public void lowerFlak() { - if (getCurrentSpeed() == 0) { - angle -= 5; - if (angle < 0) { - angle = 0; - } + @Override + public void gas(double amount) { + if ((this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= getEnginePower()) + && (amount >= 0 && amount <= 1) || rampState) { + incrementSpeed(amount); + this.setCurrentSpeed(Math.min(this.getCurrentSpeed(), getEnginePower())); } - else - throw new IllegalArgumentException("car is moving, can´t lower the flak!"); + this.move(); } - - public void raiseFlak(){ - if(getCurrentSpeed() == 0) { - angle += 5; - if (angle > 70.0) { - angle = 70; - } - } - else - throw new IllegalArgumentException("car is moving, can´t raise the flak!"); + @Override + public void raiseRamp() { + ramp.raiseRamp(this.getCurrentSpeed(), this.angle); + } + @Override + public void lowerRamp() { + ramp.raiseRamp(this.getCurrentSpeed(), this.angle); } } diff --git a/Truck.java b/Truck.java new file mode 100644 index 000000000..0e2e76271 --- /dev/null +++ b/Truck.java @@ -0,0 +1,68 @@ +import java.awt.*; + +public class Truck extends Car { + protected boolean rampState; + private double angle; + public Truck() { + super(2, 90, Color.red, "Lastbil"); + this.rampState = false; + } + + public boolean getRampState() { return rampState; } + @Override + public void gas(double amount) { + if (bool(amount) && angle == 0 ) { + incrementSpeed(amount); + this.setCurrentSpeed(Math.min(this.getCurrentSpeed(), getEnginePower())); + } + this.move(); + } + + public void setAngle(double angle) { + this.angle = angle; + } + + private boolean bool (double amount) { return this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= + getEnginePower() && (amount >= 0 && amount <= 1); + } + + private double speedFactor() { + double trimFactor = 0.8; + return this.getCurrentSpeed() * 0.01 * trimFactor; + } + + protected void incrementSpeed(double amount) { + this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, this.getEnginePower())); + } + protected void decrementSpeed(double amount) { + this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); + } + + @Override + public void move() { + if (angle != 0) { + System.out.println("Position: (" + getxPos() + ", " + getyPos() + ')' ); + } + + switch (this.getDirection()) { + case 0: // north + setYPos(getyPos() + getCurrentSpeed()); + break; + case 1: // east + setXPos(getxPos() + getCurrentSpeed()); + break; + case 2: // south + setYPos(getyPos() - getCurrentSpeed()); + break; + case 3: // west + setXPos(getxPos() - getCurrentSpeed()); + break; + default: + System.out.println("unknown direction"); + break; + } + System.out.println("Current Position: (" + this.getxPos() + ", " + this.getyPos() + ")"); + System.out.println("Current direction: " + this.getDirection()); + } + +} diff --git a/Verkstad.java b/Verkstad.java index f584f712c..a58c3c725 100644 --- a/Verkstad.java +++ b/Verkstad.java @@ -1,28 +1,32 @@ import java.awt.geom.Point2D; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -public class Verkstad { - private List verkstaden; - private Point2D.Double position; +public class Verkstad { + private List verkstaden; private final int maxCapacity; + private final boolean carAllowed; - public Verkstad(final int maxCapacity, Point2D.Double position) { - this.verkstaden = new ArrayList<>(); - this.position = position; + public Verkstad(final int maxCapacity, ArrayList verkstad) { + this.verkstaden = verkstad; this.maxCapacity = maxCapacity; + this.carAllowed = true; } - private boolean hasONECarAllowedInVerkstad() { - return true; + public int getCapacity() { + return verkstaden.size(); } - - public void loadCar(BilTransport transport) { - if (verkstaden.size() <= maxCapacity) { - + public void loadOn(T car) { + if (verkstaden.size() < maxCapacity) { verkstaden.add(car); - } else - throw new IllegalArgumentException("verkstaden is full"); + } else throw new IllegalArgumentException("verkstaden is full"); + } + + public void unLoadCar(T car) { + if (!verkstaden.isEmpty()) { + verkstaden.remove(car); + } else throw new IllegalArgumentException("verkstaden is tom"); } } diff --git a/out/production/lab1/.idea/misc.xml b/out/production/lab1/.idea/misc.xml index ea71d5024..7d53ab542 100644 --- a/out/production/lab1/.idea/misc.xml +++ b/out/production/lab1/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/out/production/lab1/lab1.iml b/out/production/lab1/lab1.iml index b107a2dd8..c02b99a42 100644 --- a/out/production/lab1/lab1.iml +++ b/out/production/lab1/lab1.iml @@ -7,5 +7,15 @@ + + + + + + + + + + \ No newline at end of file From 91fa34d453b4abf93d2b0d4ba6cb86c826437d9a Mon Sep 17 00:00:00 2001 From: ArvidJaha <146183591+ArvidJaha@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:56:37 +0100 Subject: [PATCH 14/16] Detta fungerar typ? --- BilTransport.java | 23 +++++++---------------- CarTest.java | 38 ++++++++++++++++++++------------------ Ramp.java | 31 ++++++++++++++++++++++++------- Scania.java | 19 +++++++++---------- Truck.java | 10 ++++------ 5 files changed, 64 insertions(+), 57 deletions(-) diff --git a/BilTransport.java b/BilTransport.java index 32d76441e..4f11aaa07 100644 --- a/BilTransport.java +++ b/BilTransport.java @@ -1,13 +1,9 @@ -import org.junit.Test; - -import java.awt.*; import java.awt.geom.Point2D; import java.util.ArrayDeque; import java.util.Deque; -import java.util.Queue; public class BilTransport extends Truck { - private final Ramp ramp = new Ramp(); + private Ramp ramp = new Ramp(); private boolean rampState; private Deque flaket; private final int MAXLOAD; @@ -16,23 +12,16 @@ public BilTransport(){ this.flaket = new ArrayDeque<>(); this.rampState = false; this.MAXLOAD = 5; - this.setAngle(0); } - @Override - public boolean getRampState() { return this.rampState; } - @Override public void raiseRamp() { ramp.raiseRamp(getCurrentSpeed(), rampState); } + @Override public void lowerRamp() { - if(this.getCurrentSpeed() == 0) { - this.setAngle(70); - rampState = false; - } - else throw new IllegalArgumentException("cant lower ramp car is moving"); + ramp.lowerRamp(getCurrentSpeed(), rampState); } public void loadCar(Car car) { @@ -44,10 +33,12 @@ public void loadCar(Car car) { } else throw new IllegalArgumentException("error"); } - public void unLoadCar(Car car) { + public void unLoadCar() { if(!rampState && !flaket.isEmpty()) { - flaket.remove(car); + flaket.removeLast(); } } public Deque getFlak() { return flaket; } + + public Ramp getRamp() { return ramp;} } diff --git a/CarTest.java b/CarTest.java index 750517216..9d3e75106 100644 --- a/CarTest.java +++ b/CarTest.java @@ -156,7 +156,7 @@ public void brakeVolvo() { } catch (IllegalArgumentException e) { System.out.println("amount to low"); } - //check if speed is lower, and speed dosent get higher. + //check if speed is lower, and speed doesn't get higher. volvocar.setCurrentSpeed(1); double e = volvocar.getCurrentSpeed(); volvocar.brake(0.2); @@ -200,11 +200,11 @@ public void turboOff() { public void raiseRamp() { //raise flak with 5 scania.raiseRamp(); - assertEquals(5, scania.getAngle(), 0.0001); + assertEquals(10, scania.getRamp().getAngle(), 0.001); // cant raise more than 70 - scania.setAngle(69); + scania.getRamp().angleSet(70); scania.raiseRamp(); - assertEquals(70, scania.getAngle(),0.0001); + assertEquals(70, scania.getRamp().getAngle(),0.001); //cant raise flak while car is moving scania.setDirection(40); try { @@ -215,18 +215,18 @@ public void raiseRamp() { // Same tests with BilTransport bilTransport.raiseRamp(); - assertTrue(bilTransport.getRampState()); + assertTrue(bilTransport.getRamp().getRampState()); } @Test public void lowerRamp() { - // lower flak with 5 - scania.setAngle(5); + // lower flak with 10 + scania.getRamp().angleSet(10); scania.lowerRamp(); - assertEquals(0, scania.getAngle(), 0.0001); + assertEquals(0, scania.getRamp().getAngle(), 0.001); // cant lower more than 0 scania.lowerRamp(); - assertEquals(0, scania.getAngle(),0.0001); + assertEquals(0, scania.getRamp().getAngle(),0.001); //cant lower flak while car is moving scania.setCurrentSpeed(40); try { @@ -237,7 +237,7 @@ public void lowerRamp() { // Same tests with BilTransport bilTransport.lowerRamp(); - assertFalse(bilTransport.getRampState()); + assertFalse(bilTransport.getRamp().getRampState()); } @Test @@ -292,26 +292,28 @@ public void unloadCar(){ bilTransport.lowerRamp(); bilTransport.loadCar(volvocar); bilTransport.loadCar(sabcar); - bilTransport.unLoadCar(volvocar); - assertEquals(0, bilTransport.getFlak().size()); + bilTransport.unLoadCar(); + assertEquals(1, bilTransport.getFlak().size()); + assertTrue(bilTransport.getFlak().peek().equals(volvocar)); } @Test public void getAngleFlak(){ - assertEquals(0, scania.getAngle()); + scania.getRamp().angleSet(0); + assertEquals(0, scania.getRamp().getAngle(), 0.001); } @Test public void setAngleFLak(){ - scania.setAngle(20); - assertEquals(20, scania.getAngle()); + scania.getRamp().angleSet(20); + assertEquals(20, scania.getRamp().getAngle(), 0.001); } @Test public void truckGas(){ scania.gas(0.2); // flak is 50 cant gas - bilTransport.setAngle(50); + bilTransport.getRamp().angleSet(50); try { bilTransport.gas(0.2); } catch (IllegalArgumentException e) { @@ -327,8 +329,8 @@ public void decrementSpeed(){ } @Test public void getRampState () { - scania.rampState = true; - assertTrue(scania.getRampState()); + scania.getRamp().setRampState(true); + assertTrue(scania.getRamp().getRampState()); } @Test diff --git a/Ramp.java b/Ramp.java index a179258c3..b4efbb173 100644 --- a/Ramp.java +++ b/Ramp.java @@ -1,31 +1,48 @@ import java.lang.annotation.Documented; public class Ramp implements IRamp{ + private double angle; + private boolean rampState; - public void lowerRamp(double speed, double angle) { + public void angleSet(double a) { angle = a; } + public double getAngle() { return angle; } + public boolean getRampState() { return rampState; } + public void setRampState(boolean b) { rampState = b;} + public void lowerRamp(double speed, double a) { if (speed == 0) { - angle -= 10; - if (angle < 0) { - angle = 0; + a -= 10; + if (a < 0) { + a = 0; } + angle = a; } else throw new IllegalArgumentException("Scania is moving, can't lower the flak!"); } - public void raiseRamp(double speed, double angle) { + public void raiseRamp(double speed, double a) { + angle = a; if(speed == 0) { angle += 10; - if (angle > 70) { + if (angle >= 70) { angle = 70; } } else throw new IllegalArgumentException("Scania is moving, can't raise the flak!"); } - public void raiseRamp(double speed, boolean rampState) { + public void raiseRamp(double speed, boolean r) { + rampState = r; if(speed == 0) { rampState = true; } else throw new IllegalArgumentException("cant raise ramp car is moving"); } + + public void lowerRamp(double speed, boolean r) { + rampState = r; + if(speed == 0) { + rampState = false; + } + else throw new IllegalArgumentException("cant lower ramp car is moving"); + } } diff --git a/Scania.java b/Scania.java index fdb9ca606..5ec4bfc98 100644 --- a/Scania.java +++ b/Scania.java @@ -1,19 +1,16 @@ public class Scania extends Truck { - private final Ramp ramp = new Ramp(); - private int angle; + private double angle; + private final Ramp ramp; public Scania() { - this.angle = 0; + this.ramp = new Ramp(); + this.angle = this.ramp.getAngle(); } - public int getAngle() { - return angle; - } - public void setAngle(int amount) {angle = amount;} @Override public void gas(double amount) { if ((this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= getEnginePower()) - && (amount >= 0 && amount <= 1) || rampState) { + && (amount >= 0 && amount <= 1) || ramp.getRampState()) { incrementSpeed(amount); this.setCurrentSpeed(Math.min(this.getCurrentSpeed(), getEnginePower())); } @@ -21,11 +18,13 @@ public void gas(double amount) { } @Override public void raiseRamp() { - ramp.raiseRamp(this.getCurrentSpeed(), this.angle); + ramp.raiseRamp(this.getCurrentSpeed(), ramp.getAngle()); } @Override public void lowerRamp() { - ramp.raiseRamp(this.getCurrentSpeed(), this.angle); + ramp.lowerRamp(this.getCurrentSpeed(), ramp.getAngle()); } + + public Ramp getRamp () { return ramp; } } diff --git a/Truck.java b/Truck.java index 0e2e76271..f57fff570 100644 --- a/Truck.java +++ b/Truck.java @@ -1,6 +1,6 @@ import java.awt.*; -public class Truck extends Car { +public abstract class Truck extends Car { protected boolean rampState; private double angle; public Truck() { @@ -8,7 +8,6 @@ public Truck() { this.rampState = false; } - public boolean getRampState() { return rampState; } @Override public void gas(double amount) { if (bool(amount) && angle == 0 ) { @@ -18,10 +17,6 @@ public void gas(double amount) { this.move(); } - public void setAngle(double angle) { - this.angle = angle; - } - private boolean bool (double amount) { return this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= getEnginePower() && (amount >= 0 && amount <= 1); } @@ -65,4 +60,7 @@ public void move() { System.out.println("Current direction: " + this.getDirection()); } + public abstract void raiseRamp(); + + public abstract void lowerRamp(); } From 537d5e4b17a7f28ba06497d5e5985d69195d5217 Mon Sep 17 00:00:00 2001 From: ArvidJaha <146183591+ArvidJaha@users.noreply.github.com> Date: Fri, 2 Feb 2024 12:34:52 +0100 Subject: [PATCH 15/16] Detta fungerar --- CarTest.java | 2 +- Ramp.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/CarTest.java b/CarTest.java index 9d3e75106..2a1ea80bf 100644 --- a/CarTest.java +++ b/CarTest.java @@ -294,7 +294,7 @@ public void unloadCar(){ bilTransport.loadCar(sabcar); bilTransport.unLoadCar(); assertEquals(1, bilTransport.getFlak().size()); - assertTrue(bilTransport.getFlak().peek().equals(volvocar)); + assertEquals(sabcar.toString(), bilTransport.getFlak().getFirst().toString()); } @Test diff --git a/Ramp.java b/Ramp.java index b4efbb173..f2adcb294 100644 --- a/Ramp.java +++ b/Ramp.java @@ -1,6 +1,4 @@ -import java.lang.annotation.Documented; - -public class Ramp implements IRamp{ +public class Ramp { private double angle; private boolean rampState; From bbde506dad97784bbb800f866ae9f4d337350d60 Mon Sep 17 00:00:00 2001 From: ArvidJaha <146183591+ArvidJaha@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:30:43 +0100 Subject: [PATCH 16/16] Slutresultat --- BilTransport.java | 45 +++++++-- Car.java | 16 ++-- CarTest.java | 240 ++++++++++++++++++++++++---------------------- HasRamp.java | 4 + Scania.java | 41 +++++--- Truck.java | 66 ------------- Verkstad.java | 1 - 7 files changed, 200 insertions(+), 213 deletions(-) create mode 100644 HasRamp.java delete mode 100644 Truck.java diff --git a/BilTransport.java b/BilTransport.java index 4f11aaa07..6f12d4638 100644 --- a/BilTransport.java +++ b/BilTransport.java @@ -1,19 +1,24 @@ +import java.awt.*; import java.awt.geom.Point2D; import java.util.ArrayDeque; import java.util.Deque; -public class BilTransport extends Truck { +public class BilTransport extends Car implements HasRamp { private Ramp ramp = new Ramp(); private boolean rampState; private Deque flaket; private final int MAXLOAD; public BilTransport(){ + super(2, 90, Color.red, "BilTransport"); this.flaket = new ArrayDeque<>(); - this.rampState = false; + this.rampState = true; this.MAXLOAD = 5; } + public Deque getFlak() { return flaket; } + public Ramp getRamp() { return ramp; } + @Override public void raiseRamp() { ramp.raiseRamp(getCurrentSpeed(), rampState); @@ -25,20 +30,42 @@ public void lowerRamp() { } public void loadCar(Car car) { - Point2D.Double transportCoordinates = new Point2D.Double(car.getxPos(), car.getyPos()); - if(!rampState && flaket.size() <= MAXLOAD - && car.getCordination().distance(transportCoordinates) <= 5.0) { + if(!ramp.getRampState() && flaket.size() <= MAXLOAD && car.getPosition().distance(getPosition()) <= 5.0) { flaket.push(car); - car.getCordination().setLocation(transportCoordinates); + updateBilTransport(); + } else throw new IllegalArgumentException("Cannot load Car!"); + } + + public void updateBilTransport () { + for (Car c: this.flaket) { + c.getPosition().setLocation(getPosition()); } - else throw new IllegalArgumentException("error"); } + public void unLoadCar() { if(!rampState && !flaket.isEmpty()) { flaket.removeLast(); } } - public Deque getFlak() { return flaket; } - public Ramp getRamp() { return ramp;} + @Override + public void move() { + if (!ramp.getRampState()) { + } else { + super.move(); + updateBilTransport(); + } + + } + + private double speedFactor() { + double trimFactor = 0.8; + return this.getCurrentSpeed() * 0.01 * trimFactor; + } + protected void incrementSpeed(double amount) { + this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, this.getEnginePower())); + } + protected void decrementSpeed(double amount) { + this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); + } } diff --git a/Car.java b/Car.java index def44c75d..37c26d810 100644 --- a/Car.java +++ b/Car.java @@ -24,15 +24,15 @@ public Car(int nrDoors, double enginePower, Color color, String modelName) { } // Getters och setters - public double setXPos(double d) { return xPos = d;} - public double setYPos(double d) { return yPos = d;} + public void setXPos(double x) { cordination.setLocation(x, yPos);} + public void setYPos(double y) { cordination.setLocation(xPos, y);} public double getxPos() { return xPos; } public double getyPos() { return yPos; } - public Point2D.Double getCordination(){ + public Point2D.Double getPosition(){ return cordination; } public int getNrDoors() {return nrDoors;} @@ -72,23 +72,21 @@ public int getDirection() { public void move() { switch (direction) { case 0: // north - yPos += currentSpeed; + getPosition().setLocation(xPos,yPos + currentSpeed); break; case 1: // east - xPos += currentSpeed; + getPosition().setLocation(xPos + currentSpeed,yPos); break; case 2: // south - yPos -= currentSpeed; + getPosition().setLocation(xPos,yPos - currentSpeed); break; case 3: // west - xPos -= currentSpeed; + getPosition().setLocation(xPos - currentSpeed,yPos); break; default: System.out.println("unknown direction"); break; } - System.out.println("Current Position: (" + xPos + ", " + yPos + ")"); - System.out.println("Current direction: " + direction); } public void turnleft() { diff --git a/CarTest.java b/CarTest.java index 2a1ea80bf..00544184d 100644 --- a/CarTest.java +++ b/CarTest.java @@ -3,22 +3,21 @@ import org.junit.Test; import java.awt.*; -import java.sql.SQLOutput; import java.util.ArrayList; import static org.junit.Assert.*; public class CarTest { - Volvo240 volvocar; - Saab95 sabcar; + Volvo240 volvo; + Saab95 saab; BilTransport bilTransport; Scania scania; Verkstad verkstadMedVolvo; - Verkstad verkstadMedAllaTrucks; + Verkstad verkstadMedAllaTrucks; @Before public void setUp() { - this.volvocar = new Volvo240(); - this.sabcar = new Saab95(); + this.volvo = new Volvo240(); + this.saab = new Saab95(); this.scania = new Scania(); this.bilTransport = new BilTransport(); this.verkstadMedVolvo = new Verkstad<>(2, new ArrayList<>()); @@ -27,174 +26,174 @@ public void setUp() { @Test public void getNrDoors() { - assertEquals(4, volvocar.getNrDoors()); - assertEquals(2, sabcar.getNrDoors()); + assertEquals(4, volvo.getNrDoors()); + assertEquals(2, saab.getNrDoors()); } @Test public void getEnginePower() { - assertEquals(100, volvocar.getEnginePower(), 0.0001); - assertEquals(125, sabcar.getEnginePower(), 0.0001); + assertEquals(100, volvo.getEnginePower(), 0.0001); + assertEquals(125, saab.getEnginePower(), 0.0001); } @Test public void getCurrentSpeed() { - volvocar.startEngine(); - assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); - sabcar.startEngine(); - assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); + volvo.startEngine(); + assertEquals(0.1, volvo.getCurrentSpeed(), 0.0001); + saab.startEngine(); + assertEquals(0.1, saab.getCurrentSpeed(), 0.0001); } @Test public void getColor() { - assertEquals(Color.black, volvocar.getColor()); - assertEquals(Color.red, sabcar.getColor()); + assertEquals(Color.black, volvo.getColor()); + assertEquals(Color.red, saab.getColor()); } @Test public void setColor() { - volvocar.setColor(Color.black); - assertEquals(Color.black, volvocar.getColor()); - sabcar.setColor(Color.red); - assertEquals(Color.red, sabcar.getColor()); + volvo.setColor(Color.black); + assertEquals(Color.black, volvo.getColor()); + saab.setColor(Color.red); + assertEquals(Color.red, saab.getColor()); } @Test public void startEngine() { - volvocar.startEngine(); - assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); + volvo.startEngine(); + assertEquals(0.1, volvo.getCurrentSpeed(), 0.0001); } @Test public void stopEngine() { - sabcar.stopEngine(); - assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); + saab.stopEngine(); + assertEquals(0.0, saab.getCurrentSpeed(), 0.0001); } @Test public void move() { - volvocar.startEngine(); - volvocar.move(); - assertEquals(0.1, volvocar.getCurrentSpeed(), 0.0001); - sabcar.startEngine(); - sabcar.move(); - assertEquals(0.1, sabcar.getCurrentSpeed(), 0.0001); + volvo.startEngine(); + volvo.move(); + assertEquals(0.1, volvo.getCurrentSpeed(), 0.0001); + saab.startEngine(); + saab.move(); + assertEquals(0.1, saab.getCurrentSpeed(), 0.0001); } @Test public void turnLeft() { - volvocar.setDirection(0); - if (volvocar.getDirection() == 0) { - volvocar.setDirection(4); + volvo.setDirection(0); + if (volvo.getDirection() == 0) { + volvo.setDirection(4); } - volvocar.turnleft(); - assertEquals(0, volvocar.getDirection()); - volvocar.turnleft(); - assertEquals(1, volvocar.getDirection()); - volvocar.turnleft(); - assertEquals(2, volvocar.getDirection()); - volvocar.turnleft(); - assertEquals(3, volvocar.getDirection()); - volvocar.turnleft(); - assertEquals(4, volvocar.getDirection()); + volvo.turnleft(); + assertEquals(0, volvo.getDirection()); + volvo.turnleft(); + assertEquals(1, volvo.getDirection()); + volvo.turnleft(); + assertEquals(2, volvo.getDirection()); + volvo.turnleft(); + assertEquals(3, volvo.getDirection()); + volvo.turnleft(); + assertEquals(4, volvo.getDirection()); } @Test public void turnRight() { - volvocar.setDirection(4); - if (volvocar.getDirection() == 4) { - volvocar.setDirection(-1); + volvo.setDirection(4); + if (volvo.getDirection() == 4) { + volvo.setDirection(-1); } - volvocar.turnright(); - assertEquals(4, volvocar.getDirection()); - volvocar.turnright(); - assertEquals(3, volvocar.getDirection()); - volvocar.turnright(); - assertEquals(2, volvocar.getDirection()); - volvocar.turnright(); - assertEquals(1, volvocar.getDirection()); + volvo.turnright(); + assertEquals(4, volvo.getDirection()); + volvo.turnright(); + assertEquals(3, volvo.getDirection()); + volvo.turnright(); + assertEquals(2, volvo.getDirection()); + volvo.turnright(); + assertEquals(1, volvo.getDirection()); } @Test public void gasVolvo() { //amount is higher than 1 - volvocar.gas(1.01); - assertEquals(0.0, volvocar.getCurrentSpeed(), 0.0001); + volvo.gas(1.01); + assertEquals(0.0, volvo.getCurrentSpeed(), 0.0001); //checks if speed is increasing and speed doesn't decrease when you gas - double e = volvocar.getCurrentSpeed(); - volvocar.gas(0.2); - assertEquals(0.25, volvocar.getCurrentSpeed(), 0.0001); - assertTrue(volvocar.getCurrentSpeed() >= e); + double e = volvo.getCurrentSpeed(); + volvo.gas(0.2); + assertEquals(0.25, volvo.getCurrentSpeed(), 0.0001); + assertTrue(volvo.getCurrentSpeed() >= e); //speed limit - volvocar.setCurrentSpeed(99.99); - volvocar.gas(1); - assertEquals(100, volvocar.getCurrentSpeed(), 0.0001); + volvo.setCurrentSpeed(99.99); + volvo.gas(1); + assertEquals(100, volvo.getCurrentSpeed(), 0.0001); } @Test public void gasSab() { //amount is higher than 1 - sabcar.gas(1.01); - assertEquals(0.0, sabcar.getCurrentSpeed(), 0.0001); + saab.gas(1.01); + assertEquals(0.0, saab.getCurrentSpeed(), 0.0001); //checks if speed is increasing and speed doesn't decrease when you gas - double f = sabcar.getCurrentSpeed(); - sabcar.gas(0.2); - assertEquals(0.25, sabcar.getCurrentSpeed(), 0.0001); - assertTrue(sabcar.getCurrentSpeed() >= f); + double f = saab.getCurrentSpeed(); + saab.gas(0.2); + assertEquals(0.25, saab.getCurrentSpeed(), 0.0001); + assertTrue(saab.getCurrentSpeed() >= f); //speed limit - sabcar.setCurrentSpeed(124.99); - sabcar.gas(1); - assertEquals(125, sabcar.getCurrentSpeed(), 0.0001); + saab.setCurrentSpeed(124.99); + saab.gas(1); + assertEquals(125, saab.getCurrentSpeed(), 0.0001); } @Test public void brakeVolvo() { try { //amount lower than 0 - volvocar.brake(-0.01); + volvo.brake(-0.01); } catch (IllegalArgumentException e) { System.out.println("amount to low"); } //check if speed is lower, and speed doesn't get higher. - volvocar.setCurrentSpeed(1); - double e = volvocar.getCurrentSpeed(); - volvocar.brake(0.2); - assertEquals(0.75, volvocar.getCurrentSpeed(), 0.0001); - assertTrue(volvocar.getCurrentSpeed() <= e); + volvo.setCurrentSpeed(1); + double e = volvo.getCurrentSpeed(); + volvo.brake(0.2); + assertEquals(0.75, volvo.getCurrentSpeed(), 0.0001); + assertTrue(volvo.getCurrentSpeed() <= e); // speed cant be lower than 0 - volvocar.setCurrentSpeed(0.01); - volvocar.brake(0.9); - assertEquals(0, volvocar.getCurrentSpeed(), 0.0001); + volvo.setCurrentSpeed(0.01); + volvo.brake(0.9); + assertEquals(0, volvo.getCurrentSpeed(), 0.0001); } @Test public void brakeSab() { try { //amount lower than 0 - sabcar.brake(-0.01); + saab.brake(-0.01); } catch (IllegalArgumentException e) { System.out.println("amount is to low"); } - //check if speed is lower, and speed dosent get higher. - sabcar.setCurrentSpeed(1); - double f = sabcar.getCurrentSpeed(); - sabcar.brake(0.2); - assertEquals(0.75, sabcar.getCurrentSpeed(), 0.0001); - assertTrue(sabcar.getCurrentSpeed() <= f); + //check if speed is lower, and speed doesn't get higher. + saab.setCurrentSpeed(1); + double f = saab.getCurrentSpeed(); + saab.brake(0.2); + assertEquals(0.75, saab.getCurrentSpeed(), 0.0001); + assertTrue(saab.getCurrentSpeed() <= f); // speed cant be lower than 0 - sabcar.setCurrentSpeed(0.01); - sabcar.brake(0.9); - assertEquals(0, sabcar.getCurrentSpeed(), 0.0001); + saab.setCurrentSpeed(0.01); + saab.brake(0.9); + assertEquals(0, saab.getCurrentSpeed(), 0.0001); } @Test public void turboOn() { - sabcar.setTurboOn(); - assertTrue(sabcar.getTurbo()); + saab.setTurboOn(); + assertTrue(saab.getTurbo()); } @Test public void turboOff() { - sabcar.setTurboOff(); - assertFalse(sabcar.getTurbo()); + saab.setTurboOff(); + assertFalse(saab.getTurbo()); } @Test public void raiseRamp() { @@ -243,25 +242,25 @@ public void lowerRamp() { @Test public void loadOn() { // Kollar ifall den läggs till i verkstaden. - verkstadMedVolvo.loadOn(volvocar); - //verkstadMedVolvo.loadOn(sabcar); + verkstadMedVolvo.loadOn(volvo); + //verkstadMedVolvo.loadOn(saab); assertEquals(1, verkstadMedVolvo.getCapacity()); // Kollar om det fungerar att lägga till olika typer. verkstadMedAllaTrucks.loadOn(scania); verkstadMedAllaTrucks.loadOn(bilTransport); - //verkstadMedAllaTrucks.loadOn(volvocar); + //verkstadMedAllaTrucks.loadOn(volvo); assertEquals(2, verkstadMedAllaTrucks.getCapacity()); } @Test public void unLoadCar() { - verkstadMedVolvo.loadOn(volvocar); + verkstadMedVolvo.loadOn(volvo); verkstadMedAllaTrucks.loadOn(scania); // Kollar om det går med en Volvo-verkstad - verkstadMedVolvo.unLoadCar(volvocar); - //verkstadMedVolvo.unLoadCar(sabcar); + verkstadMedVolvo.unLoadCar(volvo); + //verkstadMedVolvo.unLoadCar(saab); assertEquals(0, verkstadMedVolvo.getCapacity()); // Kollar om det går för en verkstad med alla typer av lastbilar. @@ -272,29 +271,36 @@ public void unLoadCar() { @Test public void loadCar() { bilTransport.lowerRamp(); - bilTransport.loadCar(volvocar); - bilTransport.loadCar(sabcar); + bilTransport.loadCar(volvo); + bilTransport.loadCar(saab); assertEquals(2, bilTransport.getFlak().size()); //more than MAX_LOAD - bilTransport.loadCar(volvocar); - bilTransport.loadCar(volvocar); - bilTransport.loadCar(sabcar); + bilTransport.loadCar(volvo); + bilTransport.loadCar(volvo); + bilTransport.loadCar(saab); try { - bilTransport.loadCar(volvocar); + bilTransport.loadCar(volvo); } catch (IllegalArgumentException e) { System.out.println("reached full capacity"); } } + @Test + public void onRamp() { // För att kontrollera att volvo är lastad på bilTransporten. + bilTransport.loadCar(volvo); + assertEquals(bilTransport.getPosition(), volvo.getPosition()); + scania.move(); + } + @Test public void unloadCar(){ bilTransport.lowerRamp(); - bilTransport.loadCar(volvocar); - bilTransport.loadCar(sabcar); + bilTransport.loadCar(volvo); + bilTransport.loadCar(saab); bilTransport.unLoadCar(); - assertEquals(1, bilTransport.getFlak().size()); - assertEquals(sabcar.toString(), bilTransport.getFlak().getFirst().toString()); + assertEquals(2, bilTransport.getFlak().size()); + assertEquals(saab.toString(), bilTransport.getFlak().getFirst().toString()); } @Test @@ -334,7 +340,15 @@ public void getRampState () { } @Test - public void moveTruck() { - + public void updateBilTransport() { + // Testar BilTransportens koordinater vs bilens/bilarnas. + bilTransport.getRamp().setRampState(false); + bilTransport.loadCar(volvo); + bilTransport.getRamp().setRampState(true); + + // Testar om bilarna har samma position. + bilTransport.setCurrentSpeed(20); + bilTransport.move(); + assertEquals(volvo.getPosition(), bilTransport.getPosition()); } } \ No newline at end of file diff --git a/HasRamp.java b/HasRamp.java new file mode 100644 index 000000000..cbe7be5b1 --- /dev/null +++ b/HasRamp.java @@ -0,0 +1,4 @@ +public interface HasRamp { + void raiseRamp(); + void lowerRamp(); +} diff --git a/Scania.java b/Scania.java index 5ec4bfc98..31576fd67 100644 --- a/Scania.java +++ b/Scania.java @@ -1,20 +1,10 @@ -public class Scania extends Truck { - private double angle; - private final Ramp ramp; +import java.awt.*; - public Scania() { - this.ramp = new Ramp(); - this.angle = this.ramp.getAngle(); - } +public class Scania extends Car implements HasRamp { + private final Ramp ramp = new Ramp();; - @Override - public void gas(double amount) { - if ((this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= getEnginePower()) - && (amount >= 0 && amount <= 1) || ramp.getRampState()) { - incrementSpeed(amount); - this.setCurrentSpeed(Math.min(this.getCurrentSpeed(), getEnginePower())); - } - this.move(); + public Scania() { + super(2, 90, Color.red, "Scania"); } @Override public void raiseRamp() { @@ -26,5 +16,26 @@ public void lowerRamp() { } public Ramp getRamp () { return ramp; } + + @Override + public void move() { + if (ramp.getAngle() != 0) { + System.out.println("Cannot move"); + } else { + super.move(); + this.getPosition().setLocation(getPosition()); + } + } + + private double speedFactor() { + double trimFactor = 0.8; + return this.getCurrentSpeed() * 0.01 * trimFactor; + } + protected void incrementSpeed(double amount) { + this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, this.getEnginePower())); + } + protected void decrementSpeed(double amount) { + this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); + } } diff --git a/Truck.java b/Truck.java deleted file mode 100644 index f57fff570..000000000 --- a/Truck.java +++ /dev/null @@ -1,66 +0,0 @@ -import java.awt.*; - -public abstract class Truck extends Car { - protected boolean rampState; - private double angle; - public Truck() { - super(2, 90, Color.red, "Lastbil"); - this.rampState = false; - } - - @Override - public void gas(double amount) { - if (bool(amount) && angle == 0 ) { - incrementSpeed(amount); - this.setCurrentSpeed(Math.min(this.getCurrentSpeed(), getEnginePower())); - } - this.move(); - } - - private boolean bool (double amount) { return this.getCurrentSpeed() >= 0 && this.getCurrentSpeed() <= - getEnginePower() && (amount >= 0 && amount <= 1); - } - - private double speedFactor() { - double trimFactor = 0.8; - return this.getCurrentSpeed() * 0.01 * trimFactor; - } - - protected void incrementSpeed(double amount) { - this.setCurrentSpeed(Math.min(this.getCurrentSpeed() + speedFactor() * amount, this.getEnginePower())); - } - protected void decrementSpeed(double amount) { - this.setCurrentSpeed(Math.max(this.getCurrentSpeed() - speedFactor() * amount, 0)); - } - - @Override - public void move() { - if (angle != 0) { - System.out.println("Position: (" + getxPos() + ", " + getyPos() + ')' ); - } - - switch (this.getDirection()) { - case 0: // north - setYPos(getyPos() + getCurrentSpeed()); - break; - case 1: // east - setXPos(getxPos() + getCurrentSpeed()); - break; - case 2: // south - setYPos(getyPos() - getCurrentSpeed()); - break; - case 3: // west - setXPos(getxPos() - getCurrentSpeed()); - break; - default: - System.out.println("unknown direction"); - break; - } - System.out.println("Current Position: (" + this.getxPos() + ", " + this.getyPos() + ")"); - System.out.println("Current direction: " + this.getDirection()); - } - - public abstract void raiseRamp(); - - public abstract void lowerRamp(); -} diff --git a/Verkstad.java b/Verkstad.java index a58c3c725..6ec5f8e86 100644 --- a/Verkstad.java +++ b/Verkstad.java @@ -8,7 +8,6 @@ public class Verkstad { private final int maxCapacity; private final boolean carAllowed; - public Verkstad(final int maxCapacity, ArrayList verkstad) { this.verkstaden = verkstad; this.maxCapacity = maxCapacity;