Guest User

Untitled

a guest
Feb 16th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. import java.util.*;
  2. import java.beans.XMLDecoder;
  3. import java.beans.XMLEncoder;
  4. import java.io.*;
  5.  
  6. public class Model implements Serializable {
  7. private ArrayList<Book> books;
  8. private ArrayList<Author> authors;
  9.  
  10. private transient Scanner input;
  11.  
  12. public Model() {
  13. books = new ArrayList<Book>();
  14. authors = new ArrayList<Author>();
  15.  
  16. }
  17.  
  18. public void runApp() {
  19. input = new Scanner(System.in);
  20. String response;
  21.  
  22. do {
  23. System.out.println("==================================================");
  24. System.out.println("=========== Welcome to the book store! ===========");
  25. System.out.println("==================================================");
  26. System.out.println("1) Add a book to the catalouge");
  27. System.out.println("2) Add a author to the catalogue");
  28. System.out.println("3) Print the information");
  29. System.out.println("4) Save the information to a file (Serializable)");
  30. System.out.println("5) Load the information from a file (Serializable)");
  31. System.out.println("6) Save the information to a file (XML)");
  32. System.out.println("7) Load the information from a file (XML)");
  33. System.out.println("8) Save the information to a file (Text)");
  34. System.out.println("9) Load the information from a file (Text)");
  35. System.out.println("Q) Quit the application");
  36. response = input.nextLine();
  37.  
  38. if (response.equals("1")) {
  39. System.out.println("Enter Authors name:- ");
  40. String x = input.nextLine();
  41. Author who = searchAuthors(x);
  42. if (who == null) {
  43. System.out.println("The Author needs to exist first!");
  44. } else {
  45. System.out.println("Enter the title of the book");
  46. String n = input.nextLine();
  47. System.out.println("Enter the price of the book");
  48. int p = input.nextInt();
  49.  
  50. Book what = new Book(n, p, who);
  51. books.add(what);
  52. who.add(what);
  53. }
  54. }
  55.  
  56. else if (response.equals("2")) {
  57. System.out.println("Enter the name of the Author");
  58. String n = input.nextLine();
  59. System.out.println("Enter the nationality of the Author");
  60. String nat = input.nextLine();
  61. Author newOne = new Author(n, nat, null);
  62. authors.add(newOne);
  63. }
  64.  
  65. else if (response.equals("3")) {
  66. for (Author a : authors) {
  67. System.out.println(a.toString());
  68. }
  69. }
  70.  
  71. else if (response.equals("4")) {
  72. try {
  73. write("file.file");
  74. } catch (IOException e) {
  75. System.out.println("Error whilst writing file");
  76. }
  77. }
  78.  
  79. else if (response.equals("5")) {
  80. read("file.file");
  81. }
  82.  
  83. else if (response.equals("6")) {
  84. try {
  85. writeXML("ExampleFile.xml");
  86. }
  87.  
  88. catch (IOException e) {
  89. System.out.println("Error Whilst Saving File.");
  90. }
  91. }
  92.  
  93. else if (response.equals("7")) {
  94. try {
  95. readXML("ExampleFile.xml");
  96. }
  97.  
  98. catch (IOException e) {
  99. System.out.println("Error whilst Loading File");
  100. }
  101. }
  102.  
  103. else if (response.equals("8")) {
  104.  
  105. try {
  106. saveTextAuthor("AuthorText.txt");
  107. saveTextBook("BookText.txt");
  108. } catch (IOException e) {
  109. System.out.println("Error Whilst Saving Text");
  110. }
  111. }
  112.  
  113. else if (response.equals("9")) {
  114. try {
  115. loadTextAuthor("AuthorText.txt");
  116. loadTextBook("BookText.txt");
  117. }
  118. catch (IOException e) {
  119. System.out.println("Error whilst reading text!");
  120. }
  121.  
  122. }
  123.  
  124. } while (!(response.equals("Q") || response.equals("q")));
  125. }
  126.  
  127. public Author searchAuthors(String find) {
  128. Author who = null;
  129. for (Author a : authors) {
  130. if (a.getName().equals(find))
  131. who = a;
  132. }
  133. return who;
  134. }
  135.  
  136. public Book searchBooks(String find) {
  137. Book what = null;
  138. for (Book b : books) {
  139. if (b.getTitle().equals(find))
  140. what = b;
  141. }
  142. return what;
  143. }
  144.  
  145. /* -------------------- Saving and Loading XML -------------------- */
  146.  
  147. public void writeXML(String fn) throws IOException {
  148.  
  149. XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
  150. new FileOutputStream(fn)));
  151. encoder.writeObject(this);
  152. encoder.close();
  153.  
  154. }
  155.  
  156. public static Author readXML(String fn) throws IOException {
  157. Author result = null;
  158. XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
  159. new FileInputStream(fn)));
  160.  
  161. result = (Author) decoder.readObject();
  162. decoder.close();
  163. return result;
  164. }
  165.  
  166.  
  167. /* -------------------- Saving and Loading Text -------------------- */
  168.  
  169. public void saveTextBook(String fn) throws IOException {
  170. PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
  171. new FileOutputStream(fn)));
  172.  
  173. outfile.println(books.size());
  174.  
  175. for (Book b : books) {
  176. outfile.println(b.getTitle());
  177. outfile.println(b.getPrice());
  178. outfile.println(b.getAuthor().getName());
  179. }
  180.  
  181. outfile.close();
  182. }
  183.  
  184. public void saveTextAuthor(String fn) throws IOException {
  185. PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
  186. new FileOutputStream(fn)));
  187.  
  188. outfile.println(authors.size());
  189.  
  190. for (Author a : authors) {
  191. outfile.println(a.getName());
  192. outfile.println(a.getNationality());
  193. }
  194. outfile.close();
  195. }
  196.  
  197. public void loadTextBook(String fn) throws IOException {
  198. Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
  199. fn)));
  200.  
  201. int num = infile.nextInt();
  202. infile.nextLine();
  203.  
  204. for (int i = 0; i < num; i++) {
  205. String t = infile.nextLine();
  206. double p = infile.nextDouble();
  207. }
  208. infile.close();
  209.  
  210. }
  211.  
  212. public void loadTextAuthor(String fn) throws IOException {
  213. Scanner infile = new Scanner(new InputStreamReader(new FileInputStream(
  214. fn)));
  215.  
  216. int num = infile.nextInt();
  217. infile.nextLine();
  218.  
  219. for (int i = 0; i < num; i++) {
  220. String n = infile.nextLine();
  221. String nat = infile.nextLine();
  222.  
  223. }
  224. infile.close();
  225. }
  226.  
  227. /* -------------------- Saving and Loading Serialzable -------------------- */
  228.  
  229.  
  230. public void write(String fn) throws IOException {
  231. ObjectOutputStream oos = new ObjectOutputStream(
  232. new FileOutputStream(fn));
  233. oos.writeObject(this);
  234. oos.close();
  235. }
  236.  
  237. public static Model read(String fn) {
  238. Model result = null;
  239. try {
  240. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
  241. fn));
  242.  
  243. result = (Model) ois.readObject();
  244. } catch (ClassNotFoundException cnf) {
  245. throw new IOException("Problem loading serialized file " + fn);
  246. } catch (IOException e) {
  247. System.out.println("No such file or problem");
  248. } finally {
  249. return result;
  250. }
  251. }
  252.  
  253. }
  254.  
  255. import java.beans.XMLEncoder;
  256. import java.io.*;
  257. import java.util.ArrayList;
  258.  
  259. public class Book implements Serializable {
  260.  
  261. private String title;
  262. private double price;
  263. private Author who;
  264.  
  265. public Book(String t, double p, Author w) {
  266. title=t;
  267. price=p;
  268. who = w;
  269. }
  270.  
  271. public String toString() {
  272. return "The title of the book is " +title+ " and it's price is �"+price;
  273. }
  274.  
  275. public String getTitle() {
  276. return title;
  277. }
  278.  
  279. public void setTitle(String title) {
  280. this.title = title;
  281. }
  282.  
  283. public double getPrice() {
  284. return price;
  285. }
  286.  
  287. public void setPrice(double price) {
  288. this.price = price;
  289. }
  290.  
  291. public Author getAuthor() {
  292. return who;
  293. }
  294.  
  295. public void setAuthor(Author who) {
  296. this.who = who;
  297. }
  298.  
  299. public Book() {
  300.  
  301. }
  302.  
  303. public void setAuthor(ArrayList<Author> authors) {
  304. this.who = who;
  305. }
  306.  
  307.  
  308. }
  309.  
  310. import java.beans.XMLEncoder;
  311. import java.io.BufferedOutputStream;
  312. import java.io.FileOutputStream;
  313. import java.io.IOException;
  314. import java.io.Serializable;
  315. import java.util.ArrayList;
  316. import java.io.*;
  317.  
  318. public class Author implements Serializable {
  319.  
  320. public String name;
  321. public String nationality;
  322. private ArrayList<Book> books;
  323.  
  324. public Author(String n, String nat, Book b) {
  325. name = n;
  326. nationality = nat;
  327. books = new ArrayList<Book>();
  328. }
  329.  
  330. public String toString() {
  331. String ret = "The Authors name is " + name
  332. + " and his/hers nationality is" + nationality + "\n";
  333. for (Book b : books) {
  334. ret += b.toString() + "\n";
  335. }
  336. return ret;
  337. }
  338.  
  339. public String getName() {
  340. return name;
  341. }
  342.  
  343. public void setName(String Name) {
  344. this.name = name;
  345. }
  346.  
  347. public String getNationality() {
  348. return nationality;
  349. }
  350.  
  351. public void setNationality(String Nationality) {
  352. this.nationality = nationality;
  353. }
  354.  
  355. public void add(Book b) {
  356. books.add(b);
  357. }
  358.  
  359. public void remove(Book b) {
  360. books.remove(b);
  361. }
  362.  
  363. public Author() {
  364.  
  365. }
  366.  
  367. public void setBooks(ArrayList<Book> books) {
  368. this.books = books;
  369. }
  370.  
  371. public ArrayList<Book> getBooks() {
  372. return books;
  373. }
  374.  
  375. }
  376.  
  377. import java.util.*;
  378.  
  379. public class Main {
  380.  
  381. /**
  382. * @param args
  383. */
  384. public static void main(String[] args) {
  385. Scanner in=new Scanner(System.in);
  386. System.out.println("Do you want to load a previously saved fine?");
  387. System.out.println("1 - Yes");
  388. System.out.println("2 - No");
  389. String response = in.nextLine();
  390. Model model;
  391. if (response.equals("1")) {
  392. model =Model.read("file.file");
  393. if (model==null)
  394. model = new Model();
  395. }
  396. else
  397. model = new Model();
  398. model.runApp();
  399. }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment