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();
- }
- import java.util.ArrayList;
- import java.util.Scanner;
- 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());
- }
- }
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.lang.InterruptedException;
- public class Monitoring extends Thread {
- String path;
- ArrayList<String> fileList = new ArrayList<String>();
- Monitoring(String path) {
- this.path = path;
- }
- public void addFiles() {
- Scanner scanner = null;
- try {
- scanner = new Scanner(new FileInputStream(path));
- boolean b = true;
- while (scanner.hasNextLine()) {
- fileList.add(scanner.nextLine());
- System.out.println(fileList);
- b = false;
- }
- }
- catch (FileNotFoundException ex) {
- System.out.println("Файл по этому пути: " + path + " не был найден.");
- }
- finally {
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- public void compareFiles () {
- for (int a = 0; a < fileList.size(); a++){
- if (fileList.get(0).equals(fileList.get(a))){
- System.out.println("Yes");
- } else {
- System.out.println("No");
- }
- }
- }
- public void run() {
- this.addFiles();
- }
- }
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import test.Menu;
- import test.MenuEntry;
- import test.Player;
- import test.Playlist;
- public class Tracking {
- public static void main(String args[]) throws InterruptedException {
- Menu menu = new Menu();
- System.out.print("Enter the file name" + "\n");
- Scanner scanner = new Scanner(System.in);
- boolean replay = true;
- String path = scanner.nextLine();
- Monitoring file = new Monitoring(path);
- menu.addEntry(new MenuEntry("Start") {
- @Override
- public void run() {
- try {
- Thread file1 = new Thread(file);
- Thread.sleep(1500);
- file1.start();
- } catch (InterruptedException ex) {
- System.out.print(" has been stopped!" + "\n");
- return;
- }
- }
- });
- menu.addEntry(new MenuEntry("Compare") {
- @Override
- public void run() {
- file.compareFiles();
- }
- });
- menu.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement