Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package piokolos;
- /**
- *
- * @author Student
- */
- public class Movie {
- private String title;
- private int size;
- Movie(String t, int s){
- setTitle(t);
- setSize(s);
- }
- void setTitle(String s){
- if(s != null && !s.equals("")){
- this.title = s;
- }
- else{
- throw new IllegalArgumentException();
- }
- }
- void setSize(int i){
- if(i>0){
- this.size = i;
- }
- else{
- throw new IllegalArgumentException();
- }
- }
- String getTitle(){
- return title;
- }
- int getSize(){
- return size;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package piokolos;
- /**
- *
- * @author Student
- */
- public class Piokolos {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Storage storage = new Storage();
- Movie movie;
- storage.addMovie(new Movie("Deadpool",3072));
- storage.addMovie(new Movie("Deadpool 2",3072));
- storage.addMovie(new Movie("Avengers 3",5021));
- storage.addMovie(new Movie("Deadpool 2",3072));
- storage.addMovie(new Movie("Deadpool 2",3072));
- try{
- storage.addMovie(null);
- storage.addMovie(new Movie("Avengers",-4));
- storage.addMovie(new Movie("",21232));
- }
- catch(IllegalArgumentException e){
- System.err.print("Proszę wprowadzić właściwy film");
- }
- storage.print();
- System.out.println(storage.total());
- storage.removeMovie("Deadpool 2");
- storage.print();
- System.out.println(storage.total());
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package piokolos;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /**
- *
- * @author Student
- */
- public class Storage {
- private List <Movie> list = new ArrayList();
- public void addMovie(Movie m){
- if(m == null){
- throw new IllegalArgumentException("Tytuł nie może być NULL");
- }
- else{
- list.add(m);
- }
- }
- public int total(){
- int suma = 0;
- for(Movie m : list){
- suma += m.getSize();
- }
- return suma;
- }
- public void print (){
- for(Movie m : list){
- System.out.println("Nazwa: " + m.getTitle() + " Rozmiar: " + m.getSize() +"MB");
- }
- }
- public void removeMovie(String title){
- Iterator<Movie> iter = list.iterator();
- while(iter.hasNext()){
- if(iter.next().getTitle().equals(title)){
- iter.remove();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement