Assignment Brief:
This project involved creating two classes based on two UML diagrams and a main class to run program
Download full brief here
Download zip of all source code for 'Actor's Film Database' here

MainTest is the main class:
Download source code for 'MainTest' here

// Start of code---------------------
package MainTest;

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

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class MainTest {

/**
 * This program allows for a user to input actors personal detail
 * with movie history associated with actor
 */

    public static ArrayList newActorList = new ArrayList();
    public static Actor newActor;

    public static void main(String[] args) {

        // Scanner class to allow for keyboard input
        Scanner input = new Scanner(System.in);

        // Final and variables for MainTest
        final String menuTitle = "\n***Welcome to the actors film database***";
        final String inputError = "\nInput error!!";
        final String menu = "\nPlease input corresponding digit to continue\n(Numeric digits only please!)"
                + "\n1. Add an actor to the list"
                + "\n2. List existing actors"
                + "\n3. Quit program";
        String filmName = "";
        Boolean menuLoop = false;
       
        // Printout menuTitle to screen
        System.out.println(menuTitle);

        do {
            menuLoop = false;

            // Print menu to screen
            System.out.println(menu);

            // Input from the user must be a numeric digit
            while (!input.hasNextInt()) {
                System.out.println(inputError + menu);
                input.next();
            }

            // Data to be input by user for menu switch
            int menuInput = input.nextInt();

            // Menu switch
            switch (menuInput) {

            case 1: {//Switch case to add actor to list
                // Data to be input by user for actors details
                input.nextLine();
                //Name input
                System.out.print("\nPlease enter actors name: ");
                String name = input.nextLine();
               
                //Age input
                System.out.print("Please enter " + name + "'s age: ");
                while (!input.hasNextInt()) {
                    System.out.println(inputError + "\nPlease enter " + name + "'s age\n(Numeric digits only please!): ");
                    input.next();
                }
                int age = input.nextInt();

                //Address input
                System.out.print("Please enter " + name + "'s address: ");
                String address = input.next();

                //Numeric digit for amount of films to input
                System.out.print("Please enter amount of films");
                while (!input.hasNextInt()) {
                    System.out.println(inputError + "\nPlease enter amount of films\n(Numeric digits only please!)");
                    input.next();
                }
                int maxFilmInput = input.nextInt();

                // Data to be passed for a new actor into the Actor class
                newActor = new Actor(name, age, address);
                newActorList.add(newActor);
                               
                int filmCounter = maxFilmInput;
               
                // Loop for multiple movies
                while (maxFilmInput >= 1) {

                    for (int filmNumber = 1; filmNumber <= filmCounter; filmNumber++) {
                        System.out.print("Please enter " + name + "'s film No."
                                + filmNumber + ": ");

                        filmName = input.next();
                        newActor.addfilm(new Film(filmName));

                        maxFilmInput--;
                    }
                }
                menuLoop = menuLoopOption(input, menuLoop);

                break;
            }
            case 2: {//Switch case for List of actors
                // Print method with actor details
                System.out.println(newActorList.toString().replace("[", " ")
                        .replace("]", ""));

                menuLoop = menuLoopOption(input, menuLoop);

                break;
            }
            case 3: {//Switch case to quit program
                // Print method when quit is chosen
                System.out.println("\nYou have chosen to quit. Goodbye.\n");
                System.out.println("Final list of actor and films as follows:"
                + newActorList.toString().replace("[", " ").replace("]", ""));
                System.exit(0);
                break;
            }
            }

        } while (menuLoop);//Loop back to start of menu when "true"

        input.close();//Close input scanner
        System.exit(0);//Exit program
    }
   
    // Menu loop method
    public static boolean menuLoopOption(Scanner input, Boolean menuLoop) {

        String menuLoopInput = "";

        System.out.println("\nWould you like to return to menu\n"
                      + "Input y for yes or q to quit");
        menuLoopInput = input.next();

        if (menuLoopInput.equals("y")) {
            menuLoop = true;
        } else {

            System.out.println("\nYou have chosen to quit. Goodbye.\n");
           
            System.out.println("Final list of actor and films as follows:"
                    + newActorList.toString().replace("[", " ").replace("]", ""));
           
            System.exit(0);
        }
        return menuLoop;
    }
}//End of code---------------------

Actor class:
Download source code for 'Actor class' here

// Start of code---------------------
package MainTest;

import java.util.ArrayList;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class Actor {

    // Private variables for the (Actor) class
    private String name = "";
    private String address = "";
    private int age = 0;
    private ArrayList myFilms = new ArrayList();

    // Actor details taken from MainTest
    public Actor(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // All getters for this (Actor) class
    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    // All setters for this (Actor) class
    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // Add a film to myFilms ArrayList
    public void addfilm(Film film) {
        myFilms.add(film);
    }

    public ArrayList getMyFilms() {
        return myFilms;
    }

    public void setMyFilms(ArrayList myFilms) {
        this.myFilms = myFilms;
    }

    //toString which prints out actors personal details with movie details
    public String toString()
    {
        return "\n\n" + name + " who's " + age + " and lives in " + address
                + "\nStarred in" + myFilms.toString() + ".";
    }
}//End of code---------------------

Film class:
Download source code for 'Film class' here

// Start of code---------------------
package MainTest;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class Film {

    // Private variables for the (Film) class
    private String film = "";
    private String code = "";

    public Film(String film) {
        this.film = film;
    }

    // All setters for this (Actor) class
    public void setFilm(String film) {
        this.film = film;
    }

    // All getters for this (Actor) class
    public String getFilm()
    {
        return film;
    }

    public String getCode() {
        // Use filmName to get initials of each movie
        StringBuilder filmInitials = new StringBuilder();
        String[] nameParts = film.split("\\s");
        for (String part : nameParts) {
            filmInitials.append(part.charAt(0));
        }
        code = filmInitials.toString().toUpperCase();

        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    // toString to print out actors film history
    public String toString() {
        return "\"" + film + "\"";
    }
}//End of code---------------------

0 comments:

Post a Comment

Please feel free to leave comments or ask questions related to the tutorials.