2011-04-05 82 views
0

我正在創建一個簡單的電影數據庫,它有兩個類,即Movie和Library.Library類,它有一個方法addMovie,它將電影對象添加到Movie對象數組中。另一種方法borrowMovie從電影對象數組中檢索電影,並且returnMovie將電影對象返回到數組中。它表明電影已經被返回,borrowMovie應該以不能借用電影兩次的方式工作,除非它已經被返回如果有人試圖借用它。它會顯示它已經借用,如果它不在電影數組中,它應該表明該電影不在目錄中。 另一種方法printAvailableMovies應該打印庫中的所有電影。這裏是我的電影和圖書館類的示例代碼。從java中的對象數組中添加和檢索元素

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(); 

    } 
} 

爲Library.java的主要方法應輸出

開館時間:

圖書館每天開放從上午9點到下午5點。

圖書館地址:

地塊11,Kafene路

在坎帕拉借用聖誕:

您已成功借聖誕節在坎帕拉

對不起,這部電影已經是借來的。

對不起,這部電影不在我們的目錄。

Yogera

蘇格蘭

宿舍的末代國王

電影可供選擇第二個庫:

在第一庫中的可用

電影

沒有電影目錄

Ret urning聖誕節在坎帕拉:

Yogera

最後的蘇格蘭王

宿舍

你成功地在第一庫返回聖誕節在坎帕拉

電影可用聖誕節在坎帕拉。

Now Movie。Java完美工作,不需要修改,但Library.java是一個主要的痛苦來源,因爲我只能使它工作到打印庫地址。方法borrowMovie,addMovie,returnMovie和printAvailableMovies是罪魁禍首,因爲它們涉及操作Movie對象的數組。當你測試Library.java時,主要的方法不應該改變,輸出應該和上面一樣。如果你需要這些方法,請更改代碼,因爲我的想法似乎不起作用。任何幫助,將不勝感激。

+1

哦,這麼長的描述。 :( – Nishant 2011-04-05 18:18:43

+1

http://sscce.org/ – 2011-04-05 18:19:06

+1

你需要使用數組嗎? – justkt 2011-04-05 18:20:53

回答

3

我做了一些修改 - 基本上我刪除了不必要的代碼從Movie,改變了庫是基於Map因爲你只需要每個標題的一個副本 - 這將使搜索更快。我添加了一些「錯誤處理」 - 但我認爲你可以刪除它們並用System.err.println代替這段代碼。

Movie.java:

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()); 
    } 
} 

Library.java:

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(); 

    } 
} 

如果你關心電影的順序 - 使用ArrayListLinkedList代替HashMap(並更改movieLibray成員從MapList

1

如果您需要添加和刪除東西,最好使用List而不是數組。它會讓你的生活變得更輕鬆。

基本列表示例:

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); 
} 

我第一遍看到問題:

addMovie()從未遞增i,所以你永遠不會寫任何東西,除了數組的第一個索引。此方法也非常不安全,因爲傳入比您創建的數組長的電影列表會非常容易。這讓我回到我的第一點。

如果方法是借用或返回的,您正在使用實例變量,但您可以借用一部電影。

+0

我如何使用列表,而不是我的列表工作 – 2011-04-05 18:35:40

+0

@WasswaSamuel:增加了一個例子,看看界面,它非常直前鋒。 – unholysampler 2011-04-05 19:47:42

0

立即試用:

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(); 

    } 
}