模拟一个网游积分系统,计算玩家的积分,使用配置文件确定运行时的注入对象 积分bean的主要方法是返回修改和返回用户积分,修改时的参数为用户id和增加的积分,可以为正负数;查询时的参数是用户id,返回用户的积分。
时间: 2024-06-11 16:04:33 浏览: 5
首先,需要定义一个积分Bean类,包含用户id和积分数的属性,以及修改和查询积分数的方法。
```
public class ScoreBean {
private int userId;
private int score;
public ScoreBean(int userId, int score) {
this.userId = userId;
this.score = score;
}
public int getUserId() {
return userId;
}
public int getScore() {
return score;
}
public void addScore(int point) {
score += point;
}
public int queryScore() {
return score;
}
}
```
然后,需要定义一个配置文件,用于确定运行时的注入对象,例如:
```
#config.properties
score.bean=ScoreBean
```
接下来,需要编写一个积分计算器类,用于管理积分对象,并提供修改和查询积分数的方法。
```
import java.util.HashMap;
import java.util.Map;
public class ScoreCalculator {
private Map<Integer, ScoreBean> scoreMap;
private ScoreBean scoreBean;
public ScoreCalculator() {
scoreMap = new HashMap<>();
try {
String className = PropertiesUtil.getProperty("config.properties", "score.bean");
scoreBean = (ScoreBean) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addScore(int userId, int point) {
ScoreBean bean = scoreMap.get(userId);
if (bean == null) {
bean = new ScoreBean(userId, 0);
}
bean.addScore(point);
scoreMap.put(userId, bean);
}
public int queryScore(int userId) {
ScoreBean bean = scoreMap.get(userId);
if (bean == null) {
bean = scoreBean;
}
return bean.queryScore();
}
}
```
在上述代码中,使用了PropertiesUtil类来读取配置文件中的score.bean属性,根据属性值来创建具体的积分对象。addScore方法用于增加或减少积分数,先从scoreMap中查找该用户的积分对象,如果不存在则创建一个新的对象,然后调用ScoreBean的addScore方法进行修改,并将修改后的积分对象存储回scoreMap中。queryScore方法用于查询积分数,先从scoreMap中查找该用户的积分对象,如果不存在则使用配置文件中指定的积分对象。
最后,需要编写一个测试类来验证积分计算器的功能。
```
public class ScoreCalculatorTest {
public static void main(String[] args) {
ScoreCalculator calculator = new ScoreCalculator();
// 测试增加积分
calculator.addScore(1, 10);
calculator.addScore(2, 20);
calculator.addScore(3, 30);
// 测试查询积分
System.out.println(calculator.queryScore(1)); // 输出10
System.out.println(calculator.queryScore(2)); // 输出20
System.out.println(calculator.queryScore(3)); // 输出30
System.out.println(calculator.queryScore(4)); // 输出0
}
}
```
在上述代码中,先调用addScore方法增加一些积分,然后调用queryScore方法查询积分数,并输出结果。如果用户不存在,则输出默认积分数0。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)