Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. Food.java
  2. public class Food {
  3.  
  4. private String name;
  5. private String description;
  6. private int cal;
  7. private double price;
  8.  
  9. public Food(String name, String description, int cal, double price) {
  10. super();
  11. this.name = name;
  12. this.description = description;
  13. this.cal = cal;
  14. this.price = price;
  15. }
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. public String getDescription() {
  26. return description;
  27. }
  28.  
  29. public void setDescription(String description) {
  30. this.description = description;
  31. }
  32.  
  33. public int getCal() {
  34. return cal;
  35. }
  36.  
  37. public void setCal(int cal) {
  38. this.cal = cal;
  39. }
  40.  
  41. public double getPrice() {
  42. return price;
  43. }
  44.  
  45. public void setPrice(double price) {
  46. this.price = price;
  47. }
  48.  
  49. public String getShortDesc() {
  50. String[] words = description.split(" ");
  51. if (words.length < 3) {
  52. return description;
  53. }
  54. return words[0] + " " + words[1] + " " + words[2] + "...";
  55.  
  56. }
  57.  
  58. @Override
  59. public String toString() {
  60. return "Food [name=" + name + ", description=" + description + ", cal=" + cal + ", price=" + price + "]";
  61. }
  62. }
  63.  
  64.  
  65.  
  66. -----------------------------------------------------------------------------------------------------
  67.  
  68.  
  69.  
  70.  
  71. foodmenu.java
  72.  
  73.  
  74.  
  75. import java.io.FileWriter;
  76. import java.io.IOException;
  77. import java.util.ArrayList;
  78. import java.util.List;
  79.  
  80. import org.dom4j.Document;
  81. import org.dom4j.DocumentException;
  82. import org.dom4j.DocumentHelper;
  83. import org.dom4j.Element;
  84. import org.dom4j.io.SAXReader;
  85. import org.dom4j.io.XMLWriter;
  86.  
  87. public class FoodMenu {
  88.  
  89. List<Food> menu;
  90.  
  91. public void read(String xmlFile) {
  92.  
  93. SAXReader reader = new SAXReader();
  94. Document document;
  95. try {
  96. document = reader.read(xmlFile);
  97. Element root = document.getRootElement();
  98. List<Element> food = root.elements("food");
  99. menu = new ArrayList<>();
  100. for (Element e : food) {
  101. String name = e.elementText("name");
  102. String description = e.elementText("description");
  103. double price = Double.parseDouble(e.elementText("price").substring(1));
  104. int cal = Integer.parseInt(e.elementText("calories"));
  105.  
  106. Food f = new Food(name, description, cal, price);
  107. menu.add(f);
  108. }
  109.  
  110. } catch (DocumentException e) {
  111. System.out.println("greska prilikom citanja");
  112. }
  113.  
  114. }
  115.  
  116. public String toString() {
  117. String s = "Breakfast menu:";
  118. for (Food food : menu) {
  119. s += food + "\n";
  120. }
  121.  
  122. return s;
  123.  
  124. }
  125.  
  126. public double avg() {
  127.  
  128. if (menu == null || menu.size() == 0) {
  129. return 0;
  130. }
  131. double sum = 0;
  132. for (Food food : menu) {
  133. sum += food.getPrice();
  134. }
  135. return sum / menu.size();
  136.  
  137. }
  138.  
  139. public void show() {
  140.  
  141. double avg = avg();
  142. for (Food food : menu) {
  143. if (food.getPrice() > avg) {
  144. System.out.println(food);
  145. }
  146. }
  147.  
  148. }
  149.  
  150. public void create(String xmlFile) {
  151.  
  152. Document document = DocumentHelper.createDocument();
  153. Element root = document.addElement("menu");
  154. double avg = avg();
  155. for (Food food : menu) {
  156. if (food.getPrice() > avg) {
  157. Element foodEl = root.addElement("food");
  158. foodEl.addAttribute("name", food.getName());
  159. Element priceUSD = foodEl.addElement("price");
  160. priceUSD.setText(food.getPrice() + "");
  161. priceUSD.addAttribute("currency", "USD");
  162. Element priceEur = foodEl.addElement("price");
  163. priceEur.setText(food.getPrice() * 0.85 + "");
  164. priceEur.addAttribute("currency", "EUR");
  165. foodEl.addElement("shortDescription").setText(food.getShortDesc());
  166. foodEl.addElement("calories").setText(food.getCal()+"");
  167. }
  168. }
  169. try {
  170. XMLWriter write = new XMLWriter(new FileWriter(xmlFile));
  171. write.write(document);
  172. write.close();
  173. } catch (IOException e) {
  174. System.out.println("Greska prilikom upisa.");
  175. }
  176.  
  177. }
  178. }
  179.  
  180.  
  181.  
  182. ------------------------------------------------------------------
  183.  
  184.  
  185.  
  186. main
  187.  
  188.  
  189.  
  190. public class Main {
  191.  
  192. public static void main(String[] args) {
  193.  
  194. FoodMenu fm = new FoodMenu();
  195.  
  196. fm.read("FoodMenu.xml");
  197. System.out.println(fm);
  198. System.out.println();
  199. fm.show();
  200. fm.create("novi.xml");
  201.  
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement