0% found this document useful (0 votes)
14 views7 pages

Pe SWT

The document outlines various defects in a Java program, including issues with variable declarations, null pointer dereferencing, and exception handling. It also includes a class for calculating order totals with discounts based on customer type and a series of JUnit tests to validate the functionality of the OrderCalculator class. Additionally, it presents test cases for a receipt creation module, detailing conditions for successful and failed transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Pe SWT

The document outlines various defects in a Java program, including issues with variable declarations, null pointer dereferencing, and exception handling. It also includes a class for calculating order totals with discounts based on customer type and a series of JUnit tests to validate the functionality of the OrderCalculator class. Additionally, it presents test cases for a receipt creation module, detailing conditions for successful and failed transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Question1:

Defect Line of
Defect Name Defect Description Fixing Solutio
ID Code
Not import installize BufferedReader but not import
1 2 import java.io.Buffere
java.io.BufferedReader java.io.BufferedReader
The variable n is declared outside the try try{
Variable Declaration
2 15 block, which is fine, but it's not initialized String line;
Outside Try Block
before being used within the try block. }
Dereferencing null pointer if(filePath != nul
3 Dereferencing null pointer 7
filePath.isEmpty() filePath.isEmpt
4 Variable unused 6 Unused variable reader delete line code install
public void readFile(
5 Not throw exception 31 Unreported exception IOException
IOException

Question 2:

// Main Code

package main;

public class OrderCalculator {

public OrderCalculator() {

public double calculateTotalPrice(double[] itemPrices, String customerType, boolean isVIP, String


discountCode){

if(itemPrices == null || itemPrices.length == 0){

throw new IllegalArgumentException("No item in order.");

double totalPrice = 0.0;

for(double price: itemPrices){

if(price <= 0){

throw new IllegalArgumentException("Item price must be greater than zero.");


}

totalPrice +=price;

double discount = 0.0;

if(isVIP){

discount = 0.20;

}else if(customerType.equalsIgnoreCase("Regular")){

discount = 0.05;

if(discountCode != null && !discountCode.isEmpty()){

if(discountCode.equals("SALE10")){

discount += 0.10;

}else if(discountCode.equals("WELCOME5")){

discount += 0.05;

double finalPrice = totalPrice * (1 - discount);

return finalPrice < 0 ? 0 : finalPrice;

// Test Code

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/JUnit4TestClass.java to edit this template

*/

package main;
import org.junit.Test;

import static org.junit.Assert.*;

/**

* @author PC

*/

public class OrderCalculatorTest {

public OrderCalculatorTest() {

@Test

public void testOrderCalculator() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};

String type = "Regular";

boolean isVip = true;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test(expected = IllegalArgumentException.class)

public void testItemPriceNull() {

OrderCalculator o = new OrderCalculator();

double [] item = null;

String type = "Regular";

boolean isVip = true;


String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test(expected = IllegalArgumentException.class)

public void testPriceLessThanZero() {

OrderCalculator o = new OrderCalculator();

double [] item = {-1, 20};

String type = "Regular";

boolean isVip = true;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testViptrue() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};

String type = "Regular";

boolean isVip = true;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testVipfalse() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};

String type = "Regular";


boolean isVip = false;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testRegularCustomerDiscountCodeNull() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};

String type = "Regular";

boolean isVip = false;

String discountCode = null;

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testVIPCustomerDiscountCodeNull() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};

String type = "Regular";

boolean isVip = true;

String discountCode = null;

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testCustomerTypeNull() {

OrderCalculator o = new OrderCalculator();

double [] item = {20, 30};


String type = null;

boolean isVip = false;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

@Test

public void testFinalPriceLessThanZero() {

OrderCalculator o = new OrderCalculator();

double [] item = {0};

String type = null;

boolean isVip = true;

String discountCode = "SALE10";

double rs = o.calculateTotalPrice(item, type, isVip, discountCode);

Question3:

Test case
Pass: 5
Modul Create new receipt Fail: 0
e
Code
Tester Percent Complete: 20%

ID Test Case Description Pre -Condition Test Case Procedure


TC001 Test Order Success with Standard The customer must be 1.The customer adds food items to their
logged into the system. cart
The shopping cart must 2. The customer process to checkout
have at least one item. 3. The customer check the total amount
4. The customer select credit card
payment
5. The customer enter valid credit card
details(eg valid number card, expiry
date, CVV)
6. The System process the payment
7. The System confirm the order and
generates an order confirmation
TC002 Test Order Success with valid Discount The customer must be 1.The customer adds food items to their
logged into the system. cart
The shopping cart must 2.The customer enter valid discount
have at least one item. code "SALE10" to their order
3.The system check valid discount code
4. The customer process to checkout
5. The customer check the total amount
6. The customer select credit card
payment
7. The customer enter valid credit card
details(eg valid number card, expiry
date, CVV)
8. The System process the payment
9. The System confirm the order and
generates an order confirmation
TC003 Test Order with invalid Discount The customer must be 1.The customer adds food items to their
logged into the system. cart
The shopping cart must 2.The customer enter Invalid discount
have at least one item. code to their order
3.The system check valid discount code
4. The customer process to checkout
5. The customer check the total amount
6. The customer select credit card
payment
7. The customer enter valid credit card
details(eg valid number card, expiry
date, CVV)
8. The System process the payment
9. The System confirm the order and
generates an order confirmation
TC004 Test payment failure with Credit/ Debit cards The customer must be 1.The customer adds food items to their
logged into the system. cart
The shopping cart must 2. The customer process to checkout
have at least one item. 3. The customer check the total amount
4. The customer select credit card
payment
5. The customer enter Invalid credit card
details(eg valid number card, expiry
date, CVV)
6. The System process the payment
TC005 Test payment failure with Paypal The customer must be 1.The customer adds food items to their
logged into the system. cart
The shopping cart must 2. The customer process to checkout
have at least one item. 3. The customer check the total amount
4. The customer select Payment with
Paypal
5. The System process the payment
TC006 Test Order Success with valid Discount for new The customer must be 1.The customer sign up account
Customer logged into the system. 1.The customer adds food items to their
The shopping cart must cart
have at least one item. 2.The customer enter valid discount
code "WELCOME5" to their order
3.The system check valid discount code
4. The customer process to checkout
5. The customer check the total amount
6. The customer select credit card
payment
7. The customer enter valid credit card
details(eg valid number card, expiry
date, CVV)
8. The System process the payment
9. The System confirm the order and
generates an order confirmation

You might also like