Advertisement
irmantas_radavicius

Untitled

Mar 13th, 2022
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. class Item {
  6.     private String name;
  7.     private double price;
  8.     private int amount;
  9.    
  10.     public Item(String name, double price){
  11.         setName(name);
  12.         setPrice(price);
  13.         setAmount(1);
  14.     }
  15.     public Item(String name, double price, int amount){
  16.         setName(name);
  17.         setPrice(price);
  18.         setAmount(amount);     
  19.     }
  20.    
  21.     public String getName(){
  22.         return this.name;
  23.     }
  24.     public double getPrice(){
  25.         return this.price;
  26.     }  
  27.     public int getAmount(){
  28.         return this.amount;
  29.     }
  30.    
  31.     public double getTotalPrice(){
  32.         return getPrice()*getAmount();
  33.     }
  34.    
  35.     public void setName(String name){
  36.         this.name = name;
  37.     }
  38.     public void setPrice(double price){
  39.         if (price >= 0)
  40.             this.price = price;    
  41.         else
  42.             throw new IllegalArgumentException("Price cannot be negative");
  43.     }
  44.     public void setAmount(int amount){
  45.         if (amount >= 0)
  46.             this.amount = amount;
  47.         else
  48.             throw new IllegalArgumentException("Amount cannot be negative");
  49.     }  
  50.    
  51.     public String toString(){
  52.         return this.name + "(" + this.amount + ") " + this.price;
  53.     }
  54. }
  55.  
  56.  
  57. class Song {
  58.     private String author;
  59.     private String name;
  60.     private int length;
  61.     private String genre;
  62.     private int rating;
  63.    
  64.     public Song(String author, String name, int length){
  65.         init(author, name, length, "", 0);
  66.     }
  67.     public Song(String author, String name, int length, String genre){
  68.         init(author, name, length, genre, 0);
  69.     }
  70.     public Song(String author, String name, int length, String genre, int rating){
  71.         init(author, name, length, genre, rating);
  72.     }
  73.    
  74.     private void init(String author, String name, int length, String genre, int rating){
  75.         if (length >= 0){
  76.             this.author = author;
  77.             this.name = name;      
  78.             this.length = length;                  
  79.             this.genre = genre;
  80.             setRating(rating);
  81.         } else {
  82.             throw new IllegalArgumentException("Length cannot be negative");
  83.         }              
  84.     }
  85.    
  86.     public String getAuthor(){
  87.         return author;
  88.     }
  89.     public String getName(){
  90.         return name;
  91.     }
  92.     public int getLength(){
  93.         return length;
  94.     }
  95.     public String getGenre(){
  96.         return genre;
  97.     }  
  98.     public int getRating(){
  99.         return rating;
  100.     }
  101.    
  102.    
  103.     public void setRating(int rating){
  104.         this.rating = rating;
  105.     }
  106.    
  107.    
  108.     public String toString(){
  109.         return author + " - " + name + " (" + length + " sec.), " + genre + ", " + rating + " stars";
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. public class Sandbox {  
  116.     public static void main(String args[]) {       
  117.         try {
  118.             // input
  119.             Scanner sc = new Scanner(System.in);
  120.             Song[] songs = null;
  121.             while(true){
  122.                 try {
  123.                     System.out.println("How many songs?");
  124.                     int size = Integer.parseInt(sc.nextLine());        
  125.                     songs = new Song[size];
  126.                     break;
  127.                 } catch(Exception e){
  128.                     System.out.print("Error! Please enter how many songs:");
  129.                 }  
  130.             }          
  131.                        
  132.             for(int i = 0; i < songs.length; ){    
  133.                 try {
  134.                     System.out.print("Enter author: ");
  135.                     String author = sc.nextLine();
  136.                    
  137.                     System.out.print("Enter name: ");
  138.                     String name = sc.nextLine();
  139.                
  140.                     System.out.print("Enter length: ");
  141.                     int length = Integer.parseInt(sc.nextLine());  
  142.                    
  143.                     songs[i] = new Song(author, name, length); 
  144.                    
  145.                     System.out.print("Enter rating: ");            
  146.                     songs[i].setRating(Integer.parseInt(sc.nextLine()));
  147.                     System.out.println("Song[" + i + "] was read successfully!\n");
  148.                    
  149.                     ++i;
  150.                        
  151.                 } catch(Exception e){                  
  152.                     System.out.println("Error! Please enter data again.");                                     
  153.                 }
  154.                
  155.                    
  156.             }
  157.             sc.close();
  158.  
  159.             // algorithm
  160.             int totalDuration = 0;
  161.             double averageRating = 0;
  162.             for(int i = 0; i < songs.length; ++i){             
  163.                 //System.out.println(songs[i]);            
  164.                 try {
  165.                     totalDuration += songs[i].getLength();
  166.                     averageRating += songs[i].getRating();
  167.                 } catch(NullPointerException npe){
  168.                     averageRating += 3;
  169.                 }
  170.             }
  171.             averageRating = averageRating/songs.length;
  172.            
  173.             // output
  174.             System.out.println("Total duration: " + totalDuration + " sec.");          
  175.             System.out.println("Average rating: " + averageRating + " stars");
  176.            
  177.  
  178.         } catch(Exception e){
  179.             System.out.println(e);         
  180.             System.out.println("Unexpected error, sorry!");
  181.         }          
  182.     }  
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement