🌙 DARK

More Inhertiance - Mutilevel Inheritance Java

 

More Inhertiance - Mutilevel Inheritance

Lets explore a scenario in our banking application. Well again, Creditcards, Many types of creditcards are available and most of them have the some set of shared properties and some specific, and many a times due to many types of cards, they are grouped under various levels for a better understanding to pick cards for a purpose. Lets implement a simple multi-level hierarchy as given below.

 

Follow

Follow MrProgrammer89 


CreditCard

     ---- RewardsCreditCard  

     ---- TravelCreditCard  

                  ---- InternationalTravelCreditCard

                  ---- CountryTravelCreditCard

 

Read the creditcard and travel details from user then calculate the travel amount by using of multilevel inheritance. If the user choose the creditcard type which is not mentioned in list means then display "Invalid Card Type".

Create a class CreditCard with following private data members.

  

Data Type

Variable Name

String

number

String

holderName

Double

amount

 

Use appropriate Getters Setters for CreditCard class.

 

 

Create the class TravelCreditcard which extends the class CreditCard with following  private data member

Data Type

Variable Name

Double

exchangePercentage

 

Use appropriate Getters Setters for TravelCreditcard class.

 

Create the class RewardsCreditCard which extends the class CreditCard with following  private data member

Data Type

Variable Name

Double

creditPoints

 

Methods in class RewardsCreditCard

Method Name

Function

Return Type

calculateAmount(Double amount,Integer numberOfPersons)

Use creditPoints percentage to calculate the payment amount

where user get a discount of

5% of credit points on every ticket.

Double

 

 

Create the class InternationalCard which extends the class TravelCreditcard

Method Name

Function

Return Type

calculateAmount(Double amount,Integer numberOfPersons)

Use exchange

Percentage to calculate the payment amount

where user get a discount of

10% of amount on every ticket.

Double

 

Create the class CountryCard which extends the class TravelCreditcard

Method Name

Function

Return Type

calculateAmount(Double amount,Integer numberOfPersons)

Use exchange

Percentage to calculate the payment amount

where user get a discount of

10% of amount on every ticket.

Double

 

Use Appropriate Getters Setters for the above classes.

  

Create a driver class named Main which creates an instance of the above mentioned classes.

 

Sample Input and Output 1:

[All text in bold corresponds to input and the rest corresponds to output.]

 

Enter the travel details

Travel Place

Banglore

Number of tickets

2

Cost per ticket

1500

1)Travel Creditcard

2)RewardCreditcard

Enter credit card type

1

1)International

2)National

Enter travel creditcard type

1

Enter the creditcard number

123456794

Enter the creditcard holdername

Praveen

Enter the available amount

65255

Hello Praveen, You have to pay Rs2700.0

  

Sample Input and Output 2:

 

Enter the travel details

Travel Place

Chennai

Number of tickets

20

Cost per ticket

150

1)Travel Creditcard

2)RewardCreditcard

Enter credit card type

3

Invalid Card Type

 

Sample Input and Output 3:

Enter the travel details

Travel Place


Mumbai

Number of tickets

3


Cost per ticket

1000


1)Travel Creditcard

2)RewardCreditcard

Enter credit card type

2


Enter the creditcard number

465879132


Enter the creditcard holdername

Rajesh 

Enter the available amount 

70000


Enter the available rewards

60

Hello Jimesh, You have to pay Rs2991.0 




@MRProgrammer89


CREATE CLASS < CreditCard :


package Multilevel_Inheritance;


public class CreditCard {


private String number,holderName;

private double amount;

public String getNumber() {

return number;

}

public void setNumber(String number) {

this.number = number;

}

public String getHolderName() {

return holderName;

}

public void setHolderName(String holderName) {

this.holderName = holderName;

}

public double getAmount() {

return amount;

}

public void setAmount(double amount) {

this.amount = amount;

}

}






@MRProgrammer89


CREATE CLASS < RewardsCreditCard :


package Multilevel_Inheritance;

public class RewardsCreditCard extends CreditCard{
private double creditPoints;

public double getCreditPoints() {
return creditPoints;
}

public void setCreditPoints(double creditPoints) {
this.creditPoints = creditPoints;
}
public double calculateAmount(Double amount,Integer numberOfPersons){
//double j = (amount/5)*numberOfPersons;
double pay = (amount * numberOfPersons*0.997);
return pay;
}

}

@MRProgrammer89 


CREATE CLASS < TravelCreditCard :



package Multilevel_Inheritance;

public class TravelCreditcard extends CreditCard {
private double exchangePercentage;

public double getExchangePercentage() {
return exchangePercentage;
}

public void setExchangePercentage(double exchangePercentage) {
this.exchangePercentage = exchangePercentage;
}


}




@MRProgrammer89 


CREATE CLASS < CountryCard :



package Multilevel_Inheritance;

public class CountryCard extends TravelCreditcard {
double calculateAmount(Double amount,Integer numberOfPersons){
double j = (amount/10)*numberOfPersons;
double jay = (amount * numberOfPersons)-j;
return jay;
}

}



@MRProgrammer89 


CREATE CLASS < InternationalCard :



package Multilevel_Inheritance;

public class InternationalCard extends TravelCreditcard{
double calculateAmount(Double amount,Integer numberOfPersons){
Double ttl = amount*numberOfPersons;
Double pay = ttl*((ttl/10));
return pay;
}


}



@MRProgrammer89 


CREATE CLASS < Main :


package Multilevel_Inheritance;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
CreditCard obj = new CreditCard();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the travel details");
System.out.println("Travel Place");
String place = sc.nextLine();
System.out.println("Number of tickets");
int noTick = sc.nextInt();
System.out.println("Cost per ticket");
double coPerTick = sc.nextDouble();

System.out.println("1)Travel Creditcard\n2)Reward Creditcard\nEnter credit card type");
int CardType = sc.nextInt();
if(CardType==1) {
System.out.println("1)International\n2)National\nEnter travel creditcard type");
int TravelType = sc.nextInt();
if(TravelType==1) {
CountryCard ccobj = new CountryCard();
Double payAmt = ccobj.calculateAmount(coPerTick, noTick);
System.out.println("Enter the creditcard number");
int cardno = sc.nextInt();
System.out.println("Enter the creditcard holdername");
String name = sc.next();
obj.setHolderName(name);
System.out.println("Enter the available amount");
double amount = sc.nextDouble();
System.out.println("Hello "+obj.getHolderName()+","+" You have to pay Rs"+String.format("%.1f",payAmt));
}
        
}
else if(CardType==2) {
CreditCard obj1 = new CreditCard();
InternationalCard icobj = new InternationalCard();
Double payAmt = icobj.calculateAmount(coPerTick, noTick);
System.out.println("Enter the creditcard number");
String cardno = sc.next();
System.out.println("Enter the creditcard holdername");
String name = sc.next();
obj1.setHolderName(name);
System.out.println("Enter the available amount");
double amt = sc.nextDouble();
System.out.println("Enter the available rewards");
double reward = sc.nextDouble();
RewardsCreditCard rcc = new RewardsCreditCard();
rcc.setCreditPoints(reward);
Double payAmt1 = rcc.calculateAmount(coPerTick, noTick);
System.out.println("Hello "+obj.getHolderName()+","+" You have to pay Rs"+String.format("%.1f",payAmt1));
}
else {
System.out.println("Invalid Card Type");
}
}
}



@MRProgrammer89 






MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊


Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form