Advertisement
EmreTech

WIP Command Line

May 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.75 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.*;
  5.  
  6. public class CommandLine {
  7.     public static Scanner input = new Scanner(System.in);
  8.     public Scanner inputNot = input;
  9.     public static boolean exit = false;
  10.     public static String currentDirectory;
  11.     public String currentDirectoryNot = currentDirectory;
  12.     public static GUI gui = new GUI();
  13.     public void checkReply(String reply) {
  14.         FileCommands files = new FileCommands();
  15.         Math mathCommand = new Math();
  16.         RockPaperScissors rpcGame = new RockPaperScissors();
  17.         switch(reply) {
  18.             case "help":
  19.                 System.out.println("Please check the readme file for context of what you can do.");
  20.                 break;
  21.             case "exit":
  22.                 exit = true;
  23.                 break;
  24.             case "cd":
  25.                 System.out.print("Please enter the file system here: ");
  26.                 try {
  27.                     String newFileDir = input.nextLine();
  28.                     files.setCurrentDirectory(newFileDir);
  29.                     currentDirectory = System.getProperty("user.dir");
  30.                 } catch (Exception exception) {
  31.                     System.out.println("I'm sorry, but there was some error. Please try again.");
  32.                 }
  33.                 break;
  34.             case "make":
  35.                 System.out.print("Please enter your file contents here, then hit [Enter] when finished: ");
  36.                 String fileContent = input.nextLine();
  37.                 System.out.print("Write the file name:");
  38.                 String fileName = input.nextLine();
  39.                 try {
  40.                     files.makeFile(fileContent, fileName);
  41.                 } catch (Exception exception) {}
  42.                 break;
  43.             case "move":
  44.                 System.out.print("Please enter the directory you'd like to move the file to: ");
  45.                 String secondDirectory = input.nextLine();
  46.                 System.out.print("Please enter the filename: ");
  47.                 String fileName2 = input.nextLine();
  48.                 try {
  49.                     files.moveFile(fileName2, secondDirectory);
  50.                 } catch (Exception exception) {
  51.                     System.out.println("Error: Could not move file");
  52.                 }
  53.                 break;
  54.             case "math":
  55.                 mathCommand.run();
  56.                 break;
  57.             case "games":
  58.                 System.out.println("Please select a game through 1 - 1: ");
  59.                 System.out.println("1 = Rock Paper Scissors");
  60.                 int gameNumber = input.nextInt();
  61.                 switch (gameNumber) {
  62.                     case 1:
  63.                         rpcGame.RockPaperScissorsLoop();
  64.                         break;
  65.                 }
  66.                 break;
  67.             case "":
  68.                 break;
  69.             default:
  70.                 System.out.println("Error: No such command");
  71.         }
  72.        
  73.     }
  74.     public static void main(String text) {
  75.         currentDirectory = System.getProperty("user.dir");
  76.         CommandLine main = new CommandLine();
  77.         while (true) {
  78.             gui.console.setText(currentDirectory + " $");
  79.             currentDirectory = System.getProperty("user.dir");
  80.             text = input.nextLine();
  81.             main.checkReply(text);
  82.             if (exit == true) {
  83.                 break;
  84.             }
  85.            
  86.         }
  87.         input.close();
  88.     }
  89. }
  90. class FileCommands {
  91.     public boolean setCurrentDirectory(String directory_name)
  92.     {
  93.         boolean result = false;  // Boolean indicating whether directory was set
  94.         File directory;       // Desired current working directory
  95.  
  96.         directory = new File(directory_name).getAbsoluteFile();
  97.         if (directory.exists() || directory.mkdirs())
  98.         {
  99.             result = (System.setProperty("user.dir", directory.getAbsolutePath()) != null);
  100.         }
  101.  
  102.         return result;
  103.     }
  104.  
  105.     public PrintWriter openOutputFile(String file_name)
  106.     {
  107.         PrintWriter output = null;  // File to open for writing
  108.  
  109.         try
  110.         {
  111.             output = new PrintWriter(new File(file_name).getAbsoluteFile());
  112.         }
  113.         catch (Exception exception) {}
  114.  
  115.         return output;
  116.     }
  117.     public void makeFile(String filecontent, String filename) throws IOException {
  118.         System.out.println("Creating file " + filename);
  119.         CommandLine central = new CommandLine();
  120.         String currentDirectory = central.currentDirectoryNot;
  121.         FileOutputStream fos = new FileOutputStream(currentDirectory + "/" + filename);
  122.         fos.write(filecontent.getBytes());
  123.         fos.flush();
  124.         fos.close();
  125.         System.out.println("Created file " + filename + " with no errors.");
  126.     }
  127.    
  128.     public void moveFile(String filename, String otherDirectory) throws IOException{
  129.         CommandLine central = new CommandLine();
  130.         String currentDirectory = central.currentDirectoryNot;
  131.         Path temp = Files.move
  132.         (Paths.get(currentDirectory + "/" + filename),  
  133.         Paths.get(otherDirectory + "/" + filename));
  134.  
  135.         if(temp != null)
  136.         {
  137.             System.out.println("File renamed and moved successfully");
  138.         }
  139.         else
  140.         {
  141.             System.out.println("Failed to move the file");
  142.         }
  143.     }
  144.    
  145.     public static void main(String[] args) {
  146.  
  147.     }
  148. }
  149. class Math {
  150.     int Final = 0;
  151.    
  152.     public int doMath(int number1, String opreator, int number2) {
  153.         switch (opreator) {
  154.             case "+":
  155.                 Final = number1 + number2;
  156.                 break;
  157.             case "-":
  158.                 Final = number1 - number2;
  159.                 break;
  160.             case "*":
  161.                 Final = number1 * number2;
  162.                 break;
  163.             case "/":
  164.                 Final = number1 / number2;
  165.                 break;
  166.             case "%":
  167.                 Final = number1 % number2;
  168.             default:
  169.                 System.out.println("Error: had to do with the possible opreator");
  170.         }
  171.         return Final;
  172.     }
  173.     public void run(){
  174.         CommandLine central = new CommandLine();
  175.         Scanner input = central.inputNot;
  176.         Math math = new Math();
  177.         System.out.print("Enter your first number: ");
  178.         int FirstNumber = input.nextInt();
  179.         System.out.print("Enter an opreator: ");
  180.         String opreator = input.next();
  181.         System.out.print("Enter your second number: ");
  182.         int SecondNumber = input.nextInt();
  183.         math.doMath(FirstNumber, opreator, SecondNumber);
  184.         System.out.println("Answer: " + math.Final);
  185.     }
  186.     public static void main(String[] args) {
  187.          
  188.     }
  189. }
  190. class RockPaperScissors {
  191.     public String ComputerAnswer;
  192.     public String getRandomElement(ArrayList<String> List ) {
  193.         Random random = new Random();
  194.         return List.get(random.nextInt(List.size()));
  195.     }
  196.     public RockPaperScissors() {
  197.         ArrayList<String> answerlist = new ArrayList<String>();
  198.         answerlist.add("rock");
  199.         answerlist.add("paper");
  200.         answerlist.add("scissors");
  201.         ComputerAnswer = getRandomElement(answerlist);
  202.  
  203.     }
  204.     public void checkAnswer(String answer) {
  205.         answer = answer.toLowerCase();
  206.             if (ComputerAnswer.equals(answer)) {
  207.                 System.out.println("It's a tie!");
  208.             }
  209.             else if (answer.equals("rock")) {
  210.                 if (ComputerAnswer.equals("scissors")) {
  211.                     System.out.println("You win!");
  212.                 }
  213.                 else {
  214.                     System.out.println("Computer wins!");
  215.                 }
  216.             }
  217.             else if (answer.equals("paper")) {
  218.                 if (ComputerAnswer.equals("rock")) {
  219.                     System.out.println("You win!");
  220.                 }
  221.                 else {
  222.                     System.out.println("Computer wins!");
  223.                 }
  224.             }
  225.             else if (answer.equals("scissors")) {
  226.                 if (ComputerAnswer.equals("paper")) {
  227.                     System.out.println("You win!");
  228.                 }
  229.                 else {
  230.                     System.out.println("Computer wins!");
  231.                 }
  232.             }
  233.             else if (answer.equals("exit")){
  234.                 System.out.println("Thanks for playing!");
  235.             }
  236.             else {
  237.                 System.out.println("I think there was an error somewhere...");
  238.             }
  239.     }
  240.     public void RockPaperScissorsLoop(){
  241.         CommandLine central = new CommandLine();
  242.         Scanner scan = central.inputNot;
  243.         boolean quit = true;
  244.         char ComputerAnswerFirstLetter =  Character.toUpperCase(ComputerAnswer.charAt(0));
  245.         String PrintableComputerAnswer = ComputerAnswerFirstLetter + ComputerAnswer.substring(1);
  246.         while (quit != false) {
  247.             System.out.println("Please choose between Rock, Paper, Scissors, or Exit to exit the game");
  248.             String HumanAnswer = scan.nextLine();
  249.             if (!HumanAnswer.toLowerCase().equals("exit")) {
  250.                 System.out.println(PrintableComputerAnswer);
  251.             }
  252.             checkAnswer(HumanAnswer);
  253.             ArrayList<String> answerlist = new ArrayList<String>();
  254.             answerlist.add("rock");
  255.             answerlist.add("paper");
  256.             answerlist.add("scissors");
  257.             ComputerAnswer = getRandomElement(answerlist);
  258.             ComputerAnswerFirstLetter =  Character.toUpperCase(ComputerAnswer.charAt(0));
  259.             PrintableComputerAnswer = ComputerAnswerFirstLetter + ComputerAnswer.substring(1);
  260.             if (HumanAnswer.toLowerCase().equals("exit")) {
  261.                 quit = false;
  262.             }
  263.         }
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement