#handling subtracting bills and coins in a vending machine system

1 messages · Page 1 of 1 (latest)

sand jungle
#

I need to handle each entry based on the test cases file, the only test case i am having a trouble with is the test case which requires to subtract a 1 bill from a 10 bill which is only possible if the first entry before subtraction have a 1 bill in its composition.

in other words the system should track money not as just a sum but as bills and coins as well, when a customer requests to buy an item and enters the money that he has, i need to make sure that change can be returned.

For example, if we have a 10 Dollars bill, and someone is asking for 5 Dollars in return, you cannot return the money, but if you have 10 one dollar bills you can return five one dollar bills.

tired tokenBOT
#

This post has been reserved for your question.

Hey @sand jungle! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

sand jungle
#

I need to handle each entry based on the test cases file, the only test case i am having a trouble with is the test case which requires to subtract a 1 bill from a 10 bill which is only possible if the first entry before subtraction have a 1 bill in its composition.

in other words the system should track money not as just a sum but as bills and coins as well, when a customer requests to buy an item and enters the money that he has, i need to make sure that change can be returned.

For example, if we have a 10 Dollars bill, and someone is asking for 5 Dollars in return, you cannot return the money, but if you have 10 one dollar bills you can return five one dollar bills.

mossy forgeBOT
#

here is my code


public class Money {

   private final double amount;

   public static final Money Zero = new Money(0.00);
   public static final Money OneCent = new Money(0.01);
   public static final Money FiveCent = new Money(0.05);
   public static final Money TenCent = new Money(0.10);
   public static final Money TwentyFiveCent = new Money(0.25);
   public static final Money FiftyCent = new Money(0.50);
   public static final Money OneDollar = new Money(1.00);
   public static final Money FiveDollar = new Money(5.00);
   public static final Money TenDollar = new Money(10.00);
   public static final Money TwentyDollar = new Money(20.00);
   public static final Money FiftyDollar = new Money(50.00);




   //custom constructor to handle instantiating objects & handling input violations.
   public Money(double amount) {
       if (amount < 0) {
           throw new IllegalArgumentException("Amount cannot be negative");
       }
       this.amount = amount;
   }

   public double amount() {

       return amount;
   }

   //multiplies the amount by a given count.
   public Money times(int count) {
       if (count < 0) {
           throw new IllegalArgumentException("Count cannot be negative");
       }

       return new Money(this.amount * count);
   }


   //adds the amount of another Money object to the current one.
   public Money plus(Money other) {
       return new Money(this.amount + other.amount);
   }

   //subtracts the amount of another Money object, with a check to prevent negative results.
   public Money minus(Money other) {

       if (this.amount < other.amount) {
           throw new IllegalArgumentException("Cannot subtract more than the available amount");
       }
       double result =this.amount-other.amount;
       System.out.println(" this " + this.amount +" minus "+other.amount+" IS "+result);
   return new Money(result);
   }

} ```

This message has been formatted automatically. You can disable this using /preferences.

#

and here is the test cases

@Test ```java

public void minusTest() {
    assertThrows(IllegalArgumentException.class, () -> Zero.minus(OneDollar));
    assertEquals(Zero, Zero.minus(Zero));
    assertEquals(Zero, OnePiaster.minus(OnePiaster));
    assertEquals(OneDollar, FiveDollar.plus(OneDollar).minus(FiveDollar));
    assertEquals(FiveDollar, OneDollar.times(10).minus(FiveDollar));
    assertThrows(IllegalArgumentException.class, () -> TenDollar.minus(OneDollar));
    
} ```

all tests pass except the last one. How would I compare this equals 9?

This message has been formatted automatically. You can disable this using /preferences.