title: How to add 10 specific products (objects) with different data types in each one, in an ArrayList?
body: <p>I am a newbie in java and I'm trying to create a vending machine but I can't seem to be able to add 10 products that I need. (I'm using BlueJ for coding as requested by my instructor). I  need the machine to be able to display the available products only, let the user buy products, display total money in the machine and display the total items sold!</p>
  
  <p>Here is my object code: </p>
  
  <pre><code>import java.util.*;
  
  public class VendingMachineObject {
  
         public String productName = "";
         public double unitPrice;
         public int initialQuantity;
         public int availableQuantity;
  
         public VendingMachineObject(String newProductName, double newUnitPrice, int newInitialQuantity, int newAvailableQuantity) {
             this.productName = newProductName;
             this.unitPrice = newUnitPrice;
             this.initialQuantity = newInitialQuantity;
             this.availableQuantity = newAvailableQuantity;
         }
  
         public String getProductName() {
             return this.productName;
         }
  
         public double getUnitPrice() {
             return this.unitPrice;
         }
  
         public int getInitialQuantity() {
             return this.initialQuantity;
         }
  
         public int getAvailableQuantity() {
             return this.availableQuantity;
         }
  
         public void setProductName(String newProductName) {
             this.productName = newProductName;
         }
  
         public void setUnitPrice(double newUnitPrice) {
            this.unitPrice = newUnitPrice;
         }
  
         public void setInitialQuantity(int newInitialQuantity) {
             this.initialQuantity = newInitialQuantity;
         }
  
         public void setAvailableQuantity(int newAvailableQuantity) {
             this.availableQuantity = newAvailableQuantity;
         }
  }       
  </code></pre>
  
  <p>And the main app code:</p>
  
  <pre><code>public class VendingMachine {
  
     static Scanner input = new Scanner(System.in);
  
     static ArrayList&lt;VendingMachineObject&gt; myVendor = new ArrayList&lt;VendingMachineObject&gt;(10);
  
     static public int soldProductsCounter = 0;
  
     static public double totalMoneyCounter = 0;
  
  
     public static void main (String args[]) {
  
          int choice;
  
          do {
          displayMenu();
          choice = input.nextInt();
          switch (choice) {
          case 1: displayAvailableProducts(myVendor);
                  break;
          case 2: buyProduct(myVendor);
                  break;
          case 3: displayTotalProductsSold(myVendor);
                  break;
          case 4: displayTotalMoney(myVendor);
                  break;
          case 5: System.out.println("End of program");
                  break;
          default: System.out.println("Invalid option. Try again.");
  
          }
  
  
          } while (choice != 5);
  
        System.exit(0);
  
     }
  
     public static void displayMenu() {
  
      System.out.println("1. Display available products");
  
      System.out.println("2. Buy a product");
  
      System.out.println("3. Display the total products sold");
  
      System.out.println("4. Display the total money in the machine");
  
      System.out.println("5. Exit");
  
      System.out.println("Choice ?");
     }
  
     public static void displayAvailableProducts(ArrayList&lt;VendingMachineObject&gt; myVendor) {
  
          int availableQuantity;
  
          for (VendingMachineObject x: myVendor) {
              if(x.getAvailableQuantity() &gt; 0) {
                  System.out.println(x.getProductName() + " " + x.getUnitPrice() + " " + x.getAvailableQuantity());
              }
          }
     }
  
     public static void buyProduct(ArrayList&lt;VendingMachineObject&gt; myVendor) {
  
         String productName;
  
         int availableQuantity;
  
         int choice;
  
         System.out.println("Which product do you want to buy?");
  
         productName = input.next();
  
         for (VendingMachineObject x: myVendor) {
  
             if (x.getProductName() == productName) {
  
                 System.out.println("The cost of this product is:" + x.getUnitPrice());
  
                 System.out.println("Items in stock:" + x.getAvailableQuantity());
  
                 System.out.println("How many do you want to buy?");
  
                 choice = input.nextInt();
  
                 if (choice &gt; x.getAvailableQuantity()) {
  
                  System.out.println("You can not buy so many products");
  
                  System.out.println("You can buy only:" + x.getAvailableQuantity());
  
                  choice = x.getAvailableQuantity();
  
              }
  
              System.out.println("The total cost is:" + (choice * x.getUnitPrice()));
  
              x.setAvailableQuantity(x.getAvailableQuantity()-choice);
  
              soldProductsCounter = soldProductsCounter + choice;
  
              totalMoneyCounter = totalMoneyCounter + x.getUnitPrice();
  
          }
         } 
     }
  
     public static void displayTotalProductsSold(ArrayList&lt;VendingMachineObject&gt; myVendor) {
  
         System.out.println("Items sold:" + " " + soldProductsCounter);
  
     }
  
     public static void displayTotalMoney(ArrayList&lt;VendingMachineObject&gt; myVendor) {
  
         System.out.println("Total money in the machine:" + " "+ totalMoneyCounter);
  
     }
  }
  </code></pre>