0

I have this assignment and I am about to throw this laptop. The following code runs, but when I test it in MindTap I get the bottom message. I don't know what I am doing wrong or why it says incorrect.

Assignment: Write three overloaded computeBill methods for a photo book store:

When computeBill receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. When computeBill receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. When computeBill receives three parameters, they represent the price of a photo book, the quantity ordered, and a coupon value. Multiply the quantity and price, reduce the result by the coupon value, and then add 8% tax and return the total due.

My Coding:enter code here public class Billing {

public static void main(String args[]){
   double yourTotal;
    yourTotal = computeBill(31.00);
    displayTotal (yourTotal);
    yourTotal = computeBill (31, 2);
    displayTotal(yourTotal);
    yourTotal = computeBill(31, 2, .2); 
    displayTotal (yourTotal);
 }
 public static double computeBill (double price) 
{double total = price * 1.08;
 System.out.println ("You ordered 1 photobook for $" + price);
 System.out.println("Plus sales tax 8%");
 return total;}

public static double computeBill (double price, int qty) {
double subtotal = price * qty;
double total = subtotal * 1.08;
 System.out.println ("You ordered" + qty + " photobook(s) for $" + price);
 System.out.println("Subtotal =" + subtotal);
System.out.println("Plus sales tax 8%");
return total;
 }

public static double computeBill (double price, int qty, double discount) {
double subtotal = price * qty;
 subtotal = subtotal - (subtotal * discount);    
 double total = subtotal * 1.08;
  System.out.println ("You ordered " + qty + " photobook(s) for $" + price);
  System.out.println("Subtotal = " + subtotal);
  System.out.println("Less your " + (discount * 100) + "% discount"); 
System.out.println("Plus sales tax 8%");   
return total;
}
public static void displayTotal (double total){
    System.out.println("Total: $" + total);
  }
  }  

Result MindTap gives me when I test: Build Status Build Succeeded Test Output You ordered2 photobook(s) for $31.0 Subtotal =62.0 Plus sales tax 8% [FAILED]: unitTest(CodevolveTest12f618f0): null false Test Contents Billing tester30 = new Billing();

@Test
public void unitTest() {
assertTrue(tester30.computeBill(31, 2) == 66.96);

}

Someone please help me. I am sooooo stuck!!!`

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Debug it. 1. Begin commenting out code until the error goes away or you see the problem. 2. Use the symbolic debugger that you got for free in your IDE. – nicomp Oct 11 '18 at 21:31
  • 2
    When I run your `computeBill` method, it's actually returning `66.96000000000001`, so you might be a victim of some floating point issue – MadProgrammer Oct 11 '18 at 21:31
  • 1
    You're probably going to want to look at [Round a double to 2 decimal places](https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places) and [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java), but personally, I found `new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue();` to be more elegant – MadProgrammer Oct 11 '18 at 21:35

1 Answers1

0

You need to limit the digits after decimal so that comparison becomes logical as:

public static double computeBill (double price, int qty) {
        double subtotal = price * qty;
        double total = subtotal * 1.08;
        System.out.println ("You ordered" + qty + " photobook(s) for $" + price);
        System.out.println("Subtotal =" + subtotal);
        System.out.println("Plus sales tax 8%");

        // need to limit to two digit after decimal
        return Double.parseDouble(new DecimalFormat("##.##").format(total));
}

So, you test:

@Test
public void unitTest() {
     assertTrue(tester30.computeBill(31, 2) == 66.96); // comparing two digits after decimal 
}
Yogen Rai
  • 2,961
  • 3
  • 25
  • 37
  • public static double computeBill (double price, int qty) { double subtotal = price * qty; double total = subtotal * 1.08; System.out.println ("You ordered" + qty + " photobook(s) for $" + price); System.out.println("Subtotal =" + subtotal); System.out.println("Plus sales tax 8%"); return Double.parseDouble(new DecimalFormat("##.##").format(total)); } – crystal gurley Oct 11 '18 at 23:50
  • public static double computeBill (double price, int qty) { double subtotal = price * qty; double total = subtotal * 1.08; System.out.println ("You ordered" + qty + " photobook(s) for $" + price); System.out.println("Subtotal =" + subtotal); System.out.println("Plus sales tax 8%"); return Double.parseDouble(new DecimalFormat("##.##").format(total)); } gives me an error that says: Billing.java:24: error: return Double.parseDouble(new DecimalFormat("##.##").format(total)); symbol: class Decimal location: class Billing so its still wrong. now confused!!! – crystal gurley Oct 12 '18 at 00:01
  • @crystalgurley did you import `import java.text.DecimalFormat;`?? – Yogen Rai Oct 12 '18 at 00:17
  • @crystalgurley i have working code on github https://github.com/YogenRaii/java-core/blob/master/src/main/java/com/eprogrammerz/examples/careercup/Billing.java – Yogen Rai Oct 12 '18 at 00:19
  • Thank you soooo much. I still had to do couple of edits because my discount was not calculating correct. Finally finished with that one! :) – crystal gurley Oct 12 '18 at 01:15
  • @crystalgurley good to hear :) .. would you mind upvoting/accepting answer? ;) – Yogen Rai Oct 12 '18 at 01:19