Hey
I solved this problem. I pasted here the classes.
To check program i generated Junit tests - which took me the same
amount of time as writing program( 10 minutes + 10 minutes ) If You
don't attend J2EE basics course, don't use test packages classes
( Withdrawaltest,RootSuite )
Here is project:
source classes:
file:Balance.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pacior
*/
public class Balance {
private float amount;
private float charge = 0.5f;
public Balance(float amount) {
this.amount = amount;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public float getCharge() {
return charge;
}
}
file:Withdrawal.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pacior
*/
public class Withdrawal {
private Balance balance;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Withdrawal atm = new Withdrawal();
try{
System.out.println(atm.withdraw(Float.parseFloat(args[0]),
Float.parseFloat(args[1])));//passing input parameters - must be
float, otherwise exception is made
}
catch ( NumberFormatException e ){
System.out.println("Wrong parameters, use:Withdrawal
moneys balance");
}
}
private float banknote = 5;
public float withdraw(float cash,float balance){
this.balance = new Balance(balance);
if ( cash % banknote != 0 )
return balance;
float result = this.balance.getAmount() - cash -
this.balance.getCharge();
if ( result > 0 )
return result;
else
return this.balance.getAmount();
}
}
test packages:
RootSuite.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
*
* @author pacior
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({WithdrawalTest.class})
public class RootSuite {
}
file:WithdrawalTest.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author pacior
*/
public class WithdrawalTest {
public WithdrawalTest() {
}
/**
* Test of withdraw method, of class Withdrawal.
*/
@Test
public void testWithdraw() {
System.out.println("Example - Successful Transaction");
float cash = 30.0F;
float balance = 120.0F;
Withdrawal instance = new Withdrawal();
float expResult = 89.5F;
float result = instance.withdraw(cash, balance);
assertEquals(expResult, result, 0.0);
System.out.println("Example - Incorrect Withdrawal
Amount (not multiple of 5)");
cash = 42.0F;
balance = 120.0F;
instance = new Withdrawal();
expResult = 120.0F;
result = instance.withdraw(cash, balance);
assertEquals(expResult, result, 0.0);
System.out.println("Example - Insufficient Funds");
cash = 300.0F;
balance = 120.0F;
instance = new Withdrawal();
expResult = 120.0F;
result = instance.withdraw(cash, balance);
assertEquals(expResult, result, 0.0);
// TODO review the generated test code and remove the default
call to fail.
//fail("The test case is a prototype.");
}
}
Regards
Pacior
--
Netbeans 6.7
java 1.6.0_14
On Aug 15, 9:52 pm, Pratik Mehta <[email protected]> wrote:
> Pooja would like to withdraw X $US from an ATM. The cash machine will
> only accept the transaction if X is a multiple of 5, and Pooja's
> account balance has enough cash to perform the withdrawal transaction
> (including bank charges). For each successful withdrawal the bank
> charges 0.50 $US. Calculate Pooja's account balance after an attempted
> transaction.
> Input
>
> Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes
> to withdraw.
> Nonnegative number 0<= Y <= 2000 with two digits of precision -
> Pooja's initial account balance.
> Output
>
> Output the account balance after the attempted transaction, given as a
> number with two digits of precision. If there is not enough money in
> the account to complete the transaction, output the current bank
> balance.
> Example - Successful Transaction
>
> Input:
> 30 120.00
>
> Output:
> 89.50
> Example - Incorrect Withdrawal Amount (not multiple of 5)
>
> Input:
> 42 120.00
>
> Output:
> 120.00
> Example - Insufficient Funds
>
> Input:
> 300 120.00
>
> Output:
> 120.00
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---