Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | Source Code | 0 0
  1. import java.io.*;
  2. public class EmissionsCalculator//program to take inputs from CSV file and print output
  3. {
  4.     String file="src\\Sheet1.csv";//file source location
  5.     int n=10;
  6.     int choice[]=new int[n];//array to store choice of method
  7.     double arr1[]=new double[n];//array to store first column of data
  8.     double arr2[]=new double[n];//array to store second column of data
  9.     double arr3[]=new double[n];//array to store third column of data
  10.     double emissionFB=0.0,emissionDB=0.0,emissionSB=0.0;
  11.     String GREEN="\u001B[32m", YELLOW="\u001B[33m", RED="\u001B[31m",RESET="\u001B[0m";//defining ANSI colours for colour-coding outputs
  12.     void input() throws IOException//method to read csv file and extract data
  13.     {
  14.         int i=0;
  15.         BufferedReader br=null;
  16.         String line="";
  17.         try//to eliminate all kinds of exceptions thrown by compiler
  18.         {
  19.             br=new BufferedReader(new FileReader(file));
  20.             while ((line=br.readLine())!=null)
  21.             {
  22.                 String[] row=line.split(",");//segregating data separated by commas
  23.                 choice[i]=Integer.parseInt(row[0]);
  24.                 arr1[i]=Double.parseDouble(row[1]);
  25.                 arr2[i]=Double.parseDouble(row[2]);
  26.                 arr3[i]=Double.parseDouble(row[3]);
  27.                 i++;
  28.             }
  29.         }
  30.         catch (Exception e)//to catch and assess exceptions
  31.         {
  32.             e.printStackTrace();
  33.         }
  34.         finally
  35.         {
  36.             for (int j=1;j<i;j++) {
  37.                 switch (choice[j]) {
  38.                     case 1:
  39.                         calculateFuelBased(arr1[i], arr2[i], arr3[i]);
  40.                         break;
  41.                     case 2:
  42.                         calculateDistanceBased(arr1[i], arr2[i], arr3[i]);
  43.                         break;
  44.                     case 3:
  45.                         calculateSpendBased(arr1[i], arr2[i]);
  46.                         break;
  47.                     default:
  48.                         System.out.println(RED + "Error!! Wrong Choice!!" + RESET);
  49.                 }
  50.             }
  51.             try
  52.             {
  53.                 br.close();//closing BufferedReader object
  54.             }
  55.             catch (Exception e)
  56.             {
  57.                 e.printStackTrace();
  58.             }
  59.         }
  60.     }
  61.     void calculateFuelBased(double emissfactor,double distance,double efficiency)//to calculate fuel-based emissions
  62.     {
  63.         emissionFB=emissfactor*(distance/efficiency);
  64.         display(emissionFB);
  65.     }
  66.     void calculateDistanceBased(double mass, double distance, double factor)//to calculate distance-based emissions
  67.     {
  68.         emissionDB=mass*distance*factor;
  69.         display(emissionDB);
  70.     }
  71.     void calculateSpendBased(double expenditure, double efspend)//to calculate spend-based emissions
  72.     {
  73.         emissionSB=expenditure*efspend;
  74.         display(emissionSB);
  75.     }
  76.     void display(double d)//method to display colour-coded output
  77.     {
  78.         if (d <=50)
  79.         {
  80.             System.out.printf (GREEN+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
  81.             System.out.println (GREEN+"Thank you for your contribution. Keep it up!!"+RESET);
  82.         }
  83.         else if (d>50 && d<=200)
  84.         {
  85.             System.out.printf (GREEN+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
  86.             System.out.println (GREEN+"Good going!! In addition, please try to optimise logistics and reduce empty miles."+RESET);
  87.         }
  88.         else if (d>200 && d<=500)
  89.         {
  90.             System.out.printf (YELLOW+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
  91.             System.out.println (YELLOW+"Do consider hybrids or biofuel compatible vehicles for your next purchase."+RESET);
  92.         }
  93.         else
  94.         {
  95.             System.out.printf (RED+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
  96.             System.out.println (RED+"Consider shifting to public transport or using EVs."+RESET);
  97.         }
  98.     }
  99.     public static void main(String args[]) throws IOException//main method
  100.     {
  101.         EmissionsCalculator ob = new EmissionsCalculator();
  102.         ob.input();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement