Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Purpose: Adds, creates, displays and searches the XMl database file.
- * Created by: Stanley Hao
- * Created on: December 9, 2018
- */
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import nu.xom.*;
- public class ManageData
- {
- static Element cars = new Element("cars");
- static Document carsDoc = new Document(cars);
- static File existingFile = new File("carDatabase.txt");
- //Adds an entry to the existing file, if no file exists, creates a new file.
- public static void addCar(String name, String brand, String modelYear)
- {
- //If there is already a file.
- if(existingFile.exists() && !existingFile.isDirectory())
- {
- //Structures file into XML format and adds a car entry with it's sub-elements.
- Builder builder = new Builder();
- try
- {
- carsDoc = builder.build(existingFile);
- Element cars = carsDoc.getRootElement();
- Element car = new Element("car");
- Element model = new Element("model");
- Element carBrand = new Element("brand");
- Element year = new Element("year");
- //Sets the value of each element.
- car.appendChild(name);
- model.appendChild(name);
- carBrand.appendChild(brand);
- year.appendChild(modelYear);
- //Structures the elements into parent and child elements.
- cars.appendChild(car);
- car.appendChild(model);
- car.appendChild(carBrand);
- car.appendChild(year);
- }
- catch(ValidityException e)
- {
- System.out.println("Error: " + e);
- }
- catch(ParsingException e)
- {
- System.out.println("Error: " + e);
- }
- catch(IOException e)
- {
- System.out.println("Error: " + e);
- }
- }
- //If there isn't a pre-existing file, creates a new file and adds a car entry with its sub-elements.
- else
- {
- Element car = new Element("car");
- Element model = new Element("model");
- Element carBrand = new Element("brand");
- Element year = new Element("year");
- //Sets the value of each element.
- car.appendChild(name);
- model.appendChild(name);
- carBrand.appendChild(brand);
- year.appendChild(modelYear);
- //Structures the elements into parent and child elements.
- cars.appendChild(car);
- car.appendChild(model);
- car.appendChild(carBrand);
- car.appendChild(year);
- }
- //Creates a new database file and fills it with XML data.
- try
- {
- FileWriter databaseFile = new FileWriter("carDatabase.txt");
- BufferedWriter writer = new BufferedWriter(databaseFile);
- writer.write(carsDoc.toXML());
- writer.close();
- }
- catch(IOException e)
- {
- System.out.println("Error: " + e);
- }
- }
- //Displays all the data in the database in a list with each element and their sub-elements.
- public static void showData()
- {
- //Structures existing file into recognizable XML format.
- Builder builder = new Builder();
- try
- {
- carsDoc = builder.build(existingFile);
- }
- catch (ValidityException e)
- {
- System.out.println("Error: " + e);
- }
- catch (ParsingException e)
- {
- System.out.println("Error: " + e);
- }
- catch (IOException e)
- {
- System.out.println("Error: " + e + " \nPlease create a database to display!");
- }
- //Sets variables to root element and list of elements in database.
- Element cars = carsDoc.getRootElement();
- Elements carList = cars.getChildElements();
- //Displays the list of entries.
- for(int i = 0; i < carList.size(); i++)
- {
- System.out.println(carList.get(i).getFirstChildElement("model").getValue() + ": ");
- System.out.println("Brand: " + carList.get(i).getFirstChildElement("brand").getValue());
- System.out.println("Model Year: " + carList.get(i).getFirstChildElement("year").getValue() + "\n");
- }
- /*try
- {
- Serializer serializer = new Serializer(System.out);
- serializer.setIndent(4);
- serializer.setMaxLength(64);
- serializer.write(carsDoc);
- }
- catch (IOException ex)
- {
- System.err.println(ex);
- }*/
- }
- //Searches the database for a specific car, then displays said element if it exists.
- public static void searchData(String query)
- {
- //Structures existing file into recognizable XML format.
- Builder builder = new Builder();
- try
- {
- carsDoc = builder.build(existingFile);
- }
- catch(ValidityException e)
- {
- System.out.println("Error: " + e);
- }
- catch(ParsingException e)
- {
- System.out.println("Error: " + e);
- }
- catch(IOException e)
- {
- System.out.println("Error: " + e);
- }
- //Sets variables to root element and list of elements in database.
- Element cars = carsDoc.getRootElement();
- Elements carList = cars.getChildElements();
- //Searches database and displays all entries that have the same model name as the search query.
- for(int i = 0; i < carList.size(); i++)
- {
- //Displays the vehicle's model name followed by it's brand name and model year.
- if(carList.get(i).getFirstChildElement("model").getValue().equalsIgnoreCase(query))
- {
- System.out.println(carList.get(i).getFirstChildElement("model").getValue() + ": ");
- System.out.println("Brand: " + carList.get(i).getFirstChildElement("brand").getValue());
- System.out.println("Model Year: " + carList.get(i).getFirstChildElement("year").getValue() + "\n");
- }
- else
- {
- //Displays after the entire database is searched and nothing matches.
- if(i == carList.size() - 1)
- {
- System.out.println("The item you searched does not exist.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement