Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package piokolos;
  7.  
  8. /**
  9.  *
  10.  * @author Student
  11.  */
  12. public class Movie {
  13.    
  14.     private String title;
  15.     private  int size;
  16.    
  17.     Movie(String t, int s){
  18.         setTitle(t);
  19.         setSize(s);
  20.     }
  21.    
  22.    
  23.     void setTitle(String s){
  24.         if(s != null && !s.equals("")){
  25.             this.title = s;
  26.         }
  27.         else{
  28.             throw new IllegalArgumentException();
  29.         }
  30.        
  31.     }
  32.    
  33.     void setSize(int i){
  34.         if(i>0){
  35.             this.size = i;
  36.         }
  37.         else{
  38.             throw new IllegalArgumentException();
  39.         }
  40.     }
  41.     String getTitle(){
  42.         return title;
  43.     }
  44.     int getSize(){
  45.         return size;
  46.     }
  47. }
  48.  
  49.  
  50. /*
  51.  * To change this license header, choose License Headers in Project Properties.
  52.  * To change this template file, choose Tools | Templates
  53.  * and open the template in the editor.
  54.  */
  55. package piokolos;
  56.  
  57. /**
  58.  *
  59.  * @author Student
  60.  */
  61. public class Piokolos {
  62.  
  63.     /**
  64.      * @param args the command line arguments
  65.      */
  66.     public static void main(String[] args) {
  67.         Storage storage = new Storage();
  68.         Movie movie;
  69.        
  70.         storage.addMovie(new Movie("Deadpool",3072));
  71.         storage.addMovie(new Movie("Deadpool 2",3072));
  72.         storage.addMovie(new Movie("Avengers 3",5021));
  73.         storage.addMovie(new Movie("Deadpool 2",3072));
  74.         storage.addMovie(new Movie("Deadpool 2",3072));
  75.        
  76.         try{
  77.             storage.addMovie(null);
  78.             storage.addMovie(new Movie("Avengers",-4));
  79.             storage.addMovie(new Movie("",21232));
  80.         }
  81.         catch(IllegalArgumentException e){
  82.             System.err.print("Proszę wprowadzić właściwy film");
  83.         }
  84.        
  85.         storage.print();
  86.         System.out.println(storage.total());
  87.        
  88.         storage.removeMovie("Deadpool 2");
  89.        
  90.         storage.print();
  91.         System.out.println(storage.total());
  92.        
  93.        
  94.        
  95.     }
  96.    
  97. }
  98. /*
  99.  * To change this license header, choose License Headers in Project Properties.
  100.  * To change this template file, choose Tools | Templates
  101.  * and open the template in the editor.
  102.  */
  103. package piokolos;
  104.  
  105. import java.util.ArrayList;
  106. import java.util.Iterator;
  107. import java.util.List;
  108.  
  109. /**
  110.  *
  111.  * @author Student
  112.  */
  113. public class Storage {
  114.    
  115.     private List <Movie> list = new ArrayList();
  116.    
  117.     public void addMovie(Movie m){
  118.         if(m == null){
  119.             throw new IllegalArgumentException("Tytuł nie może być NULL");
  120.         }
  121.         else{
  122.             list.add(m);
  123.         }
  124.        
  125.     }
  126.    
  127.     public int total(){
  128.         int suma = 0;
  129.        
  130.         for(Movie m : list){
  131.             suma += m.getSize();
  132.         }
  133.         return suma;
  134.     }
  135.    
  136.     public void print (){
  137.         for(Movie m : list){
  138.             System.out.println("Nazwa: " + m.getTitle() + " Rozmiar: " + m.getSize() +"MB");
  139.         }
  140.     }
  141.    
  142.     public void removeMovie(String title){
  143.         Iterator<Movie> iter = list.iterator();
  144.  
  145.         while(iter.hasNext()){
  146.             if(iter.next().getTitle().equals(title)){
  147.                 iter.remove();
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement