-
Notifications
You must be signed in to change notification settings - Fork 0
/
Man.java
120 lines (101 loc) · 3.75 KB
/
Man.java
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
package blockchain;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.*;
public abstract class Man {
private final int id;
private final String socialStatus;//社会地位
private final Blockchain blockchain;
private final List<Man> manList;
private int monkey;
private PrivateKey privateKey;
private PublicKey publicKey;
public Man(int id, String socialStatus, List<Man> manList, Blockchain blockchain) {
this.id = id;
this.socialStatus = socialStatus;
this.manList = manList;
this.monkey = 100;
this.blockchain = blockchain;
try {
Key key = new Key(1024);
key.createKeys();
this.privateKey = key.getPrivateKey();
this.publicKey = key.getPublicKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public List<Man> getManList() {
return manList;
}
public int getId() {
return id;
}
public int getManListSize() {
return manList.size();
}
public String getSocialStatus() {
return socialStatus;
}
//交易条件
public abstract boolean translationCondition(Man man);
//获取金钱
public int getMoney() {
return monkey;
}
public void income(int money) {
this.monkey += money;
}
public void expend(int money) {
this.monkey -= money;
}
//发送消息给区块链
public synchronized void sendMessage(Man toMan, int expendMoney) {
boolean create_status = false;
do {
//钱不够了,break
if (expendMoney > getMoney())
break;
SignMessage sign = new SignMessage();
int translationId = blockchain.getCurrentTranslationId();
byte[] signature = sign.sign(String.valueOf(translationId + getId() + expendMoney + toMan.getId()), privateKey);
TranslationMessage translationMessage = new TranslationMessage(translationId, publicKey, signature, this, toMan, expendMoney);
if (translationId > blockchain.getNewestChatID())
create_status = blockchain.createMessage(translationMessage);
} while (!create_status);
}
//随机消费
public synchronized void randomExpend() {
Timer timer = new Timer();
Random rand = new Random();
timer.schedule(new TimerTask() {
@Override
public void run() {
int rand_num = rand.nextInt(getManListSize());
//随机消费对象
Man translationMan = getManList().get(rand_num);
if (translationCondition(translationMan)) {
//随机金额的消费
int rand_expend_money = rand.nextInt(10) + 25;
sendMessage(translationMan, rand_expend_money);
}
}
}, 30 + rand.nextInt(10) ,
30 + rand.nextInt(10) );
}
//固定消费
public synchronized void regularExpend(List<Man> regularManList, int money, int seconds) {
Timer timer = new Timer();
Random rand = new Random();
timer.schedule(new TimerTask() {
@Override
public void run() {
for (Man man : regularManList) {
sendMessage(man, money);
}
}
}, seconds
, money);
}
}