008-003-012

Order Processing via Method Call Chain

Hard

Problem Description

Order Processing via Method Call Chain

In this problem: You will define multiple methods in an OrderProcessor class where processOrder() calls validateOrder(), calculateTotal(), applyDiscount(), and generateReceipt() in sequence. Each method displays intermediate results to standard output.

Learning Objective: Understand how methods call other methods (indirect calls) and learn to design by splitting and combining processing steps

Overview

Dividing a process into multiple small methods and calling them in sequence improves code readability and maintainability. Each method having a single responsibility is the foundation of good design.

Specifications

OrderProcessor Class

  • processOrder(String item, int quantity, int unitPrice) : Controls overall processing. Calls the other 4 methods in sequence
  • validateOrder(String item, int quantity) : Validates order and outputs Validating: [item] x [quantity]. Returns true
  • calculateTotal(int quantity, int unitPrice) : Calculates total and outputs Subtotal: [total]. Returns total
  • applyDiscount(int total) : Applies 10% discount and outputs Discount: -[discountAmount]. Returns discounted total
  • generateReceipt(String item, int finalTotal) : Outputs Receipt: [item] = [finalTotal]

Main Class

  • Create OrderProcessor and call processOrder("Laptop", 2, 1000)

Output Format

Validating: Laptop x 2
Subtotal: 2000
Discount: -200
Receipt: Laptop = 1800

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up