Guest User

Untitled

a guest
Jun 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. public class RadarSimulator {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. Vehicle v = new Ferrari();
  6. v = new RadarDetecting(v);
  7.  
  8. v.setSpeed(100);
  9.  
  10. RadarTrap r = new RadarTrap();
  11. r.enter(v);
  12. r.checkForSpeeders();
  13. }
  14. }
  15.  
  16. ------------------------------------------------
  17.  
  18. import java.util.Iterator;
  19.  
  20. public class RadarTrap {
  21.  
  22. private int SPEED_LIMIT = 65;
  23. private List<Vehicle> vehicles = new SinglyLinkedList<Vehicle>();
  24.  
  25. public void enter(Vehicle v) {
  26. vehicles.add(v);
  27. }
  28.  
  29. public int measureSpeed(Vehicle v) {
  30. v.illuminated(this);
  31. return v.getSpeed();
  32. }
  33.  
  34. public void checkForSpeeders() {
  35.  
  36. Iterator<Vehicle> it = vehicles.iterator();
  37.  
  38. while (it.hasNext()) {
  39. Vehicle v = it.next();
  40. if (measureSpeed(v) > SPEED_LIMIT) {
  41. v.getTicketed();
  42. }
  43. }
  44. }
  45. }
  46.  
  47. ------------------------------------------------
  48.  
  49. public abstract class Vehicle {
  50.  
  51. RadarBehavior radarBehavior;
  52.  
  53. int speed = 0;
  54.  
  55. public int getSpeed() {
  56. return speed;
  57. }
  58.  
  59. public void setSpeed(int speed) {
  60. this.speed = speed;
  61. }
  62.  
  63. public void getTicketed() {
  64. System.out.println("Ticket");
  65. }
  66.  
  67. public void illuminated(RadarTrap radarTrap) {
  68. System.out.println("Illuminated");
  69. }
  70. }
  71.  
  72. ------------------------------------------------
  73.  
  74. public class Ferrari extends Vehicle {
  75.  
  76. public Ferrari() {
  77. radarBehavior = new DoNothing();
  78. }
  79. }
  80.  
  81. ------------------------------------------------
  82.  
  83. public class RadarDetecting extends Decorator {
  84.  
  85. Vehicle vehicle;
  86.  
  87. public RadarDetecting(Vehicle vehicle) {
  88. this.vehicle = vehicle;
  89. }
  90.  
  91. public int getSpeed() {
  92. return speed;
  93. }
  94.  
  95. public void illuminated(RadarTrap radarTrap) {
  96. System.out.println("Got illuminated, so slowed down..");
  97. this.setSpeed(60);
  98. }
  99. }
Add Comment
Please, Sign In to add comment