Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class MenuEntry {
- private String title;
- public MenuEntry(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public abstract void run();
- }
- public class Menu {
- private static final String MENU_PATTERN = "%s - %s\n";
- private ArrayList<MenuEntry> entries = new ArrayList<MenuEntry>();
- private boolean isExit = false;
- public Menu() {
- entries.add(new MenuEntry("Exit") {
- @Override
- public void run() {
- isExit = true;
- }
- });
- }
- public void run() {
- Scanner sc = new Scanner(System.in);
- while (!isExit) {
- printMenu();
- try {
- String line = sc.nextLine();
- int choice = Integer.parseInt(line);
- MenuEntry entry = entries.get(choice - 1);
- entry.run();
- } catch (NumberFormatException e) {
- System.out.print("Wrong input!");
- } catch (IndexOutOfBoundsException e2) {
- System.out.print("Wrong input!");
- }
- }
- sc.close();
- }
- public Menu addEntry(MenuEntry entry) {
- int index = entries.size() - 1;
- entries.add(index, entry);
- return this;
- }
- private void printMenu() {
- StringBuffer buffer = new StringBuffer();
- buffer.append("\nMenu:\n");
- for (int i = 0; i < entries.size(); i++) {
- MenuEntry entry = entries.get(i);
- String entryFormatted = String.format(MENU_PATTERN, (i + 1), entry.getTitle());
- buffer.append(entryFormatted);
- }
- System.out.print(buffer.toString());
- }
- }
- public class MailSender {
- /**
- * @param args
- */
- final String userName = "marinajavatest@gmail.com";
- final String password = "javatest_5";
- Properties props = new Properties();
- public void setProperties() {
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- }
- Session session = Session.getInstance(props, new javax.mail.Authenticator() {
- protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userName, password);
- }
- });
- public void sendMessage(String adress) {
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(userName));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adress));
- message.setSubject("Changes have been found");
- message.setText("File has been chaged");
- Transport.send(message);
- System.out.println("Done! Message has been sent to the e-mail" + "'" + adress + "'");
- } catch (AddressException ex) {
- System.out.println ("E-mail adress doesn't exsist");
- } catch (MessagingException ex) {
- System.out.println ("Sorry...something was wrong. E-mail hasn't been sent");
- }
- }
- }
- public class File extends Thread {
- ArrayList<String> example;
- String name;
- String adress;
- File(String name, String adress) {
- this.name = name;
- this.adress = adress;
- this.example = new ArrayList<String>();
- this.setDaemon(true);
- }
- public static ArrayList<String> readFile(String name) throws FileNotFoundException {
- Scanner scanner = null;
- scanner = new Scanner(new BufferedReader(new FileReader(name)));
- ArrayList<String> fileList = new ArrayList<String>();
- while (scanner.hasNextLine()) {
- fileList.add(scanner.nextLine());
- }
- scanner.close();
- return fileList;
- }
- public static void sendMessage(String adress) {
- MailSender mailsender = new MailSender();
- mailsender.setProperties();
- mailsender.sendMessage(adress);
- }
- public void observeFiles() throws FileNotFoundException, InterruptedException {
- ArrayList<String> baseFile;
- ArrayList<String> observableFile;
- boolean b = true;
- while (b) {
- baseFile = File.readFile(name);
- Thread.sleep(5000);
- observableFile = File.readFile(name);
- if (!baseFile.equals(observableFile)) {
- System.out.println("Tracking for file " + name + " has been changed");
- File.sendMessage(adress);
- } else {
- System.out.println("There are no changes in the file");
- }
- baseFile.clear();
- baseFile.addAll(observableFile);
- }
- }
- public void run() {
- try {
- this.observeFiles();
- } catch (FileNotFoundException ex) {
- System.out.print("There are no files with such name!");
- return;
- } catch (InterruptedException ex) {
- System.out.print("For file " + name + " tracking has been stopped");
- return;
- }
- }
- }
- public class Test {
- public static void main(String[] args) {
- Menu menu = new Menu();
- ArrayList<File> fileCollection = new ArrayList<File>();
- Scanner sc = new Scanner(System.in);
- System.out.println(
- "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!");
- System.out.println("Enter the e-mail");
- String emailAdress = sc.nextLine();
- menu.addEntry(new MenuEntry("Start tracking file") {
- @Override
- public void run() {
- System.out.print("Enter the file name" + "\n");
- String entry = sc.nextLine();
- File file = new File(entry, emailAdress);
- fileCollection.add(file);
- file.setName(entry);
- file.start();
- }
- });
- menu.addEntry(new MenuEntry("Stop tracking file") {
- public void run() {
- System.out.println("Enter the number: 1.Stop tracking for all files" + " "
- + "2.Stop tracking for one file" + " " + "3.Cancel and return" + "\n");
- boolean b = true;
- boolean stop = false;
- while (b) {
- try {
- int input = sc.nextInt();
- switch (input) {
- case 1:
- if (fileCollection.size() != 0) {
- for (File s : fileCollection) {
- s.interrupt();
- }
- fileCollection.clear();
- } else {
- System.out.println("No active processes");
- }
- return;
- case 2:
- if (fileCollection.size() != 0) {
- System.out.print("Number of active files: " + fileCollection.size() + "\n"
- + "They are: " + "\n");
- for (File s : fileCollection) {
- System.out.println(s.getName());
- }
- System.out.println("Enter the number:");
- int number = sc.nextInt();
- while (!stop) {
- if (fileCollection.get(number - 1).isInterrupted()) {
- stop = true;
- }
- fileCollection.get(number - 1).interrupt();
- fileCollection.remove(number - 1);
- }
- } else {
- System.out.println("No active processes");
- }
- case 3:
- return;
- default:
- System.out.print("Wrong input!" + "\n");
- break;
- }
- } catch (InputMismatchException ex) {
- System.out.print("Wrong input!");
- }
- }
- }
- });
- menu.run();
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement