Write and document a Java program that inputs the name (String), quantity (int),
ID: 3755884 • Letter: W
Question
Write and document a Java program that inputs the name (String), quantity (int), and price (double) of three items. The name may contain spaces. Output a bill with tax rate of 6.25%. All prices should be formatted to two decimal places. The bill should be formatted in columns with 30 characters for the name, 10 characters for quantity, 10 characters for the price, and 10 characters for the total. Sample input and output is shown below. An invoice has a list (array) of items. Input name of item 1: lollipos Input Quantity of item 1: 10 input price of item 1: 0.50 Input name of item 2: diet soda Input Quantity of item 2: 3 input price of item 2: 1.25 Input name of item 3 chocolate bar Input quantity of item 3: 20 input price of item 3 0.75 Invoice Iteml] numltem:s methods... Item name quantity price methodsExplanation / Answer
//Java program
import java.util.Scanner;
class Item{
private String name;
private int quantity;
private double price;
public Item(String n ,int q,double p) {
name = n;
quantity = q;
price = p;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
}
class Invoice{
private Item item[];
private int size;
public Invoice() {
item = new Item[100];
size=0;
}
public void addItem(Item i) {
item[size++]=i;
}
public void printInvoice() {
double subtotal=0,total=0,tax;
for(int i=0;i<size;i++) {
total=item[i].getQuantity()*item[i].getPrice();
System.out.printf("%30s %10d %10.2f ",item[i].getName(),item[i].getQuantity(),item[i].getPrice(),total);
subtotal+=total;
}
System.out.println(" ");
System.out.printf("Subtotal : %10.2f ",subtotal);
tax=subtotal*6.25/100;
System.out.printf("Tax : %10.2f ",tax);
System.out.printf("Total: %10.2f ",subtotal+tax);
}
}
public class Bill {
public static void main(String args[]) {
Invoice invoice =new Invoice();
Scanner in =new Scanner(System.in);
String name;
int quantity,i=1;
double price;
int more;
do {
System.out.print("Input name of item "+i+" : ");
name = in.nextLine();
System.out.print("Input Quantity of item "+i+" : ");
quantity = in.nextInt();
System.out.print("Input price of item "+i+" : ");
price = in.nextDouble();
invoice.addItem(new Item(name , quantity , price));
i++;
System.out.print("Do you want to add more item ?(1/0) :");
more = in.nextInt();
}while(more==1);
invoice.printInvoice();
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.