-3

I'm coding a program to sell and buy games. I have 3 classes: Main, Person and Game. What I want it to do: Check if the (Game g) is already in the (ArrayList games), basically check for the same values. If yes, then it should return true. If false, it should return false. How can I achieve this?

My problem: For the p1.koop(g3) it still says it succeeded. However, the g2 and g3 are the same. Therefor it should not succeed.

My main:

package week3.practicum;

import java.time.LocalDate;  
public class Practicum2 {   
    public static void main(String[] args) {     
    int releaseJaar1 = LocalDate.now().getYear() - 1; // 1 jaar geleden     
    int releaseJaar2 = LocalDate.now().getYear() - 2; // 2 jaar geleden  

    Game g1 = new Game("Just Cause 3", releaseJaar1, 49.98);     
    Game g2 = new Game("Need for Speed: Rivals", releaseJaar2, 45.99);     
    Game g3 = new Game("Need for Speed: Rivals", releaseJaar2, 45.99);  
    Persoon p1 = new Persoon("Eric", 200.0);     
    Persoon p2 = new Persoon("Hans", 55.0);     
    Persoon p3 = new Persoon("Arno", 185.0);  
    System.out.println("p1 koopt g1:" + (p1.koop(g1) ? "" : " niet") + " gelukt");  
    System.out.println("p1 koopt g2:" + (p1.koop(g2) ? "" : " niet") + " gelukt");     
    System.out.println("p1 koopt g3:" + (p1.koop(g3) ? "" : " niet") + " gelukt");     
    System.out.println("p2 koopt g2:" + (p2.koop(g2) ? "" : " niet") + " gelukt");     
    System.out.println("p2 koopt g1:" + (p2.koop(g1) ? "" : " niet") + " gelukt");     
    System.out.println("p3 koopt g3:" + (p3.koop(g3) ? "" : " niet") + " gelukt");     
    System.out.println("\np1: " +p1+ "\n\np2: " +p2+ "\n\np3: " +p3+ "\n");  

    System.out.println("p1 verkoopt g1 aan p3:"+(p1.verkoop(g1, p3) ? "" : " niet")+" gelukt");     
    System.out.println("p2 verkoopt g2 aan p3:"+(p2.verkoop(g2, p3) ? "" : " niet")+" gelukt");     
    System.out.println("p2 verkoopt g1 aan p1:"+(p2.verkoop(g1, p1) ? "" : " niet")+" gelukt");     
    System.out.println("\np1: " +p1+ "\n\np2: " +p2+ "\n\np3: " +p3+ "\n");   } } 

This is my Person class:

package week3.practicum;
import java.util.ArrayList;

public class Persoon {
    private String naam;
    private Double budget;
    private ArrayList<Game> games;
    private Game gameObj;

    public Persoon(String nm, Double bud){
        naam = nm;
        budget = bud;
        games = new ArrayList<Game>();
    }

    public boolean koop(Game g){
        Double huidW = g.huidigeWaarde();
        if (budget > huidW && games.equals(g) == false){
            games.add(g);
            budget = budget - huidW;
            return true;
        }
        else{return false;}
    }

    public boolean verkoop(Game g, Persoon koper){
        Double huidW = g.huidigeWaarde();

        if (koper.budget > huidW){
            koper.budget = koper.budget = huidW;
            budget = budget + huidW;
            koper.games.add(g);
            games.remove(g);
            return true;
        }
        else{
            return false;
        }
    }

    public String toString(){
        String s = naam + " heeft een budget van " +  budget + " en bezit de volgende games:\n";
        for (Game gam: games){
            s += gam;
        }
        return s;
    }
}

Here's my equals method in the Game class.

package week3.practicum;

public class Game {
    private String naam;
    private Integer releaseJaar;
    private Double nieuwprijs;

    public Game(String nm, Integer rJ, Double nwpr){
        naam = nm;
        releaseJaar = rJ;
        nieuwprijs = nwpr;
    }

    public String getNaam(){
        return naam;
    }

    public Double huidigeWaarde(){
        Double huidigeprijs = 0.0;
        if (releaseJaar == 2016){
            huidigeprijs = nieuwprijs * 0.7;
        }
        else if (releaseJaar == 2015){
            huidigeprijs = nieuwprijs * 0.7 * 0.7;
        }
        else{
            huidigeprijs = nieuwprijs;
        }

        return huidigeprijs;
    }

  public boolean equals(Object andereObject) {     
      boolean gelijkeObjecten = false; // blijft false tenzij:  

    if (andereObject instanceof Game) {       
        Game andereGame = (Game) andereObject;  

      if (this.naam.equals(andereGame.naam)){ 
        gelijkeObjecten = true;       
        }
      }     

    return gelijkeObjecten;   }

    public String toString(){
        String s = naam + ", uitgegeven in " + releaseJaar + "; nieuwprijs: " + nieuwprijs + " nu voor: " + huidigeWaarde() + "\n";
        return s;
    }

}
Some Name
  • 561
  • 6
  • 18
  • Don't forget to implements `hashCode` too. –  Mar 18 '17 at 15:12
  • 1
    What is your Game class? What's the problem with your code? – JB Nizet Mar 18 '17 at 15:12
  • 1
    Right now you have an equals method which returns true if the parameter `andereObject` is the same type, and has the same value for the "naam" property. That looks OK - what is the specific problem you're having? – Don Cheadle Mar 18 '17 at 15:13
  • Added full classes. – Some Name Mar 18 '17 at 15:15
  • So you also need to compare the two other attributes, and only return true if they all match. Easiest way to achieve that, correctly: tell your IDE to generate the method for you. – JB Nizet Mar 18 '17 at 15:40
  • I have no idea how to go about generating that. I tried to google it, but no succes. I'm using NetBeans. – Some Name Mar 18 '17 at 15:45
  • Google is your friend. Gooogle for "Netbeans generate equals". Click on the third or four link: http://stackoverflow.com/questions/38242576/can-netbeans-auto-generate-correct-hashcode-and-equals-methods-for-a-mapping. I'm not a Netbeans user, and it took me literally 5 seconds to find it. – JB Nizet Mar 18 '17 at 15:47
  • It would work if you needed the equals method within the same class, but that's not the case for me/. – Some Name Mar 18 '17 at 15:56
  • The same class as what. You want the Game class to have an equals and a hashCode method. So open the Game class, and do what the link I posted suggests. What's the problem? – JB Nizet Mar 18 '17 at 15:58
  • @SomeName - are you looking for the ArrayList.Contains(object) method? That would tell you if the ArrayList already has the Game. http://stackoverflow.com/questions/2642589/how-does-a-arraylists-contains-method-evaluate-objects – Don Cheadle Mar 18 '17 at 16:47
  • @mmcrae I have one ArrayList games and one object (Game g). I need to see if the object Game g is already in the ArrayList. However if (!games.contains(g)) does not seem to work. – Some Name Mar 18 '17 at 16:58
  • @SomeName ArrayList.Contains() uses the object's equals method. You need to define that method so it correctly, according to your own logic, determines if Game object A is equal to Game object B. This other SO answer discusses List.contains() and how it works a bit more http://stackoverflow.com/questions/2642589/how-does-a-arraylists-contains-method-evaluate-objects. – Don Cheadle Mar 19 '17 at 22:16

2 Answers2

1

games.equals(g) == false Aside from the fact that you don't need to compare a boolean to a boolean to convert the boolean into a boolean, you're comparing a List to a Game, which can never be true.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
  • 1
    So could you give me any insight in how it should be done? – Some Name Mar 18 '17 at 18:41
  • 1
    You think of the types involved. `List` and `Game` can't be the same. But a list of `Game`s might _contain_ a `Game`. In Java that'd be `List games = new ArrayList<>(); /* fill it */ if (games.contains(game)) { /* etc. */ }` – Lew Bloch Mar 18 '17 at 20:53
-1

you are not invoking the equals Method in any way.
To address ur initial need:
1- Create a private method that will take 2 parameters: 1- the games ArrayList , and 2 the game you want to check. LAter for loop and check if the game exist return false.

private boolean doesGameExist(ArrayList gameList, Game gameToAdd){
    for (int i=0; i<gameList.size(); i++){
          if (gameList.get(i).equals(gameToAdd)){
               return true; //the game already exists
          }
    }
   return false; ///the game does not exist
 }

than call this function prior to adding a new game to the array list.

Elio Khattar
  • 320
  • 1
  • 3
  • 16