Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Java;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- //Creating the Parent class ("User") as the Abstract:
- abstract class User {
- protected String userName;
- protected String userEmail;
- //Creating the Parameter constructors:
- public User(String U_Name, String U_Email) {
- this.userName = U_Name;
- this.userEmail = U_Email;
- }
- public User(User Obj) {
- this.userName = Obj.userName;
- this.userEmail = Obj.userEmail;
- }
- //Creating the Abstract method:
- abstract public void printDetails();
- }
- //Creating the Child classes:
- class Artist extends User {
- private String Music_Genre;
- private int No_Of_Songs;
- protected String[][] songList = new String[5][2];
- //This is way to Declare a scanner object :
- Scanner scanSong = new Scanner(System.in);
- //Creating parameter constructor to set artist details:
- public Artist(String userName, String userEmail, String musicGenre, int noOfSongs) {
- //Calling parameter constructor of parent class("User")
- super(userName, userEmail);
- this.Music_Genre = musicGenre;
- this.No_Of_Songs = noOfSongs;
- }
- //In here u can how is going to Set the Song Details:
- public void setSongDetails() {
- for(int s = 0; s < songList.length; s++) {
- int d = 0;
- System.out.println("Enter Song :");
- songList[s][d] = scanSong.nextLine();
- System.out.println("Enter Rate :");
- songList[s][d + 1] = scanSong.nextLine();
- }
- System.out.println();
- }
- //Over Riding the PrintDetails Method in the Parent Class:
- public void printDetails() {
- System.out.println("Artist name is " + this.userName);
- System.out.println("Artist email is " + this.userEmail);
- System.out.println("Artist music genre is " + this.Music_Genre);
- System.out.println("Number of songs is " + this.No_Of_Songs);
- System.out.println("Song list is: ");
- for(int s = 0; s < songList.length; s++) {
- int d = 0;
- System.out.print(songList[s][d]);
- System.out.print(" \t");
- System.out.print(songList[s][d + 1]);
- System.out.println();
- }
- System.out.println();
- }
- }
- //Creating the child classes using calculation interface:
- class Operator extends User implements Calculation {
- private String empNo;
- private String jobTitle;
- //Creating parameterized constructor to set operator details:
- public Operator(String userName, String userEmail, String emp_No, String job_Title) {
- //Calling parameter constructor of parent class:
- super(userName, userEmail);
- this.empNo = emp_No;
- this.jobTitle = job_Title;
- }
- //Over Riding PrintDetails method in the Parent class:
- public void printDetails() {
- System.out.println("Operator name is " + this.userName);
- System.out.println("Oparator email is " + this.userEmail);
- System.out.println("Operator employee number is " + this.empNo);
- System.out.println("Operator designation is " + this.jobTitle);
- System.out.println("\n\n\n");
- }
- //Calculating the Revenue:
- public void calculateRevenue(Artist Art) throws NumberFormatException {
- String convert = "";
- int No_Of_Downloads = 0;
- double sum = 0;
- double average = 0;
- double revenue = 0;
- Scanner NoOfDownloads = new Scanner(System.in);
- //Checking for exceptions
- try {
- System.out.print("Enter number of downloads : ");
- No_Of_Downloads = NoOfDownloads.nextInt();
- System.out.println();
- }
- //Print the types of Exception:
- catch(InputMismatchException ex) {
- ex.printStackTrace();
- System.out.println("Input should be an integer value");
- }
- //Checking "for" exceptions:
- try {
- for(int s = 0; s < Art.songList.length; s++) {
- convert = Art.songList[s][1];
- sum = sum + Double.parseDouble(convert);
- }
- }
- //To print the type of exception:
- catch(NumberFormatException e) {
- e.printStackTrace();
- System.out.println("Unable to format as double.");
- }
- catch(ArrayIndexOutOfBoundsException e) {
- e.printStackTrace();
- System.out.println("Invalid location of array.");
- }
- //Calculate the Revenue:
- average = sum / 5.0;
- revenue = average * No_Of_Downloads;
- System.out.println("Artist : " + Art.userName);
- System.out.println("Album revenue is LKR " + revenue);
- }
- }
- //Create the Interface for "revenue calculation" :
- interface Calculation {
- //Method to use for calculations
- void calculateRevenue(Artist Art) throws NumberFormatException;
- }
- //Create the Play_A_Song ("Main") class:
- public class Play_A_Song {
- public static void main(String[] args) {
- //Create the Artist object:
- Artist Art1 = new Artist("Pablo Albo", "pablo@gmail.com", "Pop", 5);
- //Create the Operator object:
- Operator O1 = new Operator("Simon", "simon@playasong.com", "123", "accountant");
- //Call the Artist Methods:
- Art1.setSongDetails();
- Art1.printDetails();
- //Calling operator methods
- O1.printDetails();
- O1.calculateRevenue(Art1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement