-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyDrafts.java
2676 lines (2052 loc) · 67.8 KB
/
MyDrafts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Created on October, 2021
// @author: Fábio Araújo de Sá
public class Main {
public static void main (String[] args) {
//InputOutput.run();
//DataStructures.run();
OOP.run();
}
}
import java.util.Scanner; // To scan user inputs
class InputOutput {
public static void run() {
HelloWorld();
Inputs();
Calculator();
}
public static void HelloWorld() {
System.out.println("Hello World!");
}
public static void Inputs() {
Scanner input = new Scanner(System.in);
System.out.print("Your name: ");
String name = input.nextLine();
System.out.print("Your age: ");
int age = 0;
while (age <= 0 || age > 120) {
try {
age = input.nextInt();
if (age <= 0 || age > 120) {
System.out.print("Error. Please try again:\nYour age: ");
}
} catch (Exception exception) {
input.nextLine();
System.out.print("Error. Please try again:\nYour age: ");
}
}
input.nextLine();
System.out.println("Your name is " + name + " and you have " + age + " years old!");
}
public static void Calculator() {
boolean exit = false;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to your simple calculator:");
while (!exit) {
System.out.print("'1' - Calculate something\n'0' - Exit\nYour choice: ");
int option = input.nextInt();
input.nextLine();
if (option != 1 && option != 0) {
System.out.println("Input error. Please try again:");
} else if (option == 1) {
System.out.println("Choose a operation:\n1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division\n5 - SquareRoot\nYour choice: ");
int operation = input.nextInt(), operand1, operand2;
input.nextLine();
if (operation != 5) {
System.out.print("Operand 1: ");
operand1 = input.nextInt();
input.nextLine();
System.out.print("Operand 2: ");
operand2 = input.nextInt();
} else {
System.out.print("Operand: ");
operand1 = input.nextInt();
operand2 = 0;
}
input.nextLine();
switch (operation) {
case 1: {
int result = operand1 + operand2;
System.out.println(operand1 + " + " + operand2 + " = " + result);
break;
}
case 2: {
int result = operand1 - operand2;
System.out.println(operand1 + " - " + operand2 + " = " + result);
break;
}
case 3: {
long result = operand1 * operand2;
System.out.println(operand1 + " * " + operand2 + " = " + result);
break;
}
case 4: {
double result = operand1 / operand2;
System.out.println(operand1 + " / " + operand2 + " = " + result);
break;
}
case 5: {
double result = Math.sqrt(operand1);
System.out.println("SquareRoot(" + operand1 + ") = " + result);
break;
}
default: {
System.out.println("Input error!");
}
}
} else {
exit = !exit;
}
}
System.out.println("Goodbye!");
}
}
class DataStructures {
public static void run() {
Constants();
Strings();
Arrays();
}
public static void Constants() {
final double PI = 3.14159;
final double PHI = 1.612;
System.out.println("\nPI = " + PI + " and PHI = " + PHI + "\n");
}
public static void Strings() {
String FEUP = "Faculdade de Engenharia da Universidade do Porto";
String[] words = FEUP.replace(" ", "-").split("-");
for (String word : words) {
System.out.println(word.toUpperCase() + " " + word.toLowerCase());
}
System.out.println();
}
public static void Arrays() {
System.out.println("Arrays and 2D arrays:");
String[][] myName = {{"F", "á", "b", "i", "o"},
{"A", "r", "a", "ú", "j", "o"},
{"d", "e"},
{"S", "á"}};
for (int i = 0; i < myName.length; i++) {
for (int j = 0; j < myName[i].length; j++) {
System.out.print(myName[i][j]);
}
System.out.print(" ");
}
System.out.println();
}
}
class OOP {
public static void run() {
createStudents();
Heritance();
}
public static void createStudents() {
Student a = new Student();
a.setName("Araújo");
Student b = new Student("Fabio", 19, 'M', 202007658, 15.0);
System.out.println();
a.Presentation();
String answer = a == b ? " is equal to " : " isn't equal to ";
System.out.println(a.getName() + answer + b.getName() + '\n');
}
public static void Heritance() {
Bike myBike = new Bike("BMX", 2017);
String answer = myBike.haveWindows() ? "My bike has windows" : "My bike has not windows";
System.out.println(answer);
Car myCar = new Car("Ferrari", 2021);
myCar.Run();
myCar.Stop();
System.out.println("My car has " + myCar.getWheels() + " wheels\n");
}
}
class Student {
private String name;
private char gender;
public int up, age;
private double grade;
Student () {
this.name = "UNKNOWN";
this.age = 0;
this.gender = 'U';
this.up = 999999999;
this.grade = 0.0;
}
Student (String name, int age, char gender, int up, double grade) {
this.name = name;
this.age = age;
this.gender = gender;
this.up = up;
this.grade = grade;
}
public void setName(String name) { this.name = name; }
public void setUp(int up) { this.up = up; }
public void setAge(int age) { this.age = age; }
public void setGender(char gender) { this.gender = gender; }
public void setGrade(double grade) { this.grade = grade; }
public String getName() { return name; }
public int getUp() { return up; }
public int getAge() { return age; }
public char getGender() { return gender; }
public double getGrade() { return grade; }
public void Presentation() {
System.out.println(name + " have " + age + " years old, with upCode = " + up + " and grade " + grade + " values!");
}
}
// Abstract class --> only child classes can be implemented
// Interface --> .h files in C++, it declares some methods that will be implemented later in classes
interface Components {
public int getWheels();
public boolean haveWindows();
}
abstract class Vehicle {
private String name;
private int year;
Vehicle () {
this.name = "UNKNOWN";
this.year = 0000;
}
Vehicle (String name, int year) {
this.name = name;
this.year = year;
}
public void Run () { System.out.println("The vehicle is moving"); }
public void Stop () { System.out.println("The vehicle is stopped"); }
}
class Car extends Vehicle implements Components {
public int wheels = 4 ;
Car (String name, int year) {
super(name, year);
}
public int getWheels() { return this.wheels; }
public boolean haveWindows() { return true; }
}
class Bike extends Vehicle {
public int windows = 0 ;
Bike (String name, int year) {
super(name, year);
}
public boolean haveWindows() { return windows > 0 ; }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class Files {
public static void run() {
UsingFiles();
}
public static void UsingFiles () {
String name = "Lusiadas.txt";
File file = new File(name);
if (file.exists()) {
System.out.println("\n\nFile " + name + " exists");
// Extracts data and show content
try {
FileReader reader = new FileReader(name);
int data = reader.read();
System.out.println("\n" + name + ": \n");
while (data != -1) {
System.out.print((char)data);
data = reader.read();
}
reader.close();
} catch (FileNotFoundException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
} else {
System.out.println("File not found");
}
}
}
class Exceptions {
public static void run() {
System.out.println("Run");
}
}
class playWithExceptions {
public static void run() {
try {
System.out.println(simpleDivision(9, 3));
System.out.println(simpleDivision(5, 3));
System.out.println(simpleDivision(9, 9));
System.out.println(simpleDivision(9, -1));
System.out.println(simpleDivision(133, 0));
System.out.println(simpleDivision(133, 231));
} catch (MyException e) {
e.printStackTrace();
} finally {
System.out.println("Stop");
}
}
public static int simpleDivision(int x, int y) throws MyException {
if (y != 0) {
int result = x / y;
return result;
} else {
throw new MyException("My Exception Error");
}
}
}
class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super();
System.out.println(message);
}
}
class usingThreads {
public static void run() {
Model m = new Model();
View v = new View();
new Thread() {
public void run () {
while (m.a < 1000) {
m.increment();
}
}
}.start();
new Thread() {
public void run() {
while (m.a < 1000) {
v.draw(m);
}
}
}.start();
}
}
class Model {
int a = 0, b = 0;
public void increment() {
a++;
b++;
}
}
class View {
public void draw (Model m) {
System.out.println(m.a + " " + m.b);
}
}
abstract class Fruta {
String nome;
int tamanho;
public Fruta (String nome, int tamanho) {
this.nome = nome;
this.tamanho = tamanho;
}
}
class Banana extends Fruta {
String qualidade;
public Banana (String nome, int tamanho, String qualidade) {
super(nome, tamanho);
this.qualidade = qualidade;
}
}
public class DivisibleByFilter implements GenericListFilter {
@Override
public boolean accept(Integer number) {
return number != 0 ;
}
}
public interface GenericListDeduplicator {
public List<Integer> deduplicate(List<Integer> list, GenericListSorter genericListSorter);
}
public interface GenericListFilter {
public boolean accept(Integer number);
}
public interface GenericListSorter {
public List<Integer> sort(List<Integer> list);
}
/**
* A utility class that aggregates list of numbers
* into a single integer using various functions.
*/
public class ListAggregator {
/**
* Sums all numbers in a list.
* @return The sum of all the values in the list.
*/
public Integer sum(List<Integer> list) {
int sum = 0;
for (Integer number : list) sum += number;
return sum;
}
/**
* Calculates the maximum value in a list.
* @return The maximum value in the list.
*/
public Integer max(List<Integer> list) {
int max = Integer.MIN_VALUE;
for (Integer number : list) if (number > max) max = number;
return max;
}
/**
* Calculates the minimum value in a list.
* @return The minimum value in the list.
*/
public Integer min(List<Integer> list) {
int min = Integer.MAX_VALUE;
for (Integer number : list) if (number < min) min = number;
return min;
}
/**
* Counts the number of distinct numbers in a list.
* @return The number of distinct numbers.
*/
public int distinct(List<Integer> list, GenericListDeduplicator genericListDeduplicator) {
ListSorter listSorter = new ListSorter();
List<Integer> distinct = genericListDeduplicator.deduplicate(list, listSorter);
return distinct.size();
}
}
/**
* A utility class that removes duplicate numbers from a list.
*/
public class ListDeduplicator implements GenericListDeduplicator {
/**
* Removes duplicate numbers from a list.
* @return A list having the same numbers as the original
* but without duplicates. The order of the numbers might
* change.
*/
@Override
public List<Integer> deduplicate(List<Integer> list, GenericListSorter genericListSorter) {
List<Integer> sorted = genericListSorter.sort(list);
List<Integer> unique = new ArrayList<>();
Integer last = null;
for (Integer number : sorted)
if (!number.equals(last)) {
last = number;
unique.add(number);
}
return unique;
}
}
public class ListFilter {
private GenericListFilter filter;
public ListFilter(GenericListFilter filter) {
this.filter = filter;
}
public List<Integer> filter(List<Integer> list) {
List<Integer> result = new ArrayList<>();
for (Integer number : list) {
if (filter.accept(number)) {
result.add(number);
}
}
return result;
}
}
/**
* An utility class to sort list of numbers.
*/
public class ListSorter implements GenericListSorter {
/**
* Really stupid way to sort a list.
* @return A sorted version of the list.
*/
@Override
public List<Integer> sort(List<Integer> list) {
List<Integer> sorted = new ArrayList<>();
for (Integer number : list)
sorted.add(number);
for (int i = 0; i < sorted.size() - 1; i++)
for (int j = i + 1 ; j < sorted.size() ; j++)
if (sorted.get(i) > sorted.get(j))
Collections.swap(sorted, i, j);
return sorted;
}
}
public class Notifier {
String s;
public Notifier(String s) { this.s = s; }
void notify(String x) {};
}
public class PositiveFilter implements GenericListFilter {
@Override
public boolean accept(Integer number) {
return number >= 0 ;
}
}
class FirstSpecification extends Specification {
def "one plus one should equal to" () {
expect:
1 + 1 == 2
}
def "two plus two should equal four" () {
given:
int left = 2
int right = 2
when:
int result = left + right
then:
result == 4
}
def "Should be able to remove from list" () {
given:
def list = [1, 2, 3, 4]
when:
list.remove(0)
then:
list == [2, 3, 4]
// list == [1, 2, 4] --> error
}
def "Should get an index out of bounds when removing a nonexistent item" () {
given:
def list = [1, 2, 3, 4]
when:
list.remove(20)
then:
thrown(IndexOutOfBoundsException)
list.size() == 4
}
def "numbers to the power of two" (int a, int b, int c) {
expect:
Math.pow(a, b) == c
where:
a | b | c
1 | 2 | 1
2 | 2 | 4
3 | 2 | 9
}
def "Bug 8726 in Deduplicator" () {
given:
def deduplicator = Mock(GenericListDeduplicator)
def sorter = Mock(GenericListSorter)
when:
def result = deduplicator.deduplicate(Arrays.asList(1, 2, 4, 2), sorter)
then:
result == null
}
def "Using Stubs with Spock" () {
given:
def sorter = Mock(GenericListSorter)
def deduplicator = Mock(GenericListDeduplicator)
deduplicator.deduplicate(Arrays.asList(1, 2, 4, 2), sorter) >> Arrays.asList(1, 2, 4)
deduplicator.deduplicate(_) >> Arrays.asList(1, 2, 4)
deduplicator.deduplicate(_) >>> [Arrays.asList(1, 2, 4), Arrays.asList(6, 7)]
when:
def result = deduplicator.deduplicate(Arrays.asList(1, 2, 4, 2), sorter)
then:
result == Arrays.asList(1, 2, 4)
}
def "Should verify notify was called"() {
given:
def notifier = Mock(Notifier)
when:
notifier.notify('foo')
then:
// 1 * notifier.notify(_)
1 * notifier.notify('foo')
}
}
public class DivisibleByFilterTest {
@Test
public void test() {
DivisibleByFilter divisibleByFilter = new DivisibleByFilter();
Assert.assertEquals(false, divisibleByFilter.accept(0));
Assert.assertEquals(true, divisibleByFilter.accept(-2));
Assert.assertEquals(true, divisibleByFilter.accept(3));
Assert.assertEquals(false, divisibleByFilter.accept(0));
Assert.assertEquals(true, divisibleByFilter.accept(2342));
}
}
public class ListAggregatorTest {
class StubDeduplicator implements GenericListDeduplicator {
@Override
public List<Integer> deduplicate(List<Integer> list, GenericListSorter genericListSorter) {
List<Integer> unique = Arrays.asList(1, 2, 4);
return unique;
}
}
public List<Integer> helper() {
List<Integer> list = Arrays.asList(1,2,4,2,5);
return list;
}
@Test
public void sum() {
ListAggregator aggregator = new ListAggregator();
int sum = aggregator.sum(helper());
Assertions.assertEquals(14, sum);
}
@Test
public void max() {
ListAggregator aggregator = new ListAggregator();
int max = aggregator.max(helper());
Assertions.assertEquals(5, max);
}
@Test
public void min() {
ListAggregator aggregator = new ListAggregator();
int min = aggregator.min(helper());
Assertions.assertEquals(1, min);
}
@Test
public void distinct() {
ListDeduplicator deduplicator = new ListDeduplicator();
ListAggregator aggregator = new ListAggregator();
int distinct = aggregator.distinct(helper(), deduplicator);
Assertions.assertEquals(4, distinct);
}
@Test
public void max_bug_7263() {
ListAggregator aggregator = new ListAggregator();
List<Integer> list = Arrays.asList(-5, -2, -1, -4);
Integer result = aggregator.max(list);
Assertions.assertEquals(-1, result);
}
@Test
public void distinct_bug_8726() {
// Passo 5 -> StubDeduplicator stub = new StubDeduplicator();
GenericListDeduplicator deduplicator = Mockito.mock(GenericListDeduplicator.class);
// Passo 6 -> Se o deduplicate receber qualquer lista, retorna (1, 2, 4)
Mockito.when(deduplicator.deduplicate(Mockito.anyList(), Mockito.any(GenericListSorter.class)))
.thenReturn(Arrays.asList(1, 2, 4));
ListAggregator aggregator = new ListAggregator();
List<Integer> list = Arrays.asList(1, 2, 4, 2);
Integer result = aggregator.distinct(list, deduplicator); // o deduplicator injectado só retorna (1, 2, 4)
Assertions.assertEquals(3, result);
}
}
public class ListDeduplicatorTest {
/**
* Não quero que o resultado de "deduplicate" dependa da classe sorter()
* Então o deduplicate recebe um GenericListSorted, que tanto pode ser um objecto manipulado (stub) ou não
*/
class StubSorter implements GenericListSorter {
@Override
public List<Integer> sort(List<Integer> list) {
List<Integer> stub = Arrays.asList(1, 2, 2, 4);
return stub;
}
}
@Test
public void deduplicate() {
List<Integer> list = Arrays.asList(1, 2, 4, 2);
List<Integer> expected = Arrays.asList(1, 2, 4);
ListDeduplicator deduplicator = new ListDeduplicator();
StubSorter stub = new StubSorter();
List<Integer> distinct = deduplicator.deduplicate(list, stub);
Assertions.assertEquals(expected, distinct);
}
}
public class ListFilterTest {
@Test
public void filter() {
List<Integer> numbers = Arrays.asList(-3, 2, 0, -5, 2, 6, -5, 0, 2, -2, -6, 0);
ListFilter positiveFilter = new ListFilter(new PositiveFilter());
ListFilter divisibleFilter = new ListFilter(new DivisibleByFilter());
List<Integer> resultPositive = positiveFilter.filter(numbers);
List<Integer> expectedPositive = Arrays.asList(2, 0, 2, 6, 0, 2, 0);
Assertions.assertEquals(resultPositive, expectedPositive);
List<Integer> resultNotNull = divisibleFilter.filter(numbers);
List<Integer> expectedNotNull = Arrays.asList(-3, 2, -5, 2, 6, -5, 2, -2, -6);
Assertions.assertEquals(resultNotNull, expectedNotNull);
}
@Test
public void usingStubs() {
PositiveFilter positiveFilter = Mockito.mock(PositiveFilter.class);
Mockito.when(positiveFilter.accept(Mockito.anyInt()))
.thenReturn(false);
ListFilter listFilter = new ListFilter(positiveFilter);
List<Integer> numbers = Arrays.asList(3, 2, 0, 5, 2, 6, 5, 0, 2, 2, 6, 0);
List<Integer> expected = Arrays.asList();
// é independente do valor do filtro --> só está a analisar a função do listFilter()
List<Integer> result = listFilter.filter(numbers);
Assertions.assertEquals(result, expected);
}
}
public class ListSorterTest {
@Test
public void sort() {
List<Integer> list = Arrays.asList(3, 2, 6, 1, 4, 5, 7);
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
ListSorter sorter = new ListSorter();
List<Integer> sorted = sorter.sort(list);
Assertions.assertEquals(expected, sorted);
}
}
public class PositiveFilterTest {
@Test
public void test() {
PositiveFilter positiveFilter = new PositiveFilter();
Assert.assertEquals(true, positiveFilter.accept(0));
Assert.assertEquals(false, positiveFilter.accept(-1));
Assert.assertEquals(true, positiveFilter.accept(2323));
Assert.assertEquals(false, positiveFilter.accept(-134));
}
}
public abstract class AlienClient implements Client {
private OrderingStrategy strategy;
public AlienClient() {
this.strategy = createOrderingStrategy();
}
@Override
public void happyHourStarted(Bar bar) {
strategy.happyHourStarted((StringBar) bar);
}
@Override
public void happyHourEnded(Bar bar) {
strategy.happyHourEnded((StringBar) bar);
}
@Override
public void wants(StringDrink drink, StringRecipe recipe, StringBar bar) {
strategy.wants(drink, recipe, bar);
}
protected abstract OrderingStrategy createOrderingStrategy();
}
public abstract class Bar {
private boolean happyHour;
private List<BarObserver> observers;
public Bar () {
this.happyHour = false;
this.observers = new ArrayList<>();
}
public boolean isHappyHour() {
return happyHour;
}
public void startHappyHour() {
this.happyHour = true;
notifyObservers();
}
public void endHappyHour() {
this.happyHour = false;
notifyObservers();
}
public void addObserver(BarObserver observer) {
this.observers.add(observer);
}
public void removeObserver(BarObserver observer) {
this.observers.remove(observer);
}
public void notifyObservers() {
for (BarObserver observer : observers) {
if (isHappyHour()) {
observer.happyHourStarted(this);
} else {
observer.happyHourEnded(this);
}
}
}
}
public interface BarObserver {
void happyHourStarted(Bar bar);
void happyHourEnded(Bar bar);
}
public interface Client extends BarObserver {
void wants (StringDrink drink, StringRecipe recipe, StringBar bar);
}
public class FerengiClient extends AlienClient {
private OrderingStrategy strategy;
protected OrderingStrategy createOrderingStrategy() {
return new SmartStrategy();
}
public FerengiClient() {
super();
this.strategy = createOrderingStrategy();
}
@Override
public void happyHourStarted(Bar bar) {
strategy.happyHourStarted((StringBar) bar);
}
@Override
public void happyHourEnded(Bar bar) {
strategy.happyHourEnded((StringBar) bar);
}
@Override
public void wants(StringDrink drink, StringRecipe recipe, StringBar bar) {
strategy.wants(drink, recipe, bar);
}
}
public class HumanClient implements Client {
private OrderingStrategy strategy;
private StringDrink drink;
private StringRecipe recipe;
private StringBar bar;
public HumanClient() {
super();
}
public HumanClient(OrderingStrategy strategy) {
super();
this.strategy = strategy;
}
public void wants (StringDrink drink, StringRecipe recipe, StringBar bar) {
strategy.wants(drink, recipe, bar);
this.recipe = recipe;
this.bar = bar;
this.drink = drink;
}
public void happyHourStarted(Bar bar) {
this.recipe.mix(this.drink);
}
public void happyHourEnded(Bar bar) {