Created
January 16, 2018 00:27
-
-
Save baurzhan-konurbayev/44374b1dfad53b24ccc121e5ba911bcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class ConnectDemo { | |
public static void main(String[] args) { | |
ConnectionManager connMngr =new ConnectionManager(); | |
connMngr.createConnection(); | |
connMngr.createConnection(); | |
connMngr.createConnection(); | |
connMngr.createConnection(); | |
for (Connection arr:connMngr.passConnectArr()){ | |
System.out.println("connection ID: " + arr.toString()); | |
} | |
} | |
} | |
class Connection{ | |
private int size; | |
private static int counter=0; | |
private int id; | |
Connection () { | |
Random rand = new Random(); | |
this.id = rand.nextInt(1000) + 1; | |
counter++; | |
} | |
Connection (int id) { | |
this.id = id; | |
counter++; | |
} | |
int getId(){ | |
return this.id; | |
} | |
static int getCounter(){ | |
return counter; | |
} | |
public String toString(){ | |
return Integer.toString(getId()); | |
} | |
void doSomething(){ | |
System.out.println("Something happend "); | |
} | |
} | |
class ConnectionManager { | |
private Connection connectArr[]=new Connection[5]; | |
private int size=0; | |
void createConnection(){ | |
if (this.size==connectArr.length){ | |
System.out.println("all connections created "); | |
return; | |
} else | |
{ | |
connectArr[this.size]=new Connection(this.size + 1); | |
this.size++; | |
} | |
} | |
int getSize(){ | |
return this.size; | |
} | |
public Connection[] passConnectArr(){ | |
return (connectArr); | |
} | |
boolean containsId (int i) { | |
for (Connection n:connectArr) | |
if (n != null && n.getId()==i) { | |
return true; | |
} | |
return false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment