55import java .net .Socket ;
66import java .util .ArrayList ;
77import java .util .List ;
8+ import java .util .Scanner ;
89import java .util .concurrent .ExecutorService ;
910import java .util .concurrent .Executors ;
1011import java .util .stream .Collectors ;
@@ -39,7 +40,7 @@ private void startServer() {
3940 throw new RuntimeException (e );
4041 }
4142 }
42-
43+
4344 public void stopServer () {
4445 try {
4546 if (!server .isClosed ()) {
@@ -89,29 +90,76 @@ public void setBlockchain(Blockchain blockchain) {
8990 this .blockchain = blockchain ;
9091 }
9192
93+ public int getPort () {
94+ return port ;
95+ }
96+
9297 @ Override
9398 public String toString () {
9499 final StringBuilder sb = new StringBuilder ("Peer{" );
95- sb .append ("port=" ).append (port );
100+ sb .append ("blockchain=" ).append (blockchain );
101+ sb .append (", port=" ).append (port );
96102 sb .append ('}' );
97103 return sb .toString ();
98104 }
99105
100106 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+ }
116164 }
117165}
0 commit comments