Skip to content

Commit 932f210

Browse files
committed
Minor refactorings
1 parent 3ab3e7a commit 932f210

File tree

2 files changed

+69
-70
lines changed

2 files changed

+69
-70
lines changed

src/main/java/com/rampatra/blockchain/P2P.java

+68
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Iterator;
55
import java.util.List;
6+
import java.util.Scanner;
67

78
/**
89
* @author rampatra
@@ -64,4 +65,71 @@ public static void showPeersWithBlockchain() {
6465
System.out.println("Peer " + (i + 1) + " (" + peers.get(i).getPort() + "): " + peers.get(i).getBlockchain());
6566
}
6667
}
68+
69+
/**
70+
* The main starting point of the blockchain demo. At first, add some peers (option 1) and mine some data
71+
* by choosing a particular peer (option 2). You would soon see that the newly mined block is broadcast to
72+
* all the peers.
73+
*
74+
* @param args
75+
*/
76+
public static void main(String[] args) {
77+
try {
78+
int menuChoice;
79+
int peerIndex;
80+
String data;
81+
Scanner s = new Scanner(System.in);
82+
Blockchain blockchain = new Blockchain(new ArrayList<>(), 3);
83+
84+
while (true) {
85+
86+
System.out.println("\n======= Welcome to Blockchain in Java =======");
87+
System.out.println("1. Add Peer");
88+
System.out.println("2. Mine data in peer");
89+
System.out.println("3. Remove peer");
90+
System.out.println("4. Show peers");
91+
System.out.println("5. Exit");
92+
93+
menuChoice = s.nextInt();
94+
95+
switch (menuChoice) {
96+
case 1:
97+
P2P.addPeer(blockchain);
98+
System.out.println("New peer added!");
99+
P2P.showPeersWithBlockchain();
100+
break;
101+
case 2:
102+
System.out.println("Choose peer: (a number for ex. 1, 2, etc.)");
103+
P2P.showPeers();
104+
peerIndex = s.nextInt();
105+
Peer p = P2P.getPeer(peerIndex - 1);
106+
System.out.println("Enter data: (a string with no spaces)");
107+
data = s.next();
108+
p.mine(data);
109+
System.out.println("Data mined!");
110+
P2P.showPeersWithBlockchain();
111+
break;
112+
case 3:
113+
System.out.println("Choose peer: (a number for ex. 1, 2, etc.)");
114+
P2P.showPeers();
115+
peerIndex = s.nextInt();
116+
P2P.removePeer(peerIndex - 1);
117+
System.out.println("Peer " + peerIndex + " removed!");
118+
P2P.showPeersWithBlockchain();
119+
break;
120+
case 4:
121+
P2P.showPeersWithBlockchain();
122+
break;
123+
case 5:
124+
P2P.removeAllPeers();
125+
System.out.println("Bye, see you soon!");
126+
System.exit(0);
127+
default:
128+
System.out.println("Wrong choice!");
129+
}
130+
}
131+
} catch (Exception e) {
132+
throw new RuntimeException(e);
133+
}
134+
}
67135
}

src/main/java/com/rampatra/blockchain/Peer.java

+1-70
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.io.ObjectOutputStream;
44
import java.net.ServerSocket;
55
import java.net.Socket;
6-
import java.util.ArrayList;
76
import java.util.List;
8-
import java.util.Scanner;
97
import java.util.concurrent.ExecutorService;
108
import java.util.concurrent.Executors;
119
import java.util.stream.Collectors;
@@ -59,7 +57,7 @@ public void stopServer() {
5957
}
6058

6159
/**
62-
* Once a new peer is created and added to the network, it requests the latest block from
60+
* Once a new peer is created and added to the network, it requests the latest block from
6361
* all the peers and updates its blockchain if it is outdated.
6462
*/
6563
private void getLatestBlockFromPeers() {
@@ -119,71 +117,4 @@ public String toString() {
119117
sb.append('}');
120118
return sb.toString();
121119
}
122-
123-
/**
124-
* The main starting point of the blockchain demo. At first, add some peers (option 1) and mine some data
125-
* by choosing a particular peer (option 2). You would soon see that the newly mined block is broadcast to
126-
* all the peers.
127-
*
128-
* @param args
129-
*/
130-
public static void main(String[] args) {
131-
try {
132-
int menuChoice;
133-
int peerIndex;
134-
String data;
135-
Scanner s = new Scanner(System.in);
136-
Blockchain blockchain = new Blockchain(new ArrayList<>(), 3);
137-
138-
while (true) {
139-
140-
System.out.println("\n======= Welcome to Blockchain in Java =======");
141-
System.out.println("1. Add Peer");
142-
System.out.println("2. Mine data in peer");
143-
System.out.println("3. Remove peer");
144-
System.out.println("4. Show peers");
145-
System.out.println("5. Exit");
146-
147-
menuChoice = s.nextInt();
148-
149-
switch (menuChoice) {
150-
case 1:
151-
P2P.addPeer(blockchain);
152-
System.out.println("New peer added!");
153-
P2P.showPeersWithBlockchain();
154-
break;
155-
case 2:
156-
System.out.println("Choose peer: (a number for ex. 1, 2, etc.)");
157-
P2P.showPeers();
158-
peerIndex = s.nextInt();
159-
Peer p = P2P.getPeer(peerIndex - 1);
160-
System.out.println("Enter data: (a string with no spaces)");
161-
data = s.next();
162-
p.mine(data);
163-
System.out.println("Data mined!");
164-
P2P.showPeersWithBlockchain();
165-
break;
166-
case 3:
167-
System.out.println("Choose peer: (a number for ex. 1, 2, etc.)");
168-
P2P.showPeers();
169-
peerIndex = s.nextInt();
170-
P2P.removePeer(peerIndex - 1);
171-
System.out.println("Peer " + peerIndex + " removed!");
172-
P2P.showPeersWithBlockchain();
173-
break;
174-
case 4:
175-
P2P.showPeersWithBlockchain();
176-
break;
177-
case 5:
178-
P2P.removeAllPeers();
179-
System.out.println("Bye, see you soon!");
180-
System.exit(0);
181-
default:
182-
System.out.println("Wrong choice!");
183-
}
184-
}
185-
} catch (Exception e) {
186-
throw new RuntimeException(e);
187-
}
188-
}
189120
}

0 commit comments

Comments
 (0)