Skip to main content
deleted 17 characters in body; edited tags; edited title; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

A function which reads Extracting price statistics from a CSV file and displays the results

"PRODUCT_NAME","PRODUCT_CATEGORY","SALES_PRICE"

"Hair Gel","Beauty",6.99 "Scotch tape","Office Supply",2.99

"Office Chair","Office Supply",134.99

"Eraser","Office Supply",0.99

"Lipstick","Beauty",9.99

"Nail Polish","Beauty",8.99

"Note Book","Office Supply",2.99

"Flower Pot","Home & Gardening",22.76

"Garden Hose","Home & Gardening",12.87

This format is referred to as a CSV (Comma Separated Values). Review this and understand what kind of data it is. The first line is called the header. The lines following the header is the actual data in the respective order. Each attribute is separated with a comma hence the name.

Requirements: To work on this assignment, first you need to create a file called products.csv in your eclipse workspace and paste the above data in that file.

Your program should read through the file and compute the following calculations. 1). The average price for each product_category. 2). The total sales value for each product_category. 3). The number of products in each product_category. 4). The most expensive item in each product_category.

Your program should print to the screen the information. Here is an example of how it should be printed out:

Beauty

average_price = 8.66

total_sales_value = 25.97

number_of_products = 3

most_expensive = 9.99

Office Supply

average_price = xxxx etc etc...

"PRODUCT_NAME","PRODUCT_CATEGORY","SALES_PRICE"
"Hair Gel","Beauty",6.99
"Scotch tape","Office Supply",2.99
"Office Chair","Office Supply",134.99
"Eraser","Office Supply",0.99
"Lipstick","Beauty",9.99
"Nail Polish","Beauty",8.99
"Note Book","Office Supply",2.99
"Flower Pot","Home & Gardening",22.76
"Garden Hose","Home & Gardening",12.87

This format is referred to as a CSV (Comma Separated Values). Review this and understand what kind of data it is. The first line is called the header. The lines following the header is the actual data in the respective order. Each attribute is separated with a comma hence the name.

Requirements:

To work on this assignment, first you need to create a file called products.csv in your eclipse workspace and paste the above data in that file.

Your program should read through the file and compute the following calculations.

  1. The average price for each product_category.

  2. The total sales value for each product_category.

  3. The number of products in each product_category.

  4. The most expensive item in each product_category.

Your program should print to the screen the information. Here is an example of how it should be printed out:

Beauty
----------------------------------
average_price = 8.66
total_sales_value = 25.97
number_of_products = 3
most_expensive = 9.99

Office Supply
----------------------------------
average_price = xxxx

etc etc...

Here is how i solved it: Here is how i solved it:

Firstly, is there a better way to send in codes for reviews on this site? I mean like send in a jar file and it can be opened and lastly; most importantly, I just need to get your feedback on areas to improve in the coding.

ThanksI just need to get your feedback on areas to improve in the coding.

A function which reads from a file and displays the results

"PRODUCT_NAME","PRODUCT_CATEGORY","SALES_PRICE"

"Hair Gel","Beauty",6.99 "Scotch tape","Office Supply",2.99

"Office Chair","Office Supply",134.99

"Eraser","Office Supply",0.99

"Lipstick","Beauty",9.99

"Nail Polish","Beauty",8.99

"Note Book","Office Supply",2.99

"Flower Pot","Home & Gardening",22.76

"Garden Hose","Home & Gardening",12.87

This format is referred to as a CSV (Comma Separated Values). Review this and understand what kind of data it is. The first line is called the header. The lines following the header is the actual data in the respective order. Each attribute is separated with a comma hence the name.

Requirements: To work on this assignment, first you need to create a file called products.csv in your eclipse workspace and paste the above data in that file.

Your program should read through the file and compute the following calculations. 1). The average price for each product_category. 2). The total sales value for each product_category. 3). The number of products in each product_category. 4). The most expensive item in each product_category.

Your program should print to the screen the information. Here is an example of how it should be printed out:

Beauty

average_price = 8.66

total_sales_value = 25.97

number_of_products = 3

most_expensive = 9.99

Office Supply

average_price = xxxx etc etc...

Here is how i solved it:

Firstly, is there a better way to send in codes for reviews on this site? I mean like send in a jar file and it can be opened and lastly; most importantly, I just need to get your feedback on areas to improve in the coding.

Thanks

Extracting price statistics from a CSV file

"PRODUCT_NAME","PRODUCT_CATEGORY","SALES_PRICE"
"Hair Gel","Beauty",6.99
"Scotch tape","Office Supply",2.99
"Office Chair","Office Supply",134.99
"Eraser","Office Supply",0.99
"Lipstick","Beauty",9.99
"Nail Polish","Beauty",8.99
"Note Book","Office Supply",2.99
"Flower Pot","Home & Gardening",22.76
"Garden Hose","Home & Gardening",12.87

This format is referred to as a CSV (Comma Separated Values). Review this and understand what kind of data it is. The first line is called the header. The lines following the header is the actual data in the respective order. Each attribute is separated with a comma hence the name.

Requirements:

To work on this assignment, first you need to create a file called products.csv in your eclipse workspace and paste the above data in that file.

Your program should read through the file and compute the following calculations.

  1. The average price for each product_category.

  2. The total sales value for each product_category.

  3. The number of products in each product_category.

  4. The most expensive item in each product_category.

Your program should print to the screen the information. Here is an example of how it should be printed out:

Beauty
----------------------------------
average_price = 8.66
total_sales_value = 25.97
number_of_products = 3
most_expensive = 9.99

Office Supply
----------------------------------
average_price = xxxx

etc etc...

Here is how i solved it:

I just need to get your feedback on areas to improve in the coding.

Source Link

A function which reads from a file and displays the results

I got the following task to solve:

"PRODUCT_NAME","PRODUCT_CATEGORY","SALES_PRICE"

"Hair Gel","Beauty",6.99 "Scotch tape","Office Supply",2.99

"Office Chair","Office Supply",134.99

"Eraser","Office Supply",0.99

"Lipstick","Beauty",9.99

"Nail Polish","Beauty",8.99

"Note Book","Office Supply",2.99

"Flower Pot","Home & Gardening",22.76

"Garden Hose","Home & Gardening",12.87

This format is referred to as a CSV (Comma Separated Values). Review this and understand what kind of data it is. The first line is called the header. The lines following the header is the actual data in the respective order. Each attribute is separated with a comma hence the name.

Requirements: To work on this assignment, first you need to create a file called products.csv in your eclipse workspace and paste the above data in that file.

Your program should read through the file and compute the following calculations. 1). The average price for each product_category. 2). The total sales value for each product_category. 3). The number of products in each product_category. 4). The most expensive item in each product_category.

Your program should print to the screen the information. Here is an example of how it should be printed out:

Beauty

average_price = 8.66

total_sales_value = 25.97

number_of_products = 3

most_expensive = 9.99

Office Supply

average_price = xxxx etc etc...

Here is how i solved it:

//AggregateUtils

public class AggregateUtil {
//static methods that do the calculations..
    
    
    static double getTheAveragePriceForEachCategory(List<Double> product){
        double average = getTotalSalesValueForEachCategory(product)/(double)getNumberOfProductsInEachCategory(product);
        return Math.round(average * 100D) / 100D;
    }
    
    static double getTotalSalesValueForEachCategory(List<Double> product){
        double sum = 0.0;
        for(Double value : product){
            sum+=value;
        }
        return Math.round(sum * 100D) / 100D;
    }
    
    static int getNumberOfProductsInEachCategory(List<Double> product){
        return (product.size());
    }
    
    static double getTheMostExpensiveProductInEachCategory(List<Double> product){
        double highest = Double.MIN_VALUE;
        for(Double value : product){
            if(value>highest) highest = value;
        }
        return highest;
    }
}

//DisplayUtils

public class DisplayUtil {
//prints the values under each product category..
    public static void DisplayProducts( Map<String, List<Double>> db){
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String,List<Double>> entry : db.entrySet()) {
              builder = new StringBuilder();
              System.out.println(entry.getKey());
              System.out.println("----------------------------------");
              builder.append("average_price = ");
              double avrg_price = AggregateUtil.getTheAveragePriceForEachCategory(entry.getValue());
              builder.append(avrg_price);
              builder.append("\n");
              builder.append("total_sales_value = ");
              double total_sales = AggregateUtil.getTotalSalesValueForEachCategory(entry.getValue());
              builder.append(total_sales);
              builder.append("\n");
              builder.append("number_of_products = ");
              double numberOfProducts = AggregateUtil.getNumberOfProductsInEachCategory(entry.getValue());
              builder.append(numberOfProducts);
              builder.append("\n");
              builder.append("most_expensive = ");
              double mostExpensive = AggregateUtil.getTheMostExpensiveProductInEachCategory(entry.getValue());
              builder.append(mostExpensive);
              builder.append("\n");
              System.out.println(builder + "\n");
              
            }
    }
}

//FileUtils

public static void ReadFile(String path, Map<String, List<Double>> db){
    try(BufferedReader br = new BufferedReader(new FileReader(path))){
        String line;
        if((line = br.readLine()) != null){
            //this is the first heading..and needed to be skipped
        }
        while ((line = br.readLine()) != null) {
            String[] keys = line.split(",");
            keys[1] = keys[1].replace("\"", "");
            if(db.containsKey(keys[1])){
                List<Double> list = db.get(keys[1]);
                list.add(Double.valueOf(keys[2]));
                db.put(keys[1], list);
                
            }
            else{
                List<Double> list = new ArrayList<>();
                list.add(Double.valueOf(keys[2]));
                db.put(keys[1],list);
            }
        }
        
    }catch(IOException e){}
}


}

//the controller class

public class ControllerClass {
    
    public static void main(String []args){
        Map<String, List<Double>> db = new HashMap<>();
        FileUtils.ReadFile("./Files/products.csv", db);
        DisplayUtil.DisplayProducts(db);
    
    }

}

Firstly, is there a better way to send in codes for reviews on this site? I mean like send in a jar file and it can be opened and lastly; most importantly, I just need to get your feedback on areas to improve in the coding.

Thanks