how to find the total of the products added to the shopping cart in java program code example

Example 1: how to find the total of the products added to the shopping cart in java program

import java.util.Scanner;
public class ShoppingCartPrinter
{

   public static void main(String[] args) 
   {
      ItemToPurchase item1= new ItemToPurchase();
      ItemToPurchase item2= new ItemToPurchase();
      Scanner kbdInput = new Scanner(System.in);

      System.out.println("Item 1");
      System.out.println( "Enter the item name: ");
      item1.setName(kbdInput.nextLine());
  etc...

      kbdInput.nextLine();

      ...
      System.out.println ( "Enter the item quantity: ");
      item2.setQuantity(kbdInput.nextInt());



      int totalItem1 = item1.getPrice()* item1.getQuantity();


      System.out.println ("\n"+"TOTAL COST");
      System.out.println (item1.getName()+" "+item1.getQuantity()+" "+" @ $" +totalItem1);
      etc...
      System.out.println ("Total: $"+ (totalItem1 + totalItem2));         
 }
}

Example 2: how to find the total of the products added to the shopping cart in java program

public class ItemToPurchase 
{
   public String itemName;
   public int itemPrice;
   public int itemQuantity;

   public ItemToPurchase()
   {
      itemName="none";
      itemPrice=0;
      itemQuantity=0;  
   }

   public void setName(String name)
   {
      itemName = name;   
   }
   public String getName()
   {
      return itemName;
   }

    }

Tags:

Php Example