0

I have a class that acts as a bettor, and in that class the bettor can place a bet in a method that creates a bet. However, the constructor of the bet class needs to take in the same reference of that bettor from the bettor class. How does one go about doing that?

Here is the code I was trying to use for this. I realize that making a new reference of the bettor class, but I thought I'd give it a try anyways

public Bet placeBet(Bet.BetType betType, double amount)
{
    if(betType.equals(Bet.BetType.passBet))
    {
        this.bankroll=bankroll-amount;
        return new PassBet(new Bettor(this.name,this.bankroll),amount);
    }
    else if(betType.equals(Bet.BetType.any7))
    {
        this.bankroll=bankroll-amount;
        return new Any7Bet(new Bettor(this.name,this.bankroll),amount);
    }
    else if(betType.equals(Bet.BetType.hard8)||betType.equals(Bet.BetType.hard10))
    {
        this.bankroll=bankroll-amount;
        return new HardWayBet(new Bettor(this.name,this.bankroll),amount);
    }
    return null;
}

while the PassBet Class looks as such (it is a subclass of the Bet class, which hold the Bettor reference and the amount bet).

public PassBet(Bettor b, double amount)
{
    super(b,amount);
}

How would I go about passing the original Bettor as an argument into my PassBet subclass, which is then stored in superclass Bet?

  • You're question isn't 100% clear to me. Based on your code, you must know that `this` points to the current instance, since you're using it to access fields. You can also simply pass `this` to another method/constructor like any other reference - does that answer the question? – Paul Bellora Apr 03 '13 at 02:20
  • @PaulBellora passing *this* in the constructor is really a bad idea. Check out [here](http://stackoverflow.com/questions/2419410/passing-this-in-java-constructor) and [here](http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html). – Jayamohan Apr 03 '13 at 02:22
  • @Jayamohan Yes, I'm aware of that - but it would be passed from `placeBet` if I understand correctly. I'm just trying to clarify the question, otherwise that would've been an answer. – Paul Bellora Apr 03 '13 at 02:24
  • Normally, classes represent objects in the problem domain, and methods for that class represent actions done to that object. It seems to me that 'placeBet' is not an action performed on a Bettor. You might want to rethink what classes belong in your system. You can make the code work this way, but you might find it easier and cleaner following basic OO principles. And you might look into enums in Java. It will take you a bit of time, perhaps a day if you're not familiar with the concept already, and it will be *well* worth it. – arcy Apr 03 '13 at 02:26
  • @PaulBellora Yes it does, I feel slightly idiotic now. I'm new to java, but for some reason that didn't cross my mind. Thank you very much! – user2238406 Apr 03 '13 at 02:28
  • @user2238406 No problem, I've added an answer based on my comment in case you'd like to accept it. – Paul Bellora Apr 03 '13 at 02:41

2 Answers2

2

You can pass this to another method/constructor like any other reference. For example:

public Bet placeBet(Bet.BetType betType, double amount)
{
    if(betType.equals(Bet.BetType.passBet))
    {
        this.bankroll=bankroll-amount;
        return new PassBet(this,amount);
    }
    else if(betType.equals(Bet.BetType.any7))
    {
        this.bankroll=bankroll-amount;
        return new Any7Bet(this,amount);
    }
    else if(betType.equals(Bet.BetType.hard8)||betType.equals(Bet.BetType.hard10))
    {
        this.bankroll=bankroll-amount;
        return new HardWayBet(this,amount);
    }
    return null;
}

As an aside, I'd recommend against returning null when none of the conditions match. Consider throwing an IllegalArgumentException, for example. Like rcook pointed out, BetType seems like a good candidate for an enum, in which case you could use a switch instead of else if, etc. Here's a rough example:

public enum BetType {
    PASS,
    ANY_7,
    HARD_8,
    HARD_10;
}

...

public Bet placeBet(BetType betType, double amount) {

    final Bet bet;

    switch (betType) {
        case PASS:
            bet = new PassBet(this, amount);
            break;
        case ANY_7:
            bet = new Any7Bet(this, amount);
            break;
        case HARD_8:
        case HARD_10:
            bet = new HardWayBet(this, amount);
            break;
        default:
            throw new IllegalArgumentException("Invalid bet type: " + betType + ".");
    }

    this.bankroll -= amount;

    return bet;
}

This solution could still be more object-oriented, for example why not give BetType a method for creating a Bet? We could take advantage of polymorphism:

public enum BetType {
    PASS {
        @Override
        public Bet makeBet(Bettor bettor, double amount) {
            return new PassBet(bettor, amount);
        }
    },
    ANY_7 {
        @Override
        public Bet makeBet(Bettor bettor, double amount) {
            return new Any7Bet(bettor, amount);
        }
    },
    HARD_8 {
        @Override
        public Bet makeBet(Bettor bettor, double amount) {
            return new HardWayBet(bettor, amount);
        }
    },
    HARD_10 {
        @Override
        public Bet makeBet(Bettor bettor, double amount) {
            return new HardWayBet(bettor, amount);
        }
    };

    public abstract Bet makeBet(Bettor bettor, double amount);
}

...

public Bet placeBet(BetType betType, double amount) {

    this.bankroll -= amount;

    return betType.makeBet(this, amount);
}

This might not even be the best solution but I hope it at least opens up some possibilities for you.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1

just pass this keyword as the bettor instance.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • Passing *this* in the constructor is really a bad idea. Check out [here](http://stackoverflow.com/questions/2419410/passing-this-in-java-constructor) and [here](http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html). – Jayamohan Apr 03 '13 at 02:22
  • @Jayamohan thats true from a design point of view, but this answers his question. – Pradeep Pati Apr 03 '13 at 02:26
  • @Jayamohan I wonder if you have actually read and understood the link you provided? placeBet() is not the constructor of Bettor, and we are not passing "this" to other method/ctor in the constructor of Bettor. – Adrian Shum Apr 03 '13 at 02:47