title: Total profit calculation java using arrays and various classes
body: <p><a href="https://i.stack.imgur.com/ilCgr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ilCgr.png" alt="enter image description here"></a>Hi I am new to programming and am struggling to figure out how to add the profits for the various items in a shop. I have a main method where the array of the items in the shop have been initialised. I have a separate method within the specific item class for calculating the profit but am not sure how to calculate a total profit for the same specific items classes, so I am trying to get the total profit for all food items and total profit for all stationery items, if that all makes sense. I am hoping there is a basic way of solving this issue as I am new to programming. it is probably really simple for more experienced programmers, but I am struggling getting me head round this.</p>
  
  <p>Any direction or assistance would be appreciated?
  many thanks</p>
  
  <p>package shop;</p>
  
  <p>public class Shop {</p>
  
  <pre><code>private String name;
  private double sellingPrice, costPrice, amount, valueOfSoldItems, costPerMonth;
  private int stockMonth, stockRem, volSoldMonth;
  
  public Shop(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
      this.name = name;
      this.sellingPrice = sellingPrice;
      this.costPrice = costPrice;
      this.stockMonth = stockMonth;
      this.volSoldMonth = volSoldMonth;
  
  }
  
  public double foodStats() {
      return getAmount();
  
  }
  
  public double stationeryStats() {
      return getAmount();
  }
  
  public void toolsStats() {
  
  }
  </code></pre>
  
  <p>// Food Class </p>
  
  <p>package shop;</p>
  
  <p>public class Food extends Shop {</p>
  
  <pre><code>double totalProfit;
  
  public Food(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
      super(name, sellingPrice, costPrice, stockMonth, volSoldMonth);
  }
  
  public double foodStats() {
  
      System.out.printf("%-15s%-15s", "Food:   ", getName());
      System.out.printf("%-15s%-10s", "Selling Price:   £", getSellingPrice());
      System.out.printf("%-15s%-10s", "Cost Price:   £", getCostPrice());
      System.out.printf("%-15s%-10s", "Stock Ordered This Month:   ", getStockMonth());
      System.out.printf("%-15s%-10s", "Volume Sold this Month:   ", getVolSoldMonth());
  
      setStockRem(getStockMonth() - getVolSoldMonth());
      System.out.printf("%-10s%-5s", "Stock remaining:   ", getStockRem());
  
      setCostPerMonth(getCostPrice() * getStockMonth());
      setValueOfSoldItems(getSellingPrice() * getVolSoldMonth());
  
      if (getValueOfSoldItems() &gt; getCostPerMonth()) {
          setAmount(getValueOfSoldItems() - getCostPerMonth());
          System.out.printf("%-10s%-5s", "  Profit:     £", getAmount());
  
      } else if (getCostPerMonth() &gt; getValueOfSoldItems()) {
          setAmount(getCostPerMonth() - getValueOfSoldItems());
          System.out.printf("%-10s%-5s", "  Loss:       £", getAmount());
  
      } else {
          System.out.print("  No Profit No Loss! ");
  
      }
      return getAmount();
  }
  </code></pre>
  
  <p>// Stationery class</p>
  
  <p>package shop;</p>
  
  <p>public class Stationery extends Shop {</p>
  
  <pre><code>public Stationery(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
      super(name, sellingPrice, costPrice, stockMonth, volSoldMonth);
  
  }
  
  public double stationeryStats() {
  
      System.out.printf("%-15s%-15s", "Stationery:   ", getName());
      System.out.printf("%-15s%-10s", "Selling Price:   £", getSellingPrice());
      System.out.printf("%-15s%-10s", "Cost Price:   £", getCostPrice());
      System.out.printf("%-15s%-10s", "Stock Ordered This Month:   ", getStockMonth());
      System.out.printf("%-15s%-10s", "Volume Sold this Month:   ", getVolSoldMonth());
  
      setStockRem(getStockMonth() - getVolSoldMonth());
      System.out.printf("%-10s%-5s", "Stock remaining:   ", getStockRem());
  
      setCostPerMonth(getCostPrice() * getStockMonth());
      setValueOfSoldItems(getSellingPrice() * getVolSoldMonth());
  
      if (getValueOfSoldItems() &gt; getCostPerMonth()) {
          setAmount(getValueOfSoldItems() - getCostPerMonth());
          System.out.printf("%-10s%-5s", "  Profit:     £", getAmount());
  
      } else if (getCostPerMonth() &gt; getValueOfSoldItems()) {
          setAmount(getCostPerMonth() - getValueOfSoldItems());
          System.out.printf("%-10s%-5s", "  Loss:       £", getAmount());
  
      } else {
          System.out.print("  No Profit No Loss! ");
      }
  
      return getAmount();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>// main class </p>
  
  <p>package shop;</p>
  
  <p>public class PrintShop {</p>
  
  <pre><code>public static void main(String[] args) {
  
      Shop[] shops = new Shop[] { new Food("Jellies", 0.80, 0.30, 40, 35), new Food("Chocolate", 1.50, 1.80, 50, 45),
              new Food("Potatoes", 0.85, 0.25, 80, 70), new Stationery("Pens", 2.00, 1.00, 25, 18),
              new Stationery("Paper", 45.00, 23.00, 10, 4) };
  
      for (Shop shop : shops) {
          System.out.println(shop instanceof Food);
      }
  
      int shopDetails = 5;
  
      double foodProfit = 0;
      double stationeryProfit = 0;
  
      for (int i = 0; i &lt; shopDetails; i++) {
          Shop s = shop[i];
          if (shop instanceof Food) {
              foodProfit += shops[i].foodStats();
          } else {
              stationeryProfit += shops[i].stationeryStats();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><a href="https://i.stack.imgur.com/dcXRd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dcXRd.png" alt="enter image description here"></a></p>