Professional Documents
Culture Documents
1912 Comp6284 Taca TK2-W5-S7-R0 Team3 3 PDF
1912 Comp6284 Taca TK2-W5-S7-R0 Team3 3 PDF
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);
d. The Dispensable
Hal umum yang ada pada semua dispensable smell adalah adanya sesuatu yang
tidak dibutuhkan dan seharusnya dapat dihapus dari source code. Berikut adalah
contoh code smell dari kelompok the Dispensable :
e. The Coupler
The coupler merupakan kondisi dimana terdapat pelanggaran terhadap prinsip
coupling. Coupling merupakan ukuran keterikatan sebuah module dengan module
lainnya. Prinsip coupling menekankan pada penerapan low coupling, sehingga
perubahan pada satu module tidak akan (atau sedikit) mempengaruhi module
lainnya. Salah satu kondisi pelanggaran prinsip coupling adalah dimana sebuah
metode mengakses data dari object lainnya lebih sering dibandingkan data dari
dalam kelas itu sendiri. Penggunaan middle man juga merupakan indikasi
pelanggaran dari prinsip low coupling. Berikut adalah jenis jenis code smell dari
kelompok the Coupler :
Feature Envy: Sebuah method sangat bergantung pada data yang ada di class
lain, bukan di data milik class tempat method tersebut berada.
Inappropriate Intimacy: Dua class yang terlalu berhubungan erat satu sama
lainnya.
Message Chains: Untuk mendapat data, class A harus memanggil class B
yang kemudian memanggil class C lalu mengambil data dari class D;
padahal yang dibutuhkan hanya data dari class D.
Middle Man: Sebuah class yang fungsinya terlalu sederhana dan tidak
memiliki kontribusi selain hanya mendelegasikan tugasnya ke class lain.
import java.util.Random;
import java.util.Vector;
class Item {
private String name;
private int price;
private int quantity;
private int highSeasonPrice;
private final int HIGH_SEASON_START = 5;
class Transaction {
private Vector<Item> items = new Vector<>();
private int month;
private int year;
public Transaction(int month, int year) {
this.month= month;
this.year = year;
}
public Vector<Item> getItems() {
return items;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public void addItem(Item item) {
items.add(item);
}
public int getTotalPrice(){
int total = 0;
for(Item e:items) {
total += e.getPrice(this.month, e.getQuantity());
}
return total;
class Month {
private String[] months =
{"January","February","March","April","May","June","July","August","Septembe
r","October","November","December"};
protected int month;
public String getText() {
return months[month - 1];
}
}
System.out.println("=================================================");
month = monthStart;
System.out.print("Report from " + getText() + " " + yearStart + " to
");
month = monthEnd;
System.out.println(getText() + " " + yearEnd);
System.out.println("=================================================");
int total = total(monthStart, yearStart, monthEnd, yearEnd);
System.out.println("=================================================");
System.out.format("Total Transaction = %,15d\n", total);
System.out.println("=================================================");
}
https://thesolidsnake.wordpress.com/2013/05/03/belajar-mencium-bau-nya-kode-
program-code-smell/
https://www.altexsoft.com/blog/engineering/code-refactoring-best-practices-
when-and-when-not-to-do-it/
https://dzone.com/articles/smells-in-java-code-do-you-recognize-them
https://dzone.com/articles/staring-into-my-java-crystal-ball-2020
https://refactoring.guru/refactoring/smells
Eclipse – workspace
Intellij IDEA
Lecture Note