Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. /** Purpose: Adds, creates, displays and searches the XMl database file.
  2. * Created by: Stanley Hao
  3. * Created on: December 9, 2018
  4. */
  5.  
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import nu.xom.*;
  11.  
  12. public class ManageData
  13. {
  14. static Element cars = new Element("cars");
  15. static Document carsDoc = new Document(cars);
  16. static File existingFile = new File("carDatabase.txt");
  17.  
  18. //Adds an entry to the existing file, if no file exists, creates a new file.
  19. public static void addCar(String name, String brand, String modelYear)
  20. {
  21.  
  22. //If there is already a file.
  23. if(existingFile.exists() && !existingFile.isDirectory())
  24. {
  25.  
  26. //Structures file into XML format and adds a car entry with it's sub-elements.
  27. Builder builder = new Builder();
  28. try
  29. {
  30. carsDoc = builder.build(existingFile);
  31.  
  32. Element cars = carsDoc.getRootElement();
  33. Element car = new Element("car");
  34. Element model = new Element("model");
  35. Element carBrand = new Element("brand");
  36. Element year = new Element("year");
  37.  
  38. //Sets the value of each element.
  39. car.appendChild(name);
  40. model.appendChild(name);
  41. carBrand.appendChild(brand);
  42. year.appendChild(modelYear);
  43.  
  44. //Structures the elements into parent and child elements.
  45. cars.appendChild(car);
  46. car.appendChild(model);
  47. car.appendChild(carBrand);
  48. car.appendChild(year);
  49. }
  50. catch(ValidityException e)
  51. {
  52. System.out.println("Error: " + e);
  53. }
  54. catch(ParsingException e)
  55. {
  56. System.out.println("Error: " + e);
  57. }
  58. catch(IOException e)
  59. {
  60. System.out.println("Error: " + e);
  61. }
  62. }
  63.  
  64. //If there isn't a pre-existing file, creates a new file and adds a car entry with its sub-elements.
  65. else
  66. {
  67. Element car = new Element("car");
  68. Element model = new Element("model");
  69. Element carBrand = new Element("brand");
  70. Element year = new Element("year");
  71.  
  72. //Sets the value of each element.
  73. car.appendChild(name);
  74. model.appendChild(name);
  75. carBrand.appendChild(brand);
  76. year.appendChild(modelYear);
  77.  
  78. //Structures the elements into parent and child elements.
  79. cars.appendChild(car);
  80. car.appendChild(model);
  81. car.appendChild(carBrand);
  82. car.appendChild(year);
  83. }
  84.  
  85. //Creates a new database file and fills it with XML data.
  86. try
  87. {
  88. FileWriter databaseFile = new FileWriter("carDatabase.txt");
  89. BufferedWriter writer = new BufferedWriter(databaseFile);
  90. writer.write(carsDoc.toXML());
  91. writer.close();
  92. }
  93.  
  94. catch(IOException e)
  95. {
  96. System.out.println("Error: " + e);
  97. }
  98.  
  99. }
  100.  
  101. //Displays all the data in the database in a list with each element and their sub-elements.
  102. public static void showData()
  103. {
  104.  
  105. //Structures existing file into recognizable XML format.
  106. Builder builder = new Builder();
  107. try
  108. {
  109. carsDoc = builder.build(existingFile);
  110. }
  111. catch (ValidityException e)
  112. {
  113. System.out.println("Error: " + e);
  114. }
  115. catch (ParsingException e)
  116. {
  117. System.out.println("Error: " + e);
  118. }
  119. catch (IOException e)
  120. {
  121. System.out.println("Error: " + e + " \nPlease create a database to display!");
  122. }
  123.  
  124. //Sets variables to root element and list of elements in database.
  125. Element cars = carsDoc.getRootElement();
  126. Elements carList = cars.getChildElements();
  127.  
  128. //Displays the list of entries.
  129. for(int i = 0; i < carList.size(); i++)
  130. {
  131. System.out.println(carList.get(i).getFirstChildElement("model").getValue() + ": ");
  132. System.out.println("Brand: " + carList.get(i).getFirstChildElement("brand").getValue());
  133. System.out.println("Model Year: " + carList.get(i).getFirstChildElement("year").getValue() + "\n");
  134. }
  135.  
  136. /*try
  137. {
  138. Serializer serializer = new Serializer(System.out);
  139. serializer.setIndent(4);
  140. serializer.setMaxLength(64);
  141. serializer.write(carsDoc);
  142. }
  143.  
  144. catch (IOException ex)
  145. {
  146. System.err.println(ex);
  147. }*/
  148. }
  149.  
  150. //Searches the database for a specific car, then displays said element if it exists.
  151. public static void searchData(String query)
  152. {
  153.  
  154. //Structures existing file into recognizable XML format.
  155. Builder builder = new Builder();
  156. try
  157. {
  158. carsDoc = builder.build(existingFile);
  159. }
  160.  
  161. catch(ValidityException e)
  162. {
  163. System.out.println("Error: " + e);
  164. }
  165.  
  166. catch(ParsingException e)
  167. {
  168. System.out.println("Error: " + e);
  169. }
  170.  
  171. catch(IOException e)
  172. {
  173. System.out.println("Error: " + e);
  174. }
  175.  
  176. //Sets variables to root element and list of elements in database.
  177. Element cars = carsDoc.getRootElement();
  178. Elements carList = cars.getChildElements();
  179.  
  180. //Searches database and displays all entries that have the same model name as the search query.
  181. for(int i = 0; i < carList.size(); i++)
  182. {
  183.  
  184. //Displays the vehicle's model name followed by it's brand name and model year.
  185. if(carList.get(i).getFirstChildElement("model").getValue().equalsIgnoreCase(query))
  186. {
  187. System.out.println(carList.get(i).getFirstChildElement("model").getValue() + ": ");
  188. System.out.println("Brand: " + carList.get(i).getFirstChildElement("brand").getValue());
  189. System.out.println("Model Year: " + carList.get(i).getFirstChildElement("year").getValue() + "\n");
  190. }
  191. else
  192. {
  193. //Displays after the entire database is searched and nothing matches.
  194. if(i == carList.size() - 1)
  195. {
  196. System.out.println("The item you searched does not exist.");
  197. }
  198. }
  199. }
  200.  
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement