Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this java application is to use your knowledge of files and streams to create a

ID: 3806771 • Letter: T

Question

this java application is to use your knowledge of files and streams to create a command-line shell application capable of executing a few basic directory- and file-based commands.

Watch a demonstration of the application:

https://youtu.be/Vwt8VC5zPW8

General Requirements

Your application must follow these general requirements

All classes must be in a package following these rules:

The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”

Package names are all lowercase letters

All class names must start with an UPPERCASE letter then camel-cased after that.

All property names must start with a lowercase letter then came-cased after that.

All method names must start with a lowercase letter then came-cased after that.

Output must match the examples. Watch out for spaces and punctuation.

GradinG

NOTE: If the code does not compile, it’s an automatic 0!

The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”

1

Print welcome message

2

Loop displaying “prompt>”

5

Unknown command message

5

“help” command

5

“dir” command

5

“cd” command

5

“show” command

5

“exit” command

5

“cd” error check path missing

5

“cd” error check path doesn’t exist

5

“cd” error check path not directory

5

“show” error check path missing

5

“show” error check path doesn’t exist

5

“show” error check path not file

5

Print good bye message

2

The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”

1

Print welcome message

2

Loop displaying “prompt>”

5

Unknown command message

5

“help” command

5

“dir” command

5

“cd” command

5

“show” command

5

“exit” command

5

“cd” error check path missing

5

“cd” error check path doesn’t exist

5

“cd” error check path not directory

5

“show” error check path missing

5

“show” error check path doesn’t exist

5

“show” error check path not file

5

Print good bye message

2

Explanation / Answer

Dont Forget to change the package name

##########################################################

package lastname.firstLetter

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Chegg_29_03_2017_04 {

   private static final Map<String, String> COMMANDS = new LinkedHashMap<>();
   // static block to initialize commands
   static {
       COMMANDS.put("help", "Show list of commands");
       COMMANDS.put("dir", "List contents of current directory");
       COMMANDS.put("cd [dir]", "Change to directory");
       COMMANDS.put("show [file]", "Show contents of file");
       COMMANDS.put("exit", "Exit the shell");
   }

   private static void displayCommands() {
       System.out.println(" COMMANDS:");
       for (Entry<String, String> e : COMMANDS.entrySet()) {
           System.out.printf(" %-15s %s ", e.getKey(), e.getValue());
       }
   }

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("Welcome to shell! ");
       // get current working directory
       String currentDir = System.getProperty("user.dir");

       Infinite_loop: while (true) {
           System.out.print("prompt>");
           String command[] = sc.nextLine().split(" ");

           switch (command[0]) {
           case "help":
               displayCommands();
               break;
           case "dir":
               printDirectoryList(currentDir);
               break;
           case "cd":
               String directoryName = command[1];
               if ((new File(currentDir + "\" + directoryName)).exists()) {
                   currentDir += "\" + directoryName;
                   System.out.println("SUCCESS: " + currentDir);
               } else {
                   System.out.println("Invalid Directory");
               }
               break;
           case "show":
               String fileName = command[1];
               // send the absolute path
               printFileContents(currentDir + "\" + fileName);
               break;
           case "exit":
               System.out.println("bye ");
               break Infinite_loop;

           default:
               System.out.println("Unknown command "" + command[0] + """);
               displayCommands();
               break;
           }
       }
       sc.close();
   }

   private static void printDirectoryList(String directoryName) {
       System.out.println("Directory of "" + directoryName + """);

       File directory = new File(directoryName);

       // get all the files from a directory
       File[] fList = directory.listFiles();
       for (File file : fList) {
           if (file.isFile()) {
               System.out.printf(" %-10s %10d %-50s", " ", file.length(), file.getName());
           } else if (file.isDirectory()) {
               System.out.printf(" %-10s %10s %-50s", "d", " ", file.getName());
           }
       }
       System.out.println(" ");
   }

   private static void printFileContents(String filePath) {
       File file = new File(filePath);
       if (file.isFile()) {
           try (Scanner input = new Scanner(file)) {
               while (input.hasNextLine()) {
                   System.out.println(input.nextLine());
               }
           } catch (FileNotFoundException e) {
               System.out.println("Some error occured while reading the File" + e.getMessage());
           }
       } else {
           System.out.println("Entered file name is not a file");
       }
   }
}