Untitled
unknown
java
2 months ago
4.2 kB
4
Indexable
package inventorySystem; import java.util.Scanner; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; public class inventory { public static Scanner userInput = new Scanner(System.in); public static void main(String[] args) throws IOException{ while (true) { inputHandler(); } } public static void inputHandler() throws IOException { int userChoice = mainMenu(); switch (userChoice) { case 1: clearScreen(100); addItem(); break; case 2: clearScreen(100); removeItem(); break; case 3: clearScreen(100); viewItem(); break; case 0: clearScreen(100); exitProgram(); break; default: clearScreen(500); System.out.println("That is not a valid selection, please choose another option and press enter "); System.out.println("PRESS ANY KEY TO CONTINUE"); System.in.read(); clearScreen(500); break; } } public static int mainMenu() { //this is the main menu to the inventory system System.out.println("Please choose from the following option and press enter: "); System.out.println("1. Add item to inventory"); System.out.println("2. Remove item from inventory"); System.out.println("3. View item in inventory"); System.out.println("0. EXIT PROGRAM"); while (!userInput.hasNextInt() ) { //input validation System.out.println("invlaid input, please input an integer."); userInput.next(); // clear invalid input clearScreen(500); } int choice = userInput.nextInt(); userInput.nextLine(); // consume the newline character return choice; } public static void addItem() throws IOException{ System.out.println("please tell me the name of the file you would like to append data to: "); String fileName = userInput.nextLine(); System.out.println("please input the data you wish to add: "); String addData = userInput.nextLine(); addData = addData + "\n"; try (FileWriter writer = new FileWriter(fileName, true)){ writer.write(addData); System.out.println("data has been written to the file~!"); } catch (IOException e) { System.out.println("oops, something went wrong: " +e.getMessage()); } System.out.println(""); System.out.println("PRESS ANY KEY TO CONTINUE"); System.in.read(); clearScreen(500); } public static void removeItem() throws IOException{ System.out.println("please tell me the name of the file you wish to remove: "); String fileName = userInput.nextLine(); try { if (Files.deleteIfExists(Paths.get(fileName))) { System.out.println("The file " + fileName + " has been deleted."); } else { System.out.println("The file " + fileName + " can not be found"); } } catch (IOException e) { System.out.println("Error deleting the file: " + e.getLocalizedMessage()); } System.out.println(""); System.out.println("PRESS ANY KEY TO CONTINUE"); System.in.read(); clearScreen(500); } public static void viewItem() throws IOException{ System.out.println("please tell me the name of the file you would like to view: "); String fileName = userInput.nextLine(); System.out.println("The following is the contents of " + fileName + ": "); System.out.println(""); try (BufferedReader reader = new BufferedReader(new FileReader(fileName))){ String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Error reading the file: " + e.getMessage()); } System.out.println(" "); System.out.println("PRESS ANY KEY TO CONTINUE"); System.in.read(); clearScreen(500); } public static void exitProgram() { System.out.print("ARE YOU SURE YOU WANT TO EXIT?[Y/N]: "); String choice = userInput.nextLine(); if (choice.equalsIgnoreCase("y")) { clearScreen(500); System.exit(0);} else {clearScreen(500);} } public static void clearScreen(int numberLines) { for (int i = 0; i <= numberLines; i++) { System.out.println(""); } } }
Editor is loading...
Leave a Comment