🌙 DARK

Abstract Class Java Program

Abstract Class
 
Write a program to calculate total cost of the event based on the type of event and display details using Abstract class and method.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.


Consider an abstract class called Event with following protected attributes.
 

AttributesDatatype
nameString
detailString
typeString
organiserString
 
Prototype for the parametrized constructor, Event(String name, String detail, String type, String organiser)
Include appropriate getters and setters

Include the following abstract method in the class Event.
 
Method NameDescription
abstract Double calculateAmount()an abstract method

Consider a class named Exhibition which extends Event class with the following private attributes
 
AttributesDatatype
noOfStallsInteger
 rentPerStallDouble

 
Prototype for the parametrized constructor,
public Exhibition(String name, String detail, String type, String organiser, Integer noOfStalls,Double rentPerStall)
Include appropriate getters and setters
Use super( ) to call and assign values in base class constructor.

Include the following abstract method in the class Exhibition.
 
Method NameDescription
Double calculateAmount ()This method returns the product of noOfStalls and rentPerStall

Consider a class named StageEvent which extends Event class with the following private attributes.
 
AttributeDatatype
noOfShowsInteger
costPerShowDouble

Prototype for the parametrized constructor,
public StageEvent(String name, String detail, String type, String organiser, Integer noOfShows,Double costPerShow)
Include appropriate getters and setters
Use super( ) to call and assign values in base class constructor.
 

Include the following abstract method in the class StageEvent.
 

Method Name Description
Double calculateAmount()This method returns the product of noOfShows and costPerShow

Consider a driver class called Main. In the main method, obtain input from the user and create objects accordingly.

The link to download the template code is given below
Code Template

Input format:
Input format for Exhibition is in the CSV format (name,detail,type,organiser,noOfStalls,rentPerStall)
Input format for StageEvent is in the CSV format (name,detail,type,organiser,noOfShows,costPerShow)    


Output format:
Print "Invalid choice" if the input is invalid to our application and terminate.
Display one digit after the decimal point for Double datatype.
Refer to sample Input and Output for formatting specifications.


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

Sample Input and output 1:
Enter your choice
1.Exhibition
2.StageEvent
1
Enter the details in CSV format
Book expo,Special sale,Academics,Martin,100,1000
Exhibition Details
Event Name:Book expo
Detail:Special sale
Type:Academics
Organiser Name:Martin
Total Cost:100000.0

Sample Input and Output 2:
 
Enter your choice
1.Exhibition
2.StageEvent
2
Enter the details in CSV format
JJ magic show,Comedy magic,Entertainment,Steffania,5,1000
Stage Event Details
Event Name:JJ magic show
Detail:Comedy magic
Type:Entertainment
Organiser Name:Steffania
Total Cost:5000.0
 
 
Sample Input and Output 3:
 
Enter your choice
1.Exhibition
2.StageEvent
3

Invalid choice 


PROGRAM:

CLASS 1: 


public abstract class Event {


    public String name,detail,type,organiser;

    public Event(String name, String detail, String type, String organiser) {

    this.name = name;

    this.detail = detail;

    this.type = type;

    this.organiser = organiser;

    }

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDetail() {

return detail;

}

public void setDetail(String detail) {

this.detail = detail;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getOrganiser() {

return organiser;

}

public void setOrganiser(String organiser) {

this.organiser = organiser;

}

abstract Double calculateAmount();

double calculateAmount;

}

CLASS 2: 

public class Exhibition extends Event {

 

private int noOfStalls;

private double rentPerStall;

public Exhibition(String name, String detail, String type, String organiser, Integer noOfStalls,Double rentPerStall) {

super(name, detail, type, organiser);

this.noOfStalls = noOfStalls;

this.rentPerStall = rentPerStall;

}

    public int getNoOfStalls() {

return noOfStalls;

}

public void setNoOfStalls(int noOfStalls) {

this.noOfStalls = noOfStalls;

}

public double getRentPerStall() {

return rentPerStall;

}

public void setRentPerStall(double rentPerStall) {

this.rentPerStall = rentPerStall;

}



Double calculateAmount() {


calculateAmount = getNoOfStalls() * getRentPerStall();


        return calculateAmount;

    }

     

}

CLASS 3: 


public class StageEvent extends Event {

    

    private int noOfShows;

    private double costPerShow;

    

    public StageEvent(String name, String detail, String type, String organiser, Integer noOfShows,Double costPerShow) {

    super(name, detail, type, organiser);

    this.noOfShows = noOfShows;

    this.costPerShow = costPerShow;

    }

    public int getNoOfShows() {

return noOfShows;

}



public void setNoOfShows(int noOfShows) {

this.noOfShows = noOfShows;

}



public double getCostPerShow() {

return costPerShow;

}



public void setCostPerShow(double costPerShow) {

this.costPerShow = costPerShow;

}

Double calculateAmount() {

        double calculateAmount = getNoOfShows() * getCostPerShow();

        return calculateAmount;


    }  

CLASS 4: 

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

Scanner sc =new Scanner(System.in).useDelimiter("\n");

System.out.println("Enter your choice\n1.Exhibition\n2.StageEvent");

String eventType=sc.next();

Event event=null;

if(Integer.parseInt(eventType.trim())==1){

System.out.println("Enter the details in CSV format");

String input=sc.next();

String[] inputs=input.split(",");

event=new Exhibition(inputs[0].trim(), inputs[1].trim(), inputs[2].trim(), inputs[3].trim(), Integer.parseInt(inputs[4].trim()), Double.parseDouble(inputs[5].trim()));

            System.out.println("Exhibition Details");

            System.out.println("Event Name:"+event.name);

        System.out.println("Detail:"+event.detail);

    System.out.println("Type:"+event.type);

    System.out.println("Organiser Name:"+event.organiser);

    System.out.println("Total Cost:"+String.format("%.1f", event.calculateAmount()));

}

else if(Integer.parseInt(eventType.trim())==2){

System.out.println("Enter the details in CSV format");

String input=sc.next();

String[] inputs=input.split(",");

event=new StageEvent(inputs[0].trim(), inputs[1].trim(), inputs[2].trim(), inputs[3].trim(), Integer.parseInt(inputs[4].trim()), Double.parseDouble(inputs[5].trim()));

            System.out.println("Stage Event Details");

            System.out.println("Event Name:"+event.name);

            System.out.println("Detail:"+event.detail);

    System.out.println("Type:"+event.type);

    System.out.println("Organiser Name:"+event.organiser);

    System.out.println("Total Cost:"+String.format("%.1f", event.calculateAmount()));

}

else{

System.out.println("Invalid choice");

}

}

}

