Advertisement
Mary_Loskutova

Final

Mar 30th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.79 KB | None | 0 0
  1. public abstract class MenuEntry {
  2.  
  3.     private String title;
  4.     public MenuEntry(String title) {
  5.         this.title = title;
  6.     }
  7.     public String getTitle() {
  8.         return title;
  9.     }
  10.     public void setTitle(String title) {
  11.         this.title = title;
  12.     }
  13.     public abstract void run();
  14. }
  15.  
  16.  
  17. public class Menu {
  18.     private static final String MENU_PATTERN = "%s - %s\n";
  19.     private ArrayList<MenuEntry> entries = new ArrayList<MenuEntry>();
  20.     private boolean isExit = false;
  21.  
  22.     public Menu() {
  23.         entries.add(new MenuEntry("Exit") {
  24.             @Override
  25.             public void run() {
  26.                 isExit = true;
  27.             }
  28.         });
  29.     }
  30.  
  31.     public void run() {
  32.         Scanner sc = new Scanner(System.in);
  33.         while (!isExit) {
  34.             printMenu();
  35.  
  36.             try {
  37.                 String line = sc.nextLine();
  38.  
  39.                 int choice = Integer.parseInt(line);
  40.                 MenuEntry entry = entries.get(choice - 1);
  41.                 entry.run();
  42.  
  43.             } catch (NumberFormatException e) {
  44.                 System.out.print("Wrong input!");
  45.             } catch (IndexOutOfBoundsException e2) {
  46.                 System.out.print("Wrong input!");
  47.             }
  48.         }
  49.         sc.close();
  50.     }
  51.  
  52.     public Menu addEntry(MenuEntry entry) {
  53.         int index = entries.size() - 1;
  54.         entries.add(index, entry);
  55.         return this;
  56.  
  57.     }
  58.  
  59.     private void printMenu() {
  60.         StringBuffer buffer = new StringBuffer();
  61.         buffer.append("\nMenu:\n");
  62.         for (int i = 0; i < entries.size(); i++) {
  63.             MenuEntry entry = entries.get(i);
  64.             String entryFormatted = String.format(MENU_PATTERN, (i + 1), entry.getTitle());
  65.             buffer.append(entryFormatted);
  66.         }
  67.         System.out.print(buffer.toString());
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. public class MailSender {
  74.  
  75.     /**
  76.      * @param args
  77.      */
  78.  
  79.     final String userName = "marinajavatest@gmail.com";
  80.     final String password = "javatest_5";
  81.  
  82.     Properties props = new Properties();
  83.  
  84.     public void setProperties() {
  85.         props.put("mail.smtp.auth", "true");
  86.         props.put("mail.smtp.starttls.enable", "true");
  87.         props.put("mail.smtp.host", "smtp.gmail.com");
  88.         props.put("mail.smtp.port", "587");
  89.     }
  90.  
  91.     Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  92.         protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
  93.             return new PasswordAuthentication(userName, password);
  94.         }
  95.     });
  96.  
  97.     public void sendMessage(String adress) {
  98.         try {
  99.             Message message = new MimeMessage(session);
  100.             message.setFrom(new InternetAddress(userName));
  101.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adress));
  102.             message.setSubject("Changes have been found");
  103.             message.setText("File has been chaged");
  104.             Transport.send(message);
  105.             System.out.println("Done! Message has been sent to the e-mail" + "'" + adress + "'");
  106.         } catch (AddressException ex) {
  107.             System.out.println ("E-mail adress doesn't exsist");
  108.         } catch (MessagingException ex) {
  109.             System.out.println  ("Sorry...something was wrong. E-mail hasn't been sent");
  110.         }
  111.     }
  112.  
  113. }
  114.  
  115.  
  116. public class File extends Thread {
  117.  
  118.     ArrayList<String> example;
  119.     String name;
  120.     String adress;
  121.    
  122.     File(String name, String adress) {
  123.         this.name = name;
  124.         this.adress = adress;
  125.         this.example = new ArrayList<String>();
  126.         this.setDaemon(true);
  127.     }
  128.  
  129.     public static ArrayList<String> readFile(String name) throws FileNotFoundException {
  130.         Scanner scanner = null;
  131.         scanner = new Scanner(new BufferedReader(new FileReader(name)));
  132.         ArrayList<String> fileList = new ArrayList<String>();
  133.         while (scanner.hasNextLine()) {
  134.         fileList.add(scanner.nextLine());
  135.         }
  136.         scanner.close();
  137.         return fileList;
  138.     }
  139.  
  140.     public static void sendMessage(String adress) {
  141.         MailSender mailsender = new MailSender();
  142.         mailsender.setProperties();
  143.         mailsender.sendMessage(adress);
  144.     }
  145.  
  146.     public void observeFiles() throws FileNotFoundException, InterruptedException {
  147.         ArrayList<String> baseFile;
  148.         ArrayList<String> observableFile;
  149.         boolean b = true;
  150.         while (b) {
  151.             baseFile = File.readFile(name);
  152.             Thread.sleep(5000);
  153.             observableFile = File.readFile(name);
  154.             if (!baseFile.equals(observableFile)) {
  155.                 System.out.println("Tracking for file " + name + " has been changed");
  156.                 File.sendMessage(adress);
  157.                 } else {
  158.             System.out.println("There are no changes in the file");
  159.             }
  160.             baseFile.clear();
  161.             baseFile.addAll(observableFile);
  162.         }
  163.     }
  164.    
  165.     public void run() {
  166.         try {
  167.             this.observeFiles();
  168.         } catch (FileNotFoundException ex) {
  169.             System.out.print("There are no files with such name!");
  170.             return;
  171.         } catch (InterruptedException ex) {
  172.             System.out.print("For file " + name + " tracking has been stopped");
  173.             return;
  174.         }
  175.     }
  176.  
  177. }
  178. public class Test {
  179.     public static void main(String[] args) {
  180.  
  181.         Menu menu = new Menu();
  182.         ArrayList<File> fileCollection = new ArrayList<File>();
  183.         Scanner sc = new Scanner(System.in);
  184.         System.out.println(
  185.                 "Hi! Just try to track changes in the file non-stop. To start enter your e-mail adress to receive a notification about changes and then follow the menu!");
  186.         System.out.println("Enter the e-mail");
  187.         String emailAdress = sc.nextLine();
  188.         menu.addEntry(new MenuEntry("Start tracking file") {
  189.             @Override
  190.             public void run() {
  191.  
  192.                 System.out.print("Enter the file name" + "\n");
  193.                 String entry = sc.nextLine();
  194.                 File file = new File(entry, emailAdress);
  195.                 fileCollection.add(file);
  196.                 file.setName(entry);
  197.                 file.start();
  198.             }
  199.         });
  200.  
  201.         menu.addEntry(new MenuEntry("Stop tracking file") {
  202.  
  203.             public void run() {
  204.  
  205.                 System.out.println("Enter the number: 1.Stop tracking for all files" + " "
  206.                         + "2.Stop tracking for one file" + " " + "3.Cancel and return" + "\n");
  207.                 boolean b = true;
  208.                 boolean stop = false;
  209.                 while (b) {
  210.                     try {
  211.                         int input = sc.nextInt();
  212.                         switch (input) {
  213.                         case 1:
  214.                             if (fileCollection.size() != 0) {
  215.                                 for (File s : fileCollection) {
  216.                                     s.interrupt();
  217.                                 }
  218.                                 fileCollection.clear();
  219.                             } else {
  220.                                 System.out.println("No active processes");
  221.                             }
  222.                             return;
  223.                         case 2:
  224.                             if (fileCollection.size() != 0) {
  225.                                 System.out.print("Number of active files: " + fileCollection.size() + "\n"
  226.                                         + "They are: " + "\n");
  227.                                 for (File s : fileCollection) {
  228.                                     System.out.println(s.getName());
  229.                                 }
  230.                                 System.out.println("Enter the number:");
  231.                                 int number = sc.nextInt();
  232.                                 while (!stop) {
  233.                                     if (fileCollection.get(number - 1).isInterrupted()) {
  234.                                     stop = true;
  235.                                     }
  236.                                     fileCollection.get(number - 1).interrupt();
  237.                                     fileCollection.remove(number - 1);
  238.                                 }
  239.                             } else {
  240.                                 System.out.println("No active processes");
  241.                             }
  242.                         case 3:
  243.                             return;
  244.                         default:
  245.                             System.out.print("Wrong input!" + "\n");
  246.                             break;
  247.                         }
  248.                     } catch (InputMismatchException ex) {
  249.                         System.out.print("Wrong input!");
  250.                     }
  251.                 }
  252.             }
  253.         });
  254.  
  255.         menu.run();
  256.         sc.close();
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement