Tugas Pertemuan 10 - 5025231186

Agym Kamil Ramadhan

5025231186


Membuat Program Unit Testing Tentang Sales Item

Saya telah membuat program unit testing untuk Sales Item dengan menggunakan Java dan konsep OOP. Mari saya jelaskan struktur dan fitur dari kode yang telah saya buat:

Kelas SalesItem

  1. Atribut:
    • itemName: Nama barang
    • price: Harga barang
    • quantity: Jumlah barang
    • discount: Diskon dalam persentase
  2. Metode Utama:
    • calculateTotalPrice(): Menghitung total harga dengan mempertimbangkan diskon
    • getItemDescription(): Mendapatkan deskripsi lengkap item
  3. Validasi:
    • Harga tidak boleh negatif
    • Kuantitas tidak boleh negatif
    • Diskon harus antara 0-100%






SalesItem.Java

public class SalesItem {
private String itemName;
private double price;
private int quantity;
private double discount;

// Konstruktor
public SalesItem(String itemName, double price, int quantity) {
this.itemName = itemName;
this.price = price;
this.quantity = quantity;
this.discount = 0.0;
}

// Getter dan Setter
public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
if (price < 0) {
throw new IllegalArgumentException("Harga tidak boleh negatif");
}
this.price = price;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
if (quantity < 0) {
throw new IllegalArgumentException("Kuantitas tidak boleh negatif");
}
this.quantity = quantity;
}

public double getDiscount() {
return discount;
}

public void setDiscount(double discount) {
if (discount < 0 || discount > 100) {
throw new IllegalArgumentException("Diskon harus antara 0 dan 100");
}
this.discount = discount;
}

// Metode untuk menghitung total harga
public double calculateTotalPrice() {
double discountedPrice = price * (1 - (discount / 100));
return discountedPrice * quantity;
}

// Metode untuk mendapatkan deskripsi item
public String getItemDescription() {
return String.format("%s - Rp %.2f x %d (Diskon: %.2f%%)",
itemName, price, quantity, discount);
}
}


SalesItemTest.Java

