You are on page 1of 4

Tugas Kelompok ke-2

Minggu ke-5 Sesi 7


Dari coding java untuk tes cetak report berikut:
a. Cari 4 tipe code smell and jelaskan!
b. Lakukan refactor untuk memperbaiki code smell tersebut!

1. import java.util.Random;
2. import java.util.Vector;
3.
4. public class MainSystem {
5.
6. public static void main(String[] args) {
7. String [] it = {"Sandal xl","Sandal l","Sandal v", "Shoes xl","Shoes l", "Shoes v"};
8. int [] pt = {100, 90, 80, 200, 190, 185};
9. int [] hp = {1200, 1150, 1100, 2500, 2300, 2200};
10. int [] m = {1, 1, 2, 3, 3, 4, 5};
11. Random r = new Random(232);
12. Report report = new Report();
13. Transaction t = new Transaction(1, 2016);
14.
15. for(int i=0; i<it.length; i++)
16. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
17. report.AddTransaction(t);
18. t = new Transaction(2, 2016);
19. for(int i=0; i<it.length; i++)
20. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
21. report.AddTransaction(t);
22. t = new Transaction(3, 2016);
23. for(int i=0; i<it.length; i++)
24. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
25. report.AddTransaction(t);
26. t = new Transaction(4, 2016);
27. for(int i=0; i<it.length; i++)
28. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
29. report.AddTransaction(t);
30. t = new Transaction(5, 2016);
31. for(int i=0; i<it.length; i++)
32. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
33. report.AddTransaction(t);
34. t = new Transaction(6, 2016);
35. for(int i=0; i<it.length; i++)
36. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
37. report.AddTransaction(t);
38. t = new Transaction(7, 2016);
39. for(int i=0; i<it.length; i++)
40. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
41. report.AddTransaction(t);
42. t = new Transaction(8, 2016);
43. for(int i=0; i<it.length; i++)
44. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
45. report.AddTransaction(t);
46. t = new Transaction(9, 2016);
47. for(int i=0; i<it.length; i++)
48. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
49. report.AddTransaction(t);
50. t = new Transaction(10, 2016);
51. for(int i=0; i<it.length; i++)
52. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
53. report.AddTransaction(t);
54. t = new Transaction(11, 2016);

COMP6284 – Code Reengineering


55. for(int i=0; i<it.length; i++)
56. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
57. report.AddTransaction(t);
58. t = new Transaction(12, 2016);
59. for(int i=0; i<it.length; i++)
60. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
61. report.AddTransaction(t);
62. t = new Transaction(1, 2017);
63. for(int i=0; i<it.length; i++)
64. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
65. report.AddTransaction(t);
66. t = new Transaction(2, 2017);
67. for(int i=0; i<it.length; i++)
68. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
69. report.AddTransaction(t);
70. t = new Transaction(3, 2017);
71. for(int i=0; i<it.length; i++)
72. t.addItem(new Item(it[i], pt[i], hp[i], r.nextInt(500) + 1));
73. report.AddTransaction(t);
74.
75. report.printTransaction(1, 2016, 3, 2016);
76. report.printTransaction(3, 2016, 12, 2016);
77. report.printTransaction(5, 2016, 1, 2017);
78. }
79.
80. }
81.
82. class Item {
83. private String name;
84. private int price;
85. private int quantity;
86. private int highSeasonPrice;
87. private final int HIGH_SEASON_START = 5;
88. private final int HIGH_SEASON_END = 8;
89.
90. public Item(String name, int price, int highSeasonPrice, int quantity) {
91. this.name = name;
92. this.price = price;
93. this.highSeasonPrice = highSeasonPrice;
94. this.quantity = quantity;
95. }
96. public String getName() {
97. return name;
98. }
99.
100. public int getPrice(int currentMonth, int quantity) {
101. int price;
102. if(currentMonth >= HIGH_SEASON_START && currentMonth <= HIGH_SEASON_END)
103. price = highSeasonPrice * quantity;
104. else price = this.price * quantity;
105. return moreThanFiveThousand(price)?(price - 100): price;
106. }
107. public int getQuantity() {
108. return quantity;
109. }
110.
111. private boolean moreThanFiveThousand(int price) {
112. return price > 5000;
113. }
114.}
115.
116.class Transaction {
117. private Vector<Item> items = new Vector<>();
118. private int month;
119. private int year;

COMP6284 – Code Reengineering


120. public Transaction(int month, int year) {
121. this.month= month;
122. this.year = year;
123. }
124. public Vector<Item> getItems() {
125. return items;
126. }
127. public int getMonth() {
128. return month;
129. }
130. public int getYear() {
131. return year;
132. }
133. public void addItem(Item item) {
134. items.add(item);
135. }
136. public int getTotalPrice(){
137. int total = 0;
138. for(Item e:items) {
139. total += e.getPrice(this.month, e.getQuantity());
140. }
141. return total;
142. }
143.}
144.
145.class Month {
146. private String[] months =
{"January","February","March","April","May","June","July","August","September","October","November","Dec
ember"};
147. protected int month;
148. public String getText() {
149. return months[month - 1];
150. }
151.}
152.
153.class Report extends Month {
154. private Vector<Transaction> transactions = new Vector<>();
155. public void AddTransaction(Transaction tran){
156. transactions.add(tran);
157. }
158.
159. public int getTotalFor(int monthStart, int yearStart, int monthEnd, int yearEnd){
160. int sum = 0;
161. for(Transaction t: transactions)
162. if( (yearStart*100+monthStart) <= (t.getYear()*100+t.getMonth()) &&
(t.getYear()*100+t.getMonth()) <= (yearEnd*100+monthEnd) )
163. sum += t.getTotalPrice();
164. return sum;
165. }
166.
167. public void printTransaction(int monthStart, int yearStart, int monthEnd, int yearEnd) {
168.
169.
170. System.out.println("=================================================");
171. month = monthStart;
172. System.out.print("Report from " + getText() + " " + yearStart + " to ");
173. month = monthEnd;
174. System.out.println(getText() + " " + yearEnd);
175. System.out.println("=================================================");
176. int total = 0;
177. for(int year=yearStart; year<=yearEnd; year++) {
178. int me = year < yearEnd ? 12 : monthEnd;
179. int ms = year == yearStart ? monthStart : 1;
180. for(int m=ms; m<=me; m++) {
181. month = m;

COMP6284 – Code Reengineering


182. System.out.format("Year %d Month %-10s : %,15d\n", year, getText(),
getTotalFor(m, year, m, year));
183. total += getTotalFor(m, year, m, year);
184. }
185. }
186. System.out.println("=================================================");
187. System.out.format("Total Transaction = %,15d\n", total);
188. System.out.println("=================================================");
189. }
190. }

COMP6284 – Code Reengineering

You might also like