Abstract - More Practice

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

INTERFACES Analyse

Abstract - More Practice


 
Let's retry the abstract example in our CreditCard hierarchy. On every transaction, you get reward points &
cashback based on the transaction. In a TravelCreditCard, you get 10 dollars cashback for every 1000
dollars whereas in a RewardsCreditCard you get 100 reward points for every 1000 dollars.

1. Create a class CreditCard, RewardsCreditCard & TravelCreditCard based on the specification given
below
2. Have a method redeemGifts() in all three Card classes
3. Mark the method as abstract in CreditCard and inturn mark the class abstract as well 
4. As mentioned above implement the logic in the other two classes.

Create an abstract class CreditCard with 3 private attributes


Datatype Attribute
String accountNumber
Double transactionAmount
String holderName

Include the following method in the class CreditCard


Method Name Function
redeemGifts() Abstract method

Create a class TravelCreditCard which extends the class CreditCard and implements the method called


redeemGifts().

Create a class RewardsCreditCard which extends the class CreditCard and implements the method


called redeemGifts() .

Use Appropriate Getters Setters for the above classes.

Create a driver class named Main which creates an instance of the above-mentioned classes. Cash backs
and reward points must be calculated separately for all the base classes. If Cashbacks and reward points
are not more than 1000, display the account holder name and account number

Input and Output format:


Refer to sample Input and Output for formatting specifications. 

Note: All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:

Enter the Card Type


1.Travel Credit Card
2.Reward Credit Card
1
Enter the Holder Name
Madhu
Enter the Account Number
45612FG
Enter the tranaction amount
5000
Amount Paid Successfully !!!
Holder Name :Madhu
Account Number :45612FG
Your have won 50.0 dollars cashbacks

Sample Input and Output 2:

Enter the Card Type


1.Travel Credit Card
2.Reward Credit Card
1
Enter the Holder Name
Anand
Enter the Account Number
23456SDA
Enter the tranaction amount
500
Amount Paid Successfully !!!
Holder Name :Anand
Account Number :23456SDA

Sample Input and Output 3:

Enter the Card Type


1.Travel Credit Card
2.Reward Credit Card
3
Invalid Card type

Sample Input and Output 4:

Enter the Card Type


1.Travel Credit Card
2.Reward Credit Card
2
Enter the Holder Name
Kalai
Enter the Account Number
457845DSF
Enter the tranaction amount
45000
Amount Paid Successfully !!!
Holder Name :Kalai
Account Number :457845DSF
Your have scored 4500.0 reward points

//Credit Card
abstract class CreditCard {
private String accountNumber;
private Double transactionAmount;
private String holderName;

abstract public void redeemGifts();

public void setAccountNumber(String accountNumber) {


this.accountNumber=accountNumber;
}
public String getAccountNumber()
{
return this.accountNumber;
}

public void setHolderName(String holderName)


{
this.holderName=holderName;
}
public String getHolderName()
{
return this.holderName;
}

public void setTransactionAmount(Double transactionAmount)


{
this.transactionAmount=transactionAmount;
}
public Double getTransactionAmount()
{
return this.transactionAmount;
}
}
//TravelCreditCard
class TravelCreditCard extends CreditCard
{
public void redeemGifts() {
if(super.getTransactionAmount()>=1000)
{
double n=super.getTransactionAmount()/100;
double n1=(Double)(n*1);
System.out.println("You have won "+String.format("%.1f",n1)+" dollars cashbacks");
}
}
}

//RewardsCreditCard
class RewardsCreditCard extends CreditCard {
public void redeemGifts() {
if(super.getTransactionAmount()>=1000)
{
double n2=(super.getTransactionAmount())/100;
double n3=(Double)(n2*10);
System.out.println("You have scored "+String.format("%.1f",n3)+" reward points");
}
}
}

//Main
class Main {
public staic void main(String[] args) {

}
}

You might also like