[java] nutritioninfo

Viewer

copydownloadembedprintName: nutritioninfo
  1. class FoodItem {
  2.     NutritionInfo nutritionInfo;
  3.     int countOfItem;
  4.  
  5.     public FoodItem(NutritionInfo nutritionInfo){
  6.         //'this' refers to the class variable, not the paramter
  7.         //we use this to set the class FoodItem.nutritionInfo on line 2 to the value passed into this constructor
  8.         this.nutritionInfo = nutritionInfo;
  9.         this.countOfItem = 0;
  10.     }
  11.  
  12.     public int increase(int quantity){
  13.         this.countOfItem += quantity;
  14.     }
  15.     public int decrease(int quantity){ 
  16.         //need to set countOfItem to max of 0 or countOfItem-quantity, since we don't want this to ever be less than 0/
  17.         this.countOfItem = Math.max(0this.countOfItem-quantity);
  18.         if (this.countOfItem === 0) {
  19.             //item is now considered deleted
  20.             this.nutritionInfo = null;
  21.         }
  22.     }
  23.  
  24.     public void setNutritionInfo(NutritionInfo nutritionInfo){
  25.         //we set nutrition info in the class to nutritioninfo in the setter
  26.         this.nutritionInfo = nutritionInfo;
  27.     }
  28.  
  29.     public NutritionInfo getNutritionInfo(){
  30.         // we return the nutritionInfo object
  31.         return this.nutritionInfo;
  32.     }
  33.  
  34. }

Editor

You can edit this paste and save as new:


File Description
  • nutritioninfo
  • Paste Code
  • 28 Oct-2020
  • 1.16 Kb
You can Share it: