Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class EmissionsCalculator//program to take inputs from CSV file and print output
- {
- String file="src\\Sheet1.csv";//file source location
- int n=10;
- int choice[]=new int[n];//array to store choice of method
- double arr1[]=new double[n];//array to store first column of data
- double arr2[]=new double[n];//array to store second column of data
- double arr3[]=new double[n];//array to store third column of data
- double emissionFB=0.0,emissionDB=0.0,emissionSB=0.0;
- String GREEN="\u001B[32m", YELLOW="\u001B[33m", RED="\u001B[31m",RESET="\u001B[0m";//defining ANSI colours for colour-coding outputs
- void input() throws IOException//method to read csv file and extract data
- {
- int i=0;
- BufferedReader br=null;
- String line="";
- try//to eliminate all kinds of exceptions thrown by compiler
- {
- br=new BufferedReader(new FileReader(file));
- while ((line=br.readLine())!=null)
- {
- String[] row=line.split(",");//segregating data separated by commas
- choice[i]=Integer.parseInt(row[0]);
- arr1[i]=Double.parseDouble(row[1]);
- arr2[i]=Double.parseDouble(row[2]);
- arr3[i]=Double.parseDouble(row[3]);
- i++;
- }
- }
- catch (Exception e)//to catch and assess exceptions
- {
- e.printStackTrace();
- }
- finally
- {
- for (int j=1;j<i;j++) {
- switch (choice[j]) {
- case 1:
- calculateFuelBased(arr1[i], arr2[i], arr3[i]);
- break;
- case 2:
- calculateDistanceBased(arr1[i], arr2[i], arr3[i]);
- break;
- case 3:
- calculateSpendBased(arr1[i], arr2[i]);
- break;
- default:
- System.out.println(RED + "Error!! Wrong Choice!!" + RESET);
- }
- }
- try
- {
- br.close();//closing BufferedReader object
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- void calculateFuelBased(double emissfactor,double distance,double efficiency)//to calculate fuel-based emissions
- {
- emissionFB=emissfactor*(distance/efficiency);
- display(emissionFB);
- }
- void calculateDistanceBased(double mass, double distance, double factor)//to calculate distance-based emissions
- {
- emissionDB=mass*distance*factor;
- display(emissionDB);
- }
- void calculateSpendBased(double expenditure, double efspend)//to calculate spend-based emissions
- {
- emissionSB=expenditure*efspend;
- display(emissionSB);
- }
- void display(double d)//method to display colour-coded output
- {
- if (d <=50)
- {
- System.out.printf (GREEN+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
- System.out.println (GREEN+"Thank you for your contribution. Keep it up!!"+RESET);
- }
- else if (d>50 && d<=200)
- {
- System.out.printf (GREEN+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
- System.out.println (GREEN+"Good going!! In addition, please try to optimise logistics and reduce empty miles."+RESET);
- }
- else if (d>200 && d<=500)
- {
- System.out.printf (YELLOW+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
- System.out.println (YELLOW+"Do consider hybrids or biofuel compatible vehicles for your next purchase."+RESET);
- }
- else
- {
- System.out.printf (RED+"\nEmissions= %.2fkg CO2.\n"+RESET,d);
- System.out.println (RED+"Consider shifting to public transport or using EVs."+RESET);
- }
- }
- public static void main(String args[]) throws IOException//main method
- {
- EmissionsCalculator ob = new EmissionsCalculator();
- ob.input();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement