gpt4 book ai didi

java - 检查 future 的对象是否相等

转载 作者:行者123 更新时间:2023-12-01 12:25:31 25 4
gpt4 key购买 nike

所以我的问题是:我可以将任意两个对象等同起来,即使其中一个对象可以稍后构造?例如:在下面的代码中,我有一个电影创作类,这一切都很好。我对此没有任何问题(只是为了上下文)真正的问题是在电影商店类中。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 类中构造新电影时,我都必须添加一行新代码。那么有什么方法可以使其成为“全局”(没有公共(public)字段),这样我就不必为rentMovie(String title)定义一个特定的电影标题字符串来等于?

这是电影类:

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

最佳答案

目前,电影对象是使用电影名称查找的,因此基于查找的解决方案应该可以工作,

示例:创建一个 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");
}

关于java - 检查 future 的对象是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com