Advertisement
Guest User

Milana

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