簡體   English   中英

對於以下Java演示,我將如何實現,使方法不會是靜態的?

[英]For the following java demo, how would I make it so the methods would not be static?

當我嘗試簡單地刪除static它給了我錯誤消息,並且我不知道如何創建演示類的實例。 這是代碼:

import java.util.Scanner;

public class FilmDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        Film[] filmArray = new Film[10];
        int numberOfFilms = 0;
        int option;
        do {
            System.out.println("Press 1 to create a Film");
            System.out.println("Press 2 to create a Foreign Film");
            System.out.println("Press 3 to create a Bollywood Film");
            System.out.println("Press 4 to view all the films");
            System.out.println("Press 5 to exit");
            option = keyboard.nextInt();

            if(option == 1) {
                Film currentFilm = new Film();
                enterInformation(currentFilm);
                boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms,currentFilm);
                if(!alreadyInTheArray) {
                    numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentFilm);
                }
                else {
                    System.out.println("This film already exists in the system");
                }
            }
            else if(option == 2) {
                ForeignFilm currentForeignFilm = new ForeignFilm();
                enterInformation(currentForeignFilm);
                boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms, currentForeignFilm);
                if(!alreadyInTheArray) {
                    numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentForeignFilm);
                }
                else {
                    System.out.println("This film already exists in the system.");
                }
            }
            else if(option == 3) {
                BollywoodFilm currentBollywoodFilm = new BollywoodFilm();
                enterInformation(currentBollywoodFilm);
                boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms, currentBollywoodFilm);
                if(!alreadyInTheArray) {
                    numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentBollywoodFilm);
                } 
                else {
                    System.out.println("This film already exists in the system");
                }
            }
            else if(option == 4) {
                print(filmArray,numberOfFilms);
            }
            else if(option == 5) {
                System.out.println("Thank you for using the Film Library prototype.");
            }
            else {
                System.out.println("Error. Invalid entry");
            }


        }while(option != 5);

    }

    public static boolean checkForDuplicate(Film [] array, int numberOfFilms, Film newFilm) {
        boolean alreadyInTheArray = false;
        for(int i=0; i < numberOfFilms; i++) {
            if(array[i].equals(newFilm)) {
                alreadyInTheArray = true;
            }
        }

        return alreadyInTheArray;

    }

    public static int insertIntoArray(Film [] array, int numberOfFilms, Film newFilm) {
        array[numberOfFilms] = newFilm;
        numberOfFilms++;
        return numberOfFilms;
    }

    public static void print(Film [] array, int numberOfFilms) {
        for(int i=0; i < numberOfFilms; i++) {
            System.out.println(array[i]);
        }
    }

    public static void enterInformation(Film newFilm) {
        Scanner keyboard = new Scanner(System.in);
        if(newFilm != null) {
            System.out.println("Enter the name of the film");
            String name = keyboard.nextLine();
            System.out.println("Enter the director of the film");
            String director = keyboard.nextLine();
            System.out.println("Enter the year of the film");
            int year = keyboard.nextInt();
            keyboard.nextLine();

            newFilm.setName(name);
            newFilm.setDirector(director);
            newFilm.setYear(year);

            if(newFilm instanceof ForeignFilm) {
                System.out.println("Enter the native language the film is: ");
                String nativeLanguage = keyboard.nextLine();
                System.out.println("Enter translation of the name in English");
                String translationOfNameInEnglish = keyboard.nextLine();
                System.out.println("How many subtitles does this film have?");
                int number = keyboard.nextInt();
                keyboard.nextLine();
                ForeignFilm newForeignFilm = (ForeignFilm)newFilm;
                newForeignFilm.setNativeLanguage(nativeLanguage);
                newForeignFilm.setTranslationOfNameInEnglish(translationOfNameInEnglish);

                for(int i =0; i < number; i++) {
                    System.out.println("Enter the next subtitle");
                    String nextSubtitle = keyboard.nextLine();
                    if(newForeignFilm.getNumberOfSubtitles() < ForeignFilm.getMaxSubtitles()) {
                        newForeignFilm.addSubtitle(nextSubtitle);
                    }
                }

                if(newFilm instanceof BollywoodFilm) {
                    BollywoodFilm newBollywoodFilm = (BollywoodFilm)newFilm;

                    System.out.println("How many secondary languages does this film have?");
                    int secondary = keyboard.nextInt();
                    keyboard.nextLine();

                    for(int i=0; i < secondary; i++) {
                        System.out.println("Enter the next secondary language");
                        String nextSecondaryLanguage = keyboard.nextLine();
                        if(newBollywoodFilm.getNumberOfSecondaryLanguages() < BollywoodFilm.getMaxSecondaryLanguages()) {
                            newBollywoodFilm.addSecondaryLanguage(nextSecondaryLanguage);
                        }
                    }

                    System.out.println("How many songs does this film have?");
                    int songs = keyboard.nextInt();
                    keyboard.nextLine();

                    for(int i=0; i < songs; i++) {
                        System.out.println("Enter the next song");
                        String nextSong = keyboard.nextLine();
                        if(newBollywoodFilm.getNumberOfSongs() < BollywoodFilm.getMaxSongs()) {
                            newBollywoodFilm.addSong(nextSong);
                        }
                    }



                }


            }


        }
    }

}

public static void main(String[] args) {是從命令行啟動應用程序時的入口點; 不能刪除它 ,它將出現在每個可啟動的 Java應用程序中。

相反,您可以在此方法中創建當前類的實例,然后將所有代碼移至構造函數和方法調用中。

public static void main(String[] args) {

    FilmDemo filmDemo = new FilmDemo(args);
    filmDemo.doSomething();
}

public FilmDemo(String[] args) {

    // Init anything here
}

public void doSomething() {

    // Do something here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM