Content ITV PRO
This is Itvedant Content department
Calculating Fare prices
Business Scenario
Previously, we learned Inheritance and Polymorphism by extending airline services, enabling code reusability and flexible behavior.
Welcome back,Developers!
Pre-Lab Preparation
Before starting this experiment, students should be familiar with:
Relational Operators and Logical Operators
Defining conditions using relational operators
Conditional statements - if, if-else, nesting, switch-case
git pull origin branchNameGit Pull
Imagine you are working in an airline company.
There are many flights:
Some go within India → Domestic
Some go outside India → International
All flights have:
Flight Number
Source
Destination
Base Fare
But ; Fare calculation is different for each type
So we say:
“Flight is a general idea — we don’t know how fare is calculated.”
That’s why:
Flight = Abstract Class
Add abstract keyword to flight class which you have created
Earlier, we were writing calculateFare() inside the same class:
Fare calculation is different for each flight type
But here we are forcing same logic for all flights
This is wrong design
Solution
“We don’t know how fare is calculated for every flight.”
So we make it abstract
“Flight knows that fare must be calculated…but it doesn’t know HOW.”
“The parent defines WHAT to do, the child defines HOW to do.”
Override in Child Classes
Domestic Flight
Calculates fare with domestic charges
Explains fare clearly to user
International Flight
Calculates fare with international charges
Gives detailed breakdown
“Both flights are doing the same job — calculateFare()
but each one explains it in their own way.”
“We created flights… but where will we store them?”
In array
In memory
Suppose Airline company hires a manager to handle all flight records.”
This manager:
Adds flights
Shows flights
Searches flights
Deletes flights
“Company defines rules for manager — what work must be done.”
addFlight()
showAllFlights()
searchFlight()
deleteFlight()
“Interface defines WHAT to do, not HOW to do”
1
Define Rules (Interface)
Create interface
Add abstract methods:
Airline company tells manager:
“You MUST do these 4 tasks… but how you do it is your choice.”
Now create FlightDAOImpl class
Inherit to FlightDAO interface by using implements keyword
Override all methods
Create array and declare index variable which is initialized to zero
Add Logic for add Flight
Add Logic for show all flights
Add logic for search all flights
Add logic for delete flight
Manager says:
“I will store flights in an array and manage them.”
Output :
By Content ITV