Advertisement
Guest User

Untitled

a guest
Jan 28th, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     private static File myFile = new File("libraryList.txt"); //Change to something sensible
  12.     private static Scanner deleteQuery = new Scanner(System.in);
  13.     private static ArrayList<String> bookTitles = new ArrayList<String>();
  14.     private static Scanner inputScanner = new Scanner(System.in);
  15.  
  16.     public static void main(String[] args) {
  17.  
  18.  
  19.         System.out.println("If you need to create a new file, click [Y]. If you already have one, click [N].");
  20.         String createNew = inputScanner.nextLine(); //assigns the answer for addAnother to the variable
  21.         if (createNew.equals("Y")) {
  22.             CreateFile();
  23.         }
  24.  
  25.  
  26.  
  27.         try { //try|catch error check initiation
  28.             boolean addNewBook = true;
  29.             while (addNewBook == true) { //initiate a loop that continues until they want to stop adding books
  30.  
  31.                 GetTitle();
  32.                 GetAuthor();
  33.                 GetISBN();
  34.                 GetGenre();
  35.  
  36.  
  37.                 System.out.println("Would you like to add another book? Y/N");
  38.                 String addAnother = inputScanner.nextLine(); //assigns the answer for addAnother to the variable
  39.                 if (addAnother.equals("N")) {
  40.                     addNewBook = false; //if they don't want to add another, end the loop.
  41.                     WriteToFile();
  42.                 }
  43.  
  44.             }
  45.             String titleList = bookTitles.toString();
  46.             System.out.println(titleList);
  47.  
  48.         } catch(Exception e){
  49.             System.out.println("An error has occurred!");//try|catch error check
  50.             e.printStackTrace();
  51.         }
  52.  
  53.     }
  54.  
  55.  
  56.     public static void GetTitle() {
  57.  
  58.         try { //try|catch error check initiation
  59.  
  60.             System.out.println("Please add a book name.");
  61.             String bookInput = inputScanner.nextLine(); //dumps the book title into the scanner
  62.             bookTitles.add(bookInput); //dumps scanner contents into the array
  63.  
  64.  
  65.         } catch (Exception e) {
  66.             System.out.println("An error has occurred!");//try|catch error check
  67.             e.printStackTrace();
  68.         }
  69.  
  70.     }
  71.  
  72.     public static void GetAuthor() {
  73.  
  74.         try { //try|catch error check initiation
  75.  
  76.             System.out.println("Please add the book's author.");
  77.             String bookInput = inputScanner.nextLine(); //dumps the book author into the scanner
  78.             bookTitles.add(bookInput); //dumps scanner contents into the array
  79.  
  80.  
  81.         } catch (Exception e) {
  82.             System.out.println("An error has occurred!");//try|catch error check
  83.             e.printStackTrace();
  84.         }
  85.  
  86.     }
  87.  
  88.     public static void GetISBN() {
  89.  
  90.         try { //try|catch error check initiation
  91.  
  92.             System.out.println("Please add the book's ISBN.");
  93.             String bookInput = inputScanner.nextLine(); //dumps the book ISBN into the scanner
  94.             bookTitles.add(bookInput); //dumps scanner contents into the array
  95.  
  96.  
  97.         } catch (Exception e) {
  98.             System.out.println("An error has occurred!");//try|catch error check
  99.             e.printStackTrace();
  100.         }
  101.  
  102.     }
  103.  
  104.     public static void GetGenre() {
  105.  
  106.         try { //try|catch error check initiation
  107.  
  108.             System.out.println("Please add the book's genre.");
  109.             String bookInput = inputScanner.nextLine(); //dumps the book genre into the scanner
  110.             bookTitles.add(bookInput); //dumps scanner contents into the array
  111.  
  112.  
  113.         } catch (Exception e) {
  114.             System.out.println("An error has occurred!");//try|catch error check
  115.             e.printStackTrace();
  116.         }
  117.  
  118.     }
  119.  
  120.     public static void CreateFile() {
  121.         try {
  122.             if (myFile.createNewFile()) {
  123.                 System.out.println("File created: " + myFile.getName());
  124.             } else {
  125.                 System.out.println("File already exists.");
  126.             }
  127.         } catch (IOException e) {
  128.             System.out.println("An error occurred.");
  129.             e.printStackTrace();
  130.         }
  131.     }
  132.  
  133.     public static void WriteToFile() {
  134.         try {
  135.             FileWriter myWriter = new FileWriter(myFile.getName(), true); //True means append to file contents, False means overwrite
  136.             System.out.println("This is the contents of the file:");
  137.             myWriter.write(""); //makes a space between each book
  138.             myWriter.write(bookTitles.get(0)+(" ")); // adds the current list to the file
  139.             myWriter.write(bookTitles.get(1)+(" "));
  140.             myWriter.write(bookTitles.get(2)+(" "));
  141.             myWriter.write(bookTitles.get(3)+("\n\n"));
  142.             myWriter.close();
  143.             System.out.println("Successfully wrote to the file.");
  144.         } catch (IOException e) {
  145.             System.out.println("An error occurred.");
  146.             e.printStackTrace();
  147.         }
  148.     }
  149.  
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement