← Vissza
A feladat forráskódja:
package ip;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
class IpAddress implements Comparable<IpAddress> {
public enum IpType {
DOC,
GLOBAL,
LOCAL,
OTHER
}
private String ip;
private Enum<IpType> ipType;
public IpAddress(String ip) {
this.ip = ip;
String[] ipString = ip.split(":");
if (ip.startsWith("2001:0db8")) {
this.ipType = IpType.DOC;
} else if (ip.startsWith("2001:0e")) {
this.ipType = IpType.GLOBAL;
} else if (ip.startsWith("fc") || ip.startsWith("fd")) {
this.ipType = IpType.LOCAL;
} else {
this.ipType = IpType.OTHER;
}
}
@Override
public int compareTo(IpAddress t) {
for (int i = 0; i < this.ip.length(); i++) {
if (this.ip.charAt(i) < t.ip.charAt(i)) {
return -1;
} else if (this.ip.charAt(i) > t.ip.charAt(i)) {
return 1;
}
}
return 0;
}
public String getIp() {
return ip;
}
public Enum<IpType> getIpType() {
return ipType;
}
public boolean has18Zeros() {
int zeros = 0;
for (int i = 0; i < ip.length(); i++) {
if (ip.charAt(i) == '0') {
zeros++;
}
}
return zeros >= 18;
}
public String getIpWithoutFirstZeros() {
String[] ipArray = ip.split(":");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ipArray.length; i++) {
if (ipArray[i].startsWith("000")) {
sb.append(ipArray[i].substring(3));
} else if (ipArray[i].startsWith("00")) {
sb.append(ipArray[i].substring(2));
} else if (ipArray[i].startsWith("0")) {
sb.append(ipArray[i].substring(1));
} else {
sb.append(ipArray[i]);
}
sb.append(":");
}
return sb.toString().substring(0, sb.length() - 1);
}
}
/**
*
* @author comtur
*/
public class Ip {
private static List<IpAddress> ipList;
public static void main(String[] args) {
task1();
task2();
task3();
task4();
task5();
int ipNum = task6();
task7(ipNum);
}
private static void task1() {
taskNum(1);
System.out.println("(Adatok beolvasása)");
ipList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("ip.txt"))) {
String line = null;
while ((line = br.readLine()) != null) {
IpAddress ipAddress = new IpAddress(line);
ipList.add(ipAddress);
}
} catch (IOException ex) {
Logger.getLogger(Ip.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat");
System.out.println("----------");
}
private static void task2() {
taskNum(2);
System.out.printf("Az állományban %d db adatsor van.%n", ipList.size());
}
private static void task3() {
taskNum(3);
System.out.print("A legalacsonyabb tárolt IP-cím: ");
IpAddress minIp = ipList.get(0);
for (IpAddress ipAddress : ipList) {
if (ipAddress.compareTo(minIp) < 0) {
minIp = ipAddress;
}
}
System.out.println(minIp.getIp());
}
private static void task4() {
taskNum(4);
int doc = 0, global = 0, local = 0, other = 0;
for (IpAddress ipAddress : ipList) {
if (ipAddress.getIpType().equals(IpAddress.IpType.DOC)) {
doc++;
} else if (ipAddress.getIpType().equals(IpAddress.IpType.GLOBAL)) {
global++;
} else if (ipAddress.getIpType().equals(IpAddress.IpType.LOCAL)) {
local++;
} else {
other++;
}
}
System.out.println("Dokumentációs cím: " + doc + " darab");
System.out.println("Globális egyedi cím: " + global + " darab");
System.out.println("Helyi egyedi cím: " + local + " darab");
if (other > 0) {
System.out.println("Egyéb, nem besorolt cím: " + other + " darab");
}
}
private static void task5() {
taskNum(5);
System.out.println("(\"sok.txt\" állomány létrehozása)");
try (PrintWriter pw = new PrintWriter("sok.txt")) {
for (int i = 0; i < ipList.size(); i++) {
if (ipList.get(i).has18Zeros()) {
pw.println((i + 1) + " " + ipList.get(i).getIp());
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Ip.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static int task6() {
taskNum(6);
Scanner sc = new Scanner(System.in);
System.out.print("Kérek egy sorszámot: ");
int ipNum = sc.nextInt();
sc.nextLine();
System.out.println(ipList.get(ipNum - 1).getIp());
System.out.println(ipList.get(ipNum - 1).getIpWithoutFirstZeros());
return ipNum;
}
private static void task7(int ipNum) {
taskNum(7);
String ipString = ipList.get(ipNum - 1).getIpWithoutFirstZeros();
String[] ipToShort = ipString.split(":");
int dedicatedZeroPlace = -1;
boolean zeroRuns = false;
int zeroNums = 0;
int maxZeroNums = 0;
for (int i = 0; i < ipToShort.length; i++) {
if (ipToShort[i] .equals("0")) {
zeroNums++;
zeroRuns = true;
} else {
if (zeroRuns) {
if (zeroNums > maxZeroNums) {
maxZeroNums = zeroNums;
dedicatedZeroPlace = i - maxZeroNums;
}
}
zeroNums = 0;
zeroRuns = false;
}
}
if (zeroRuns) {
if (zeroNums > maxZeroNums) {
maxZeroNums = zeroNums;
dedicatedZeroPlace = ipToShort.length - 1 - maxZeroNums;
}
}
if (dedicatedZeroPlace == -1) {
System.out.println("Nem rövidíthető tovább.");
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ipToShort.length; i++) {
if (i < dedicatedZeroPlace || i > dedicatedZeroPlace + maxZeroNums - 1) {
sb.append(ipToShort[i]);
sb.append(":");
} else if (i == dedicatedZeroPlace) {
sb.append(":");
}
}
System.out.println(sb.toString().substring(0, sb.length() - 1));
}
}
}