Create a Java class named Invoicing that includes three overloaded computeInvoic
ID: 3723676 • Letter: C
Question
Create a Java class named Invoicing that includes three overloaded computeInvoice() methods for a clothes store:
- When computeInvoice() receives a single parameter, it represents the price of one shirt ordered. Add 8% tax, and display the total due.
- When computeInvoice() receives two parameters, they represent the price of a store and the quantity ordered. Multiply the two values, add 8% tax, and display the total due.
- When computeInvoice() receives three parameters, they represent the price of a shirt, 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 display the total due.
Create a driver class named TestInvoice with a main() method that tests all three overloaded methods using the following data:
- Price $24.95
- Price $17.50, quantity 4
- Price $10.00, quantity 6, coupon $20.00
Example Output:
Total price: $26.95
Total price: $75.60, quantity 4
Total price: $43.20, quantity 6, coupon $20.00
Explanation / Answer
Hi.. I have written java program for the above. Please check the below.
TestInvoice.java
import java.text.DecimalFormat;
public class TestInvoice {
public static void main(String[] args) {
// TODO Auto-generated method stub
computeInvoice(24.95);
computeInvoice(17.50,4);
computeInvoice(10,6,20);
}
private static void computeInvoice(double price, int qty, double coupon) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat(".##");
double sbt = (price*qty)-coupon;
double tax = sbt*0.08;
double total = tax+sbt;
System.out.println("Total price: $"+df.format(total)+", Quantity: "+qty+", coupon: $"+df.format(coupon));
}
private static void computeInvoice(double price, int qty) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat(".##");
double sbt = price*qty;
double tax = sbt*0.08;
double total = tax+sbt;
System.out.println("Total price: $"+df.format(total)+", Quantity: "+qty);
}
private static void computeInvoice(double price) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat(".##");
double tax = price*0.08;
double total = tax+price;
System.out.println("Total price: $"+df.format(total));
}
}
Output:
Total price: $26.95
Total price: $75.6, Quantity: 4
Total price: $43.2, Quantity: 6, coupon: $20.0
Please test the code and let me know any issues. Thank you. All the best.