Advertisement
Guest User

Vimukthi

a guest
Mar 31st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. package Java;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. //Creating the Parent class ("User") as the Abstract:
  7. abstract class User {
  8. protected String userName;
  9. protected String userEmail;
  10.  
  11. //Creating the Parameter constructors:
  12. public User(String U_Name, String U_Email) {
  13. this.userName = U_Name;
  14. this.userEmail = U_Email;
  15. }
  16.  
  17.  
  18. public User(User Obj) {
  19. this.userName = Obj.userName;
  20. this.userEmail = Obj.userEmail;
  21. }
  22.  
  23. //Creating the Abstract method:
  24. abstract public void printDetails();
  25. }
  26.  
  27. //Creating the Child classes:
  28. class Artist extends User {
  29. private String Music_Genre;
  30. private int No_Of_Songs;
  31. protected String[][] songList = new String[5][2];
  32.  
  33. //This is way to Declare a scanner object :
  34. Scanner scanSong = new Scanner(System.in);
  35.  
  36. //Creating parameter constructor to set artist details:
  37. public Artist(String userName, String userEmail, String musicGenre, int noOfSongs) {
  38. //Calling parameter constructor of parent class("User")
  39. super(userName, userEmail);
  40. this.Music_Genre = musicGenre;
  41. this.No_Of_Songs = noOfSongs;
  42. }
  43. //In here u can how is going to Set the Song Details:
  44. public void setSongDetails() {
  45. for(int s = 0; s < songList.length; s++) {
  46. int d = 0;
  47. System.out.println("Enter Song :");
  48. songList[s][d] = scanSong.nextLine();
  49. System.out.println("Enter Rate :");
  50. songList[s][d + 1] = scanSong.nextLine();
  51. }
  52. System.out.println();
  53. }
  54. //Over Riding the PrintDetails Method in the Parent Class:
  55. public void printDetails() {
  56. System.out.println("Artist name is " + this.userName);
  57. System.out.println("Artist email is " + this.userEmail);
  58. System.out.println("Artist music genre is " + this.Music_Genre);
  59. System.out.println("Number of songs is " + this.No_Of_Songs);
  60. System.out.println("Song list is: ");
  61.  
  62. for(int s = 0; s < songList.length; s++) {
  63. int d = 0;
  64. System.out.print(songList[s][d]);
  65. System.out.print(" \t");
  66. System.out.print(songList[s][d + 1]);
  67. System.out.println();
  68. }
  69. System.out.println();
  70. }
  71. }
  72.  
  73. //Creating the child classes using calculation interface:
  74. class Operator extends User implements Calculation {
  75. private String empNo;
  76. private String jobTitle;
  77.  
  78. //Creating parameterized constructor to set operator details:
  79. public Operator(String userName, String userEmail, String emp_No, String job_Title) {
  80. //Calling parameter constructor of parent class:
  81. super(userName, userEmail);
  82. this.empNo = emp_No;
  83. this.jobTitle = job_Title;
  84. }
  85. //Over Riding PrintDetails method in the Parent class:
  86. public void printDetails() {
  87. System.out.println("Operator name is " + this.userName);
  88. System.out.println("Oparator email is " + this.userEmail);
  89. System.out.println("Operator employee number is " + this.empNo);
  90. System.out.println("Operator designation is " + this.jobTitle);
  91. System.out.println("\n\n\n");
  92. }
  93. //Calculating the Revenue:
  94. public void calculateRevenue(Artist Art) throws NumberFormatException {
  95. String convert = "";
  96. int No_Of_Downloads = 0;
  97. double sum = 0;
  98. double average = 0;
  99. double revenue = 0;
  100.  
  101. Scanner NoOfDownloads = new Scanner(System.in);
  102.  
  103. //Checking for exceptions
  104. try {
  105. System.out.print("Enter number of downloads : ");
  106. No_Of_Downloads = NoOfDownloads.nextInt();
  107. System.out.println();
  108. }
  109. //Print the types of Exception:
  110. catch(InputMismatchException ex) {
  111. ex.printStackTrace();
  112. System.out.println("Input should be an integer value");
  113. }
  114.  
  115. //Checking "for" exceptions:
  116. try {
  117. for(int s = 0; s < Art.songList.length; s++) {
  118. convert = Art.songList[s][1];
  119. sum = sum + Double.parseDouble(convert);
  120. }
  121. }
  122. //To print the type of exception:
  123. catch(NumberFormatException e) {
  124. e.printStackTrace();
  125. System.out.println("Unable to format as double.");
  126. }
  127. catch(ArrayIndexOutOfBoundsException e) {
  128. e.printStackTrace();
  129. System.out.println("Invalid location of array.");
  130. }
  131.  
  132. //Calculate the Revenue:
  133. average = sum / 5.0;
  134. revenue = average * No_Of_Downloads;
  135.  
  136. System.out.println("Artist : " + Art.userName);
  137. System.out.println("Album revenue is LKR " + revenue);
  138. }
  139. }
  140.  
  141. //Create the Interface for "revenue calculation" :
  142. interface Calculation {
  143. //Method to use for calculations
  144. void calculateRevenue(Artist Art) throws NumberFormatException;
  145. }
  146.  
  147. //Create the Play_A_Song ("Main") class:
  148. public class Play_A_Song {
  149.  
  150. public static void main(String[] args) {
  151. //Create the Artist object:
  152. Artist Art1 = new Artist("Pablo Albo", "pablo@gmail.com", "Pop", 5);
  153. //Create the Operator object:
  154. Operator O1 = new Operator("Simon", "simon@playasong.com", "123", "accountant");
  155.  
  156. //Call the Artist Methods:
  157. Art1.setSongDetails();
  158. Art1.printDetails();
  159.  
  160. //Calling operator methods
  161. O1.printDetails();
  162. O1.calculateRevenue(Art1);
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement