5
5
import java .net .Socket ;
6
6
import java .util .ArrayList ;
7
7
import java .util .List ;
8
+ import java .util .Scanner ;
8
9
import java .util .concurrent .ExecutorService ;
9
10
import java .util .concurrent .Executors ;
10
11
import java .util .stream .Collectors ;
@@ -39,7 +40,7 @@ private void startServer() {
39
40
throw new RuntimeException (e );
40
41
}
41
42
}
42
-
43
+
43
44
public void stopServer () {
44
45
try {
45
46
if (!server .isClosed ()) {
@@ -89,29 +90,76 @@ public void setBlockchain(Blockchain blockchain) {
89
90
this .blockchain = blockchain ;
90
91
}
91
92
93
+ public int getPort () {
94
+ return port ;
95
+ }
96
+
92
97
@ Override
93
98
public String toString () {
94
99
final StringBuilder sb = new StringBuilder ("Peer{" );
95
- sb .append ("port=" ).append (port );
100
+ sb .append ("blockchain=" ).append (blockchain );
101
+ sb .append (", port=" ).append (port );
96
102
sb .append ('}' );
97
103
return sb .toString ();
98
104
}
99
105
100
106
public static void main (String [] args ) {
101
- Blockchain blockchain = new Blockchain (new ArrayList <>(), 3 );
102
- Peer peer1 = P2P .addPeer (blockchain );
103
- System .out .println ("Peers:" + P2P .getPeers ());
104
- peer1 .mine ("1st block by p1" );
105
- System .out .println ("Blockchain with p1: " + peer1 .getBlockchain ().getBlocks ());
106
- Peer peer2 = P2P .addPeer (blockchain );
107
- System .out .println ("Peers: " + P2P .getPeers ());
108
- System .out .println ("Blockchain with p1: " + peer1 .getBlockchain ().getBlocks ());
109
- System .out .println ("Blockchain with p2: " + peer2 .getBlockchain ().getBlocks ());
110
- peer2 .mine ("2nd block by p2" );
111
- System .out .println ("Blockchain with p1: " + peer1 .getBlockchain ().getBlocks ());
112
- System .out .println ("Blockchain with p2: " + peer2 .getBlockchain ().getBlocks ());
113
- peer1 .mine ("3rd block by p1" );
114
- System .out .println ("Blockchain with p1: " + peer1 .getBlockchain ().getBlocks ());
115
- System .out .println ("Blockchain with p2: " + peer2 .getBlockchain ().getBlocks ());
107
+ try {
108
+ int menuChoice ;
109
+ int peerIndex ;
110
+ String data ;
111
+ Scanner s = new Scanner (System .in );
112
+ Blockchain blockchain = new Blockchain (new ArrayList <>(), 3 );
113
+
114
+ while (true ) {
115
+
116
+ System .out .println ("\n ======= Welcome to Blockchain in Java =======" );
117
+ System .out .println ("1. Add Peer" );
118
+ System .out .println ("2. Mine data in peer" );
119
+ System .out .println ("3. Remove peer" );
120
+ System .out .println ("4. Show peers" );
121
+ System .out .println ("5. Exit" );
122
+
123
+ menuChoice = s .nextInt ();
124
+
125
+ switch (menuChoice ) {
126
+ case 1 :
127
+ P2P .addPeer (blockchain );
128
+ System .out .println ("New peer added!" );
129
+ P2P .showPeersWithBlockchain ();
130
+ break ;
131
+ case 2 :
132
+ System .out .println ("Choose peer: (ex. 1, 2, etc.)" );
133
+ P2P .showPeers ();
134
+ peerIndex = s .nextInt ();
135
+ Peer p = P2P .getPeer (peerIndex - 1 );
136
+ System .out .println ("Enter data: " );
137
+ data = s .next ();
138
+ p .mine (data );
139
+ System .out .println ("Data mined!" );
140
+ P2P .showPeersWithBlockchain ();
141
+ break ;
142
+ case 3 :
143
+ System .out .println ("Choose peer: (ex. 1, 2, etc.)" );
144
+ P2P .showPeers ();
145
+ peerIndex = s .nextInt ();
146
+ P2P .removePeer (peerIndex - 1 );
147
+ System .out .println ("Peer " + peerIndex + " removed!" );
148
+ P2P .showPeersWithBlockchain ();
149
+ break ;
150
+ case 4 :
151
+ P2P .showPeersWithBlockchain ();
152
+ break ;
153
+ case 5 :
154
+ P2P .removeAllPeers ();
155
+ System .out .println ("Bye, see you soon!" );
156
+ System .exit (0 );
157
+ default :
158
+ System .out .println ("Wrong choice!" );
159
+ }
160
+ }
161
+ } catch (Exception e ) {
162
+ throw new RuntimeException (e );
163
+ }
116
164
}
117
165
}
0 commit comments