-
Notifications
You must be signed in to change notification settings - Fork 0
Update all classes #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,75 @@ | ||
| package main.java; | ||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| public class Actor implements Serializable { | ||
| private String firstName; | ||
| private String lastName; | ||
| private String country; | ||
|
|
||
| public Actor(String firstName, String lastName) { | ||
| public Actor(String firstName, String lastName, String country) { | ||
| setFirstName(firstName); | ||
| setLastName(lastName); | ||
| setCountry(country); | ||
| } | ||
|
|
||
| public void setFirstName(String firstName) { | ||
| if (firstName == null || firstName == "") { | ||
| throw new IllegalArgumentException("First Name shouldn't be empty."); | ||
| } | ||
| this.firstName = firstName; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public void setLastName(String lastName) { | ||
| if (lastName == null || lastName == "") { | ||
| throw new IllegalArgumentException("Last Name shouldn't be empty."); | ||
| } | ||
| this.lastName = lastName; | ||
| } | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| public void setCountry(String country) { | ||
| if (country == null) { | ||
| throw new IllegalArgumentException("Country shouldn't be null."); | ||
| } | ||
| this.country = country; | ||
| } | ||
|
|
||
| public String getCountry() { | ||
| return country; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode(){ | ||
| return Objects.hash(firstName, lastName, country); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object compareObject) { | ||
| if (compareObject == null) { | ||
| return false; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно ещё сразу проверить на совпадение ссылок: this == compareObject
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавила |
||
| if (this == compareObject) { | ||
| return true; | ||
| }; | ||
| if (!(compareObject instanceof Actor)) { | ||
| return false; | ||
| } | ||
| Actor compareActor = (Actor) compareObject; | ||
| return (Objects.equals(firstName, compareActor.firstName) && | ||
| Objects.equals(lastName, compareActor.lastName) && | ||
| Objects.equals(country, compareActor.country)); | ||
| } | ||
|
|
||
| @Override | ||
| @Override | ||
| public String toString() { | ||
| return firstName + " " + lastName; | ||
| return firstName + " " + lastName + " (" + country + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,127 @@ | ||
| package main.java; | ||
| import java.io.Serializable; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| public class Film implements Serializable { | ||
|
|
||
| private String filmName; | ||
| private String country; | ||
| private int release; | ||
| private ArrayList<Actor> mainFilmActors; | ||
| private Set<Actor> mainFilmActors; | ||
|
|
||
| public Film(String filmName, int release, ArrayList<Actor> mainFilmActors) { | ||
| this.filmName = filmName; | ||
| this.release = release; | ||
| this.mainFilmActors = mainFilmActors; | ||
| public Film(String filmName, String country, int release, Set<Actor> mainFilmActors) { | ||
| setFilmName(filmName); | ||
| setCountry(country); | ||
| setRelease(release); | ||
| this.mainFilmActors = new HashSet<>(); | ||
| setMainActors(mainFilmActors); | ||
| } | ||
|
|
||
| public Film(String filmName, String country, int release, Actor actor) { | ||
| setFilmName(filmName); | ||
| setCountry(country); | ||
| setRelease(release); | ||
| this.mainFilmActors = new HashSet<>(); | ||
| setMainActors(actor); | ||
| } | ||
|
|
||
| public void setFilmName(String filmName) { | ||
| if (filmName == null || filmName == "") { | ||
| throw new IllegalArgumentException("Film Name shouldn't be empty."); | ||
| } | ||
| this.filmName = filmName; | ||
| } | ||
|
|
||
| public String getFilmName(){ | ||
| return filmName; | ||
| } | ||
|
|
||
| public void setMainActors(ArrayList<Actor> mainFilmActors) { | ||
| this.mainFilmActors = mainFilmActors; | ||
| public void setRelease(int release) { | ||
| if (release == 0) { | ||
| throw new IllegalArgumentException("Release year shouldn't be 0."); | ||
| } | ||
| this.release = release; | ||
| } | ||
|
|
||
| public int getRelease(){ | ||
| return release; | ||
| } | ||
|
|
||
| public ArrayList<Actor> getMainActors() { | ||
| public void setCountry(String country) { | ||
| if (country == null) { | ||
| throw new IllegalArgumentException("Country shouldn't be empty."); | ||
| } | ||
| this.country = country; | ||
| } | ||
|
|
||
| public String getCountry(){ | ||
| return country; | ||
| } | ||
|
|
||
| public void setMainActors(Actor actor) { | ||
| if (actor == null) { | ||
| throw new IllegalArgumentException("Actors List shouldn't be null."); | ||
| } | ||
| this.mainFilmActors.add(actor); | ||
| } | ||
|
|
||
| public void setMainActors(Set<Actor> mainFilmActors) { | ||
| if (mainFilmActors == null) { | ||
| throw new IllegalArgumentException("Actors List shouldn't be null."); | ||
| } | ||
| this.mainFilmActors.addAll(mainFilmActors); | ||
| } | ||
|
|
||
| public Set<Actor> getMainActors() { | ||
| return mainFilmActors; | ||
| } | ||
|
|
||
| public void addActor(Actor actor) { | ||
| if (actor == null) { | ||
| throw new IllegalArgumentException("Actor shouldn't be null."); | ||
| } | ||
| mainFilmActors.add(actor); | ||
| } | ||
|
|
||
| public void deleteActor(Actor actor){ | ||
| mainFilmActors.remove(actor); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode(){ | ||
| int mainFilmActorsHashCode = 0; | ||
| for (Actor actor : mainFilmActors) { | ||
| mainFilmActorsHashCode += Objects.hashCode(actor); | ||
| } | ||
| return Objects.hash(filmName, country, release)+ mainFilmActorsHashCode; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object compareObject) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. надо добавить реализацию для hashCode
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавила |
||
| if (compareObject == null) { | ||
| return false; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сравнить ссылки
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавила |
||
| if (this == compareObject) { | ||
| return true; | ||
| } | ||
| if (!(compareObject instanceof Film)) { | ||
| return false; | ||
| } | ||
| Film compareFilm = (Film) compareObject; | ||
| return (Objects.equals(filmName, compareFilm.filmName) && | ||
| Objects.equals(mainFilmActors, compareFilm.mainFilmActors) && | ||
| Objects.equals(release, compareFilm.release) && | ||
| Objects.equals(country, compareFilm.country)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| String filmInfo = "Film: " + filmName + " (" + release + ")\n" + "Stars: "; | ||
| String filmInfo = "Film: " + filmName + " (" + country + ", " + release + ")\n" + "Stars: "; | ||
| for (Actor actor : mainFilmActors) { | ||
| filmInfo += actor.toString() + "\n"; | ||
| filmInfo = filmInfo + actor.toString() + "\n"; | ||
| } | ||
| return filmInfo; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,86 @@ | ||
| package main.java; | ||
|
|
||
| import java.io.*; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.Calendar; | ||
| import java.util.*; | ||
|
|
||
| public class FilmsCollection implements Serializable { | ||
| private ArrayList<Film> films; | ||
| private Set<Film> films; | ||
|
|
||
| public FilmsCollection(){ | ||
| films = new ArrayList<Film>(); | ||
| public FilmsCollection() { | ||
| films = new HashSet<Film>(); | ||
| } | ||
|
|
||
| public void serialisationCollection(Object object, String filePath) { | ||
| try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) { | ||
| oos.writeObject(object); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| public FilmsCollection(Film film) { | ||
| this.films = new HashSet<>(); | ||
| setFilms(film); | ||
| } | ||
|
|
||
| public FilmsCollection(Set<Film> films) { | ||
| this.films = new HashSet<>(); | ||
| setFilms(films); | ||
| } | ||
|
|
||
| public void setFilms(Film film) { | ||
| if (films == null) { | ||
| throw new IllegalArgumentException("Films list shouldn't be null."); | ||
| } | ||
| this.films.add(film); | ||
| } | ||
|
|
||
| public void deserialisationCollection(String fileName) { | ||
| try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) { | ||
| FilmsCollection filmsCollection = (FilmsCollection)in.readObject(); | ||
|
|
||
| } catch (FileNotFoundException e) { | ||
| e.printStackTrace(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } catch (ClassNotFoundException e) { | ||
| e.printStackTrace(); | ||
| public void setFilms(Set<Film> films) { | ||
| if (films == null) { | ||
| throw new IllegalArgumentException("Films list shouldn't be null."); | ||
| } | ||
| this.films.addAll(films); | ||
| } | ||
|
|
||
| public Set<Film> getFilms() { | ||
| return films; | ||
| } | ||
|
|
||
| public void addFilmToCollection(Film newFilm) { | ||
| films.add(newFilm); | ||
| } | ||
|
|
||
| public boolean deleteFilmFromCollectionByName(String name) { | ||
| boolean flag = films.removeIf(film -> film.getFilmName().equals(name)); | ||
| if (!flag) { | ||
| System.out.println("Film " + name + "hasn't been found"); | ||
| } | ||
| return flag; | ||
| public void deleteFilmFromCollection(Film film) { | ||
| films.remove(film); | ||
| } | ||
|
|
||
| public boolean deleteActorFromFilm(String filmName, String name) { | ||
| boolean flag = false; | ||
| for (Film film : films){ | ||
| if (film.getFilmName().equals(filmName)) { | ||
| flag = film.getMainActors().removeIf(actor -> film.getMainActors().equals(name)); | ||
| } | ||
| } | ||
| if (!flag) { | ||
| System.out.println("Actor " + name + "hasn't been found"); | ||
| public static void serialisationCollection(Object object, String filePath) { | ||
| try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) { | ||
| oos.writeObject(object); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return flag; | ||
| } | ||
|
|
||
| public void addActorToMainActorsList(String filmName, String name) { | ||
| public static FilmsCollection deserialisationCollection(String fileName) { | ||
| FilmsCollection filmsCollection = null; | ||
| try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) { | ||
| filmsCollection = (FilmsCollection) in.readObject(); | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("Не удается найти указанный файл " + fileName); | ||
| } catch (ClassNotFoundException e) { | ||
| e.printStackTrace(); | ||
| } catch (StreamCorruptedException e) { | ||
| System.out.println("Deserialisation error"); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return filmsCollection; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder filmCollectionInfo = new StringBuilder("Films collection:"); | ||
| if (films.size() != 0) { | ||
| for (Film film : films) { | ||
| filmCollectionInfo.append(film.toString()); | ||
| } | ||
| } | ||
| return filmCollectionInfo.toString(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если Вы переопределяете в классе метод equals, то нужно переопределять и hashcode обязательно
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
переопределила