java将对象数组放入对象数组中

时间:2017-05-24 12:01:34

标签: java arrays

大家好,提前抱歉我的问题写得不好。我只允许4个类,电影,Moviedatabase,播放列表和界面。在界面方法播放列表中,

1.我正在尝试将对象数组存储到对象数组中,并不是每个人都确定如何这样做。由于播放列表数组可以调整大小。

2.当我尝试使用Moviedatabase中的list方法打印我的结果时,我只得到空行。困惑为什么

编辑。这是一个学校的评估,并没有完成所以..我猜这里和那里会有相当多的错误。

public class Interface
{
//create a scanner
static Scanner input = new Scanner(System.in);  
static MovieDatabase md = new MovieDatabase(); 
static Playlist p = new Playlist();
private int total = 0;
private Playlist[] playlistArray = [total];
private Playlist[] playlist;

public static void main (String [] args)
{
    int choice = 0;


    while(choice != 15)
    {   
        showSelection();
        choice = input.nextInt();
        input.nextLine();
        switch(choice) 
        {

            case 1: addMovie();  
                    break;

            case 2: md.list(1,"",0);
                    break;

            case 3: System.out.println("What is the name of the movie you wish to delete?");
                    String name = input.nextLine();
                    md.delete(name);
                    break;

            case 4: editMovie();
                    break;

            case 5: directorList();
                    break;

            case 6: durationList();
                    break;

            case 7: playlist();

            case 13: copyFile();
                    break;

            case 14: openFile();
                    break;

            case 15: System.exit(0); 
                    break;
        }
    }
}

public static void addMovie()
{


    String name,director;
    double filesize,duration;

    System.out.println("What is the Name of the Movie:");
    name = input.nextLine();

    System.out.println("What is the Director of the Movie:");
    director = input.nextLine();

    System.out.println("What is the Duration of the Movie in minutes:");
    filesize = input.nextDouble();

    System.out.println("What is the Filesize of the Movie in mb:");
    duration = input.nextDouble();

    md.addMovie(name,director,filesize,duration);           
}

public static void editMovie()
{

    System.out.println("What is the name of the movie you wish to edit?");
    String name = input.nextLine();

    System.out.println("Please retype the information of the movie you wish to edit");

    System.out.println("What is the Name of the Movie:");
    String mname = input.nextLine();

    System.out.println("What is the Director of the Movie:");
    String director = input.nextLine();

    System.out.println("What is the Duration of the Movie in minutes:");
    double filesize = input.nextDouble();

    System.out.println("What is the Filesize of the Movie in mb:");
    double duration = input.nextDouble();

    md.edit(name,mname,director,filesize,duration);   
}

public static void directorList()
{
    System.out.println("What is the name of the director?");
    String director = input.nextLine();
    md.list(2,director,0);
}

public static void durationList()
{
    System.out.println("Duration the movies have to be under in Minutes");
    double duration = input.nextDouble();
    md.list(3,"",duration);
}

public static void playlist()
{   
    playlist[total] = new Playlist();

    total++;

    System.out.println("How many movies do you wish to save in this playlist?");
    num int = input.nextInt();

    storage = new Playlist();

    for(int i = 0; i < num; i ++)
    {
        System.out.println("Type in the next movie:").
        String name = nextLine;

        if(md.check(name)==1)
        {
            storage[i] = md.getMovie1(name);
        }   

        else if(md.check(name)==0)
        {
            System.out.println("Movie you typed does not exist");
            i--;
        }
    }

    playlistarray[total] = storage;

    if(total == playlist.length)
    {
        increaseResize();
    }
}

public static void increasseResize()
{
    Playlist[] replaylist;
    replaylist = new Playlist[playlist.length+1];

    for(int i = 0; i < playlist.length; i++)
    {
        replaylist[i] = playlist[i];
    }

    playlist = replaylist;
}

public static void copyFile()
{
    String fileName = "out.text";
    PrinterWriter outputStream = null;

    try
    {
        outputStream = new PrintWriter (fileName);
    }
    catch (FileNotFoundException e)
    {
        System.out.println ("error opening the file");
        System.exit (0);
    }

    for(int i = 0; i < md.total; i++)
    {
        outputStream.println(md.getName(i) + line);
        outputStream.println(md.getDirector(i) + line);
        outputStream.println(md.getFilesize(i) + line);
        outputStream.println(md.getDuration(i) + line);
    }

    outputStream.close();

}

public static void openFile()
{
    String fileName = "readMe";
    System.out.println ("The file " + fileName + "\ncontains the following lines:\n");
    Scanner inputStream = new Scanner (new File (fileName));

    while (inputStream.hasNextLine ())
    {
        if(inputStream.hasNextInt())
        {
            while(inputStream.hasNextInt())
            {
                System.out.print(inputStream.nextInt());
            }
        }
        else
        {
        String line = inputStream.nextLine ();
        System.out.println (line);
        }
    }
    inputStream.close ();
}

public static void showSelection()
{
    System.out.println("1.Add Movies");
    System.out.println("2.Show All Movies Currently Listed");
    System.out.println("3.Delete a Movie");
    System.out.println("4.Edit an Existing Movie");
    System.out.println("5.Show All Movies from certain Director");
    System.out.println("6.Show all Movies under a certain Duration");
    System.out.println("7.Create a Playlist");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("13.Save Current movie database to a file");
    System.out.println("14.Open a movie database from a file");
    System.out.println("15.End Program");
}

}

    public class Movie
    {
    private String name;
    private String director;
    private double filesize;
    private double duration;

    public Movie()
    {
        name = "";
        director = "";
        filesize = 0;
        duration = 0;
    }

    //setting the method name
    public void setName(String name)
    {
        name = name;
    }

    //getting the method name
    public String getName()
    {
        return name;
    }

    //setting the method director 
    public void setDirector(String director)
    {
        director = director;
    }

    //getting the method director
    public String getDirector()
    {
        return director;
    }

    // setting the method filesize
    public void setFilesize(double filesize)
    {
        filesize = filesize;
    }

    //getting the method filesize
    public double getFilesize()
    {
        return filesize;
    }

    //setting the method duration
    public void setDuration(double duration)
    {
        duration = duration;
    }

    //getting the method duration
    public double getDuration()
    {
        return duration;
    }

}

    public class MovieDatabase
    {
    final private int MAX = 5;
    private Movie[] movies;
    private int total;

    public MovieDatabase()
    {
        movies = new Movie[MAX];
        total = 0;
    }

    public void addMovie(String newName, String newDirector, double newFileSize, double newDuration)
    {
        if(check(newName) == 0)
        {
            movies[total] = new Movie();
            setMovie(movies[total],newName,newDirector,newFileSize,newDuration);
            total++;

            if(total == movies.length)
            {
                increaseResize();
            }
        }

        else if(check(newName) == 1)
            System.out.println("Movie already exists");
    }

    private void setMovie(Movie m,String newName, String newDirector, double newFileSize, double newDuration)
    {
        m.setName(newName);
        m.setDirector(newDirector);
        m.setFilesize(newFileSize);
        m.setDuration(newDuration);
    }

    public int movieTotal()
    {
        return total;
    }

    public double check(String name)
    {
        if(total > 0)
        {
            for(int i = 0; i < total; i++)
            {
                if(movies[i].getName() == name)
                {   
                    return 1;
                }

                else 
                    return 0;
            }
        }

        else
            return 0;
    }

    public String getMovie1(String name)
    {
        int i = position(name);
        setMovie = movies[i].getName();
        return setMovie;
    }

    public getMovie(int i)
    {
        String nom = movies[i].getName();
        return nom;
    }

    public getDirector(int i)
    {
        String nom = movies[i].getDirector();
        return nom;
    }

    public getFilesize(int i)
    {
        String nom = movies[i].getFilesize();
        return nom;
    }

    public getDuration(int i)
    {
        String nom = movies[i].getDuration();
        return nom;
    }

    private void increaseResize()
    {
        Movie [] removie;
        removie = new Movie[movies.length+1];

        for(int i = 0; i < movies.length; i++)
        {
            removie[i] = movies[i];
        }

        movies = removie;
    }

    public void list(int n,String name,double number)
    {
        //display all Movies
        if(n == 1)
        {
            if(total>0)
            {
                for(int i = 0; i < total; i++)
                {
                    System.out.println(movies[i].getName());
                }
            }

            else 
            {
                System.out.println("Cannot Display Movies if you haven't saved any yet.");
            }
        }
        //Display all Movies under certain Director
        else if(n == 2)
        {
            if(total > 0)
            {
                for(int i = 0; i < total; i++)
                {
                    if(movies[i].getDirector() == name)
                    {
                        System.out.println(movies[i].getName());
                    }
                }
            }

            else
            {
                System.out.println("Either no Movies exist or their are no movies under that Directors name");
            }           
        }
        //Display all Movies under certain Durtation
        else if(n == 3)
        {
            if(total > 0)
            {
                for(int i = 0; i < total; i++)
                {
                    if(movies[i].getDuration() == number)
                    {
                        System.out.println(movies[i].getName());
                    }
                }
            }

            else
            {
                System.out.println("Either no Movies exist or their are no movies under that Directors name");
            }
        }   
    }


    public void edit(String name, String newName, String newDirector, double newFileSize, double newDuration)
    {
        int p;
        p = position(name);
        movies[p] = new Movie();


            for(int i = -2; i < total -1; i++)
            {
                if(p == -1)
                {
                    System.out.println("The Movie you wish to delete does not exist");
                }

                else if(i == p)
                {
                    setMovie(movies[p],newName,newDirector,newFileSize,newDuration);
                }

            }
    }


    public void delete(String name)
    {
        int p;
        p = position(name);
        if(p == -1)
        {
            System.out.println("The Movie you wish to delete does not exist");
        }

        else
        {
            for(int i = p; i < total -1; i++)
            {
                movies[i] = movies[i+1];
                total --;
            }
        }
    }

    private int position(String name)
    {
        for(int i = 0; i < total; i++)
        {
            if(movies[i].getName() == name)
                return i;
        }
        return -1;
    }

}

    public class Playlist
    {
    final private int MAX = 4;
    final private int MAX_TIME = 1000;
    final private int MAX_SIZE = 20000;
    private Movie[] movies;
    private int total;


    public Playlist()
    {
        movies = new Movie[MAX]
    }


}

`

2 个答案:

答案 0 :(得分:0)

由于您需要在Playlist中使用Storage的dinamic数组,因此您只需使用List<Playlist>

类似的东西:

List<Playlist> storage = new ArrayList<Playlist>();

System.out.println("How many movies do you wish to save in this playlist?");
num int = input.nextInt();
Playlist p = new Playlist();

for(int i = 0; i < num; i ++)
{
    System.out.println("Type in the next movie:").
    String name = nextLine;
    //Verifications
    if(md.check(name) == 1)
        storage[i] = md.getMovie1(name);
    else if(md.check(name) == 0)
    {
        System.out.println("Movie you typed does not exist");
        i--;
    }
    //Add the movie to Playlist
    p.add(md.getMovie1(name);
}
storage.add(p);

答案 1 :(得分:0)

猜测您的MD类返回Storage

类型的东西
 public static void playlist()
 {   
  final List<Object> playlist = new ArrayList();

  final Scanner finput = new Scanner(System.in);
  String input;

  System.out.println("How many movies do you wish to save in this playlist?");

  int num = input.nextInt();

  // final List<Object> storage = Storage.getStorage();

  for(int i = 0; i < num; i ++)
  {
     System.out.println("Type in the next movie:").
     String name = finput.nextLine();

     if(md.check(name)==1)
     {
         playlist.add(md.getMovie1(name));
     }   

     else
     {
         System.out.println("Movie you typed does not exist");
         i--;
     }
  }

 }

如果不是playlist.add(),您可以添加storage.get(name)

但是没有从你的代码中看到更多,只是在猜测。