Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Scanner;
- import java.util.HashMap;
- public class Food{
- public static void main(String[] args){
- String[] nutrients = {"Carbs", "Protein", "Fat", "Vitamin A(%)", "Vitamin C(%)", "Vitamin E(%)", "Vitamin K(%)", "Iron(%)", "Magnesium(%)", "Zinc(%)", "Calcium(%)", "Potassium(%)", "Calories"};
- Double[] foodssum = new Double[13];
- setFoodSumZero(foodssum);
- HashMap<String, Double[]> map = new HashMap<String, Double[]>();
- createAndSetupData(map);
- System.out.println("Welcome to Paul's Food Nutrient Track");
- System.out.print("Foods avaiable: ");
- getFoods(map);
- System.out.print("Type the exact food name to add the food. Type \"done\" to conclude \nFood:");
- Scanner in = new Scanner(System.in);
- String n;
- while(!((n = in.next()).equals("done"))){
- try{
- if(!map.containsKey(n)) throw new MyException("Food isn't supported");
- Double[] selected = map.get(n);
- System.out.println("Amount in grams?");
- int g;
- do{
- g = in.nextInt();
- if(g<=0) System.out.println("Please don't enter negative numbers or zero. Try again");
- else setupGrams(selected, g);
- } while(g<=0);
- for(int i = 0; i<selected.length;i++){
- foodssum[i] = foodssum[i] + selected[i];
- }
- } catch (Exception e){
- System.out.println("Error:" + e);
- }
- System.out.print("Next food:");
- }
- printSumNutri(foodssum, nutrients);
- }
- //carbs protei fat vitA vitC vitE vitK iron mag zinc calci potasi cals, per 100g
- public static void createAndSetupData(HashMap map){
- 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};
- 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};
- 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};
- 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};
- 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};
- 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 };
- 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};
- String[] keys = {"orange", "broccoli", "rice", "oats", "flaxseeds","wheat", "almonds"};
- Double[][] values = {orange, broccoli, rice, oats, flaxseeds, wheat, almonds};
- for (int i = 0; i < keys.length; i++){
- map.put(keys[i], values[i]);
- }
- }
- public static void printSumNutri(Double[] ar, String[] nutri){
- System.out.println("The sum of all your foods contains:");
- for(int i=0; i<ar.length; i++){
- System.out.println(nutri[i]+": "+ar[i]);
- }
- }
- public static void setFoodSumZero(Double[] ar){
- for(int i=0; i<ar.length; i++){
- ar[i] = 0.0;
- }
- }
- public static void getFoods(HashMap<String, Double[]> list){
- Object[] names = list.keySet().toArray();
- for(int i=0; i<list.size(); i++){
- System.out.print(names[i]);
- if(i!=list.size()-1) System.out.print(", ");
- if(i%10==0) System.out.println();
- }
- System.out.println();
- //System.out.println(list.keySet());
- }
- public static void setupGrams(Double[] sel, int g){
- for(int i=0; i<sel.length; i++){
- sel[i] = sel[i]*g*0.01;
- }
- }
- }
- class MyException extends Exception{
- String strin;
- MyException(String str) {
- strin=str;
- }
- public String toString(){
- return (strin);
- }
- }
Add Comment
Please, Sign In to add comment