public class SalesItemTest {
// Metode untuk melakukan assert (pengecekan)
private static void assertEquals(Object expected, Object actual, String message) {
if (!expected.equals(actual)) {
throw new AssertionError(message +
". Expected: " + expected + ", Actual: " + actual);
}
}

private static void assertEquals(double expected, double actual, double delta, String message) {
if (Math.abs(expected - actual) > delta) {
throw new AssertionError(message +
". Expected: " + expected + ", Actual: " + actual);
}
}

private static void assertThrows(Class<? extends Exception> exceptionClass, Runnable executable, String message) {
try {
executable.run();
throw new AssertionError(message + ". No exception thrown");
} catch (Exception e) {
if (!exceptionClass.isInstance(e)) {
throw new AssertionError(message +
". Expected: " + exceptionClass.getName() +
", Actual: " + e.getClass().getName());
}
}
}

// Metode untuk menjalankan semua test
public static void runTests() {
int totalTests = 0;
int passedTests = 0;

try {
testCreateSalesItem();
totalTests++;
passedTests++;
System.out.println("Test Create Sales Item: PASSED");
} catch (AssertionError e) {
System.out.println("Test Create Sales Item: FAILED - " + e.getMessage());
}

try {
testSetPrice();
totalTests++;
passedTests++;
System.out.println("Test Set Price: PASSED");
} catch (AssertionError e) {
System.out.println("Test Set Price: FAILED - " + e.getMessage());
}

try {
testSetQuantity();
totalTests++;
passedTests++;
System.out.println("Test Set Quantity: PASSED");
} catch (AssertionError e) {
System.out.println("Test Set Quantity: FAILED - " + e.getMessage());
}

try {
testSetDiscount();
totalTests++;
passedTests++;
System.out.println("Test Set Discount: PASSED");
} catch (AssertionError e) {
System.out.println("Test Set Discount: FAILED - " + e.getMessage());
}

try {
testCalculateTotalPrice();
totalTests++;
passedTests++;
System.out.println("Test Calculate Total Price: PASSED");
} catch (AssertionError e) {
System.out.println("Test Calculate Total Price: FAILED - " + e.getMessage());
}

try {
testGetItemDescription();
totalTests++;
passedTests++;
System.out.println("Test Get Item Description: PASSED");
} catch (AssertionError e) {
System.out.println("Test Get Item Description: FAILED - " + e.getMessage());
}

// Tampilkan ringkasan hasil test
System.out.println("\nTotal Tests: " + totalTests);
System.out.println("Passed Tests: " + passedTests);
System.out.println("Failed Tests: " + (totalTests - passedTests));
}

// Test Case 1: Membuat Sales Item
public static void testCreateSalesItem() {
SalesItem item = new SalesItem("Laptop", 5000000.0, 2);

assertEquals("Laptop", item.getItemName(), "Nama Item");
assertEquals(5000000.0, item.getPrice(), 0.001, "Harga Item");
assertEquals(2, item.getQuantity(), "Kuantitas Item");
assertEquals(0.0, item.getDiscount(), 0.001, "Diskon Awal");
}

// Test Case 2: Pengaturan Harga
public static void testSetPrice() {
SalesItem item = new SalesItem("Smartphone", 3000000.0, 1);
item.setPrice(3500000.0);
assertEquals(3500000.0, item.getPrice(), 0.001, "Perubahan Harga");

// Test exception untuk harga negatif
assertThrows(IllegalArgumentException.class, () -> {
item.setPrice(-1000.0);
}, "Harga Negatif");
}

// Test Case 3: Pengaturan Kuantitas
public static void testSetQuantity() {
SalesItem item = new SalesItem("Tablet", 2000000.0, 3);
item.setQuantity(5);
assertEquals(5, item.getQuantity(), "Perubahan Kuantitas");

// Test exception untuk kuantitas negatif
assertThrows(IllegalArgumentException.class, () -> {
item.setQuantity(-1);
}, "Kuantitas Negatif");
}

// Test Case 4: Pengaturan Diskon
public static void testSetDiscount() {
SalesItem item = new SalesItem("Headphone", 500000.0, 1);
item.setDiscount(10.0);
assertEquals(10.0, item.getDiscount(), 0.001, "Perubahan Diskon");

// Test exception untuk diskon negatif
assertThrows(IllegalArgumentException.class, () -> {
item.setDiscount(-5.0);
}, "Diskon Negatif");

// Test exception untuk diskon melebihi 100%
assertThrows(IllegalArgumentException.class, () -> {
item.setDiscount(110.0);
}, "Diskon Melebihi 100%");
}

// Test Case 5: Kalkulasi Total Harga
public static void testCalculateTotalPrice() {
SalesItem item = new SalesItem("Camera", 7000000.0, 2);
item.setDiscount(15.0);

// Perhitungan manual:
// Harga asli: 7.000.000
// Diskon 15%: 7.000.000 * 0.85 = 5.950.000
// Total untuk 2 item: 5.950.000 * 2 = 11.900.000
assertEquals(11900000.0, item.calculateTotalPrice(), 0.001, "Kalkulasi Total Harga dengan Diskon");
}

// Test Case 6: Deskripsi Item
public static void testGetItemDescription() {
SalesItem item = new SalesItem("Monitor", 2500000.0, 3);
item.setDiscount(5.0);

String expectedDescription = "Monitor - Rp 2500000.00 x 3 (Diskon: 5.00%)";
assertEquals(expectedDescription, item.getItemDescription(), "Deskripsi Item");
}

// Metode main untuk menjalankan test
public static void main(String[] args) {
runTests();
}
}


Komentar

Postingan populer dari blog ini

Pemrograman Berorientasi Objek A - ETS

Tugas Pertemuan 3 - 5025231186

Tugas Pertemuan 4 - 5025231186