用JAVA代码实现上述系统
时间: 2024-12-18 19:20:11 浏览: 3
下面是一个简化的Java代码示例,展示了如何实现上述个人财务管理系统的基本功能。这里我们假设使用了`ArrayList`代替`HashMap`来进行简单的存储,实际生产环境中会更倾向于使用数据库来持久化数据。同时,我们将`Comparable`接口应用到`PersonalFinanceRecord`上以便进行排序。
```java
import java.util.*;
// 定义财务记录类
class PersonalFinanceRecord implements Comparable<PersonalFinanceRecord> {
private int id;
private String category;
private double amount;
private String otherProperty;
// 构造函数和getter/setter...
@Override
public int compareTo(PersonalFinanceRecord o) {
return Double.compare(this.amount, o.amount);
}
}
// 财务管理系统类
public class FinanceManagementSystem {
private List<PersonalFinanceRecord> records = new ArrayList<>();
// 添加记录
public void addRecord(PersonalFinanceRecord record) {
records.add(record);
}
// 根据ID查询
public PersonalFinanceRecord findById(int id) {
for (PersonalFinanceRecord r : records) {
if (r.getId() == id) {
return r;
}
}
return null;
}
// 指定范围查询(升序)
public List<PersonalFinanceRecord> searchByAmountRange(double min, double max) {
return records.stream()
.filter(r -> r.getAmount() >= min && r.getAmount() <= max)
.sorted().collect(Collectors.toList());
}
// 分类统计
public double getCategoryTotal(String category) {
double total = 0;
for (PersonalFinanceRecord r : records) {
if (r.getCategory().equals(category)) {
total += r.getAmount();
}
}
return total;
}
// 排行榜(按金额)
public List<PersonalFinanceRecord> getTopTen() {
return records.stream()
.sorted(Comparator.reverseOrder())
.limit(10)
.collect(Collectors.toList());
}
// 输出记录
public void printRecord(PersonalFinanceRecord record) {
System.out.println("ID: " + record.getId() + ", Category: " + record.getCategory()
+ ", Amount: " + record.getAmount() + ", Other Property: " + record.getOtherProperty());
}
}
```
阅读全文