Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.beans.XMLDecoder;
- import java.beans.XMLEncoder;
- import java.io.*;
- public class Model implements Serializable {
- private ArrayList<Book> books;
- private ArrayList<Author> authors;
- private transient Scanner input;
- public Model() {
- books = new ArrayList<Book>();
- authors = new ArrayList<Author>();
- }
- public void runApp() {
- input = new Scanner(System.in);
- String response;
- do {
- System.out.println("==================================================");
- System.out.println("=========== Welcome to the book store! ===========");
- System.out.println("==================================================");
- System.out.println("1) Add a book to the catalouge");
- System.out.println("2) Add a author to the catalogue");
- System.out.println("3) Print the information");
- System.out.println("4) Save the information to a file (Serializable)");
- System.out.println("5) Load the information from a file (Serializable)");
- System.out.println("6) Save the information to a file (XML)");
- System.out.println("7) Load the information from a file (XML)");
- System.out.println("8) Save the information to a file (Text)");
- System.out.println("9) Load the information from a file (Text)");
- System.out.println("Q) Quit the application");
- response = input.nextLine();
- if (response.equals("1")) {
- System.out.println("Enter Authors name:- ");
- String x = input.nextLine();
- Author who = searchAuthors(x);
- if (who == null) {
- System.out.println("The Author needs to exist first!");
- } else {
- System.out.println("Enter the title of the book");
- String n = input.nextLine();
- System.out.println("Enter the price of the book");
- int p = input.nextInt();
- Book what = new Book(n, p, who);
- books.add(what);
- who.add(what);
- }
- }
- else if (response.equals("2")) {
- System.out.println("Enter the name of the Author");
- String n = input.nextLine();
- System.out.println("Enter the nationality of the Author");
- String nat = input.nextLine();
- Author newOne = new Author(n, nat, null);
- authors.add(newOne);
- }
- else if (response.equals("3")) {
- for (Author a : authors) {
- System.out.println(a.toString());
- }
- }
- else if (response.equals("4")) {
- try {
- write("file.file");
- } catch (IOException e) {
- System.out.println("Error whilst writing file");
- }
- }
- else if (response.equals("5")) {
- read("file.file");
- }
- else if (response.equals("6")) {
- try {
- writeXML("ExampleFile.xml");
- }
- catch (IOException e) {
- System.out.println("Error Whilst Saving File.");
- }
- }
- else if (response.equals("7")) {
- try {
- readXML("ExampleFile.xml");
- }
- catch (IOException e) {
- System.out.println("Error whilst Loading File");
- }
- }
- else if (response.equals("8")) {
- try {
- saveTextAuthor("AuthorText.txt");
- saveTextBook("BookText.txt");
- } catch (IOException e) {
- System.out.println("Error Whilst Saving Text");
- }
- }
- else if (response.equals("9")) {
- try {
- loadTextAuthor("AuthorText.txt");
- loadTextBook("BookText.txt");
- }
- catch (IOException e) {
- System.out.println("Error whilst reading text!");
- }
- }
- } while (!(response.equals("Q") || response.equals("q")));
- }
- public Author searchAuthors(String find) {
- Author who = null;
- for (Author a : authors) {
- if (a.getName().equals(find))
- who = a;
- }
- return who;
- }
- public Book searchBooks(String find) {
- Book what = null;
- for (Book b : books) {
- if (b.getTitle().equals(find))
- what = b;
- }
- return what;
- }
- /* -------------------- Saving and Loading XML -------------------- */
- public void writeXML(String fn) throws IOException {
- XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
- new FileOutputStream(fn)));
- encoder.writeObject(this);
- encoder.close();
- }
- public static Author readXML(String fn) throws IOException {
- Author result = null;
- XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
- new FileInputStream(fn)));
- result = (Author) decoder.readObject();
- decoder.close();
- return result;
- }
- /* -------------------- Saving and Loading Text -------------------- */
- public void saveTextBook(String fn) throws IOException {
- PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
- new FileOutputStream(fn)));
- outfile.println(books.size());
- for (Book b : books) {
- outfile.println(b.getTitle());
- outfile.println(b.getPrice());
- outfile.println(b.getAuthor().getName());
- }
- outfile.close();
- }
- public void saveTextAuthor(String fn) throws IOException {
- PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
- new FileOutputStream(fn)));
- outfile.println(authors.size());
- for (Author a : authors) {
- outfile.println(a.getName());
- outfile.println(a.getNationality());
- }
- outfile.close();
- }
- public void loadTextBook(String fn) throws IOException {
- Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
- fn)));
- int num = infile.nextInt();
- infile.nextLine();
- for (int i = 0; i < num; i++) {
- String t = infile.nextLine();
- double p = infile.nextDouble();
- }
- infile.close();
- }
- public void loadTextAuthor(String fn) throws IOException {
- Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
- fn)));
- int num = infile.nextInt();
- infile.nextLine();
- for (int i = 0; i < num; i++) {
- String n = infile.nextLine();
- String nat = infile.nextLine();
- }
- infile.close();
- }
- /* -------------------- Saving and Loading Serialzable -------------------- */
- public void write(String fn) throws IOException {
- ObjectOutputStream oos = new ObjectOutputStream(
- new FileOutputStream(fn));
- oos.writeObject(this);
- oos.close();
- }
- public static Model read(String fn) {
- Model result = null;
- try {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
- fn));
- result = (Model) ois.readObject();
- } catch (ClassNotFoundException cnf) {
- throw new IOException("Problem loading serialized file " + fn);
- } catch (IOException e) {
- System.out.println("No such file or problem");
- } finally {
- return result;
- }
- }
- }
- import java.beans.XMLEncoder;
- import java.io.*;
- import java.util.ArrayList;
- public class Book implements Serializable {
- private String title;
- private double price;
- private Author who;
- public Book(String t, double p, Author w) {
- title=t;
- price=p;
- who = w;
- }
- public String toString() {
- return "The title of the book is " +title+ " and it's price is �"+price;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public Author getAuthor() {
- return who;
- }
- public void setAuthor(Author who) {
- this.who = who;
- }
- public Book() {
- }
- public void setAuthor(ArrayList<Author> authors) {
- this.who = who;
- }
- }
- import java.beans.XMLEncoder;
- import java.io.BufferedOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.io.*;
- public class Author implements Serializable {
- public String name;
- public String nationality;
- private ArrayList<Book> books;
- public Author(String n, String nat, Book b) {
- name = n;
- nationality = nat;
- books = new ArrayList<Book>();
- }
- public String toString() {
- String ret = "The Authors name is " + name
- + " and his/hers nationality is" + nationality + "\n";
- for (Book b : books) {
- ret += b.toString() + "\n";
- }
- return ret;
- }
- public String getName() {
- return name;
- }
- public void setName(String Name) {
- this.name = name;
- }
- public String getNationality() {
- return nationality;
- }
- public void setNationality(String Nationality) {
- this.nationality = nationality;
- }
- public void add(Book b) {
- books.add(b);
- }
- public void remove(Book b) {
- books.remove(b);
- }
- public Author() {
- }
- public void setBooks(ArrayList<Book> books) {
- this.books = books;
- }
- public ArrayList<Book> getBooks() {
- return books;
- }
- }
- import java.util.*;
- public class Main {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- System.out.println("Do you want to load a previously saved fine?");
- System.out.println("1 - Yes");
- System.out.println("2 - No");
- String response = in.nextLine();
- Model model;
- if (response.equals("1")) {
- model =Model.read("file.file");
- if (model==null)
- model = new Model();
- }
- else
- model = new Model();
- model.runApp();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment