Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program with comments using the follwoing insructions: Design and c

ID: 3802717 • Letter: W

Question

Write a java program with comments using the follwoing insructions:

Design and code a class called InvoiceItem to store information about items purchased at a store. The following attributes are private instance variables:

Part description (String)

Item price (int)

Quantity purchased (int)

The class must include the following public methods:

method description

default constructor Sets description to “none”, item and quantity to 0.

Parameterized constructor Sets all three instance variables to data that is passed to it

setDescription Recieves a value to assign to private instance variable

setQuantity Recieves a value to assign to private instance variable. If it is not positive, set it to 0.

setPrice Recieves a value to assign to private instance variable. If it is not positive, set it to 0.

getDescription Returns private instance variable getQuantity Returns private instance variable

getPrice Returns private instance variable (inches)

getItemTotal Calculates and returns the amount for this item (quantity * price)

You are going to use this class in an “app” to process orders for a shop that sells weapons, shields, armor, potions for gaming characters.

Create parallel arrays in main to represent the products and prices of the items in an equipment shop. You will initialize the products and prices to the data listed in the sample output below.

Create an ArrayList of InvoiceItem objects to represent the items on customer’s order (their “cart”). As items are selected, objects with the appropriate data are added to this arraylist. A customer can purchase any number of items.

Create the following methods:

showProducts Receives the two arrays with products and prices, prints the menu of available items. Label the first item (in element 0) as “1”, the second (in element 1) as “2”, etc. (this does not need to be an additional array)

printInvoice Receives the arraylist which has the items ordered by the customer. Prints the invoice as shown in sample output.

Program operation:

Call showProducts to print the menu of available items and prices.

Then loop to allow customer to place select a product or enter 0 to end the order. Validate the user’s choice with a loop. (Note that item 1 is stored in element 0 of the array, item 2 is stored in element 1 of the array, and so on – make any necessary adjustments in your code to address this.)

Display the item and price, ask for quantity (VALIDATE).

Add to the arraylist

Display total for this item

When order is done, call printInvoice to print the customer invoice

SAMPLE OUTPUT: (User input in bold)

Stuff R Us: Items in stock

1 - common sword 50

2 - jumping potion 85

3 - chain shirt 115

4 - breastplate 230

5 - steel shield 100

6 - wooden shield 250

Enter your choice or 0 to quit: 8

Invalid choice, re-enter: 5

steel shield , price is 100

How many to purchase? 0

Quantity must be greater than 0, re-enter: 3

Cost is $300

Enter your choice or 0 to quit: 1

common sword , price is 50

How many to purchase? 2

Cost is $100

Enter your choice or 0 to quit: 0

INVOICE

Qty Desc Price Item Total

3 steel shield $100 $300

2 common sword $50 $100

Total Due $400

Thank you for shopping with Stuff R Us

Explanation / Answer

InvoiceItem.java

public class InvoiceItem {
   //
   private String partDescription;
   private int price;
   private int quantityPurchased;
  
  
   public InvoiceItem() {
       super();
       this.partDescription="None";
       this.price=0;
       this.quantityPurchased=0;
   }


   public InvoiceItem(String partDescription, int price, int quantityPurchased) {
       super();
       this.partDescription = partDescription;
       this.price = price;
       this.quantityPurchased = quantityPurchased;
   }


   public String getPartDescription() {
       return partDescription;
   }


   public void setPartDescription(String partDescription) {
       this.partDescription = partDescription;
   }


   public int getPrice() {
       return price;
   }


   public void setPrice(int price) {
       if(price<0)
           this.price=-price;
       else
       this.price = price;
   }


   public int getQuantityPurchased() {
       return quantityPurchased;
   }


   public void setQuantityPurchased(int quantityPurchased) {
       if(quantityPurchased<0)
           this.quantityPurchased=-quantityPurchased;
       else
       this.quantityPurchased = quantityPurchased;
   }
  
   public int getItemTotal()
   {
       return quantityPurchased*price;
   }
  
  

}

___________________

App.java

import java.util.ArrayList;
import java.util.Scanner;

public class App {

   public static void main(String[] args) {
       int choice;
       int tot=0;
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
       String[] products={"common sword","jumping potion","chain shirt","breastplate","steel shield","wooden shield"};
       int[] price={50,85,115,230,100,250};
      
       ArrayList<InvoiceItem> arl=new ArrayList<InvoiceItem>();
       showProducts(products,price);
  
       System.out.print("Enter your choice or 0 to quit:");
       while(true)
       {
          
           choice=sc.nextInt();
          
           if(choice==0)
               break;
           else
           {
               if(choice<1 || choice>6)
               {
                   System.out.print("Invalid choice, re-enter: ");
                   continue;
               }
              
               System.out.println(products[choice-1]+" , price is "+price[choice-1] );
           System.out.print("How many to purchase?");
           int no_of_items=sc.nextInt();
           do
           {
               if(no_of_items<=0)
               {
               System.out.print("Quantity must be greater than 0, re-enter: ");
               no_of_items=sc.nextInt();
               }
              
           }while(no_of_items<=0);
          
           InvoiceItem ii=new InvoiceItem(products[choice-1],price[choice-1],no_of_items);
           arl.add(ii);
           System.out.println("Cost is $"+ii.getItemTotal());
           System.out.print("Enter your choice or 0 to quit:");
                      
           }
       }
      
       System.out.println("INVOICE");
       System.out.println("Qty Desc Price Item Total");
       for(int i=0;i<arl.size();i++)
       {
           System.out.println(arl.get(i).getQuantityPurchased()+" "+arl.get(i).getPartDescription()+" "+arl.get(i).getPrice()+" "+arl.get(i).getItemTotal());
           tot+=arl.get(i).getItemTotal();
       }
       System.out.println("Total Due $"+tot);
       System.out.println("Thank you for shopping with Stuff R Us");

      
   }

   private static void showProducts(String[] products, int[] price) {
       for(int i=0;i<products.length;i++)
       {
           System.out.println((i+1)+" - "+products[i]+" "+price[i]);
       }  
      
   }

}

______________________

Output:

1 - common sword 50
2 - jumping potion 85
3 - chain shirt 115
4 - breastplate 230
5 - steel shield 100
6 - wooden shield 250
Enter your choice or 0 to quit:8
Invalid choice, re-enter: 5
steel shield , price is 100
How many to purchase?0
Quantity must be greater than 0, re-enter: 3
Cost is $300
Enter your choice or 0 to quit:1
common sword , price is 50
How many to purchase?2
Cost is $100
Enter your choice or 0 to quit:0
INVOICE
Qty Desc Price Item Total
3 steel shield 100 300
2 common sword 50 100

____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote