-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductSale.java
More file actions
74 lines (62 loc) · 2.48 KB
/
ProductSale.java
File metadata and controls
74 lines (62 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
class ProductSale {
double price;
int quantity;
public ProductSale(double price, int quantity) {
this.price = price;
this.quantity = quantity;
}
public double getTotalRevenue() {
return price * quantity;
}
}
public class salesAnalyzer {
public static Map<String, Integer> countProductsByPriceRange(List<ProductSale> sales) {
Map<String, Integer> countByPriceRange = new HashMap<>();
for (ProductSale sale : sales) {
String priceRange = getPriceRange(sale.price);
countByPriceRange.put(priceRange, countByPriceRange.getOrDefault(priceRange, 0) + sale.quantity);
}
return countByPriceRange;
}
public static Map<String, Double> calculateRevenueByPriceRange(List<ProductSale> sales) {
Map<String, Double> revenueByPriceRange = new HashMap<>();
for (ProductSale sale : sales) {
String priceRange = getPriceRange(sale.price);
revenueByPriceRange.put(priceRange, revenueByPriceRange.getOrDefault(priceRange, 0.0) + sale.getTotalRevenue());
}
return revenueByPriceRange;
}
public static String getPriceRange(double price) {
if (price < 50) {
return "$0-50";
} else if (price < 100) {
return "$50-100";
} else if (price < 200) {
return "$100-200";
} else {
return "Above $200";
}
}
public static void main(String[] args) {
// Sample product sales
List<ProductSale> sales = new ArrayList<>();
sales.add(new ProductSale(25, 3));
sales.add(new ProductSale(75, 2));
sales.add(new ProductSale(150, 5));
sales.add(new ProductSale(210, 1));
// Count products by price range
Map<String, Integer> productCounts = countProductsByPriceRange(sales);
// Calculate revenue by price range
Map<String, Double> revenueByPriceRange = calculateRevenueByPriceRange(sales);
// Display results
System.out.println("Products sold by price range:");
for (Map.Entry<String, Integer> entry : productCounts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " products");
}
System.out.println("\nRevenue generated by price range:");
for (Map.Entry<String, Double> entry : revenueByPriceRange.entrySet()) {
System.out.println(entry.getKey() + ": $" + entry.getValue());
}
}
}