Surf directory Learn programming Java

Lesson:

Additional Libraries


Exercise:

Surf directory


Objetive:

Create a program that displays the files in the current folder and allows the user to move up and down the list. If the user presses Enter on a folder name, they will enter that folder. If they press Enter on a file, the file will be opened.


Code:

import java.util.*;
public class Main
{
	private static int position = 0;

	private static ArrayList items;

	private static String directory = ".";

	static void main(String[] args)
	{
		while (true)
		{
			Console.Clear();

			items = GetItems(directory);

			ShowItems();
			ShowIndications();
			ReadKeys();
			Thread.sleep(200);
		}
	}

	private static void OpenFile(Item item)
	{
		if (item.getIsFile())
		{
			Process.Start(item.getName());
		}
	}

	private static void ReadKeys()
	{
		ConsoleKeyInfo key = Console.ReadKey();
		switch (key.Key)
		{
			case UpArrow:
				if (position > 0)
				{
					position--;
				}
				break;
			case DownArrow:
				if (position < items.size() - 1)
				{
					position++;
				}
				break;
			case Enter:
				Item item = items.get(position);

				if (item.getIsFile())
				{
					OpenFile(item);
				}
				else
				{
					directory = item.getPath();
				}
				break;
		}
	}

	private static void ShowSelected(int i)
	{
		if (i == position)
		{
			Console.SetCursorPosition(0, position);
			Console.BackgroundColor = ConsoleColor.DarkCyan;
		}
		else
		{
			Console.BackgroundColor = ConsoleColor.Black;
		}
	}

	private static ArrayList GetItems(String direc)
	{
		try
		{
			ArrayList items = new List();

			String[] directories = (new java.io.File(direc)).list(java.io.File::isDirectory);
			for (String directory : directories)
			{
				items.add(new Item(directory, false));
			}

			String[] files = (new java.io.File(direc)).list(java.io.File::isFile);
			for (String file : files)
			{
				items.add(new Item(file, true));
			}

			return items;
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error reading items.");
			return null;
		}
	}

	private static void ShowItems()
	{
		int i = 0;
		for (Item item : items)
		{
			ShowSelected(i);
			System.out.println(item.getPath());
			i++;
		}
		Console.BackgroundColor = ConsoleColor.Black;
	}

	private static void ShowIndications()
	{
		Console.SetCursorPosition(0, 23);
		System.out.println("Press arrow up for move up  | Press arrow down for move down");
	}
}

public class Item
{
	private String Path;
	public final String getPath()
	{
		return Path;
	}
	public final void setPath(String value)
	{
		Path = value;
	}

	private boolean IsFile;
	public final boolean getIsFile()
	{
		return IsFile;
	}
	public final void setIsFile(boolean value)
	{
		IsFile = value;
	}

	public final String getName()
	{
		return getPath().substring(2);
	}

	public Item(String path, boolean isFile)
	{
		this.setPath(path);
		this.setIsFile(isFile);
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.