每位同学独立设计一个药店的药品销售统计系统,实现医药公司定期对销售各药品的记录进行统计,可按药品的编号、单价、销售量或销售额做出排名。在本系统中,对单价的排序采用冒泡排序法,对销售量的排序采用快速排序法,对销售额的排序采用堆排序法。 用读取文件的方法的JAVA代码实现
时间: 2024-04-05 14:32:43 浏览: 100
JAVA 冒泡法,选择法,插入法,快速排序法,实现代码
以下是一个简单的实现示例,假设数据文件的格式为每行包含药品编号、单价、销售量和销售额:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class DrugStoreStatistics {
public static void main(String[] args) {
String fileName = "sales_data.txt"; // 数据文件名
int numDrugs = 100; // 假设药品数量不超过100
Drug[] drugs = new Drug[numDrugs]; // 存储药品信息的数组
// 读取数据文件并初始化药品数组
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
int i = 0;
while ((line = br.readLine()) != null) {
String[] fields = line.split("\\s+"); // 假设数据字段间用空格分隔
int code = Integer.parseInt(fields[0]);
double price = Double.parseDouble(fields[1]);
int quantity = Integer.parseInt(fields[2]);
double sales = Double.parseDouble(fields[3]);
Drug drug = new Drug(code, price, quantity, sales);
drugs[i++] = drug;
}
} catch (IOException e) {
e.printStackTrace();
}
// 按单价排序
bubbleSortByPrice(drugs);
// 按销售量排序
quickSortByQuantity(drugs, 0, numDrugs-1);
// 按销售额排序
heapSortBySales(drugs);
// 输出排序结果
for (Drug drug : drugs) {
System.out.println(drug);
}
}
// 药品类
static class Drug {
int code;
double price;
int quantity;
double sales;
public Drug(int code, double price, int quantity, double sales) {
this.code = code;
this.price = price;
this.quantity = quantity;
this.sales = sales;
}
@Override
public String toString() {
return String.format("%d\t%.2f\t%d\t%.2f", code, price, quantity, sales);
}
}
// 冒泡排序按单价排序
static void bubbleSortByPrice(Drug[] drugs) {
int n = drugs.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (drugs[j].price > drugs[j+1].price) {
Drug temp = drugs[j];
drugs[j] = drugs[j+1];
drugs[j+1] = temp;
}
}
}
}
// 快速排序按销售量排序
static void quickSortByQuantity(Drug[] drugs, int left, int right) {
if (left < right) {
int pivot = partitionByQuantity(drugs, left, right);
quickSortByQuantity(drugs, left, pivot-1);
quickSortByQuantity(drugs, pivot+1, right);
}
}
static int partitionByQuantity(Drug[] drugs, int left, int right) {
Drug pivot = drugs[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (drugs[j].quantity < pivot.quantity) {
i++;
Drug temp = drugs[i];
drugs[i] = drugs[j];
drugs[j] = temp;
}
}
Drug temp = drugs[i+1];
drugs[i+1] = drugs[right];
drugs[right] = temp;
return i+1;
}
// 堆排序按销售额排序
static void heapSortBySales(Drug[] drugs) {
int n = drugs.length;
for (int i = n/2-1; i >= 0; i--) {
heapifyBySales(drugs, n, i);
}
for (int i = n-1; i >= 0; i--) {
Drug temp = drugs[0];
drugs[0] = drugs[i];
drugs[i] = temp;
heapifyBySales(drugs, i, 0);
}
}
static void heapifyBySales(Drug[] drugs, int n, int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < n && drugs[left].sales > drugs[largest].sales) {
largest = left;
}
if (right < n && drugs[right].sales > drugs[largest].sales) {
largest = right;
}
if (largest != i) {
Drug temp = drugs[i];
drugs[i] = drugs[largest];
drugs[largest] = temp;
heapifyBySales(drugs, n, largest);
}
}
}
```
注意:以上代码仅供参考,实际场景下需要根据具体需求进行修改和优化。
阅读全文