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());
}
shsteimer
28.9k30 gold badges81 silver badges95 bronze badges
-
is that homework ? yeah that's definitely possible ... but ...RageZ– RageZ2009年11月06日 05:06:11 +00:00Commented Nov 6, 2009 at 5:06
-
3damn. soda in our vending machine costs a buckOmnipresent– Omnipresent2009年11月06日 05:07:51 +00:00Commented Nov 6, 2009 at 5:07
-
3who upvoted that question ?!?!? ......RageZ– RageZ2009年11月06日 05:17:56 +00:00Commented Nov 6, 2009 at 5:17
-
@RageZ whoever downvoted your answer. it wasn't me though :)Omnipresent– Omnipresent2009年11月06日 05:35:33 +00:00Commented Nov 6, 2009 at 5:35
3 Answers 3
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");
}
}
?>
answered Nov 6, 2009 at 5:07
Asaph
164k25 gold badges204 silver badges204 bronze badges
Sign up to request clarification or add additional context in comments.
11 Comments
Josh Leitzel
This is not valid.
. is the concatenation operator in PHP. You need things like $sodaVendor->insertCoin(50);.Josh Leitzel
@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
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.
Amarghosh
"It's not rocket science" is condescending? Subjective. I beg to differ.
Josh
I removed my downvote as well now that the answer has been corrected.
|
Any Java code can be converted to PHP
answered Nov 6, 2009 at 5:06
Itay Moav -Malimovka
53.8k68 gold badges198 silver badges292 bronze badges
4 Comments
Asaph
@Itay Moav: That's not generally true. PHP doesn't support threading for example.
Kevin Montrose
@Asaph: Well, you could still pull it off effectively - just emulate co-operative multithreading. Not that I'd recommend it.
Asaph
@Kevin Montrose: @Itay Moav: Another example of a java program that would not be possible to write in PHP is an Applet.
Itay Moav -Malimovka
@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...
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());
}
answered Nov 6, 2009 at 5:08
RageZ
27.3k12 gold badges72 silver badges76 bronze badges
4 Comments
Josh
This isn't valid PHP. PHP variables start with $
Josh Leitzel
If you think a question is "nonsense," flag it and be on your way.
RageZ
@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.
default