tags:

views:

121

answers:

3

Is it possible to convert this java code snippet to php?

public void testBalanceReturnsToZeroOnVending()
        {
            sodaVendor.insertCoin(50);
            sodaVendor.insertCoin(20);
            sodaVendor.insertCoin(5);
            // The price is right!
            assertEquals("We have entered correct money", 
                SODA_PRICE,
                sodaVendor.getCurrentBalance());
            sodaVendor.dispenseItem();
            assertEquals("After vending, the balance of soda vending machine is zero", 
                0,
                sodaVendor.getCurrentBalance());
        }
+1  A: 

Any Java code can be converted to PHP

Itay Moav
@Itay Moav: That's not generally true. PHP doesn't support threading for example.
Asaph
@Asaph: Well, you could still pull it off effectively - just emulate co-operative multithreading. Not that I'd recommend it.
Kevin Montrose
@Kevin Montrose: @Itay Moav: Another example of a java program that would not be possible to write in PHP is an Applet.
Asaph
@Applet: I believe that with the correct plugin and GTK you actually can do something like an Applet, it is just that no one ever thought of trying it...
Itay Moav
+5  A: 

Assuming PHPUnit is your unit testing framework:

<?php
require_once 'PHPUnit/Framework.php';
// require the file containing the class that sodaVendor is an instance of

define('SODA_PRICE', 75);

class SodaVendorTest extends PHPUnit_Framework_TestCase {
    private $sodaVendor;

    public function setUp() {
        // set up $this->sodaVendor somehow...
    }

    public function tearDown() {
        $this->sodaVendor = null;
    }

    public function testBalanceReturnsToZeroOnVending() {
        $this->sodaVendor->insertCoin(50);
        $this->sodaVendor->insertCoin(20);
        $this->sodaVendor->insertCoin(5);
        // The price is right!
        $this->assertEquals(SODA_PRICE,
            $this->sodaVendor->getCurrentBalance(),
            "We have entered correct money");
        $this->sodaVendor->dispenseItem();
        $this->assertEquals(0,
            $this->sodaVendor->getCurrentBalance(),
            "After vending, the balance of soda vending machine is zero");
    }
}
?>
Asaph
This is not valid. `.` is the concatenation operator in PHP. You need things like `$sodaVendor->insertCoin(50);`.
Josh Leitzel
@Josh Leitzel: Whoops. That's what I get for not testing it first. I fixed it...
Asaph
Oh come on, Why the downvotes?
Asaph
Because your answer was incorrect and condescending?
Josh Leitzel
@Josh Leitzel: The answer was totally appropriate for the question and any minor errors have been corrected.
Asaph
@Josh .... someone would close that question anyway ... that's not a real question !!!!
RageZ
@Rage: You didn't seem to mind posting your 'response' to try to get some rep, did you?
Josh Leitzel
@Josh yeah that's right but also flaged it ...
RageZ
@Asaph: I have removed my downvote because you fixed the errors and removed the condescension. But for the record, I don't believe "It's not rocket science" is something appropriate for *any* question, especially when it's being posed by a relatively new user.
Josh Leitzel
@Josh Leitzel: I appreciate that and I see your point.
Asaph
Also just went ahead and upvoted since you've made a good effort to show the OP how something (generally) in Java can be implemented with PHP.
Josh Leitzel
@Asaph: nice to have guessed it was a test case ;-) ....
RageZ
@Josh Leitzel: I appreciate the upvote. Glad I was able to convert you from an downvoter to an upvoter. Ironically (perhaps from your perspective), the final answer I ended up with for this question was what I had in mind from the start. I just worked on it incrementally in the way many SO users do. So the first few drafts were not as pretty, I suppose.
Asaph
@RageZ: the method name `assertEquals()` was a big hint for me. :)
Asaph
"It's not rocket science" is condescending? Subjective. I beg to differ.
Amarghosh
@Amarghosh: I removed it in any case. If even a single person found it distasteful, it doesn't belong in my answer.
Asaph
I removed my downvote as well now that the answer has been corrected.
Josh
A: 

yes you can "translate" your Java code to PHP. Just it would have been nice you explain in the question a bit more what you are attending to do.

public function testBalanceReturnsToZeroOnVending()
        {
            $sodaVendor->insertCoin(50);
            $sodaVendor->insertCoin(20);
            $sodaVendor->insertCoin(5);
            // The price is right!

            assert("We have entered correct money", 
               SODA_PRICE ==
                $sodaVendor->getCurrentBalance());
            $sodaVendor->dispenseItem();
            assert("After vending, the balance of soda vending machine is zero", 
                0 == 
                $sodaVendor->getCurrentBalance());
        }
RageZ
This isn't valid PHP. PHP variables start with $
Josh
oups sorry ... have to edit
RageZ
If you think a question is "nonsense," flag it and be on your way.
Josh Leitzel
@josh: sure Josh ... I will pass next time
RageZ
cleaned away bad comments and the "nonsense" which is right is not nice to the user who asked the question, with my excuses.
RageZ