所以我的问题是:我可以将任何两个对象等同,即使其中一个是后来可能构造的对象吗? 例如:在下面的代码中,我有一个电影创作课,这一切都很好。我没有问题(它只是在这里用于上下文)真正的问题是在Movie Store类中。 Movie Store类初始化了三个电影对象:Batman,Serenity和Clueless 我想编写一个名为rentMovie(String title)的方法,在调用时将检查该参数的标题字符串是否与其中一个电影的标题字符串匹配。我知道有一种方法可以做到:
if(title.equals("Batman")) //The title being any one of the initialized movies
{
movie1.rentMovie(); //rentMovie() also exists in class Movie and is the actual method which does the calculations
}
然而,这将需要多个if语句,每次我想在MovieStore类中构造一个新的电影,我将不得不添加一行新的代码。那么有没有办法让这个“全局”(没有公共字段),这样我就不必定义一个特定的电影标题String for rentMovie(String title)等于?
这是Movie课程:
public class Movie
{
private String title;
private int cost;
private int inStock;
private int totalRentals;
public Movie(String title, int cost)
{
this.title = title;
this.cost = cost;
inStock = 5+(int)(Math.random()*((15-5)+1)); //random value in interval [5-15]
totalRentals = 0;
}
public void rentMovie(String title)throws Exception
{
if(this.title.equals(title) && inStock>0)
{
totalRentals++;
inStock--;
}
else if(!this.title.equals(title))
{
throw new Exception("False. movie rented does not match movie given");
}
}
public int getCost()
{
return cost;
}
public int getTotalRentals()
{
return totalRentals;
}
public void printDetails()
{
System.out.println("----------------");
System.out.println("Title:\t\t"+title);
System.out.println("Cost:\t\t"+cost);
System.out.println("In Stock:\t"+inStock);
System.out.println("Rented:\t\t"+totalRentals);
System.out.println("----------------");
}
}
这是电影商店类:(我已经把if语句基本上不完整了,因为我在这里做什么不知所措)
public class MovieStore
{
private Movie movie1;
private Movie movie2;
private Movie movie3;
public MovieStore()
{
movie1 = new Movie("Batman",3);
movie2 = new Movie("Clueless",5);
movie3 = new Movie("Serenity",4);
}
public void rentMovie(String title)
{
if(title.equals())
{
//not sure what to put here.rentMovie(title);
// there is a bit of a redundacy check here because of Movie class rentMovie() method
//but I NEED to check the movie titles for equality here first that way I'm sure that rentMovie()
//from Movie class will run.
System.out.println("YES"); //used this just to check if what I was doing was working
}
}
public void printDetails()
{
movie1.printDetails();
movie2.printDetails();
movie3.printDetails();
int total = (movie1.getCost()*movie1.getTotalRentals())+(movie2.getCost()*movie2.getTotalRentals())+(movie3.getCost()*movie3.getTotalRentals());
System.out.println(" Total Revenues for Rentals were: " + "$"+total);
}
}
答案 0 :(得分:1)
目前使用电影名称查找电影对象,因此基于查找的解决方案应该有效,
实施例: 创建一个HashMap,键为lower_case(movie-name),值为Movie对象,这将用于查找/查找电影是否可用。
让movieMap成为HashMap
类似以下内容:
Movie movie = movieMap.get(title.toLowerCase());
if (movie == null) {
// print error message
} else {
System.out.println("YES");
}
答案 1 :(得分:0)
Movie
类型已经有了'标题。相反,您的Movie
方法rentMovie()
应该看起来更像,
public boolean rentMovie() {
if (inStock > 0) {
totalRentals++;
inStock--;
return true;
}
return false;
}
然后MovieStore
可以使用List
部电影,
public class MovieStore {
private List<Movie> movies = new ArrayList<>();
public MovieStore() {
movies.add(new Movie("Batman", 3));
movies.add(new Movie("Clueless", 5));
movies.add(new Movie("Serenity", 4));
}
public boolean rentMovie(String title) {
for (Movie m : movies) {
if (m.getTitle().equalsIgnoreCase(title)) {
return m.rentMovie();
}
}
return false;
}
public void printDetails() {
int total = 0;
for (Movie m : movies) {
m.printDetails();
total += m.getCost() * m.getTotalRentals();
}
System.out.println(" Total Revenues for Rentals were: " + "$"+total);
}
}