Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Packet {
- private String msg;
- private int number;
- private double integrity;
- private String destinationAddress;
- public Packet(String msg, int number, double integrity, String destinationAddress) {
- this.setMessage(msg);
- this.setNumber(number);
- this.setIntegrity(integrity);
- this.setDestinationAddress(destinationAddress);
- }
- private double calcIntegrity(double integrity){
- String[] msgSymbols = this.msg.split("");
- for (String symbol: msgSymbols){
- if(symbol.equals("-")){
- integrity -= 0.01;
- }
- }
- return integrity;
- }
- void setIntegrity(double integrity) {
- integrity = calcIntegrity(integrity);
- if (integrity < 0) {
- this.integrity = 0;
- } else if (integrity > 1) {
- this.integrity = 1;
- } else {
- this.integrity = integrity;
- }
- }
- private void setNumber(int number) {
- this.number = Math.abs(number);
- }
- private void setMessage(String msg) {
- this.msg = msg;
- }
- public int getNumber() {
- return this.number;
- }
- public double getIntegrity() {
- return this.integrity;
- }
- public String getDestinationAddress() {
- return destinationAddress;
- }
- private boolean checkIP(String destinationAddress){
- String zeroTo255
- = "(\\d{1,2}|(0|1)\\"
- + "d{2}|2[0-4]\\d|25[0-5])";
- String regex
- = zeroTo255 + "\\."
- + zeroTo255 + "\\."
- + zeroTo255 + "\\."
- + zeroTo255;
- Pattern p = Pattern.compile(regex);
- if (destinationAddress == null){
- return false;
- }
- Matcher m = p.matcher(destinationAddress);
- return m.matches();
- }
- public void setDestinationAddress(String destinationAddress) {
- if(checkIP(destinationAddress)){
- this.destinationAddress = destinationAddress;
- }
- else{
- this.destinationAddress = "0.0.0.0";
- }
- }
- @Override
- public String toString() {
- if (this.integrity < 0.5) {
- return "corrupted message";
- }
- return this.msg;
- }
- }
- //--------------------------------------------------------------
- import java.util.ArrayList;
- import java.util.Scanner;
- public class PacketMain {
- static void bubbleSort(ArrayList<Packet> al) {
- int n = al.size();
- for (int i = 0; i < n-1; i++) {
- for (int j = 0; j < n-i-1; j++) {
- if (al.get(j).getNumber() > al.get(j + 1).getNumber()){
- // swap arr[j+1] and arr[j]
- Packet temp = al.get(j);
- al.set(j, al.get(j + 1));
- al.set(j + 1, temp);
- }
- }
- }
- }
- static void selectionSort(ArrayList<Packet> al) {
- int n = al.size();
- // One by one move boundary of unsorted subarray
- for (int i = 0; i < n; i++) {
- // Find the minimum element in unsorted array
- int min_idx = i;
- for (int j = i + 1; j < n; j++){
- if (al.get(j).getNumber() < al.get(min_idx).getNumber()){
- min_idx = j;
- // Swap the found minimum element with the first
- // element
- Packet temp = al.get(min_idx);
- al.set(min_idx, al.get(i));
- al.set(i, temp);
- }
- }
- }
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- ArrayList<Packet> packets = new ArrayList<Packet>();
- System.out.print("Enter your IP address: ");
- String clientAddress = scan.nextLine();
- System.out.print("Enter packets count: ");
- int n = Integer.parseInt(scan.nextLine());
- for (int i = 0; i < n; i++) {
- // read info, create object, add packets
- System.out.print("Read message: ");
- String msg = scan.nextLine();
- System.out.print("Read packet number: ");
- int num = Integer.parseInt(scan.nextLine());
- System.out.print("Read packet integrity: ");
- double integrity = Double.parseDouble(scan.nextLine());
- System.out.print("Read destination IP address: ");
- String destIP = scan.nextLine();
- packets.add(new Packet(msg, num, integrity, destIP));
- }
- selectionSort(packets);
- System.out.println("Message: ");
- for (Packet p : packets) {
- if (clientAddress.equals(p.getDestinationAddress())) {
- if (p.getIntegrity() >= 0.5) {
- System.out.print(p);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment