Interface Exercise 1| Programming Using Java | Infosys Springboard
Introduction to Interface Exercise 1
Overview of the Problem Statement
- Shreyas introduces the video, focusing on solving an interface exercise related to tax calculations for an e-commerce company.
- The task involves implementing an application that calculates taxes based on customer purchases and seller locations, as per a provided class diagram.
Understanding the Tax Interface
- The problem includes a
Taxinterface with two implementing classes:PurchaseDetailsandSeller.
- The
calculateTaxmethod is central to both classes, which will determine tax rates based on payment types and seller locations.
Implementation Steps in Eclipse IDE
Creating the PurchaseDetails Class
- In the
PurchaseDetailsclass, three variables are declared:purchaseId,paymentType, andtaxPercentage.
- A parameterized constructor initializes these variables. Getter and setter methods are also generated for encapsulation.
Tax Calculation Logic for Purchases
- The logic in the
calculateTaxmethod sets tax percentages based on payment type:
- Debit card: 2%
- Credit card: 3%
- Other payment methods: 4%
- The method returns the total amount after applying tax using the formula: price + (price * taxPercentage / 100).
Creating the Seller Class
- Similar to
PurchaseDetails, theSellerclass has two variables:locationandtaxPercentage.
- A parameterized constructor initializes these variables, followed by getter and setter methods.
Tax Calculation Logic for Sellers
Implementing Location-Based Tax Rates
- In the seller's
calculateTaxmethod, different tax percentages are assigned based on location:
- Canada: 22%
- Middle East: 15%
- Japan: 12%
- Europe: 25%
Finalizing Implementation
- Both classes return amounts after applying their respective taxes using similar formulas as in purchase calculations.
Testing the Implementation
Running Test Cases
- A tester class creates objects of both
PurchaseDetailsandSeller, invoking their respective calculate methods.
- [] (No specific timestamp provided here; general execution note.) Results are printed with two decimal precision to verify correctness before running test cases online.