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

标签 java string object equality

所以我的问题是:我可以将任意两个对象等同起来,即使其中一个对象可以稍后构造? 例如:在下面的代码中,我有一个电影创作类,这一切都很好。我对此没有任何问题(只是为了上下文)真正的问题是在电影商店类中。 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/

相关文章:

java - 如何管理团队的 Eclipse 插件、首选项和配置?

java - 将 JLabel 数组导入 JComponent

java - 在 AWS Lambda Java 项目中从 API 网关获取映射模板作为 JSON

string - 如何在 os.Args 中保留引号

Java 对象本身在数组中

Java:结合泛型、内部类和 "implements"的问题

python - 不使用内置函数将python字典转换为xml字符串

javascript - 类和数组真的只是 JavaScript 中的对象吗?

javascript - PHP 获取对象键

java - 使用带有自定义比较器的优先级队列根据字符串中的单词数进行排序?