INPUT OUTPUT:

Sample Input and output 1:
Enter your choice
1.Exhibition
2.StageEvent
1
Enter the details in CSV format
Book expo,Special sale,Academics,Martin,100,1000
Exhibition Details
Event Name:Book expo
Detail:Special sale
Type:Academics
Organiser Name:Martin
Total Cost:100000.0

Sample Input and Output 2:
 
Enter your choice
1.Exhibition
2.StageEvent
2
Enter the details in CSV format
JJ magic show,Comedy magic,Entertainment,Steffania,5,1000
Stage Event Details
Event Name:JJ magic show
Detail:Comedy magic
Type:Entertainment
Organiser Name:Steffania
Total Cost:5000.0
 
 
Sample Input and Output 3:
 
Enter your choice
1.Exhibition
2.StageEvent
3
Invalid choice

                                 



CALCULATE REWARD POINTS 

Calculate Reward Points


ABC Bank announced a new scheme of reward points for a transaction using an ATM card. Each transaction using the normal card will be provided 1% of the transaction amount as a reward point. If a transaction is made using a premium card and it is for fuel expenses, additional 10 points will be rewarded. Write a java program to calculate the total reward points.

[Note:  Strictly adhere to the object-oriented specifications given as a part of the problem statement.
Follow the naming conventions as mentioned]


Consider a class VISACard with the following method.

 
MethodDescription
public Double computeRewardPoints(String type, Double amount)This method returns the 1% of the transaction amount as reward points


Consider a class HPVISACard which extends VISACard class and overrides the following method.
 
MethodDescription
public Double computeRewardPoints(String type, Double amount)In this method, calculate the reward points from the base class and add 10 points if it is for fuel expense

Hint:

Use super keyword to calculate reward points from base class.

Consider the Main class with the main method and read all the transaction details in the main method.
Card type will be either ‘VISA card’ or ‘HPVISA card’. Otherwise, display ‘Invalid data’

Calculate the reward points corresponding to the card type and transaction type and print the reward points(upto two decimal places).


The link to download the template code is given below
Code Template

Input and Output Format:

Refer sample input and output for formatting specifications.

Enter the transaction details in CSV format ( Transaction type, amount, card type)

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

Sample Input and Output 1:

Enter the transaction detail
Shopping,5000
,VISA card
Total reward points earned in this transaction is 50.00
Do you want to continue?(Yes/No)
Yes
Enter the transaction detail
Fuel,5000,
HIVISA card
Invalid data
Do you want to continue?(Yes/No)
Yes
Enter the transaction detail
Fuel,5000,HPVISA card
Total reward points earned in this transaction is 60.00
Do you want to continue?(Yes/No)
No

Sample Input and Output 2:

Enter the transaction detail
Fuel,1000
,HPVISA card
Total reward points earned in this transaction is 20.00
Do you want to continue?(Yes/No)
No


PROGRAM:


CLASS 1: 

public class VISACard {

public Double computeRewardPoints(String type, Double amount){

//Double computeRewardPoints = *0.01;

        amount = amount/100;


        return amount;

    }


}

CLASS 2: 

class HPVISACard extends VISACard{

    public Double computeRewardPoints(String type, Double amount){

if(type.equals("Fuel")){

            amount = (amount*0.01)+10;}

            else{

                amount = (amount*0.01);

            }

    //Double computeRewardPoints = (super.computeRewardPoints(type, amount)*0.01)+10;


         return amount;

    }

}

CLASS 3: 

import java.io.*;
import java.util.Scanner;
public class Main { 
    public static void main(String[] args){ 
        
    Scanner sc=new Scanner(System.in).useDelimiter("\n");
    
    VISACard visaCard;
    String contd;
    do{
        System.out.println("Enter the transaction detail");
        String input=sc.next();
        String[] inputs=input.split(",");
        if(inputs[2].equals("VISA card")){
           visaCard=new VISACard();
           double reward=visaCard.computeRewardPoints(inputs[0],Double.parseDouble(inputs[1]));
           
           System.out.println("Total reward points earned in this transaction is "+String.format("%.2f",reward));
        }
        else if(inputs[2].equals("HPVISA card")){
            visaCard=new HPVISACard();
            double reward=visaCard.computeRewardPoints(inputs[0],Double.parseDouble(inputs[1]));
           
            System.out.println("Total reward points earned in this transaction is "+String.format("%.2f",reward));
        }
        else{
            System.out.println("Invalid data");
        }
        System.out.println("Do you want to continue?(Yes/No)");
        contd=sc.next();
    
    }
    while(contd.equalsIgnoreCase("Yes"));
    } 
}



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



1 Comments

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