Java Shipping Logic and Software Architecture

Shipping Cost Calculation Logic

The following rules define the shipping cost based on weight:

  • Return -1 if the weight is less than 0 or greater than 100 kg.
  • Return 10.00 if the weight is between 0 and 10 kg.
  • Return 20.00 if the weight is between 10.01 and 20 kg.
  • Return 50.00 if the weight is between 20.01 and 50 kg.
  • Return 100.00 if the weight is between 50.01 and 100 kg.

Java Method Implementation

public static double calculateShippingCost(double weight) {
    if (weight < 0 || weight > 100) return -1;
    if (weight <= 10) return 10.00;
    if (weight <= 20) return 20.00;
    if (weight <= 50) return 50.00;
    return 100.00;
}

Equivalence Partitions

  • Invalid low: weight < 0 → -1
  • Valid 1: 0 to 10 → 10.00
  • Valid 2: 10.01 to 20 → 20.00
  • Valid 3: 20.01 to 50 → 50.00
  • Valid 4: 50.01 to 100 → 100.00
  • Invalid high: weight > 100 → -1

Boundary Values

-1, 0, 10, 10.01, 20, 20.01, 50, 50.01, 100, 100.01

System Interaction Scenario

Scenario: User logs in through Authentication. User searches for a product using the SearchEngine. SearchEngine checks Inventory through SearchInventory. User adds a product to the ShoppingCart. ShoppingCart sends the order through ManageOrders to Orders. Orders checks/updates Inventory, manages customer data through Customers, and handles account/payment details through Accounts.

JUnit Test Suite

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class ShippingCalculatorTest {
    ShippingCalculator sc = new ShippingCalculator();

    @Test
    public void testInvalidLow() {
        assertEquals(-1, sc.calculateShippingCost(-1));
    }

    @Test
    public void test0To10() {
        assertEquals(10.00, sc.calculateShippingCost(0));
        assertEquals(10.00, sc.calculateShippingCost(10));
    }

    @Test
    public void test10To20() {
        assertEquals(20.00, sc.calculateShippingCost(10.01));
        assertEquals(20.00, sc.calculateShippingCost(20));
    }

    @Test
    public void test20To50() {
        assertEquals(50.00, sc.calculateShippingCost(20.01));
        assertEquals(50.00, sc.calculateShippingCost(50));
    }

    @Test
    public void test50To100() {
        assertEquals(100.00, sc.calculateShippingCost(50.01));
        assertEquals(100.00, sc.calculateShippingCost(100));
    }

    @Test
    public void testInvalidHigh() {
        assertEquals(-1, sc.calculateShippingCost(100.01));
    }
}

Model-Driven Engineering Implementation

Question: Your team proposes using model-driven engineering to develop a new system. What factors should you consider before introducing this approach?

Answer: Model-driven engineering (MDE) means using models as the main artifacts in software development, sometimes with code generated from these models. Before using it, the team should consider the complexity of the system. It is more useful for large or complex systems, but for a small, simple system, it may add unnecessary work.

The team should also consider whether the requirements are stable. If requirements change often, the models must be updated many times, which can slow development. Another important factor is the skills of the developers, because they need to understand UML, modeling tools, and possibly code generation.

Tool support and cost are also important. The team needs reliable tools for modeling, code generation, debugging, and maintenance. Finally, the team should check the quality of the generated code and whether it is easy to test and maintain.

Limitations of Software Testing

Question: Explain why testing can only detect the presence of errors, not their absence.

Answer: Testing means running software with selected test cases to check its behavior and find defects. A failed test proves that an error exists because the actual result is different from the expected result. However, passing tests does not prove that the program has no errors.

This is because it is impossible to test every possible input, every user action, every system state, every environment, and every combination of features. The program may work correctly for the tested cases but still fail in an untested case. For example, a login system may pass tests for valid and invalid passwords, but it may still fail with very long inputs, special characters, or empty spaces.

Component Diagram Architecture

Subsystems and Roles

  • WebStore: User-facing online shopping subsystem. Components include SearchEngine (handles product search), ShoppingCart (manages selected products), and Authentication (manages user sessions/login).
  • Warehouses: Inventory management subsystem. Component includes Inventory (checks and updates product stock).
  • Accounting: Manages orders, customers, and accounts. Components include Orders (creates/manages orders), Customers (manages customer information), and Accounts (manages payment/account details).

System Interfaces

  • ProductSearch: Connects users to SearchEngine.
  • OnlineShopping: Connects users to ShoppingCart.
  • UserSession: Connects users to Authentication.
  • SearchInventory: Connects SearchEngine to Inventory.
  • ManageOrders: Connects ShoppingCart to Orders.
  • ManageCustomers: Connects Authentication/Orders to Customers.
  • ManageAccounts: Connects Customers to Accounts.
  • ManageInventory: Connects Orders/Warehouse to Inventory.

Interaction Scenario

Scenario: User logs in through Authentication. User searches for a product using the SearchEngine. SearchEngine checks Inventory through SearchInventory. User adds a product to the ShoppingCart. ShoppingCart sends the order through ManageOrders to Orders. Orders checks/updates Inventory, manages customer data through Customers, and handles account/payment details through Accounts.

Software Architecture and Design Patterns FAQ

  • Which statement about layered architecture is correct?
    Answer: Each layer should depend only on the layer immediately below it.
  • Language processing systems are primarily used for which purpose?
    Answer: Interpreting users’ intentions specified in a formal language.
  • Which is NOT a common architectural pattern?
    Answer: Observer.
  • Which statement correctly distinguishes MVC, MVP, and MVVM?
    Answer: MVC: Controller handles input. MVP: Presenter mediates. MVVM: ViewModel binds Model/View.
  • Which best illustrates separation of concerns?
    Answer: Splitting UI, business logic, and data access into separate layers/modules.
  • Which statement about cross-cutting concerns is correct?
    Answer: Logging, security, and performance affect multiple layers/modules.
  • Which is NOT a guideline for layered architecture?
    Answer: Higher layers directly access any lower layer to improve performance.
  • Transaction processing applications are mainly characterized by what?
    Answer: Processing user requests and updating a system database.
  • What is the primary goal of software inspections?
    Answer: Detect defects early and ensure code follows standards.
  • What is the relationship between inspections and testing?
    Answer: They are complementary Verification and Validation (V&V) techniques and should be used together.
  • How are design patterns organized by purpose?
    Answer: Creational, Structural, Behavioral.
  • What is the primary focus of the abstraction level in software reuse?
    Answer: Reusing knowledge of successful abstractions.
  • What is a design pattern in software engineering?
    Answer: Reusable abstract knowledge about a problem and its solution.
  • In N-tier architecture, what does “tier” refer to?
    Answer: A specific function or service layer.
  • Which architecture is a single self-contained application?
    Answer: Monolithic architecture.
  • What is an example of software reuse at the component level?
    Answer: Datepicker.
  • Which design pattern category focuses on communication between objects?
    Answer: Behavioral patterns.
  • When should unit tests be written in the development process?
    Answer: As the code is being developed.
  • Which organization criterion determines whether a pattern applies to classes or objects?
    Answer: Scope.
  • What is the difference between closed and open layered architecture?
    Answer: Closed = only layer directly below. Open = can skip to lower layers.