Pedagogy1st

Foodprog

Mar 9th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3. import java.util.HashMap;
  4. public class Food{
  5.     public static void main(String[] args){
  6.         String[] nutrients = {"Carbs", "Protein", "Fat", "Vitamin A(%)", "Vitamin C(%)", "Vitamin E(%)", "Vitamin K(%)", "Iron(%)", "Magnesium(%)", "Zinc(%)", "Calcium(%)", "Potassium(%)", "Calories"};
  7.         Double[] foodssum = new Double[13];
  8.         setFoodSumZero(foodssum);
  9.         HashMap<String, Double[]> map = new HashMap<String, Double[]>();
  10.         createAndSetupData(map);
  11.  
  12.         System.out.println("Welcome to Paul's Food Nutrient Track");
  13.         System.out.print("Foods avaiable: ");
  14.         getFoods(map);
  15.         System.out.print("Type the exact food name to add the food. Type \"done\" to conclude \nFood:");
  16.  
  17.  
  18.         Scanner in = new Scanner(System.in);
  19.         String n;
  20.         while(!((n = in.next()).equals("done"))){
  21.             try{
  22.                 if(!map.containsKey(n)) throw new MyException("Food isn't supported");
  23.                 Double[] selected = map.get(n);
  24.                 System.out.println("Amount in grams?");
  25.                 int g;
  26.                 do{
  27.                     g = in.nextInt();
  28.                     if(g<=0) System.out.println("Please don't enter negative numbers or zero. Try again");
  29.                     else setupGrams(selected, g);
  30.                 } while(g<=0);
  31.                 for(int i = 0; i<selected.length;i++){
  32.                     foodssum[i] = foodssum[i] + selected[i];
  33.                 }
  34.             } catch (Exception e){
  35.                 System.out.println("Error:" + e);
  36.             }
  37.             System.out.print("Next food:");
  38.         }
  39.         printSumNutri(foodssum, nutrients);
  40.     }
  41.                         //carbs protei fat  vitA vitC   vitE  vitK iron  mag   zinc calci potasi  cals, per 100g
  42.     public static void createAndSetupData(HashMap map){
  43.         Double[] orange = {11.7, 0.9, 0.1,  4.0, 89.0,   1.0, 0.0,  1.0, 2.0,   0.1, 4.0, 5.0,    47.0};
  44.         Double[] broccoli = {7.2, 2.4, 0.4,  31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,    11.0};
  45.         Double[] rice = {28.6, 2.4, 0.2,    0.0, 0.0,   0.0,  0.0, 8.0,  3.0,  3.0, 0.1,  1.0,    130.0};
  46.         Double[] oats = {66.3, 12.0, 6.9,   0.0, 0.0,   0.0,  0.0, 20.0, 40.0, 20.0, 5.0, 12.0,   380.0};
  47.         Double[] flaxseeds={10.0, 20.0, 40.0,0.0, 1.0,   2.0,  5.0, 28.5, 90.0, 30.0, 25.0, 20.0, 516.0};
  48.         Double[] wheat = {70.0, 10.0, 1.9,  0.0, 0.0,   6.0,  3.0, 18.0, 25.0, 15.0, 4.0, 10.0,   322.0 };
  49.         Double[] almonds = {19.3, 22.1, 50.8,0.0,0.0,   124.0, 0.0, 21.0, 69.0, 21.0, 22.0, 20.0, 597.0};
  50.        
  51.  
  52.         String[] keys   = {"orange", "broccoli", "rice", "oats", "flaxseeds","wheat", "almonds"};
  53.         Double[][] values = {orange, broccoli, rice, oats, flaxseeds, wheat, almonds};
  54.  
  55.         for (int i = 0; i < keys.length; i++){
  56.             map.put(keys[i], values[i]);
  57.         }
  58.     }
  59.     public static void printSumNutri(Double[] ar, String[] nutri){
  60.         System.out.println("The sum of all your foods contains:");
  61.         for(int i=0; i<ar.length; i++){
  62.             System.out.println(nutri[i]+": "+ar[i]);
  63.         }
  64.     }
  65.  
  66.     public static void setFoodSumZero(Double[] ar){
  67.         for(int i=0; i<ar.length; i++){
  68.             ar[i] = 0.0;
  69.         }
  70.     }
  71.  
  72.     public static void getFoods(HashMap<String, Double[]> list){
  73.         Object[] names = list.keySet().toArray();
  74.         for(int i=0; i<list.size(); i++){
  75.             System.out.print(names[i]);
  76.             if(i!=list.size()-1) System.out.print(", ");
  77.             if(i%10==0) System.out.println();
  78.         }
  79.         System.out.println();
  80.         //System.out.println(list.keySet());
  81.     }
  82.  
  83.     public static void setupGrams(Double[] sel, int g){
  84.         for(int i=0; i<sel.length; i++){
  85.             sel[i] = sel[i]*g*0.01;
  86.         }
  87.     }
  88. }
  89.  
  90. class MyException extends Exception{
  91.     String strin;
  92.     MyException(String str) {
  93.         strin=str;
  94.     }
  95.     public String toString(){
  96.         return (strin);
  97.     }
  98. }
Add Comment
Please, Sign In to add comment