0

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
asked Nov 6, 2009 at 5:04
4
  • is that homework ? yeah that's definitely possible ... but ... Commented Nov 6, 2009 at 5:06
  • 3
    damn. soda in our vending machine costs a buck Commented Nov 6, 2009 at 5:07
  • 3
    who upvoted that question ?!?!? ...... Commented Nov 6, 2009 at 5:17
  • @RageZ whoever downvoted your answer. it wasn't me though :) Commented Nov 6, 2009 at 5:35

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

11 Comments

This is not valid. . is the concatenation operator in PHP. You need things like $sodaVendor->insertCoin(50);.
@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.
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.
"It's not rocket science" is condescending? Subjective. I beg to differ.
I removed my downvote as well now that the answer has been corrected.
|
0

Any Java code can be converted to PHP

answered Nov 6, 2009 at 5:06

4 Comments

@Itay Moav: That's not generally true. PHP doesn't support threading for example.
@Asaph: Well, you could still pull it off effectively - just emulate co-operative multithreading. Not that I'd recommend it.
@Kevin Montrose: @Itay Moav: Another example of a java program that would not be possible to write in PHP is an Applet.
@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...
0

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

4 Comments

This isn't valid PHP. PHP variables start with $
If you think a question is "nonsense," flag it and be on your way.
@josh: sure Josh ... I will pass next time
cleaned away bad comments and the "nonsense" which is right is not nice to the user who asked the question, with my excuses.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.