[java] nutritioninfo
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- class FoodItem {
- NutritionInfo nutritionInfo;
- int countOfItem;
- public FoodItem(NutritionInfo nutritionInfo){
- //'this' refers to the class variable, not the paramter
- //we use this to set the class FoodItem.nutritionInfo on line 2 to the value passed into this constructor
- this.nutritionInfo = nutritionInfo;
- this.countOfItem = 0;
- }
- public int increase(int quantity){
- this.countOfItem += quantity;
- }
- public int decrease(int quantity){
- //need to set countOfItem to max of 0 or countOfItem-quantity, since we don't want this to ever be less than 0/
- this.countOfItem = Math.max(0, this.countOfItem-quantity);
- if (this.countOfItem === 0) {
- //item is now considered deleted
- this.nutritionInfo = null;
- }
- }
- public void setNutritionInfo(NutritionInfo nutritionInfo){
- //we set nutrition info in the class to nutritioninfo in the setter
- this.nutritionInfo = nutritionInfo;
- }
- public NutritionInfo getNutritionInfo(){
- // we return the nutritionInfo object
- return this.nutritionInfo;
- }
- }
Editor
You can edit this paste and save as new: