-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMallItemDataProcessing.java
More file actions
152 lines (130 loc) · 5.13 KB
/
Copy pathMallItemDataProcessing.java
File metadata and controls
152 lines (130 loc) · 5.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package exp3;
import java.util.function.Function;
public class MallItemDataProcessing {
private static Item[] items = new Item[]{
new Item("P001", "红米 Turbo 4 (16GB/256GB)", "手机", 1699.00, 100),
new Item("P002", "红米 Turbo 4 (12GB/256GB)", "手机", 1599.00, 150),
new Item("P003", "红米 Turbo 4 (12GB/512GB)", "手机", 1899.00, 200),
new Item("P004", "红米 Turbo 4 (16GB/512GB)", "手机", 1999.00, 250),
new Item("P005", "iPhone 17 (256GB)", "手机", 5299.00, 50),
new Item("P006", "iPhone 17 (512GB)", "手机", 7699.00, 30),
new Item("P007", "iPhone 17 Pro (256GB)", "手机", 7499.00, 40),
new Item("P008", "iPhone 17 Pro (512GB)", "手机", 9699.00, 20),
new Item("P009", "iPhone 17 Pro (1TB)", "手机", 11499.00, 10),
new Item("P010", "iPhone 17 Pro Max (256GB)", "手机", 8499.00, 30),
new Item("P011", "iPhone 17 Pro Max (512GB)", "手机", 10699.00, 25),
new Item("P012", "iPhone 17 Pro Max (1TB)", "手机", 12499.00, 15),
new Item("P013", "iPhone 17 Pro Max (2TB)", "手机", 16499.00, 5),
new Item("P014", "Macbook Air M5 (16GB/512GB)", "电脑", 9249.00, 20),
new Item("P015", "Macbook Air M5 (16GB/1TB)", "电脑", 11499.00, 15),
new Item("P016", "Macbook Air M5 (24GB/1TB)", "电脑", 12999.00, 10)};
private Item[] QuickDesendingSort(Item[] items) {
if (items == null || items.length <= 1) {
return items;
}
quickSort(items, 0, items.length - 1);
return items;
}
private void quickSort(Item[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
private int partition(Item[] arr, int low, int high) {
Item pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j].getPrice() > pivot.getPrice()) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
private void swap(Item[] arr, int i, int j) {
Item temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private Item[] getMostExpensiveItems(int n, double minPrice) {
// 前 n 个价格高于 minPrice 的物品
if (n <= 0 || n > items.length) {
throw new IllegalArgumentException("Invalid number of items requested.");
}
Item[] sortedItems = QuickDesendingSort(items);
Item[] mostExpensiveItems = new Item[n];
int count = 0;
for (int i = 0; i < sortedItems.length && count < n; i++) {
if (sortedItems[i].getPrice() >= minPrice) {
mostExpensiveItems[count] = sortedItems[i];
count++;
}
}
return mostExpensiveItems;
}
private static String toUpperCase(String str) {
Function<String, String> f = String::toUpperCase;
return f.apply(str);
}
public double getTotalPrice() {
double total = 0.0;
for (Item item : items) {
total += item.getPrice() * item.getStock();
}
return total;
}
public static void main(String[] args) {
MallItemDataProcessing processor = new MallItemDataProcessing();
// 排序
Item[] sortedItems = processor.QuickDesendingSort(items);
System.out.println("按价格逆序排列商品列表:");
for (Item item : sortedItems) {
System.out.println(item.getName() + " - " + item.getPrice());
}
// 获取前 5 个价格 >= 2000 的物品
Item[] mostExpensiveItems = processor.getMostExpensiveItems(5, 2000.00);
System.out.println("\n筛选高价物品(>=2000)前 5 条:[");
for (Item item : mostExpensiveItems) {
if (item != null) {
System.out.print(item.getItemId() + ":" + item.getName() + "(" + item.getPrice() + "), ");
}
}
System.out.println("]");
System.out.println("统计结果:总数量:" + items.length + ",总价:" + processor.getTotalPrice() + ",最高价:" + sortedItems[0].getPrice() + ",最低价:" + sortedItems[sortedItems.length - 1].getPrice());
}
}
class Item {
private String itemId;
private String name;
private String category;
private double price;
private int stock;
Item() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Empty constructor is not supported.");
}
Item(String itemId, String name, String category, double price, int stock) {
this.itemId = itemId;
this.name = name;
this.category = category;
this.price = price;
this.stock = stock;
}
public String getItemId() {
return itemId;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
}