Compare commits
28 Commits
ddc6253386
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
4af29e8f0f
|
|||
|
5220b27146
|
|||
|
927a2232dd
|
|||
|
fa5b8bf520
|
|||
|
b964385405
|
|||
|
ca9d0ec062
|
|||
|
6eee9df1fe
|
|||
|
23829388cb
|
|||
|
64f7f56ffe
|
|||
|
51f5d352c6
|
|||
|
7c614b0c5c
|
|||
|
3743b47887
|
|||
|
94df35881e
|
|||
|
8caae17dd4
|
|||
|
80b7f9e80d
|
|||
|
475afc0db5
|
|||
|
f37a8cd665
|
|||
|
dfd4dba1d7
|
|||
|
73129fd4c1
|
|||
|
42c63045b0
|
|||
|
d46443f6d5
|
|||
|
fd99c16e89
|
|||
|
8f38c89d7c
|
|||
|
8cefde97d3
|
|||
|
876ce99c77
|
|||
|
6acdcaffa7
|
|||
|
061a475a6b
|
|||
|
312a5276d7
|
17
src/lab10_memento/ex1/CheckpointManager.java
Normal file
17
src/lab10_memento/ex1/CheckpointManager.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package lab10_memento.ex1;
|
||||
|
||||
public class CheckpointManager {
|
||||
private Player.Checkpoint lastCheckpoint = null;
|
||||
|
||||
public void save(Player player) {
|
||||
lastCheckpoint = player.makeCheckpoint();
|
||||
}
|
||||
|
||||
public void restore(Player player) {
|
||||
if (lastCheckpoint != null) {
|
||||
lastCheckpoint.restore(player);
|
||||
} else {
|
||||
System.out.println("No checkpoint to restore");
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/lab10_memento/ex1/Game.java
Normal file
60
src/lab10_memento/ex1/Game.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package lab10_memento.ex1;
|
||||
|
||||
public class Game {
|
||||
private final Player player;
|
||||
private final CheckpointManager checkpointManager;
|
||||
|
||||
public Game() {
|
||||
player = new Player(0, 0, 0);
|
||||
checkpointManager = new CheckpointManager();
|
||||
}
|
||||
|
||||
public void printInfo() {
|
||||
System.out.println("Player position: (" + player.getX() + "," + player.getY() + ")");
|
||||
System.out.println("Player score: " + player.getScore());
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void doTurn(int dx, int dy, int score, boolean isCheckpoint) {
|
||||
player.moveTo(player.getX() + dx, player.getY() + dy);
|
||||
System.out.print("Player moves to (" + player.getX() + "," + player.getY() + ")");
|
||||
if (score != 0) {
|
||||
player.increaseScore(score);
|
||||
System.out.print(" and gains +" + score + " score");
|
||||
}
|
||||
if (isCheckpoint) {
|
||||
checkpointManager.save(player);
|
||||
System.out.print(". It's a checkpoint and their position and score are saved");
|
||||
}
|
||||
System.out.println();
|
||||
printInfo();
|
||||
}
|
||||
|
||||
public void doTurn(int dx, int dy) {
|
||||
doTurn(dx, dy, 0, false);
|
||||
}
|
||||
|
||||
public void doTurn(int dx, int dy, int score) {
|
||||
doTurn(dx, dy, score, false);
|
||||
}
|
||||
|
||||
public void doTurn(int dx, int dy, boolean isCheckpoint) {
|
||||
doTurn(dx, dy, 0, isCheckpoint);
|
||||
}
|
||||
|
||||
public void die() {
|
||||
checkpointManager.restore(player);
|
||||
System.out.println("Player died ! Returning to previous checkpoint");
|
||||
printInfo();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Game game = new Game();
|
||||
|
||||
game.printInfo();
|
||||
game.doTurn(1, 1, 100);
|
||||
game.doTurn(1, 1, true);
|
||||
game.doTurn(4, 4, 100);
|
||||
game.die();
|
||||
}
|
||||
}
|
||||
68
src/lab10_memento/ex1/Player.java
Normal file
68
src/lab10_memento/ex1/Player.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package lab10_memento.ex1;
|
||||
|
||||
public class Player {
|
||||
private int x;
|
||||
private int y;
|
||||
private int score;
|
||||
|
||||
public Player(int x, int y, int score) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public void moveTo(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void increaseScore(int score) {
|
||||
this.score += score;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public Checkpoint makeCheckpoint() {
|
||||
return new Checkpoint(this);
|
||||
}
|
||||
|
||||
public class Checkpoint {
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final int score;
|
||||
|
||||
public Checkpoint(Player player) {
|
||||
x = player.getX();
|
||||
y = player.getY();
|
||||
score = player.getScore();
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void restore(Player player) {
|
||||
player.x = this.getX();
|
||||
player.y = this.getY();
|
||||
player.score = this.getScore();
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/lab11_flyweight/ex1/Brush.java
Normal file
16
src/lab11_flyweight/ex1/Brush.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public class Brush implements DrawingTool {
|
||||
private final Props props;
|
||||
|
||||
public Brush(Props props) {
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(String text) {
|
||||
System.out.println("Drawing '" + text + "' in " + props.size + ", color:" + props.color);
|
||||
}
|
||||
|
||||
public record Props(Size size, Color color) {}
|
||||
}
|
||||
17
src/lab11_flyweight/ex1/BrushFactory.java
Normal file
17
src/lab11_flyweight/ex1/BrushFactory.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BrushFactory {
|
||||
private final Map<Brush.Props, Brush> brushes = new HashMap<>();
|
||||
|
||||
public Brush getBrush(Brush.Props props) {
|
||||
Brush brush = brushes.get(props);
|
||||
if (brush == null) {
|
||||
brush = new Brush(props);
|
||||
brushes.put(props, brush);
|
||||
}
|
||||
return brush;
|
||||
}
|
||||
}
|
||||
6
src/lab11_flyweight/ex1/Color.java
Normal file
6
src/lab11_flyweight/ex1/Color.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public enum Color {
|
||||
BLUE,
|
||||
RED
|
||||
}
|
||||
5
src/lab11_flyweight/ex1/DrawingTool.java
Normal file
5
src/lab11_flyweight/ex1/DrawingTool.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public interface DrawingTool {
|
||||
void draw(String text);
|
||||
}
|
||||
31
src/lab11_flyweight/ex1/Main.java
Normal file
31
src/lab11_flyweight/ex1/Main.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
BrushFactory factory = new BrushFactory();
|
||||
Brush.Props props1 = new Brush.Props(Size.THICK, Color.RED);
|
||||
Brush.Props props2 = new Brush.Props(Size.THIN, Color.BLUE);
|
||||
DrawingTool brush1 = factory.getBrush(props1);
|
||||
DrawingTool brush2 = factory.getBrush(props1);
|
||||
|
||||
brush1.draw("I am drawing with my first thick red brush");
|
||||
brush2.draw("I am drawing with my second thick red brush");
|
||||
System.out.println("first thick red brush hashcode: " + brush1.hashCode());
|
||||
System.out.println("second thick red brush hashcode: " + brush2.hashCode());
|
||||
|
||||
System.out.println();
|
||||
DrawingTool pencil = new Pencil();
|
||||
pencil.draw("Bonjour");
|
||||
System.out.println();
|
||||
|
||||
DrawingTool brush3 = factory.getBrush(props2);
|
||||
DrawingTool brush4 = factory.getBrush(props2);
|
||||
|
||||
brush3.draw("I am drawing with my first thin blue brush");
|
||||
brush4.draw("I am drawing with my second thin blue brush");
|
||||
System.out.println("first thin blue brush hashcode: " + brush3.hashCode());
|
||||
System.out.println("second thin blue brush hashcode: " + brush4.hashCode());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
8
src/lab11_flyweight/ex1/Pencil.java
Normal file
8
src/lab11_flyweight/ex1/Pencil.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public class Pencil implements DrawingTool {
|
||||
@Override
|
||||
public void draw(String text) {
|
||||
System.out.println("Pencil writes some content: " + text);
|
||||
}
|
||||
}
|
||||
6
src/lab11_flyweight/ex1/Size.java
Normal file
6
src/lab11_flyweight/ex1/Size.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package lab11_flyweight.ex1;
|
||||
|
||||
public enum Size {
|
||||
THIN,
|
||||
THICK
|
||||
}
|
||||
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal file
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
import lab12_singleton.ex1.sensors.*;
|
||||
import lab12_singleton.ex1.managers.*;
|
||||
|
||||
public class CarSensorSingletonLauncher {
|
||||
public CarSensorSingletonLauncher() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CarSensorSingletonLauncher launcher = new CarSensorSingletonLauncher();
|
||||
launcher.launch();
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
this.test(SimpleSingletonSensorsManager.getInstance());
|
||||
this.test(SynchSingletonSensorsManager.getInstance());
|
||||
this.test(EagerSingletonSensorsManager.getInstance());
|
||||
this.test(DbleCheckedSingletonSensorsManager.getInstance());
|
||||
}
|
||||
|
||||
private void test(SingletonSensorManager manager) {
|
||||
manager.addSensor(new TemperatureSensor());
|
||||
manager.addSensor(new SpeedSensor());
|
||||
manager.addSensor(new RoadConditionSensor());
|
||||
manager.printState();
|
||||
}
|
||||
}
|
||||
15
src/lab12_singleton/ex1/Sensor.java
Normal file
15
src/lab12_singleton/ex1/Sensor.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
public abstract class Sensor {
|
||||
private boolean ok = true;
|
||||
|
||||
public abstract String getName();
|
||||
public void setOk(boolean ok) {
|
||||
this.ok = ok;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + ": " + (ok ? "Ok" : "Warning");
|
||||
}
|
||||
}
|
||||
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal file
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package lab12_singleton.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class SingletonSensorManager {
|
||||
private final ArrayList<Sensor> sensors = new ArrayList<>();
|
||||
|
||||
protected SingletonSensorManager() {}
|
||||
|
||||
public void addSensor(Sensor sensor) {
|
||||
sensors.add(sensor);
|
||||
}
|
||||
|
||||
public void printState() {
|
||||
System.out.println("<" + this.getClass().getSimpleName() + ": " + sensors.toString() + ">");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class DbleCheckedSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static DbleCheckedSingletonSensorsManager instance;
|
||||
|
||||
public static DbleCheckedSingletonSensorsManager getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (DbleCheckedSingletonSensorsManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new DbleCheckedSingletonSensorsManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class EagerSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static final EagerSingletonSensorsManager instance = new EagerSingletonSensorsManager();
|
||||
|
||||
public static EagerSingletonSensorsManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class SimpleSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static SimpleSingletonSensorsManager instance;
|
||||
|
||||
public static SingletonSensorManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SimpleSingletonSensorsManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package lab12_singleton.ex1.managers;
|
||||
|
||||
import lab12_singleton.ex1.SingletonSensorManager;
|
||||
|
||||
public class SynchSingletonSensorsManager extends SingletonSensorManager {
|
||||
private static SynchSingletonSensorsManager instance;
|
||||
|
||||
public static synchronized SynchSingletonSensorsManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SynchSingletonSensorsManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class RoadConditionSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Road Condition";
|
||||
}
|
||||
}
|
||||
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class SpeedSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Speed";
|
||||
}
|
||||
}
|
||||
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab12_singleton.ex1.sensors;
|
||||
|
||||
import lab12_singleton.ex1.Sensor;
|
||||
|
||||
public class TemperatureSensor extends Sensor {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Temperature";
|
||||
}
|
||||
}
|
||||
27
src/lab13_proxy/ex1/Image.java
Normal file
27
src/lab13_proxy/ex1/Image.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class Image {
|
||||
protected final String path;
|
||||
protected final String resolution;
|
||||
protected boolean loaded = false;
|
||||
|
||||
public Image(String path, String resolution) {
|
||||
this.path = path;
|
||||
this.resolution = resolution;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (!loaded) {
|
||||
System.out.println("Image " + path + " is loaded in " + resolution + " resolution");
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public void showImage(User user) {
|
||||
System.out.println("Image " + path + " is shown in " + resolution + " resolution for user " + user.getName());
|
||||
}
|
||||
}
|
||||
24
src/lab13_proxy/ex1/ImageProxy.java
Normal file
24
src/lab13_proxy/ex1/ImageProxy.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class ImageProxy extends Image {
|
||||
private final Image lowResImage;
|
||||
private final Image highResImage;
|
||||
|
||||
public ImageProxy(String path) {
|
||||
super(path, "high");
|
||||
lowResImage = new Image(path, "low");
|
||||
highResImage = new Image(path, "high");
|
||||
}
|
||||
|
||||
public void showImage(User user) {
|
||||
System.out.println(user.getName() + " selects preview image " + path + " and wants to see its full resolution.");
|
||||
if (RegistrationService.isRegistered(user)) {
|
||||
highResImage.load();
|
||||
highResImage.showImage(user);
|
||||
} else {
|
||||
System.out.println("User " + user.getName() + " is not registered. Showing preview image in low resolution");
|
||||
lowResImage.load();
|
||||
lowResImage.showImage(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/lab13_proxy/ex1/Main.java
Normal file
19
src/lab13_proxy/ex1/Main.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
User jean = new User("Jean");
|
||||
User paul = new User("Paul");
|
||||
User pierre = new User("Pierre");
|
||||
|
||||
RegistrationService.register(paul);
|
||||
|
||||
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
|
||||
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
|
||||
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
|
||||
|
||||
highResolutionImage1.showImage(jean);
|
||||
highResolutionImage2.showImage(paul);
|
||||
highResolutionImage3.showImage(pierre);
|
||||
}
|
||||
}
|
||||
15
src/lab13_proxy/ex1/RegistrationService.java
Normal file
15
src/lab13_proxy/ex1/RegistrationService.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RegistrationService {
|
||||
private static final ArrayList<User> users = new ArrayList<>();
|
||||
public static void register(User user) {
|
||||
System.out.println(user.getName() + " is now registered");
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
public static boolean isRegistered(User user) {
|
||||
return users.contains(user);
|
||||
}
|
||||
}
|
||||
13
src/lab13_proxy/ex1/User.java
Normal file
13
src/lab13_proxy/ex1/User.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package lab13_proxy.ex1;
|
||||
|
||||
public class User {
|
||||
private final String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
36
src/lab13_proxy/ex2/Account.java
Normal file
36
src/lab13_proxy/ex2/Account.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Account {
|
||||
private int balance;
|
||||
private int accountNumber;
|
||||
private String owner;
|
||||
|
||||
public Account(String owner, int startBalance) {
|
||||
this.owner = owner;
|
||||
this.balance = startBalance;
|
||||
this.accountNumber = new Random().hashCode();
|
||||
}
|
||||
|
||||
public void deposit(int amount) {
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
public void withdraw(int amount) {
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "account " + this.accountNumber + " belonging to " + this.owner;
|
||||
}
|
||||
}
|
||||
52
src/lab13_proxy/ex2/Bank.java
Normal file
52
src/lab13_proxy/ex2/Bank.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bank {
|
||||
private final ArrayList<String> blacklistedClients = new ArrayList<>();
|
||||
|
||||
public void blacklist(String client) {
|
||||
blacklistedClients.add(client);
|
||||
}
|
||||
public boolean isBlacklisted(String client) {
|
||||
return blacklistedClients.contains(client);
|
||||
}
|
||||
|
||||
public void deposit(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot deposit a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to DEPOSIT money into " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
account.deposit(amount);
|
||||
System.out.println(client + " has deposited " + amount + " on " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
|
||||
public void withdraw(String client, Account account, int amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Cannot withdraw a negative amount");
|
||||
return;
|
||||
}
|
||||
if (isBlacklisted(client)) {
|
||||
System.out.println(client + " is on a blacklist and does not have the right to WITHDRAW money from " + account);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!account.getOwner().equals(client)) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because they are not the owner");
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.getBalance() < amount) {
|
||||
System.out.println(client + " cannot WITHDRAW money from " + account + " because there is not enough money on the account.");
|
||||
return;
|
||||
}
|
||||
|
||||
account.withdraw(amount);
|
||||
System.out.println(client + " has withdrawn " + amount + " from " + account + ". New balance is " + account.getBalance());
|
||||
}
|
||||
}
|
||||
18
src/lab13_proxy/ex2/Main.java
Normal file
18
src/lab13_proxy/ex2/Main.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package lab13_proxy.ex2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Account account = new Account("Pascale", 16000);
|
||||
|
||||
Bank bank = new Bank();
|
||||
bank.blacklist("Jean");
|
||||
bank.blacklist("Pierre");
|
||||
|
||||
bank.deposit("Marcel", account, 1000);
|
||||
bank.deposit("Jean", account, 1000);
|
||||
bank.withdraw("Marcel", account, 1000);
|
||||
bank.withdraw("Pascale", account, 1000000);
|
||||
bank.withdraw("Pascale", account, 1000);
|
||||
bank.withdraw("Pierre", account, 1000);
|
||||
}
|
||||
}
|
||||
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public class AxeBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "chops with an axe";
|
||||
}
|
||||
}
|
||||
19
src/lab14_strategy/ex1/Character.java
Normal file
19
src/lab14_strategy/ex1/Character.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public abstract class Character {
|
||||
private WeaponBehavior weapon;
|
||||
|
||||
public Character(WeaponBehavior weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public void setWeapon(WeaponBehavior weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public void fight() {
|
||||
System.out.println(getName() + " " + weapon.useWeapon());
|
||||
}
|
||||
}
|
||||
25
src/lab14_strategy/ex1/Game.java
Normal file
25
src/lab14_strategy/ex1/Game.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
import lab14_strategy.ex1.characters.King;
|
||||
import lab14_strategy.ex1.characters.Knight;
|
||||
import lab14_strategy.ex1.characters.Troll;
|
||||
import lab14_strategy.ex1.characters.Queen;
|
||||
import lab14_strategy.ex1.weapons.BowAndArrowBehavior;
|
||||
import lab14_strategy.ex1.weapons.KnifeBehavior;
|
||||
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
Character king = new King(new KnifeBehavior());
|
||||
Character queen = new Queen(new BowAndArrowBehavior());
|
||||
Character knight = new Knight(new SwordBehavior());
|
||||
Character troll = new Troll(new AxeBehavior());
|
||||
|
||||
king.fight();
|
||||
queen.fight();
|
||||
knight.fight();
|
||||
troll.fight();
|
||||
|
||||
king.setWeapon(new SwordBehavior());
|
||||
king.fight();
|
||||
}
|
||||
}
|
||||
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public class SwordBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "swings a sword";
|
||||
}
|
||||
}
|
||||
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab14_strategy.ex1;
|
||||
|
||||
public interface WeaponBehavior {
|
||||
String useWeapon();
|
||||
}
|
||||
15
src/lab14_strategy/ex1/characters/King.java
Normal file
15
src/lab14_strategy/ex1/characters/King.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class King extends Character {
|
||||
public King(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "King";
|
||||
}
|
||||
}
|
||||
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Knight extends Character {
|
||||
public Knight(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Knight";
|
||||
}
|
||||
}
|
||||
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Queen extends Character {
|
||||
public Queen(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Queen";
|
||||
}
|
||||
}
|
||||
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab14_strategy.ex1.characters;
|
||||
|
||||
import lab14_strategy.ex1.Character;
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class Troll extends Character {
|
||||
public Troll(WeaponBehavior weapon) {
|
||||
super(weapon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Troll";
|
||||
}
|
||||
}
|
||||
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab14_strategy.ex1.weapons;
|
||||
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class BowAndArrowBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "shoots an arrow with a bow";
|
||||
}
|
||||
}
|
||||
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab14_strategy.ex1.weapons;
|
||||
|
||||
import lab14_strategy.ex1.WeaponBehavior;
|
||||
|
||||
public class KnifeBehavior implements WeaponBehavior {
|
||||
@Override
|
||||
public String useWeapon() {
|
||||
return "cuts with a knife";
|
||||
}
|
||||
}
|
||||
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
public interface SortAlgorithm {
|
||||
void sort(int[] vector);
|
||||
}
|
||||
21
src/lab14_strategy/ex2/Sorter.java
Normal file
21
src/lab14_strategy/ex2/Sorter.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
public class Sorter {
|
||||
private SortAlgorithm algorithm;
|
||||
public void sort(int[] vector) {
|
||||
algorithm.sort(vector);
|
||||
}
|
||||
|
||||
public void setAlgorithm(SortAlgorithm algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
public void showVectorData(int[] vector) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int j : vector) {
|
||||
sb.append(j).append("\t");
|
||||
}
|
||||
System.out.println(sb);
|
||||
}
|
||||
}
|
||||
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal file
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
import lab14_strategy.ex2.algorithms.BubbleSort;
|
||||
import lab14_strategy.ex2.algorithms.InsertionSort;
|
||||
import lab14_strategy.ex2.algorithms.SelectionSort;
|
||||
|
||||
public class StrategySortLauncher {
|
||||
public static void main(String[] args) {
|
||||
StrategySortLauncher launcher = new StrategySortLauncher();
|
||||
launcher.test();
|
||||
}
|
||||
|
||||
public void test() {
|
||||
int[] tab1 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
int[] tab2 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
int[] tab3 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
|
||||
|
||||
Sorter sorter = new Sorter();
|
||||
|
||||
System.out.println("test bubble sort");
|
||||
sorter.setAlgorithm(new BubbleSort());
|
||||
sorter.sort(tab1);
|
||||
sorter.showVectorData(tab1);
|
||||
|
||||
System.out.println("test insert sort");
|
||||
sorter.setAlgorithm(new InsertionSort());
|
||||
sorter.sort(tab2);
|
||||
sorter.showVectorData(tab2);
|
||||
|
||||
System.out.println("test selection sort");
|
||||
sorter.setAlgorithm(new SelectionSort());
|
||||
sorter.sort(tab3);
|
||||
sorter.showVectorData(tab3);
|
||||
}
|
||||
}
|
||||
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal file
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class BubbleSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp;
|
||||
int nbrePermutation = -1;
|
||||
int nbreIteration = 0;
|
||||
while (nbrePermutation != 0) {
|
||||
nbrePermutation = 0;
|
||||
for (int i=0; i<vector.length - nbreIteration - 1; i++) {
|
||||
if (vector[i] > vector[i + 1]) {
|
||||
nbrePermutation++;
|
||||
temp = vector[i + 1];
|
||||
vector[i + 1] = vector[i];
|
||||
vector[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class InsertionSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp;
|
||||
for (int i=1; i<vector.length; i++) {
|
||||
temp = vector[i];
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
if (vector[j] > temp) {
|
||||
vector[j + 1] = vector[j];
|
||||
vector[j] = temp;
|
||||
} else {
|
||||
vector[j + 1] = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab14_strategy.ex2.algorithms;
|
||||
|
||||
import lab14_strategy.ex2.SortAlgorithm;
|
||||
|
||||
public class SelectionSort implements SortAlgorithm {
|
||||
@Override
|
||||
public void sort(int[] vector) {
|
||||
int temp, cursor = 0;
|
||||
for (int i=0; i<vector.length; i++) {
|
||||
temp = vector[i];
|
||||
cursor = i;
|
||||
for (int j = i; j < vector.length; j++) {
|
||||
if (vector[j] < temp) {
|
||||
temp = vector[j];
|
||||
cursor = j;
|
||||
}
|
||||
}
|
||||
vector[cursor] = vector[i];
|
||||
vector[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/lab15_observer/ex1/Main.java
Normal file
24
src/lab15_observer/ex1/Main.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
PatientMonitoring pm = new PatientMonitoring(1, 140, 85);
|
||||
|
||||
MedicalEmployee jean = new MedicalEmployee("Jean", pm);
|
||||
MedicalEmployee pauline = new MedicalEmployee("Pauline", pm);
|
||||
MedicalEmployee matthieu = new MedicalEmployee("Matthieu", pm);
|
||||
MedicalEmployee symeon = new MedicalEmployee("Symeon", pm);
|
||||
|
||||
pm.setBloodPressure(110);
|
||||
pm.setPosition(3);
|
||||
|
||||
pm.setPulseOximetry(90);
|
||||
pm.setPulseOximetry(70);
|
||||
pm.setPosition(7);
|
||||
pm.setBloodPressure(150);
|
||||
|
||||
pm.removeObserver(matthieu);
|
||||
pm.setBloodPressure(145);
|
||||
pm.setPosition(9);
|
||||
}
|
||||
}
|
||||
19
src/lab15_observer/ex1/MedicalEmployee.java
Normal file
19
src/lab15_observer/ex1/MedicalEmployee.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public class MedicalEmployee implements PatientObserver {
|
||||
private final String name;
|
||||
|
||||
public MedicalEmployee(String name, PatientMonitoring pm) {
|
||||
this.name = name;
|
||||
pm.registerObserver(this);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Problem problem, PatientMonitoring pm) {
|
||||
System.out.println(getName() + " has been notified of " + problem + " for patient " + pm);
|
||||
}
|
||||
}
|
||||
70
src/lab15_observer/ex1/PatientMonitoring.java
Normal file
70
src/lab15_observer/ex1/PatientMonitoring.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PatientMonitoring {
|
||||
private final List<PatientObserver> observers = new ArrayList<>();
|
||||
|
||||
private int position;
|
||||
private int bloodPressure;
|
||||
private int pulseOximetry;
|
||||
|
||||
public PatientMonitoring(int position, int bloodPressure, int pulseOximetry) {
|
||||
this.position = position;
|
||||
this.bloodPressure = bloodPressure;
|
||||
this.pulseOximetry = pulseOximetry;
|
||||
}
|
||||
|
||||
public void registerObserver(PatientObserver o) {
|
||||
observers.add(o);
|
||||
}
|
||||
|
||||
public void removeObserver(PatientObserver o) {
|
||||
observers.remove(o);
|
||||
}
|
||||
|
||||
private void notifyObservers(Problem problem) {
|
||||
for (PatientObserver o : observers) {
|
||||
o.update(problem, this);
|
||||
}
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
public int getBloodPressure() {
|
||||
return bloodPressure;
|
||||
}
|
||||
public int getPulseOximetry() {
|
||||
return pulseOximetry;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
|
||||
public void setBloodPressure(int bloodPressure) {
|
||||
this.bloodPressure = bloodPressure;
|
||||
if (getBloodPressure() > 145) {
|
||||
notifyObservers(Problem.BLOOD_PRESSURE);
|
||||
} else {
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPulseOximetry(int pulseOximetry) {
|
||||
this.pulseOximetry = pulseOximetry;
|
||||
if (getPulseOximetry() > 85) {
|
||||
notifyObservers(Problem.OXIMETRY);
|
||||
} else {
|
||||
notifyObservers(Problem.NO_PROBLEM);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<Patient Position: " + position + ", BP: " + bloodPressure + ", PO: " + pulseOximetry + ">";
|
||||
}
|
||||
}
|
||||
5
src/lab15_observer/ex1/PatientObserver.java
Normal file
5
src/lab15_observer/ex1/PatientObserver.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public interface PatientObserver {
|
||||
void update(Problem problem, PatientMonitoring pm);
|
||||
}
|
||||
7
src/lab15_observer/ex1/Problem.java
Normal file
7
src/lab15_observer/ex1/Problem.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package lab15_observer.ex1;
|
||||
|
||||
public enum Problem {
|
||||
NO_PROBLEM,
|
||||
BLOOD_PRESSURE,
|
||||
OXIMETRY
|
||||
}
|
||||
103
src/lab15_observer/ex2/AnalogTimer.java
Normal file
103
src/lab15_observer/ex2/AnalogTimer.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class AnalogTimer implements TimerObserver {
|
||||
private final MyTimer timer;
|
||||
private static final String[][] digits = {
|
||||
{
|
||||
"┌─┐",
|
||||
"│ │",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
" ╷",
|
||||
" │",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
"┌─┘",
|
||||
"└─╴"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
" ─┤",
|
||||
"╶─┘"
|
||||
},
|
||||
{
|
||||
"╷ ╷",
|
||||
"└─┤",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"┌─╴",
|
||||
"└─┐",
|
||||
"╶─┘"
|
||||
},
|
||||
{
|
||||
"┌─╴",
|
||||
"├─┐",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
"╶─┐",
|
||||
" │",
|
||||
" ╵"
|
||||
},
|
||||
{
|
||||
"┌─┐",
|
||||
"├─┤",
|
||||
"└─┘"
|
||||
},
|
||||
{
|
||||
"┌─┐",
|
||||
"└─┤",
|
||||
"╶─┘"
|
||||
},
|
||||
};
|
||||
|
||||
public AnalogTimer(MyTimer timer) {
|
||||
this.timer = timer;
|
||||
timer.registerObserver(this);
|
||||
}
|
||||
|
||||
public void update(int hours, int minutes, int seconds) {
|
||||
String[] lines = {"", "", ""};
|
||||
|
||||
addDigit(lines, hours / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, hours % 10);
|
||||
|
||||
addSep(lines);
|
||||
|
||||
addDigit(lines, minutes / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, minutes % 10);
|
||||
|
||||
addSep(lines);
|
||||
|
||||
addDigit(lines, seconds / 10);
|
||||
addSpace(lines);
|
||||
addDigit(lines, seconds % 10);
|
||||
|
||||
System.out.println(lines[0]);
|
||||
System.out.println(lines[1]);
|
||||
System.out.println(lines[2]);
|
||||
}
|
||||
|
||||
private void addDigit(String[] lines, int digit) {
|
||||
String[] parts = digits[digit];
|
||||
for (int i = 0; i < 3; i++) {
|
||||
lines[i] += parts[i];
|
||||
}
|
||||
}
|
||||
private void addSpace(String[] lines) {
|
||||
lines[0] += " ";
|
||||
lines[1] += " ";
|
||||
lines[2] += " ";
|
||||
}
|
||||
private void addSep(String[] lines) {
|
||||
lines[0] += " o ";
|
||||
lines[1] += " ";
|
||||
lines[2] += " o ";
|
||||
}
|
||||
}
|
||||
8
src/lab15_observer/ex2/ClockTimer.java
Normal file
8
src/lab15_observer/ex2/ClockTimer.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public interface ClockTimer {
|
||||
int getHour();
|
||||
int getMinute();
|
||||
int getSecond();
|
||||
void tick();
|
||||
}
|
||||
15
src/lab15_observer/ex2/DigitalTimer.java
Normal file
15
src/lab15_observer/ex2/DigitalTimer.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class DigitalTimer implements TimerObserver {
|
||||
private final MyTimer timer;
|
||||
|
||||
public DigitalTimer(MyTimer timer) {
|
||||
this.timer = timer;
|
||||
timer.registerObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(int hours, int minutes, int seconds) {
|
||||
System.out.printf("Time: %02d:%02d:%02d%n", hours, minutes, seconds);
|
||||
}
|
||||
}
|
||||
10
src/lab15_observer/ex2/Main.java
Normal file
10
src/lab15_observer/ex2/Main.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
MyTimer clockTimer = new MyTimer();
|
||||
|
||||
new AnalogTimer(clockTimer);
|
||||
new DigitalTimer(clockTimer);
|
||||
}
|
||||
}
|
||||
53
src/lab15_observer/ex2/MyTimer.java
Normal file
53
src/lab15_observer/ex2/MyTimer.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MyTimer implements ClockTimer {
|
||||
private final List<TimerObserver> observers = new ArrayList<>();
|
||||
private Calendar cal;
|
||||
private Timer timer;
|
||||
|
||||
public MyTimer() {
|
||||
cal = Calendar.getInstance();
|
||||
timer = new Timer();
|
||||
timer.schedule(new TimerAction(), 0, 1000);
|
||||
}
|
||||
|
||||
class TimerAction extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
cal = Calendar.getInstance();
|
||||
tick();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHour() {
|
||||
return cal.get(Calendar.HOUR_OF_DAY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinute() {
|
||||
return cal.get(Calendar.MINUTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSecond() {
|
||||
return cal.get(Calendar.SECOND);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
for (TimerObserver o : observers) {
|
||||
o.update(getHour(), getMinute(), getSecond());
|
||||
}
|
||||
}
|
||||
|
||||
public void registerObserver(TimerObserver o) {
|
||||
observers.add(o);
|
||||
}
|
||||
|
||||
public void removeObserver(TimerObserver o) {
|
||||
observers.remove(o);
|
||||
}
|
||||
}
|
||||
5
src/lab15_observer/ex2/TimerObserver.java
Normal file
5
src/lab15_observer/ex2/TimerObserver.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab15_observer.ex2;
|
||||
|
||||
public interface TimerObserver{
|
||||
void update(int hours, int minutes, int seconds);
|
||||
}
|
||||
8
src/lab16_composite/ex1/Entity.java
Normal file
8
src/lab16_composite/ex1/Entity.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public interface Entity {
|
||||
void cry();
|
||||
void simulateInjury();
|
||||
void enterField();
|
||||
void shoot();
|
||||
}
|
||||
33
src/lab16_composite/ex1/Game.java
Normal file
33
src/lab16_composite/ex1/Game.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
Player jo = new Player(1);
|
||||
Player jean = new Player(2);
|
||||
Player paul = new Player(3);
|
||||
jo.cry();
|
||||
jean.cry();
|
||||
jo.enterField();
|
||||
Team team1 = new Team();
|
||||
team1.add(jo);
|
||||
team1.add(jean);
|
||||
team1.add(paul);
|
||||
team1.enterField();
|
||||
team1.cry();
|
||||
team1.simulateInjury();
|
||||
Player martine = new Player(3);
|
||||
Player isabelle = new Player(4);
|
||||
Player mariePaule = new Player(5);
|
||||
|
||||
Team team2 = new Team();
|
||||
team2.add(martine);
|
||||
team2.add(isabelle);
|
||||
team2.add(mariePaule);
|
||||
team2.add(team1);
|
||||
team2.enterField();
|
||||
team2.cry();
|
||||
team2.simulateInjury();
|
||||
team2.remove(team1);
|
||||
team2.simulateInjury();
|
||||
}
|
||||
}
|
||||
30
src/lab16_composite/ex1/Player.java
Normal file
30
src/lab16_composite/ex1/Player.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public class Player implements Entity {
|
||||
private int number;
|
||||
|
||||
public Player(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[Player " + number + "]";
|
||||
}
|
||||
|
||||
public void cry() {
|
||||
System.out.println(this + " ouin ouin");
|
||||
}
|
||||
|
||||
public void enterField() {
|
||||
System.out.println(this + " let's go !");
|
||||
}
|
||||
|
||||
public void simulateInjury() {
|
||||
System.out.println(this + " ouch !");
|
||||
}
|
||||
|
||||
public void shoot() {
|
||||
System.out.println(this + " encara Messi...");
|
||||
}
|
||||
}
|
||||
44
src/lab16_composite/ex1/Team.java
Normal file
44
src/lab16_composite/ex1/Team.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Team implements Entity {
|
||||
private List<Entity> entities = new ArrayList<>();
|
||||
|
||||
public void add(Entity entity) {
|
||||
entities.add(entity);
|
||||
}
|
||||
|
||||
public void remove(Entity entity) {
|
||||
entities.remove(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cry() {
|
||||
for (Entity entity : entities) {
|
||||
entity.cry();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simulateInjury() {
|
||||
for (Entity entity : entities) {
|
||||
entity.simulateInjury();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterField() {
|
||||
for (Entity entity : entities) {
|
||||
entity.enterField();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shoot() {
|
||||
for (Entity entity : entities) {
|
||||
entity.shoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
74
src/lab16_composite/ex3/TestTraversalAlgorithms.java
Normal file
74
src/lab16_composite/ex3/TestTraversalAlgorithms.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package lab16_composite.ex3;
|
||||
|
||||
import lab16_composite.ex3.algorithm.BFSSearch;
|
||||
import lab16_composite.ex3.algorithm.DFSSearch;
|
||||
import lab16_composite.ex3.algorithm.TraversalAlgorithm;
|
||||
import lab16_composite.ex3.component.Component;
|
||||
import lab16_composite.ex3.composite.Composite;
|
||||
import lab16_composite.ex3.leaf.Leaf;
|
||||
|
||||
public class TestTraversalAlgorithms {
|
||||
public static void main(String[] args) {
|
||||
TestTraversalAlgorithms launcher = new TestTraversalAlgorithms();
|
||||
launcher.launch();
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
|
||||
Component treeRoot = this.createTreeStruct();
|
||||
|
||||
//Instanciating one searchAlgoritm of each type.
|
||||
TraversalAlgorithm dfs = new DFSSearch();
|
||||
TraversalAlgorithm bfs = new BFSSearch();
|
||||
|
||||
//Launching DFS search for one existing element.
|
||||
Component result = dfs.search(treeRoot, "Leaf 5");
|
||||
System.out.println("1 element found by DFS: " + result);
|
||||
|
||||
//Launching BFS search for one existing element.
|
||||
result = bfs.search(treeRoot, "Leaf 5");
|
||||
System.out.println("1 element found by BFS : " + result);
|
||||
}
|
||||
|
||||
//1. We create the tree.
|
||||
// node1
|
||||
// / \
|
||||
// node2 node3
|
||||
// / \ / \
|
||||
// l1 n4 n5 n6
|
||||
// /\ \ / \
|
||||
// l2 l3 l4 l5 l6
|
||||
//
|
||||
//Creating the leafs.
|
||||
private Component createTreeStruct() {
|
||||
Leaf l1 = new Leaf("Leaf 1");
|
||||
Leaf l2 = new Leaf("Leaf 2");
|
||||
Leaf l3 = new Leaf("Leaf 3");
|
||||
Leaf l4 = new Leaf("Leaf 4");
|
||||
Leaf l5 = new Leaf("Leaf 5");
|
||||
Leaf l6 = new Leaf("Leaf 6");
|
||||
|
||||
//Creating the nodes
|
||||
Composite n1 = new Composite("Node 1");
|
||||
Composite n2 = new Composite("Node 2");
|
||||
Composite n3 = new Composite("Node 3");
|
||||
Composite n4 = new Composite("Node 4");
|
||||
Composite n5 = new Composite("Node 5");
|
||||
Composite n6 = new Composite("Node 6");
|
||||
|
||||
//Assigning leaf and nodes to parents (nodes).
|
||||
n6.add(l5);
|
||||
n6.add(l6);
|
||||
n5.add(l4);
|
||||
n4.add(l2);
|
||||
n4.add(l3);
|
||||
n3.add(n5);
|
||||
n3.add(n6);
|
||||
n2.add(l1);
|
||||
n2.add(n4);
|
||||
n1.add(n2);
|
||||
n1.add(n3);
|
||||
|
||||
return n1;
|
||||
}
|
||||
}
|
||||
13
src/lab16_composite/ex3/algorithm/BFSSearch.java
Normal file
13
src/lab16_composite/ex3/algorithm/BFSSearch.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BFSSearch extends TraversalAlgorithm {
|
||||
|
||||
@Override
|
||||
protected void addSuccessor(ArrayList<Component> newSuccessors) {
|
||||
successors.addAll(newSuccessors);
|
||||
}
|
||||
}
|
||||
12
src/lab16_composite/ex3/algorithm/DFSSearch.java
Normal file
12
src/lab16_composite/ex3/algorithm/DFSSearch.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DFSSearch extends TraversalAlgorithm {
|
||||
@Override
|
||||
protected void addSuccessor(ArrayList<Component> newSuccessors) {
|
||||
successors.addAll(0, newSuccessors);
|
||||
}
|
||||
}
|
||||
27
src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java
Normal file
27
src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TraversalAlgorithm {
|
||||
protected List<Component> successors = new ArrayList<>();
|
||||
|
||||
public Component search(Component root, String target) {
|
||||
successors = new ArrayList<>();
|
||||
successors.add(root);
|
||||
while (true) {
|
||||
if (successors.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Component component = successors.removeFirst();
|
||||
if (component.getName().equals(target)) {
|
||||
return component;
|
||||
}
|
||||
addSuccessor(component.getChildren());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void addSuccessor(ArrayList<Component> newSuccessors);
|
||||
}
|
||||
23
src/lab16_composite/ex3/component/Component.java
Normal file
23
src/lab16_composite/ex3/component/Component.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package lab16_composite.ex3.component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Component {
|
||||
public abstract String getName();
|
||||
|
||||
public void add(Component c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void remove(Component c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Component getChild(int i) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public ArrayList<Component> getChildren() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
52
src/lab16_composite/ex3/composite/Composite.java
Normal file
52
src/lab16_composite/ex3/composite/Composite.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package lab16_composite.ex3.composite;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Composite extends Component {
|
||||
private String name;
|
||||
private final ArrayList<Component> children;
|
||||
|
||||
public Composite(String n) {
|
||||
name = n;
|
||||
children = new ArrayList<Component>();
|
||||
}
|
||||
|
||||
public void setName(String n) {
|
||||
name = n;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Component c) {
|
||||
children.add(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Component c) {
|
||||
children.remove(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getChild(int i) {
|
||||
return children.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Component> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String temp = "Composite " + name;
|
||||
for (Component n : this.children) {
|
||||
temp = temp + n.toString();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
30
src/lab16_composite/ex3/leaf/Leaf.java
Normal file
30
src/lab16_composite/ex3/leaf/Leaf.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package lab16_composite.ex3.leaf;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Leaf extends Component {
|
||||
//Attributes
|
||||
|
||||
private final String name;
|
||||
|
||||
//Constructor
|
||||
public Leaf(String n) {
|
||||
name = n;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Leaf " + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Component> getChildren() {
|
||||
return new ArrayList<Component>();
|
||||
}
|
||||
}
|
||||
13
src/lab5_adapter/ex1/Engineer.java
Normal file
13
src/lab5_adapter/ex1/Engineer.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public class Engineer {
|
||||
private String name;
|
||||
public Engineer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Engineer " + name;
|
||||
}
|
||||
}
|
||||
10
src/lab5_adapter/ex1/Machine.java
Normal file
10
src/lab5_adapter/ex1/Machine.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public interface Machine {
|
||||
int getNumberOfCapsules();
|
||||
boolean isCompletelyUp();
|
||||
void stop();
|
||||
|
||||
void setNumberOfCapsules(int capsules);
|
||||
void setCompletelyUp(boolean completelyUp);
|
||||
}
|
||||
44
src/lab5_adapter/ex1/MachineAdapter.java
Normal file
44
src/lab5_adapter/ex1/MachineAdapter.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public class MachineAdapter implements NewMachineInterface {
|
||||
private OldMachine machine;
|
||||
|
||||
public MachineAdapter(OldMachine machine) {
|
||||
this.machine = machine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfCapsules() {
|
||||
return machine.getNumberOfCapsules();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompletelyUp() {
|
||||
return machine.isCompletelyUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
machine.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumberOfCapsules(int capsules) {
|
||||
machine.setNumberOfCapsules(capsules);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCompletelyUp(boolean completelyUp) {
|
||||
machine.setCompletelyUp(completelyUp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Engineer getEngineer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return machine.toString() + ", supervised by engineer " + getEngineer();
|
||||
}
|
||||
}
|
||||
27
src/lab5_adapter/ex1/Main.java
Normal file
27
src/lab5_adapter/ex1/Main.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Engineer engineer1 = new Engineer("Jean");
|
||||
Engineer engineer2 = new Engineer("Maria");
|
||||
Machine machine1 = new NewMachine("1159190947", engineer1);
|
||||
Machine machine2 = new NewMachine("1247233941", engineer1);
|
||||
Machine machine3 = new NewMachine("258952499", engineer2);
|
||||
OldMachine oldMachine = new OldMachine("603742814");
|
||||
Machine machine4 = new MachineAdapter(oldMachine);
|
||||
|
||||
machine1.setNumberOfCapsules(30);
|
||||
machine2.setNumberOfCapsules(20);
|
||||
machine3.setNumberOfCapsules(40);
|
||||
oldMachine.setNumberOfCapsules(80);
|
||||
//Machine 1159190947 is currently processing 30 capsules, supervised by engineer Engineer Jean
|
||||
//Machine 1247233941 is currently processing 20 capsules, supervised by engineer Engineer Jean
|
||||
//Machine 258952499 is currently processing 40 capsules, supervised by engineer Engineer Maria
|
||||
//Machine 603742814 is currently processing 80 capsules, supervised by engineer null
|
||||
|
||||
System.out.println(machine1);
|
||||
System.out.println(machine2);
|
||||
System.out.println(machine3);
|
||||
System.out.println(machine4);
|
||||
}
|
||||
}
|
||||
19
src/lab5_adapter/ex1/NewMachine.java
Normal file
19
src/lab5_adapter/ex1/NewMachine.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public class NewMachine extends OldMachine implements NewMachineInterface {
|
||||
private Engineer engineer;
|
||||
|
||||
public NewMachine(String name, Engineer engineer) {
|
||||
super(name);
|
||||
this.engineer = engineer;
|
||||
}
|
||||
|
||||
public Engineer getEngineer() {
|
||||
return engineer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + ", supervised by engineer " + getEngineer();
|
||||
}
|
||||
}
|
||||
5
src/lab5_adapter/ex1/NewMachineInterface.java
Normal file
5
src/lab5_adapter/ex1/NewMachineInterface.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public interface NewMachineInterface extends Machine {
|
||||
Engineer getEngineer();
|
||||
}
|
||||
39
src/lab5_adapter/ex1/OldMachine.java
Normal file
39
src/lab5_adapter/ex1/OldMachine.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package lab5_adapter.ex1;
|
||||
|
||||
public class OldMachine implements Machine {
|
||||
private String name;
|
||||
private int capsules = 0;
|
||||
private boolean completelyUp = false;
|
||||
|
||||
public OldMachine(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setNumberOfCapsules(int capsules) {
|
||||
this.capsules = capsules;
|
||||
}
|
||||
|
||||
public void setCompletelyUp(boolean completelyUp) {
|
||||
this.completelyUp = completelyUp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfCapsules() {
|
||||
return capsules;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompletelyUp() {
|
||||
return completelyUp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
System.out.println("Stopping machine " + name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Machine " + name + " is currently processing " + getNumberOfCapsules() + " capsules";
|
||||
}
|
||||
}
|
||||
32
src/lab6_template_method/ex1/HellsKitchen.java
Normal file
32
src/lab6_template_method/ex1/HellsKitchen.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package lab6_template_method.ex1;
|
||||
|
||||
public class HellsKitchen extends Restaurant {
|
||||
public HellsKitchen(String name) {
|
||||
super("HK " + name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareVegetables() {
|
||||
log("Preparing nice salad");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareMainMenu() {
|
||||
log("Creating flavorful dish");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareCheese() {
|
||||
log("Preparing finest cheese");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareDessert() {
|
||||
log("Giving birth to awesome dessert");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareCoffee() {
|
||||
log("Brewing the best coffee");
|
||||
}
|
||||
}
|
||||
32
src/lab6_template_method/ex1/MacDonalds.java
Normal file
32
src/lab6_template_method/ex1/MacDonalds.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package lab6_template_method.ex1;
|
||||
|
||||
public class MacDonalds extends Restaurant {
|
||||
public MacDonalds(String name) {
|
||||
super("McDonalds " + name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareVegetables() {
|
||||
log("Vegetables ? What's that ?");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareMainMenu() {
|
||||
log("McChicken incoming");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareCheese() {
|
||||
log("Hmmm, creamy cheese");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareDessert() {
|
||||
log("Preparing McFlurry");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareCoffee() {
|
||||
log("Preparing short and hot coffee");
|
||||
}
|
||||
}
|
||||
11
src/lab6_template_method/ex1/Main.java
Normal file
11
src/lab6_template_method/ex1/Main.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package lab6_template_method.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Restaurant r1 = new MacDonalds("Sion");
|
||||
Restaurant r2 = new HellsKitchen("London");
|
||||
|
||||
r1.prepareDinner();
|
||||
r2.prepareDinner();
|
||||
}
|
||||
}
|
||||
36
src/lab6_template_method/ex1/Restaurant.java
Normal file
36
src/lab6_template_method/ex1/Restaurant.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package lab6_template_method.ex1;
|
||||
|
||||
public abstract class Restaurant {
|
||||
private String name;
|
||||
public Restaurant(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public final void prepareDinner() {
|
||||
serveWater();
|
||||
prepareVegetables();
|
||||
prepareMainMenu();
|
||||
prepareCheese();
|
||||
prepareDessert();
|
||||
prepareCoffee();
|
||||
cleanTable();
|
||||
}
|
||||
|
||||
protected void log(String msg) {
|
||||
System.out.println("[" + name + "] " + msg);
|
||||
}
|
||||
|
||||
protected final void serveWater() {
|
||||
log("Serving water");
|
||||
}
|
||||
|
||||
protected final void cleanTable() {
|
||||
log("Cleaning table");
|
||||
}
|
||||
|
||||
protected abstract void prepareVegetables();
|
||||
protected abstract void prepareMainMenu();
|
||||
protected abstract void prepareCheese();
|
||||
protected abstract void prepareDessert();
|
||||
protected abstract void prepareCoffee();
|
||||
}
|
||||
101
src/lab7_state/ex1/Machine.java
Normal file
101
src/lab7_state/ex1/Machine.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package lab7_state.ex1;
|
||||
|
||||
import lab7_state.ex1.states.*;
|
||||
|
||||
public class Machine {
|
||||
private MachineState currentState;
|
||||
private final MachineState offState;
|
||||
private final MachineState idleState;
|
||||
private final MachineState choiceState;
|
||||
private final MachineState serviceNeededState;
|
||||
private final MachineState coffeeReadyState;
|
||||
|
||||
private int cups;
|
||||
private boolean jammed = false;
|
||||
|
||||
public Machine() {
|
||||
offState = new OffState(this);
|
||||
idleState = new IdleState(this);
|
||||
choiceState = new ChoiceState(this);
|
||||
serviceNeededState = new ServiceNeededState(this);
|
||||
coffeeReadyState = new CoffeeReadyState(this);
|
||||
currentState = offState;
|
||||
this.setCups(3);
|
||||
}
|
||||
|
||||
public MachineState getOffState() {
|
||||
return offState;
|
||||
}
|
||||
|
||||
public MachineState getIdleState() {
|
||||
return idleState;
|
||||
}
|
||||
|
||||
public MachineState getChoiceState() {
|
||||
return choiceState;
|
||||
}
|
||||
|
||||
public MachineState getServiceNeededState() {
|
||||
return serviceNeededState;
|
||||
}
|
||||
|
||||
public MachineState getCoffeeReadyState() {
|
||||
return coffeeReadyState;
|
||||
}
|
||||
|
||||
public void returnMoney(double value) {
|
||||
System.out.println("Returning CHF " + value);
|
||||
}
|
||||
|
||||
public void setCurrentState(MachineState currentState) {
|
||||
System.out.println(this.currentState.toString() + " -> " + currentState);
|
||||
this.currentState = currentState;
|
||||
}
|
||||
|
||||
public void powerUp() {
|
||||
currentState.powerUp();
|
||||
}
|
||||
|
||||
public void insertCoin(double value) {
|
||||
currentState.insertCoin(value);
|
||||
}
|
||||
|
||||
public void returnCoin() {
|
||||
currentState.returnCoin();
|
||||
}
|
||||
|
||||
public void pushButton() {
|
||||
currentState.pushButton();
|
||||
}
|
||||
|
||||
public void resetButton() {
|
||||
currentState.reset();
|
||||
}
|
||||
|
||||
public void removeCup() {
|
||||
currentState.removeCup();
|
||||
}
|
||||
|
||||
public void setCups(int cups) {
|
||||
this.cups = cups;
|
||||
}
|
||||
|
||||
public boolean hasCups() {
|
||||
return cups != 0;
|
||||
}
|
||||
|
||||
public boolean hasCoffee() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isJammed() {
|
||||
return jammed;
|
||||
}
|
||||
|
||||
public void makeCoffee() {
|
||||
cups--;
|
||||
if (Math.random() < 0.2) {
|
||||
jammed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/lab7_state/ex1/MachineState.java
Normal file
21
src/lab7_state/ex1/MachineState.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package lab7_state.ex1;
|
||||
|
||||
public abstract class MachineState {
|
||||
protected Machine machine;
|
||||
|
||||
public MachineState(Machine machine) {
|
||||
this.machine = machine;
|
||||
}
|
||||
|
||||
protected void powerUp() {}
|
||||
protected void insertCoin(double value) {}
|
||||
protected void returnCoin() {}
|
||||
protected void reset() {}
|
||||
protected void pushButton() {}
|
||||
protected void removeCup() {}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
||||
29
src/lab7_state/ex1/Main.java
Normal file
29
src/lab7_state/ex1/Main.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package lab7_state.ex1;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Machine machine = new Machine();
|
||||
System.out.println("A");
|
||||
machine.powerUp();
|
||||
machine.insertCoin(1);
|
||||
|
||||
System.out.println("B");
|
||||
machine.insertCoin(0.25);
|
||||
machine.returnCoin();
|
||||
|
||||
System.out.println("C");
|
||||
machine.insertCoin(0.25);
|
||||
machine.pushButton();
|
||||
machine.removeCup();
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
System.out.println("D" + i);
|
||||
machine.insertCoin(0.25);
|
||||
machine.pushButton();
|
||||
machine.removeCup();
|
||||
}
|
||||
|
||||
System.out.println("E");
|
||||
machine.resetButton();
|
||||
}
|
||||
}
|
||||
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package lab7_state.ex1.states;
|
||||
|
||||
import lab7_state.ex1.Machine;
|
||||
import lab7_state.ex1.MachineState;
|
||||
|
||||
public class ChoiceState extends MachineState {
|
||||
public ChoiceState(Machine machine) {
|
||||
super(machine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void returnCoin() {
|
||||
machine.returnMoney(0.25);
|
||||
machine.setCurrentState(machine.getIdleState());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void pushButton() {
|
||||
if (!machine.hasCups()) {
|
||||
System.out.println("The machine is out of cups");
|
||||
} else if (!machine.hasCoffee()) {
|
||||
System.out.println("The machine is out of coffee beans");
|
||||
} else if (machine.isJammed()) {
|
||||
System.out.println("The machine is jammed");
|
||||
} else {
|
||||
machine.makeCoffee();
|
||||
machine.setCurrentState(machine.getCoffeeReadyState());
|
||||
return;
|
||||
}
|
||||
machine.setCurrentState(machine.getServiceNeededState());
|
||||
}
|
||||
}
|
||||
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab7_state.ex1.states;
|
||||
|
||||
import lab7_state.ex1.Machine;
|
||||
import lab7_state.ex1.MachineState;
|
||||
|
||||
public class CoffeeReadyState extends MachineState {
|
||||
public CoffeeReadyState(Machine machine) {
|
||||
super(machine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeCup() {
|
||||
machine.setCurrentState(machine.getIdleState());
|
||||
}
|
||||
}
|
||||
20
src/lab7_state/ex1/states/IdleState.java
Normal file
20
src/lab7_state/ex1/states/IdleState.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package lab7_state.ex1.states;
|
||||
|
||||
import lab7_state.ex1.Machine;
|
||||
import lab7_state.ex1.MachineState;
|
||||
|
||||
public class IdleState extends MachineState {
|
||||
public IdleState(Machine machine) {
|
||||
super(machine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void insertCoin(double value) {
|
||||
if (value != 0.25) {
|
||||
machine.returnMoney(value);
|
||||
} else {
|
||||
System.out.println("Please choose your coffee");
|
||||
machine.setCurrentState(machine.getChoiceState());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/lab7_state/ex1/states/OffState.java
Normal file
15
src/lab7_state/ex1/states/OffState.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab7_state.ex1.states;
|
||||
|
||||
import lab7_state.ex1.Machine;
|
||||
import lab7_state.ex1.MachineState;
|
||||
|
||||
public class OffState extends MachineState {
|
||||
public OffState(Machine machine) {
|
||||
super(machine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void powerUp() {
|
||||
machine.setCurrentState(machine.getIdleState());
|
||||
}
|
||||
}
|
||||
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab7_state.ex1.states;
|
||||
|
||||
import lab7_state.ex1.Machine;
|
||||
import lab7_state.ex1.MachineState;
|
||||
|
||||
public class ServiceNeededState extends MachineState {
|
||||
public ServiceNeededState(Machine machine) {
|
||||
super(machine);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
machine.setCurrentState(machine.getIdleState());
|
||||
}
|
||||
}
|
||||
25
src/lab7_state/ex2/Main.java
Normal file
25
src/lab7_state/ex2/Main.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package lab7_state.ex2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
User user = new User("Baryhobal");
|
||||
user.connect();
|
||||
user.enter();
|
||||
user.leave();
|
||||
|
||||
user.enter();
|
||||
user.ask();
|
||||
user.leave();
|
||||
|
||||
user.enter();
|
||||
user.ask();
|
||||
user.handOver();
|
||||
user.leave();
|
||||
|
||||
user.enter();
|
||||
user.ask();
|
||||
user.handOver();
|
||||
user.over();
|
||||
user.leave();
|
||||
}
|
||||
}
|
||||
71
src/lab7_state/ex2/User.java
Normal file
71
src/lab7_state/ex2/User.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package lab7_state.ex2;
|
||||
|
||||
import lab7_state.ex2.states.*;
|
||||
|
||||
public class User {
|
||||
private final String name;
|
||||
private UserState currentState;
|
||||
private final UserState unregisteredState;
|
||||
private final UserState registeredState;
|
||||
private final UserState inMeetingState;
|
||||
private final UserState waitingToSpeakState;
|
||||
private final UserState speakingState;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
unregisteredState = new UnregisteredState(this);
|
||||
registeredState = new RegisteredState(this);
|
||||
inMeetingState = new InMeetingState(this);
|
||||
waitingToSpeakState = new WaitingToSpeakState(this);
|
||||
speakingState = new SpeakingState(this);
|
||||
currentState = unregisteredState;
|
||||
}
|
||||
|
||||
public void setCurrentState(UserState currentState) {
|
||||
this.currentState = currentState;
|
||||
}
|
||||
|
||||
public UserState getUnregisteredState() {
|
||||
return unregisteredState;
|
||||
}
|
||||
|
||||
public UserState getRegisteredState() {
|
||||
return registeredState;
|
||||
}
|
||||
|
||||
public UserState getInMeetingState() {
|
||||
return inMeetingState;
|
||||
}
|
||||
|
||||
public UserState getWaitingToSpeakState() {
|
||||
return waitingToSpeakState;
|
||||
}
|
||||
|
||||
public UserState getSpeakingState() {
|
||||
return speakingState;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
currentState.connect();
|
||||
}
|
||||
public void enter() {
|
||||
currentState.enter();
|
||||
}
|
||||
public void leave() {
|
||||
currentState.leave();
|
||||
}
|
||||
public void ask() {
|
||||
currentState.ask();
|
||||
}
|
||||
public void handOver() {
|
||||
currentState.handOver();
|
||||
}
|
||||
public void over() {
|
||||
currentState.over();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User " + name;
|
||||
}
|
||||
}
|
||||
18
src/lab7_state/ex2/UserState.java
Normal file
18
src/lab7_state/ex2/UserState.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package lab7_state.ex2;
|
||||
|
||||
public abstract class UserState {
|
||||
protected User user;
|
||||
public UserState(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
protected void log(String msg) {
|
||||
System.out.println("[" + user + "] " + msg);
|
||||
}
|
||||
|
||||
public void connect() {};
|
||||
public void enter() {};
|
||||
public void leave() {};
|
||||
public void ask() {};
|
||||
public void handOver() {};
|
||||
public void over() {};
|
||||
}
|
||||
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab7_state.ex2.states;
|
||||
|
||||
import lab7_state.ex2.User;
|
||||
import lab7_state.ex2.UserState;
|
||||
|
||||
public class InMeetingState extends UserState {
|
||||
public InMeetingState(User user) {
|
||||
super(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave() {
|
||||
user.setCurrentState(user.getRegisteredState());
|
||||
log("Left meeting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ask() {
|
||||
user.setCurrentState(user.getWaitingToSpeakState());
|
||||
log("Asking to speak");
|
||||
}
|
||||
}
|
||||
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package lab7_state.ex2.states;
|
||||
|
||||
import lab7_state.ex2.User;
|
||||
import lab7_state.ex2.UserState;
|
||||
|
||||
public class RegisteredState extends UserState {
|
||||
public RegisteredState(User user) {
|
||||
super(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enter() {
|
||||
user.setCurrentState(user.getInMeetingState());
|
||||
log("Entered meeting");
|
||||
}
|
||||
}
|
||||
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab7_state.ex2.states;
|
||||
|
||||
import lab7_state.ex2.User;
|
||||
import lab7_state.ex2.UserState;
|
||||
|
||||
public class SpeakingState extends UserState {
|
||||
public SpeakingState(User user) {
|
||||
super(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void over() {
|
||||
user.setCurrentState(user.getInMeetingState());
|
||||
log("Finished speaking");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave() {
|
||||
user.setCurrentState(user.getRegisteredState());
|
||||
log("Finished speaking, left meeting");
|
||||
}
|
||||
}
|
||||
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package lab7_state.ex2.states;
|
||||
|
||||
import lab7_state.ex2.User;
|
||||
import lab7_state.ex2.UserState;
|
||||
|
||||
public class UnregisteredState extends UserState {
|
||||
public UnregisteredState(User user) {
|
||||
super(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
user.setCurrentState(user.getRegisteredState());
|
||||
log("Registered");
|
||||
}
|
||||
}
|
||||
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab7_state.ex2.states;
|
||||
|
||||
import lab7_state.ex2.User;
|
||||
import lab7_state.ex2.UserState;
|
||||
|
||||
public class WaitingToSpeakState extends UserState {
|
||||
public WaitingToSpeakState(User user) {
|
||||
super(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave() {
|
||||
user.setCurrentState(user.getRegisteredState());
|
||||
log("Lost patience, left meeting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handOver() {
|
||||
user.setCurrentState(user.getSpeakingState());
|
||||
log("Starts speaking");
|
||||
}
|
||||
}
|
||||
70
src/lab8_builder/ex1/Car.java
Normal file
70
src/lab8_builder/ex1/Car.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package lab8_builder.ex1;
|
||||
|
||||
public class Car {
|
||||
private String power;
|
||||
private String engine;
|
||||
private String breaks;
|
||||
private String seats;
|
||||
private String windows;
|
||||
private String fuelType;
|
||||
private final String carType;
|
||||
public Car (String carType){
|
||||
this.carType = carType;
|
||||
}
|
||||
public String getPower() {
|
||||
return power;
|
||||
}
|
||||
public void setPower(String power) {
|
||||
this.power = power;
|
||||
}
|
||||
public String getEngine() {
|
||||
return engine;
|
||||
}
|
||||
public void setEngine(String engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
public String getBreaks() {
|
||||
return breaks;
|
||||
}
|
||||
public void setBreaks(String breaks) {
|
||||
this.breaks = breaks;
|
||||
}
|
||||
public String getSeats() {
|
||||
return seats;
|
||||
}
|
||||
public void setSeats(String seats) {
|
||||
this.seats = seats;
|
||||
}
|
||||
public String getWindows() {
|
||||
return windows;
|
||||
}
|
||||
public void setWindows(String windows) {
|
||||
this.windows = windows;
|
||||
}
|
||||
public String getFuelType() {
|
||||
return fuelType;
|
||||
}
|
||||
public void setFuelType(String fuelType) {
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("--------------"+carType+"--------------------- \n");
|
||||
sb.append("\n Power: ");
|
||||
sb.append(power);
|
||||
sb.append("\n Engine: ");
|
||||
sb.append(engine);
|
||||
sb.append("\n Breaks: ");
|
||||
sb.append(breaks);
|
||||
sb.append("\n Seats: ");
|
||||
sb.append(seats);
|
||||
sb.append("\n Windows: ");
|
||||
sb.append(windows);
|
||||
sb.append("\n Fuel Type: ");
|
||||
sb.append(fuelType);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
20
src/lab8_builder/ex1/CarBuilder.java
Normal file
20
src/lab8_builder/ex1/CarBuilder.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package lab8_builder.ex1;
|
||||
|
||||
public abstract class CarBuilder {
|
||||
protected Car car;
|
||||
|
||||
protected abstract String getCarType();
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public void setCar(Car car) {
|
||||
this.car = car;
|
||||
}
|
||||
|
||||
public abstract void buildEngine();
|
||||
public abstract void buildBreaks();
|
||||
public abstract void buildSeats();
|
||||
public abstract void buildWindows();
|
||||
}
|
||||
20
src/lab8_builder/ex1/CarFactory.java
Normal file
20
src/lab8_builder/ex1/CarFactory.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package lab8_builder.ex1;
|
||||
|
||||
public class CarFactory {
|
||||
private CarBuilder builder;
|
||||
private Car car;
|
||||
|
||||
public void setBuilder(CarBuilder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Car construct() {
|
||||
car = new Car(builder.getCarType());
|
||||
builder.setCar(car);
|
||||
builder.buildEngine();
|
||||
builder.buildBreaks();
|
||||
builder.buildSeats();
|
||||
builder.buildWindows();
|
||||
return builder.getCar();
|
||||
}
|
||||
}
|
||||
22
src/lab8_builder/ex1/Main.java
Normal file
22
src/lab8_builder/ex1/Main.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package lab8_builder.ex1;
|
||||
|
||||
import lab8_builder.ex1.builders.BerlinBuilder;
|
||||
import lab8_builder.ex1.builders.SportBuilder;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
CarFactory factory = new CarFactory();
|
||||
|
||||
CarBuilder builder1 = new BerlinBuilder();
|
||||
CarBuilder builder2 = new SportBuilder();
|
||||
|
||||
factory.setBuilder(builder1);
|
||||
Car car1 = factory.construct();
|
||||
|
||||
factory.setBuilder(builder2);
|
||||
Car car2 = factory.construct();
|
||||
|
||||
System.out.println(car1);
|
||||
System.out.println(car2);
|
||||
}
|
||||
}
|
||||
32
src/lab8_builder/ex1/builders/BerlinBuilder.java
Normal file
32
src/lab8_builder/ex1/builders/BerlinBuilder.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package lab8_builder.ex1.builders;
|
||||
|
||||
import lab8_builder.ex1.CarBuilder;
|
||||
|
||||
public class BerlinBuilder extends CarBuilder {
|
||||
@Override
|
||||
protected String getCarType() {
|
||||
return "Berlin";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildEngine() {
|
||||
car.setEngine("V12");
|
||||
car.setFuelType("Diesel");
|
||||
car.setPower("12'000 Horses");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildBreaks() {
|
||||
car.setBreaks("Premium Double Tungsten-Platinum Alloy");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildSeats() {
|
||||
car.setSeats("5");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildWindows() {
|
||||
car.setWindows("Tinted bullet-proof");
|
||||
}
|
||||
}
|
||||
32
src/lab8_builder/ex1/builders/SportBuilder.java
Normal file
32
src/lab8_builder/ex1/builders/SportBuilder.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package lab8_builder.ex1.builders;
|
||||
|
||||
import lab8_builder.ex1.CarBuilder;
|
||||
|
||||
public class SportBuilder extends CarBuilder {
|
||||
@Override
|
||||
protected String getCarType() {
|
||||
return "Sports";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildEngine() {
|
||||
car.setEngine("V8");
|
||||
car.setFuelType("Unleaded 95");
|
||||
car.setPower("7'000 horses");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildBreaks() {
|
||||
car.setBreaks("Holo-magnetic and semi-cyclonic");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildSeats() {
|
||||
car.setSeats("2");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildWindows() {
|
||||
car.setWindows("Standard");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user