I want to build and IP Address management tool. I want to store used IPs in a database with information on the device that uses the IP. I want to be able to then pull the "used" ips out and compare them against a list of IPs from a given subnet and determine if the ip is "used" or "available". I need the "Available" list to generate a list I can choose from to add to new devices, without picking a "used" ip.
Here is what I tried but it doesnt' seem to be comparing the two objects of type IPAddress.
ArrayList<IPAddress> used = new ArrayList<>();
ArrayList<IPAddress> available = new ArrayList<>();
ArrayList<IPAddress> subnet = new ArrayList<>();
//I reference my IPAddress class which has four int fields:
//oct_1, oct_2, oct_3, oct_4
//the constructor for IPAddress takes for Ints and I build my IPAddress
//from the for octets.
//build a list of all ips in the subnet 10.50.2.0 - 10.50.2.255
//assuming a class C subnet 255.255.255.0
for(int i = 0; i < 256; i++){
subnet.add(new IPAddress(10,50,2,i));
}
//identify used ips. Eventually want to pull these from a database
//but for now these are just random IP's representing possible used IPs
used.add(new IPAddress(10,50,2,3));
used.add(new IPAddress(10,50,2,5));
used.add(new IPAddress(10,50,2,9));
used.add(new IPAddress(10,50,2,13));
used.add(new IPAddress(10,50,2,17));
used.add(new IPAddress(10,50,2,22));
//**** NEEDED CODE ********
//i want to iterate through each IP in the subnet and check if it's in
//the 'used' list. If it IS NOT add the ip to the 'available' list.
//my failed try
for(IPAddress ip : subnet){
if(!used.contains(ip)){ //if ip is NOT in used add it to available
available.add(ip);
}
}
//print results out to the screen
for(IPAddress ip : available){
System.out.println(ip.toString() + " is available.");
}
for(IPAddress ip: used){
System.out.println(ip.toString() + " is used.");
}
//******************** Here is my IPAddress Class if it helps ****************
public class IPAddress {
private int oct_1 = 0;
private int oct_2 = 0;
private int oct_3 = 0;
private int oct_4 = 0;
public IPAddress(int Oct_1, int Oct_2, int Oct_3, int Oct_4){
oct_1 = Oct_1;
oct_2 = Oct_2;
oct_3 = Oct_3;
oct_4 = Oct_4;
}
@Override
public String toString(){
String ipAddress = "";
if(getOct_1() != 0){
ipAddress = getOct_1() + "." + getOct_2() + "." + getOct_3() + "." + getOct_4();
}
return ipAddress;
}