top of page
Interesting Recent Posts :
Writer's pictureRohit chopra

Java 17- Records Example


This is a follow up post from my earlier post on Java 17 Records.

You can read about Java -17 Records here.


Here's a basic example of a program that creates a movie record in Java 17:


import java.util.ArrayList;
import java.util.Scanner;

public class Movie {
    private String title;
    private int year;
    private String director;
    private ArrayList<String> actors;

    public Movie(String title, int year, String director, ArrayList<String> actors) {
        this.title = title;
        this.year = year;
        this.director = director;
        this.actors = actors;
    }

    public String getTitle() {
        return title;
    }

    public int getYear() {
        return year;
    }

    public String getDirector() {
        return director;
    }

    public ArrayList<String> getActors() {
        return actors;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "title='" + title + '\'' +
                ", year=" + year +
                ", director='" + director + '\'' +
                ", actors=" + actors +
                '}';
    }
}

class MovieDatabase {
    private ArrayList<Movie> movies;

    public MovieDatabase() {
        this.movies = new ArrayList<>();
    }

    public void addMovie(Movie movie) {
        movies.add(movie);
    }

    public ArrayList<Movie> getMovies() {
        return movies;
    }

    @Override
    public String toString() {
        return "MovieDatabase{" +
                "movies=" + movies +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        MovieDatabase movieDatabase = new MovieDatabase();

        System.out.println("Enter number of movies: ");
        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            System.out.println("Enter movie details: ");
            System.out.println("Enter title: ");
            String title = scanner.next();

            System.out.println("Enter year: ");
            int year = scanner.nextInt();

            System.out.println("Enter director: ");
            String director = scanner.next();

            System.out.println("Enter number of actors: ");
            int numActors = scanner.nextInt();
            ArrayList<String> actors = new ArrayList<>();

            for (int j = 0; j < numActors; j++) {
                System.out.println("Enter actor name: ");
                actors.add(scanner.next());
            }

            Movie movie = new Movie(title, year, director, actors);
            movieDatabase.addMovie(movie);
        }

        System.out.println(movieDatabase.toString());
    }
}

This code defines a basic movie database system in Java 17. It contains three classes: Movie, MovieDatabase, and Main.

The Movie class has four instance variables: title, year, director, and actors. It also has getter methods to access the values of these variables, and a toString method to display the information about a movie in a readable format.

The MovieDatabase class has an ArrayList of Movie objects, and it provides methods to add movies and retrieve the list of movies.

The Main class is the entry point of the program and contains the main method. It creates a MovieDatabase object and allows the user to add movies to the database. The user inputs the number of movies they want to add, and for each movie, the program prompts the user to enter the movie's title, year, director, and actors. The information is then used to create a Movie object, which is added to the MovieDatabase using the addMovie method. Finally, the program displays the complete list of movies in the database by calling the toString method of the MovieDatabase class.


Here's an example of the output of the program:




Enter number of movies: 
2
Enter movie details: 
Enter title: 
The Shawshank Redemption
Enter year: 
1994
Enter director: 
Frank Darabont
Enter number of actors: 
2
Enter actor name: 
Tim Robbins
Enter actor name: 
Morgan Freeman
Enter movie details: 
Enter title: 
The Godfather
Enter year: 
1972
Enter director: 
Francis Ford Coppola
Enter number of actors: 
3
Enter actor name: 
Marlon Brando
Enter actor name: 
Al Pacino
Enter actor name: 
James Caan
MovieDatabase{movies=[Movie{title='The Shawshank Redemption', year=1994, director='Frank Darabont', actors=[Tim Robbins, Morgan Freeman]}, Movie{title='The Godfather', year=1972, director='Francis Ford Coppola', actors=[Marlon Brando, Al Pacino, James Caan]}]}

69 views

Recent Posts

See All

Comments


bottom of page