Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import javax.swing.JOptionPane;
  7.  
  8.  
  9. public class Controller{
  10.     private View interfata;
  11.    
  12.     public Controller(View interfata){
  13.         this.interfata = interfata;
  14.         interfata.getBtnDoIt().addActionListener(new DoAction());
  15.        
  16.     }
  17.    
  18.     class DoAction implements ActionListener{
  19.    
  20.     public void actionPerformed(ActionEvent arg0) {
  21.         if(arg0.getSource() == interfata.getBtnDoIt()){
  22.            
  23.             String from1 = interfata.getFrom1().getText();
  24.             String from2 = interfata.getFrom2().getText();
  25.             String from3 = interfata.getFrom3().getText();
  26.             String from4 = interfata.getFrom4().getText();
  27.            
  28.             String to1   = interfata.getTo1().getText();
  29.             String to2   = interfata.getTo2().getText();
  30.             String to3   = interfata.getTo3().getText();
  31.             String to4   = interfata.getTo4().getText();
  32.            
  33.  
  34.            
  35.             if(isValid(from1) && isValid(from2) && isValid(from3) && isValid(from4) && isValid(to1) && isValid(to2) && isValid(to3) && isValid(to4)){
  36.                
  37.                 boolean cont=true;
  38.                
  39.                 String temp;
  40.                 int ifrom1   = Integer.parseInt(from1);
  41.                 int ifrom2   = Integer.parseInt(from2);
  42.                 int ifrom3   = Integer.parseInt(from3);
  43.                 int ifrom4   = Integer.parseInt(from4);
  44.                
  45.                 int ito1 = Integer.parseInt(to1);
  46.                 int ito2 = Integer.parseInt(to2);
  47.                 int ito3 = Integer.parseInt(to3);
  48.                 int ito4 = Integer.parseInt(to4);
  49.                
  50.                 if (correctInput(ifrom1,ifrom2,ifrom3,ifrom4,ito1,ito2,ito3,ito4)){
  51.                    
  52.                     try {
  53.                         BufferedWriter out = new BufferedWriter(new FileWriter("result.txt"));
  54.    
  55.                     while(cont) {
  56.  
  57.                     temp = ifrom1+"."+ifrom2+"."+ifrom3+"."+ifrom4+"\n";
  58.                     out.write(temp);
  59.                     ifrom4++;
  60.                     if(ifrom4==256){
  61.                         ifrom4=0;
  62.                         ifrom3++;
  63.                     }
  64.                     if (ifrom3==256){
  65.                         ifrom3=0;
  66.                         ifrom2++;
  67.                     }
  68.                     if (ifrom2==256){
  69.                         ifrom2=0;
  70.                         ifrom1++;
  71.                     }
  72.                     if( (ifrom1 == ito1) && (ifrom2 == ito2) && (ifrom3 == ito3) && (ifrom4 == ito4) ) {
  73.                         temp = ifrom1+"."+ifrom2+"."+ifrom3+"."+ifrom4+"\n";
  74.                         out.write(temp);
  75.                         cont = false;
  76.                         }
  77.                                
  78.             }
  79.                     out.close();
  80.                     } catch (IOException e) {
  81.                     }
  82.             }  
  83.                    
  84.                
  85.             }else JOptionPane.showMessageDialog(null, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
  86.             interfata.setLblstatus("Done!");
  87.         }
  88.        
  89.     }
  90.    
  91.     }
  92.    
  93.    
  94.     public boolean correctInput(int ifrom1, int ifrom2, int ifrom3, int ifrom4, int ito1, int ito2, int ito3, int ito4){
  95.         boolean check=true;
  96.         if(ifrom1 <= ito1){
  97.             if(ifrom1 == ito1){
  98.                 if (ifrom2 <= ito2){
  99.                     if(ifrom2 == ito2){
  100.                         if(ifrom3 <= ito3){
  101.                             if(ifrom3 == ito3){
  102.                                 if(ifrom4 <= ito4){
  103.                                    
  104.                                 }else {
  105.                                     JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
  106.                                     check = false;
  107.                                     }
  108.                             }
  109.                         }else {
  110.                             JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
  111.                             check = false;
  112.                             }
  113.                     }
  114.                 }else {
  115.                     JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
  116.                     check = false;
  117.                     }
  118.             }
  119.         }else {
  120.             JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
  121.             check = false;
  122.             }
  123.         return check;
  124.     }
  125.    
  126.    
  127.     public boolean isValid(String arg){
  128.         int test;
  129.         try{
  130.         test = Integer.parseInt(arg);
  131.         }
  132.         catch (NumberFormatException ex){
  133.             return false;
  134.         }
  135.         if (test < 0 || test > 255) return false;
  136.         else return true;
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement