我正在创建一个简单的电影数据库,它有两个类,即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时,主要的方法不应该改变,输出应该和上面一样。如果你需要这些方法,请更改代码,因为我的想法似乎不起作用。任何帮助,将不胜感激。
哦,这么长的描述。 :( – Nishant 2011-04-05 18:18:43
http://sscce.org/ – 2011-04-05 18:19:06
你需要使用数组吗? – justkt 2011-04-05 18:20:53