Advertisement
Mary_Loskutova

Example

Mar 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. public abstract class MenuEntry {
  2.  
  3.     private String title;
  4.  
  5.     public MenuEntry(String title) {
  6.         this.title = title;
  7.     }
  8.  
  9.     public String getTitle() {
  10.         return title;
  11.     }
  12.  
  13.     public void setTitle(String title) {
  14.         this.title = title;
  15.     }
  16.  
  17.     public abstract void run();
  18. }
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Scanner;
  22.  
  23. public class Menu {
  24.     private static final String MENU_PATTERN = "%s - %s\n";
  25.     private ArrayList<MenuEntry> entries = new ArrayList<MenuEntry>();
  26.     private boolean isExit = false;
  27.  
  28.     public Menu() {
  29.         entries.add(new MenuEntry("Exit") {
  30.             @Override
  31.             public void run() {
  32.                 isExit = true;
  33.             }
  34.         });
  35.     }
  36.  
  37.     public void run() {
  38.         Scanner sc = new Scanner(System.in);
  39.         while (!isExit) {
  40.             printMenu();
  41.  
  42.             try {
  43.                 String line = sc.nextLine();
  44.  
  45.                 int choice = Integer.parseInt(line);
  46.                 MenuEntry entry = entries.get(choice - 1);
  47.                 entry.run();
  48.  
  49.             } catch (NumberFormatException e) {
  50.                 System.out.print("Wrong input!");
  51.             } catch (IndexOutOfBoundsException e2) {
  52.                 System.out.print("Wrong input!");
  53.             }
  54.         }
  55.         sc.close();
  56.     }
  57.  
  58.     public Menu addEntry(MenuEntry entry) {
  59.         int index = entries.size() - 1;
  60.         entries.add(index, entry);
  61.         return this;
  62.  
  63.     }
  64.  
  65.     private void printMenu() {
  66.         StringBuffer buffer = new StringBuffer();
  67.         buffer.append("\nMenu:\n");
  68.         for (int i = 0; i < entries.size(); i++) {
  69.             MenuEntry entry = entries.get(i);
  70.             String entryFormatted = String.format(MENU_PATTERN, (i + 1), entry.getTitle());
  71.             buffer.append(entryFormatted);
  72.         }
  73.         System.out.print(buffer.toString());
  74.     }
  75.  
  76. }
  77.  
  78. import java.io.BufferedReader;
  79. import java.io.FileInputStream;
  80. import java.io.FileNotFoundException;
  81. import java.io.FileReader;
  82. import java.util.ArrayList;
  83. import java.util.Scanner;
  84. import java.lang.InterruptedException;
  85.  
  86. public class Monitoring extends Thread {
  87.  
  88.     String path;
  89.     ArrayList<String> fileList = new ArrayList<String>();
  90.  
  91.     Monitoring(String path) {
  92.         this.path = path;
  93.     }
  94.  
  95.     public void addFiles() {
  96.         Scanner scanner = null;
  97.        
  98.         try {
  99.             scanner = new Scanner(new FileInputStream(path));
  100.             boolean b = true;
  101.             while (scanner.hasNextLine()) {
  102.                 fileList.add(scanner.nextLine());
  103.                 System.out.println(fileList);
  104.                 b = false;
  105.                            
  106.             }
  107.         }
  108.         catch (FileNotFoundException ex) {
  109.             System.out.println("Файл по этому пути: " + path + " не был найден.");
  110.         }
  111.         finally {
  112.             if (scanner != null) {
  113.                 scanner.close();
  114.             }
  115.         }
  116.     }
  117.     public void compareFiles () {
  118.          for (int a = 0; a < fileList.size(); a++){                
  119.              if (fileList.get(0).equals(fileList.get(a))){                    
  120.                  System.out.println("Yes");                                          
  121.              } else {
  122.                   System.out.println("No");
  123.              }
  124.          }
  125.     }
  126.    
  127.  
  128.     public void run() {
  129.         this.addFiles();
  130.     }
  131. }
  132.  
  133. import java.io.FileNotFoundException;
  134. import java.util.ArrayList;
  135. import java.util.Scanner;
  136.  
  137. import test.Menu;
  138. import test.MenuEntry;
  139. import test.Player;
  140. import test.Playlist;
  141.  
  142. public class Tracking {
  143.  
  144.     public static void main(String args[]) throws InterruptedException {
  145.         Menu menu = new Menu();
  146.         System.out.print("Enter the file name" + "\n");
  147.         Scanner scanner = new Scanner(System.in);
  148.         boolean replay = true;
  149.         String path = scanner.nextLine();
  150.         Monitoring file = new Monitoring(path);
  151.        
  152.         menu.addEntry(new MenuEntry("Start") {
  153.             @Override
  154.             public void run() {
  155.            
  156.             try {
  157.             Thread file1 = new Thread(file);
  158.             Thread.sleep(1500);
  159.             file1.start();
  160.                
  161.         } catch (InterruptedException ex) {
  162.             System.out.print(" has been stopped!" + "\n");
  163.             return;
  164.         }
  165.             }
  166.            
  167.         });
  168.        
  169.         menu.addEntry(new MenuEntry("Compare") {
  170.             @Override
  171.             public void run() {
  172.                 file.compareFiles();
  173.             }
  174.         });
  175.        
  176.         menu.run();
  177.        
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement