在java中从对象数组添加和检索元素

在java中从对象数组添加和检索元素,java,arrays,Java,Arrays,我正在创建一个简单的电影数据库,其中包含两个类,即电影和库。库类有一个addMovie方法,该方法将电影对象添加到电影对象数组中。另一种方法borrowMovie从电影对象数组中检索电影,returnMovie将电影对象返回到数组中。它表示电影已被返回,borrowMovie的工作方式应该是,除非电影已被返回,否则无法将其借用两次,如果有人试图借用它。它将显示其已借用,如果其不在电影数组中,则应指示该电影不在目录中。 另一个方法printAvailableMovies应该打印库中的所有电影 Mo

我正在创建一个简单的电影数据库,其中包含两个类,即电影和库。库类有一个addMovie方法,该方法将电影对象添加到电影对象数组中。另一种方法borrowMovie从电影对象数组中检索电影,returnMovie将电影对象返回到数组中。它表示电影已被返回,borrowMovie的工作方式应该是,除非电影已被返回,否则无法将其借用两次,如果有人试图借用它。它将显示其已借用,如果其不在电影数组中,则应指示该电影不在目录中。 另一个方法printAvailableMovies应该打印库中的所有电影

Movie.java

public class Movie {

    public String title;
    boolean borrowed,returned;

    // Creates a new Movie
    public Movie(String movieTitle) {
        
       title = movieTitle;
    }
   
    
    // Marks the movie as rented
    public void borrowed() {
        
        borrowed = true;
    }
   
    // Marks the movie as not rented
    public void returned() {
        
           returned = true;
    }
   
    // Returns true if the movie is rented, false otherwise
    public boolean isBorrowed() {
        
        if(borrowed && !returned)
          return true;
          else
          return false;
         
    }
   
    // Returns the title of the movie
    public String getTitle() {
       
        
      return title;
    }

    public static void main(String[] arguments) {
        // Small test of the Movie class
        Movie example = new Movie("Christmas in Kampala");
        System.out.println("Title (should be Christmas in Kampala): " + example.getTitle());
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        example.borrowed();
        System.out.println("Borrowed? (should be true): " + example.isBorrowed());
        example.returned();
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }
} 
Library.java

public class Library {
    
    String address;
    boolean borrowMovie,returnMovie;
    Movie[] libMovies =new Movie[5] ;
    
    public Library(String libraryAddress){
           address = libraryAddress;
    }
    
    public void addMovie(Movie... movies ){
       int i = 0;
       for( Movie item: movies){
       libMovies[i] = item;}
    }
    public void borrowMovie(String movieTitle){
        for(Movie item: libMovies){
        if(movieTitle.equals(item.title))
        borrowMovie = true;
         else
         System.out.println("Sorry, this movie is not in our catalog.");
         }
        if(borrowMovie&&!returnMovie)
        System.out.println("You have successfully borrowed " + movieTitle);
        else
        System.out.println("Sorry, this movie is already borrowed.");
        
    }*/
    public void returnMovie(String movieTitle){
           returnMovie = true;
           System.out.println("You successfully returned " + movieTitle);
           }
      
    public static void printOpeningHours(){
       System.out.println ("Libraries are open daily from 9am to 5pm.");
    }
    public void printAddress(){
          System.out.println( address);
    }
    public void printAvailableMovies(){
           
       for( int i =0;i < libMovies.length;i++){
       System.out.println(libMovies[i].getTitle());}
      // if(item == null)
       //System.out.println("No movie in catalog.");

    }
    public static void main(String[] args) {
        // Create two libraries
        Library firstLibrary = new Library("Plot 11, Kafene Road");
        Library secondLibrary = new Library("Plot 28, Kayembe Road");

        // Add four movies to the first library
        firstLibrary.addMovie(new Movie("Yogera"));
        firstLibrary.addMovie(new Movie("The Last King of Scotland"));
        firstLibrary.addMovie(new Movie("The Hostel"));
        firstLibrary.addMovie(new Movie("Christmas in Kampala"));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        printOpeningHours();
        System.out.println();
        
        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        
        // Try to borrow Christmas in Kampala from both libraries
        System.out.println("Borrowing Christmas in Kampala:");
        firstLibrary.borrowMovie("Christmas in Kampala");
        firstLibrary.borrowMovie("Christmas in Kampala");
        secondLibrary.borrowMovie("Christmas in Kampala");
        System.out.println();

        Print the titles of all available movies from both libraries
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();
        System.out.println();
        System.out.println("Movies available in the second library:");
        secondLibrary.printAvailableMovies();
        System.out.println();

        // Return Christmas in Kampala to the first library
        System.out.println("Returning Christmas in Kampala:");
        firstLibrary.returnMovie("Christmas in Kampala");
        System.out.println();

        // Print the titles of available movies from the first library
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();
        
    }
} 
公共类库{
字符串地址;
布尔电影,返回电影;
电影[]libMovies=新电影[5];
公共图书馆(字符串图书馆地址){
地址=图书馆地址;
}
公共电影(电影…电影){
int i=0;
用于(电影项目:电影){
libMovies[i]=item;}
}
公共电影(字符串电影){
用于(电影项目:libMovies){
if(电影名称等于(项目名称))
电影=真;
其他的
对不起,这部电影不在我们的目录中;
}
如果(借用电影和返回电影)
System.out.println(“您已成功借用”+电影字幕);
其他的
System.out.println(“对不起,这部电影已经借来了。”);
}*/
公共电影(字符串电影){
returnMovie=true;
System.out.println(“您成功返回”+movieTitle);
}
公共静态void printOpeningHours(){
System.out.println(“图书馆每天从上午9点到下午5点开放”);
}
公共地址(){
系统输出打印项次(地址);
}
public void printAvailableMovies(){
for(int i=0;i
Library.java的主要方法应该输出

图书馆时间:

图书馆每天从上午9点到下午5点开放

图书馆地址:

加分道11号地块

坎帕拉圣诞节:

你在坎帕拉成功地借到了圣诞节

对不起,这部电影已经借来了

对不起,这部电影不在我们的目录中

第一个库中提供的电影:

约格拉

苏格兰最后的国王

旅馆

第二个库中提供的电影:

目录中没有电影

坎帕拉回归圣诞节:

你在坎帕拉成功地回到了圣诞节

第一个库中提供的电影:

约格拉

苏格兰最后的国王

旅馆

坎帕拉的圣诞节


现在Movie.Java工作得很好,不需要修改,但是Library.Java是一个主要的痛苦来源,因为我只能让它工作到打印库地址。方法borrowMovie、addMovie、returnMovie和printAvailableMovies是罪魁祸首,因为它们涉及操纵电影对象数组。测试Library.java时,主方法不应更改,输出应如上所述。如果必须更改这些方法的代码,请更改代码,因为我的想法似乎不起作用。任何帮助都将不胜感激。

如果您需要添加和删除内容,最好使用数组而不是数组。这会让你的生活轻松很多

基本列表示例:

List<String> ls = new ArrayList<String>();
ls.add("one");
ls.add("Three");
ls.add("two");
ls.add("four");

for(String value : ls) {
  System.out.println("Value :"+value);
}

ls.remove("two");

for(String value : ls) {
  System.out.println("Value :"+value);
}
List ls=new ArrayList();
ls.添加(“一”);
ls.添加(“三”);
ls.添加(“两”);
ls.添加(“四”);
for(字符串值:ls){
System.out.println(“值:”+Value);
}
ls.删除(“两个”);
for(字符串值:ls){
System.out.println(“值:”+Value);
}
我在t上看到的问题
public class Library {

    String address;
    boolean borrowMovie,returnMovie;
    Movie[] libMovies =new Movie[5] ;
    int count = 0;

    public Library(String libraryAddress){
           address = libraryAddress;
    }

    public void addMovie(Movie... movies ){
       for( Movie item: movies){
       libMovies[count++] = item;}
    }

    public void borrowMovie(String movieTitle) {
        for (Movie item : libMovies) {
            if (movieTitle.equals(item.title)) {
                borrowMovie = true;
                break;
            }

        }

        if(!borrowMovie) {
            System.out.println("Sorry, this movie is not in our catalog.");
            return;
        }


        if (borrowMovie && !returnMovie)
            System.out.println("You have successfully borrowed " + movieTitle);
        else
            System.out.println("Sorry, this movie is already borrowed.");

    }
    public void returnMovie(String movieTitle){
           returnMovie = true;
           System.out.println("You successfully returned " + movieTitle);
           }

    public static void printOpeningHours(){
       System.out.println ("Libraries are open daily from 9am to 5pm.");
    }
    public void printAddress(){
          System.out.println( address);
    }
    public void printAvailableMovies(){

       for( int i =0;i < libMovies.length;i++){
           if(libMovies[i] != null)
               System.out.println(libMovies[i].getTitle());}
      // if(item == null)
       //System.out.println("No movie in catalog.");

    }
    public static void main(String[] args) {
        // Create two libraries
        Library firstLibrary = new Library("Plot 11, Kafene Road");
        Library secondLibrary = new Library("Plot 28, Kayembe Road");

        // Add four movies to the first library
        firstLibrary.addMovie(new Movie("Yogera"));
        firstLibrary.addMovie(new Movie("The Last King of Scotland"));
        firstLibrary.addMovie(new Movie("The Hostel"));
        firstLibrary.addMovie(new Movie("Christmas in Kampala"));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        printOpeningHours();
        System.out.println();

        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();


        // Try to borrow Christmas in Kampala from both libraries
        System.out.println("Borrowing Christmas in Kampala:");
        firstLibrary.borrowMovie("Christmas in Kampala");
        firstLibrary.borrowMovie("Christmas in Kampala");
//        secondLibrary.borrowMovie("Christmas in Kampala");
        System.out.println();

//        Print the titles of all available movies from both libraries
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();
        System.out.println();
        System.out.println("Movies available in the second library:");
        secondLibrary.printAvailableMovies();
        System.out.println();

        // Return Christmas in Kampala to the first library
        System.out.println("Returning Christmas in Kampala:");
        firstLibrary.returnMovie("Christmas in Kampala");
        System.out.println();

        // Print the titles of available movies from the first library
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();

    }
}
public class Movie
{
    public String title;
    boolean borrowed;

    // Creates a new Movie
    public Movie(String movieTitle)
    {
        title = movieTitle;
    }

    // Marks the movie as rented
    void borrow() throws Exception
    {
        if (this.borrowed)
        {
            throw new Exception("The movie <" + this.title + "> is already borrowed - cannot borrow it again");
        }
        else
        {
            this.borrowed = true;
        }
    }

    // Marks the movie as not rented
    void returnMovie() throws Exception
    {
        if (this.borrowed)
        {
            this.borrowed = false;
        }
        else
        {
            throw new Exception("The movie <" + this.title + "> has not been borrowed - it cannot be returned");
        }
    }

    // Returns true if the movie is rented, false otherwise
    public boolean isBorrowed()
    {
        return this.borrowed;
    }

    // Returns the title of the movie
    public String getTitle()
    {
        return title;
    }

    public static void main(String[] arguments) throws Exception
    {
        // Small test of the Movie class
        Movie example = new Movie("Christmas in Kampala");
        System.out.println("Title (should be Christmas in Kampala): " + example.getTitle());
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        example.borrow();
        System.out.println("Borrowed? (should be true): " + example.isBorrowed());
        example.returnMovie();
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Library
{
    String address;
    Map<String, Movie> movieLibrary;

    public Library(String libraryAddress)
    {
        address = libraryAddress;
        this.movieLibrary = new HashMap<String, Movie>();
    }

    public void addMovie(Movie... movies)
    {
        for (Movie item : movies)
        {
            this.movieLibrary.put(item.getTitle(), item);
        }
    }

    public void borrowMovie(String movieTitle)
    {
        Movie movie = this.movieLibrary.get(movieTitle);

        if (movie == null) // Not in libray
        {
            System.out.println("Sorry, this movie is not in our catalog.");
        }
        else
        {
            if (movie.isBorrowed())
            {
                System.out.println("Sorry, this movie is already borrowed.");
            }
            else
            {
                try
                {
                    movie.borrow();
                    System.out.println("You have successfully borrowed " + movieTitle);
                }
                catch (Exception e)
                {
                    System.out.println("An internal error has occured while attempting to borrow " + movieTitle + ", error details: " + e.getMessage());
                }
            }
        }
    }

    public void returnMovie(String movieTitle)
    {
        Movie movie = this.movieLibrary.get(movieTitle);

        if (movie == null) // Not in libray
        {
            System.out.println("Sorry, this movie is not in our catalog.");
        }
        else
        {
            if (movie.isBorrowed())
            {
                try
                {
                    movie.returnMovie();
                    System.out.println("You successfully returned " + movieTitle);
                }
                catch (Exception e)
                {
                    System.out.println("An internal error has occured while attempting to return " + movieTitle + ", error details: " + e.getMessage());
                }
            }
            else
            {
                System.out.println("Sorry, this movie is has not been borrowed.");
            }
        }
    }

    public static void printOpeningHours()
    {
        System.out.println("Libraries are open daily from 9am to 5pm.");
    }

    public void printAddress()
    {
        System.out.println(address);
    }

    public void printAvailableMovies()
    {
        Collection<Movie> movies = this.movieLibrary.values();

        if (movies.size() > 0)
        {
            for (Movie movie : movies)
            {
                if (!movie.isBorrowed())
                {
                    System.out.println(movie.getTitle());
                }
            }
        }
        else
        {
            System.out.println("No movie in catalog.");
        }

    }

    public static void main(String[] args)
    {
        // Create two libraries
        Library firstLibrary = new Library("Plot 11, Kafene Road");
        Library secondLibrary = new Library("Plot 28, Kayembe Road");

        // Add four movies to the first library
        firstLibrary.addMovie(new Movie("Yogera"));
        firstLibrary.addMovie(new Movie("The Last King of Scotland"));
        firstLibrary.addMovie(new Movie("The Hostel"));
        firstLibrary.addMovie(new Movie("Christmas in Kampala"));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        printOpeningHours();
        System.out.println();

        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        // Try to borrow Christmas in Kampala from both libraries
        System.out.println("Borrowing Christmas in Kampala:");
        firstLibrary.borrowMovie("Christmas in Kampala");
        firstLibrary.borrowMovie("Christmas in Kampala");
        secondLibrary.borrowMovie("Christmas in Kampala");
        System.out.println();

        // Print the titles of all available movies from both libraries
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();
        System.out.println();
        System.out.println("Movies available in the second library:");
        secondLibrary.printAvailableMovies();
        System.out.println();

        // Return Christmas in Kampala to the first library
        System.out.println("Returning Christmas in Kampala:");
        firstLibrary.returnMovie("Christmas in Kampala");
        System.out.println();

        // Print the titles of available movies from the first library
        System.out.println("Movies available in the first library:");
        firstLibrary.printAvailableMovies();

    }
}