Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Food.java
- public class Food {
- private String name;
- private String description;
- private int cal;
- private double price;
- public Food(String name, String description, int cal, double price) {
- super();
- this.name = name;
- this.description = description;
- this.cal = cal;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public int getCal() {
- return cal;
- }
- public void setCal(int cal) {
- this.cal = cal;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public String getShortDesc() {
- String[] words = description.split(" ");
- if (words.length < 3) {
- return description;
- }
- return words[0] + " " + words[1] + " " + words[2] + "...";
- }
- @Override
- public String toString() {
- return "Food [name=" + name + ", description=" + description + ", cal=" + cal + ", price=" + price + "]";
- }
- }
- -----------------------------------------------------------------------------------------------------
- foodmenu.java
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- public class FoodMenu {
- List<Food> menu;
- public void read(String xmlFile) {
- SAXReader reader = new SAXReader();
- Document document;
- try {
- document = reader.read(xmlFile);
- Element root = document.getRootElement();
- List<Element> food = root.elements("food");
- menu = new ArrayList<>();
- for (Element e : food) {
- String name = e.elementText("name");
- String description = e.elementText("description");
- double price = Double.parseDouble(e.elementText("price").substring(1));
- int cal = Integer.parseInt(e.elementText("calories"));
- Food f = new Food(name, description, cal, price);
- menu.add(f);
- }
- } catch (DocumentException e) {
- System.out.println("greska prilikom citanja");
- }
- }
- public String toString() {
- String s = "Breakfast menu:";
- for (Food food : menu) {
- s += food + "\n";
- }
- return s;
- }
- public double avg() {
- if (menu == null || menu.size() == 0) {
- return 0;
- }
- double sum = 0;
- for (Food food : menu) {
- sum += food.getPrice();
- }
- return sum / menu.size();
- }
- public void show() {
- double avg = avg();
- for (Food food : menu) {
- if (food.getPrice() > avg) {
- System.out.println(food);
- }
- }
- }
- public void create(String xmlFile) {
- Document document = DocumentHelper.createDocument();
- Element root = document.addElement("menu");
- double avg = avg();
- for (Food food : menu) {
- if (food.getPrice() > avg) {
- Element foodEl = root.addElement("food");
- foodEl.addAttribute("name", food.getName());
- Element priceUSD = foodEl.addElement("price");
- priceUSD.setText(food.getPrice() + "");
- priceUSD.addAttribute("currency", "USD");
- Element priceEur = foodEl.addElement("price");
- priceEur.setText(food.getPrice() * 0.85 + "");
- priceEur.addAttribute("currency", "EUR");
- foodEl.addElement("shortDescription").setText(food.getShortDesc());
- foodEl.addElement("calories").setText(food.getCal()+"");
- }
- }
- try {
- XMLWriter write = new XMLWriter(new FileWriter(xmlFile));
- write.write(document);
- write.close();
- } catch (IOException e) {
- System.out.println("Greska prilikom upisa.");
- }
- }
- }
- ------------------------------------------------------------------
- main
- public class Main {
- public static void main(String[] args) {
- FoodMenu fm = new FoodMenu();
- fm.read("FoodMenu.xml");
- System.out.println(fm);
- System.out.println();
- fm.show();
- fm.create("novi.xml");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement