Below is the Java code I pick out from a Java package, how can fix the code for
ID: 3692911 • Letter: B
Question
Below is the Java code I pick out from a Java package, how can fix the code for meeting below requirement?
When I enter "1" to creat a film, if the film I have already inputted, system will not store it again. If the film I have not inputted, the system will restore that film.
For example:
Name of film: AA
Name of director:BB
Year of release:2000
*******************
Name of film: AA
Name of director:BB
Year of release:2000
Beacuse these two films have the same name "AA", how can print one film?
Thank you
***************************************************
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
int option;
Film [] filmArray = new Film[5];
int numberOfFilms = 0;
do{
System.out.println("Enter 1 to create a film");
System.out.println("Enter 2 to view all of the films");
System.out.println("Enter 3 to exit the program");
option = keyboard.nextInt();
if(option == 1)
{
Scanner filmInput = new Scanner(System.in);
Film movie = new Film();
System.out.println("Enter the name of the Film");
String name = filmInput.nextLine();
movie.setFilm(name);
System.out.println("Enter the name(s) of the director");
String director = filmInput.nextLine();
movie.setDirector(director);
System.out.println("Enter the year of release");
int release = keyboard.nextInt();
movie.setYearOfRelease(release);
boolean sameMovie = false;
for(int index = 0; index < numberOfFilms; index++) {
if(filmArray[index].equals(movie)) {
sameMovie = true;
System.out.println("This film already exists");
}
}
if(sameMovie == false) {
filmArray[numberOfFilms] = movie;
numberOfFilms++;
}
}
else if(option ==2)
{ for(int index = 0; index < numberOfFilms; index++) {
System.out.println(filmArray[index].toString());
}
}
else if(option == 3)
{
System.out.println("The system will now close");
}
else
{
System.out.println("Error!");
}
Explanation / Answer
Demo.java
package com.rbc.test;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Scanner filmInput = null;
int option;
Film[] filmArray = new Film[5];
int numberOfFilms = 0;
do {
System.out.println("Enter 1 to create a film");
System.out.println("Enter 2 to view all of the films");
System.out.println("Enter 3 to exit the program");
option = keyboard.nextInt();
if (option == 1) {
filmInput = new Scanner(System.in);
Film movie = new Film();
System.out.println("Enter the name of the Film");
String name = filmInput.nextLine();
movie.setFilm(name);
System.out.println("Enter the name(s) of the director");
String director = filmInput.nextLine();
movie.setDirector(director);
System.out.println("Enter the year of release");
int release = filmInput.nextInt();
movie.setYearOfRelease(release);
// checking the movie is same or different
boolean sameMovie = false;
for (int index = 0; index < numberOfFilms; index++) {
if (filmArray[index].getFilm().equals(movie.getFilm())) {
sameMovie = true;
System.out.println("This film already exists");
}
}
// If movie id different adding to array
if (sameMovie == false) {
filmArray[numberOfFilms] = movie;
numberOfFilms++;
}
} else if (option == 2) {
for (int index = 0; index < numberOfFilms; index++) {
System.out.println(filmArray[index].toString());
}
} else if (option == 3) {
System.out.println("The system will now close");
} else {
System.out.println("Error!");
}
} while (option != 3);
filmInput.close();
keyboard.close();
}
}
Film.java
package com.rbc.test;
public class Film {
private String film;
private String director;
private int yearOfRelease;
/**
* @return the film
*/
public String getFilm() {
return film;
}
/**
* @param film
* the film to set
*/
public void setFilm(String film) {
this.film = film;
}
/**
* @return the director
*/
public String getDirector() {
return director;
}
/**
* @param director
* the director to set
*/
public void setDirector(String director) {
this.director = director;
}
/**
* @return the yearOfRelease
*/
public int getYearOfRelease() {
return yearOfRelease;
}
/**
* @param yearOfRelease
* the yearOfRelease to set
*/
public void setYearOfRelease(int yearOfRelease) {
this.yearOfRelease = yearOfRelease;
}
public String toString() {
return "Name of the film :" + film + " Name of the director :"
+ director + " Year of release :" + yearOfRelease;
}
}
Output:
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
1
Enter the name of the Film
AA
Enter the name(s) of the director
BB
Enter the year of release
2000
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
1
Enter the name of the Film
AA
Enter the name(s) of the director
BB
Enter the year of release
2000
This film already exists
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
2
Name of the film :AA Name of the director :BB Year of release :2000
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
1
Enter the name of the Film
CC
Enter the name(s) of the director
DD
Enter the year of release
2000
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
2
Name of the film :AA Name of the director :BB Year of release :2000
Name of the film :CC Name of the director :DD Year of release :2000
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
4
Error!
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
6
Error!
Enter 1 to create a film
Enter 2 to view all of the films
Enter 3 to exit the program
3
The system will now close
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.