Advertisement
Guest User

How to fix errors in recommend()

a guest
Apr 4th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | Source Code | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class EmissionsCalculatorNew
  4. {
  5.     double mass=0.0,distance=0.0,emission=1.0;
  6.     int num=0;
  7.     String make="";
  8.     String GREEN="\u001B[32m", YELLOW="\u001B[33m", RED="\u001B[31m",RESET="\u001B[0m";
  9.     String path="src\\Book1.csv";
  10.     char ch,state;
  11.     ArrayList<String> name=new ArrayList<>();
  12.     ArrayList<Character> fueltype=new ArrayList<>();
  13.     ArrayList<String> payload=new ArrayList<>();
  14.     ArrayList<String> efficiency = new ArrayList<>();
  15.     void input()//method to accept input
  16.     {
  17.         Scanner sc = new Scanner(System.in);
  18.         System.out.println ("\t\tScope 3 Emissions calculator (based on mass carried and distance traveled):");//heading
  19.         System.out.print ("Enter the model: ");//asking for model name
  20.         make=sc.nextLine().toLowerCase();
  21.         System.out.print ("\nEnter weight of goods transported in tonnes: ");
  22.         mass=sc.nextDouble()*1000.0;//tonnes -> kg
  23.         if (mass<=3500)//if load carried is less than 3.5 tonnes, ie, a light goods vehicle
  24.         {
  25.             System.out.print ("\nEnter P for petrol or D for diesel engine: ");//asking if the carrier is a petrol or a diesel model
  26.             ch=Character.toLowerCase(sc.next().charAt(0));
  27.             state='l';//light goods vehicle
  28.         }
  29.         else//if load carried is greater than 3.5 tonnes, ie, a heavy goods vehicle
  30.         {
  31.             ch='d';//strictly diesel engine
  32.             state='h';//heavy goods vehicle
  33.         }
  34.         System.out.print ("\nEnter distance transported: ");//asking for distance travelled
  35.         distance=sc.nextDouble();
  36.         sc.close();
  37.         if (name.contains(make)==true)
  38.         {
  39.             num= name.indexOf(make);
  40.             if (Character.toLowerCase(fueltype.get(num))==ch)
  41.                 System.out.println (GREEN+"We have the model on offer"+RESET);
  42.             else
  43.                 System.out.println (RED+"Sorry! We do not offer the model requested."+RESET);
  44.         }
  45.  
  46.     }
  47.     void read()//method to read dataset imported as a csv file
  48.     {
  49.         String line="";
  50.         try(BufferedReader br=new BufferedReader(new FileReader(path));)
  51.         {
  52.             while ((line=br.readLine())!=null)
  53.             {
  54.                 String[] row=line.split(",");
  55.                 name.add(row[0].toLowerCase());
  56.                 fueltype.add(row[1].charAt(0));
  57.                 payload.add(row[2]);
  58.                 efficiency.add(row[3]);
  59.             }
  60.             br.close();//closes BufferedReader
  61.         }
  62.         catch (IOException e)
  63.         {
  64.             System.out.println (e);
  65.         }
  66.     }
  67.     void calculate()//method to calculate emission in kg.CO2/tonne-km
  68.     {
  69.         emission=mass*distance;
  70.         switch(ch)
  71.         {
  72.             case 'p':
  73.                 emission*=0.83;//assuming general emission factor of a petrol engine of an LGV is 0.83kg CO2/tonne-km
  74.                 break;
  75.             case 'd':
  76.                 if (state=='l')
  77.                     emission*=0.78;//assuming general emission factor of a petrol engine of an LGV is 0.78kg CO2/tonne-km
  78.                 else
  79.                     emission*=0.154;//assuming general emission factor of a petrol engine of an LGV is 0.154kg CO2/tonne-km
  80.                 break;
  81.             default:
  82.                 System.out.println (RED+"Error! Wrong Choice!"+RESET);
  83.                 System.exit(0);
  84.         }
  85.     }
  86.     void display()//method to display emissions
  87.     {
  88.         Scanner sc = new Scanner(System.in);
  89.         System.out.printf ("Emissions= %.2fkg CO2.\n",emission);//displays emissions
  90.         if (emission <=50)
  91.             System.out.println ("Thank you for your contribution. Keep it up!!");
  92.         else if (emission>50 && emission<=200)
  93.             System.out.println ("Good going!! In addition, please try to optimise logistics and reduce empty miles.");
  94.         else if (emission>200 && emission<=500)
  95.             System.out.println ("Do consider hybrids or biofuel compatible vehicles for your next purchase.");
  96.         else
  97.             System.out.println ("Consider shifting to public transport or using EVs.");
  98.         System.out.println ("\nWould you like us to recommend alternatives (y/n): ");//asking if user wants to see alternative trucks
  99.         char ch=Character.toLowerCase(sc.next().charAt(0));
  100.         switch(ch)
  101.         {
  102.             case 'y':
  103.                 recommend();//calling recommend method to generate recommendations
  104.                 break;
  105.             case 'n':
  106.                 System.out.println ("Thank you for choosing us. Please do continue to keep emissions under control!");//ending program
  107.                 System.exit(0);//exiting program
  108.                 break;
  109.             default:
  110.                 System.out.println (RED+"Wrong choice!!"+RESET);//error message
  111.                 System.exit(0);//exiting program
  112.         }
  113.     }
  114.     void recommend()//method to recommend alternatives
  115.     {
  116.         System.out.println ("May we recommend: ");
  117.         ArrayList<Integer> load =new ArrayList<>();
  118.         ArrayList<Double> eff =new ArrayList<>();
  119.         for (String str : payload)
  120.             load.add(Integer.parseInt(str));
  121.         for (String str : efficiency)
  122.             eff.add(Double.parseDouble(str));
  123.         for (int j=0;j<payload.size();j++)
  124.         {
  125.             if ((load.get(num)-mass)>(load.get(j)-mass))//if a more optimum carrying capacity exists
  126.             {
  127.                 if (eff.get(j)>eff.get(num))//if it is more efficient
  128.                 {
  129.                     System.out.println (name.get(j));
  130.                     System.out.println (GREEN+"PROS: more efficient"+RESET);
  131.                 }
  132.                 else if (eff.get(j).equals(eff.get(num)))//if it is just as efficient
  133.                 {
  134.                     System.out.println (name.get(j));
  135.                     System.out.println (YELLOW+"PROS: just as efficient but more optimum option"+RESET);
  136.                 }
  137.                 else//if it is less efficient
  138.                 {
  139.                     System.out.println (name.get(j));
  140.                     System.out.println (RED+"CONS: less efficient but more optimum option"+RESET);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     public static void main(String args[])
  146.     {
  147.         EmissionsCalculatorNew ob = new EmissionsCalculatorNew();
  148.         ob.read();//reads input from csv database
  149.         //ob.input();//takes input from user
  150.         //ob.calculate();//calculates emissions
  151.         //ob.display();//displays result
  152.         ob.recommend();
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement