ArthurDn

Untitled

Sep 14th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1.  
  2. import java.io.File;
  3.  
  4. import java.io.FileOutputStream;
  5.  
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Scanner;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public class PlantCollection {
  16.    
  17. private static boolean bflower=false;
  18. private static char timeOfLife[] ;
  19. private static char name[] ;
  20. private static char color[] ;
  21.  
  22. public static char[] getTimeOfLife() {
  23.     return timeOfLife;
  24. }
  25.  
  26.  
  27. public static void setTimeOfLife(char[] timeOfLife) {
  28.     PlantCollection.timeOfLife = timeOfLife;
  29. }
  30.  
  31.  
  32. public static char[] getName() {
  33.     return name;
  34. }
  35.  
  36.  
  37. public static void setName(char[] name) {
  38.     PlantCollection.name = name;
  39. }
  40.  
  41.  
  42. public static char[] getColor() {
  43.     return color;
  44. }
  45.  
  46.  
  47. public static void setColor(char[] color) {
  48.     PlantCollection.color = color;
  49. }
  50.  
  51.  
  52. static String str;
  53. static String scolor;
  54. static String sname;
  55.    
  56.     public static void main(String[] args) throws IOException {
  57.         ArrayList<Plant> list = new ArrayList<Plant>();
  58.         list.add(new Flower("Red", 200));
  59.        
  60.         list.add(new Flower("Green", 250));
  61.         list.add(new Flower("Yellow", 300));
  62.         list.add(new Tree(100, 300));
  63.         list.add(new Tree(200, 600));
  64.        
  65.         saveToXML(list);   
  66.         saveToCSV(list);
  67.         System.out.println(readFromXML("C:\\file.xml"));
  68.        
  69.         str=new String (timeOfLife);
  70.         scolor=new String(color);
  71.         sname=new String(name);
  72.         if(bflower==true){
  73.             System.out.println(bflower);
  74.             list.add(new Flower(scolor, sname, Integer.parseInt(str)));
  75.         }
  76.     }
  77.    
  78.    
  79.     public static void saveToXML(List<Plant> list) throws IOException{
  80.         StringBuffer bufXML = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  81.        
  82.         bufXML.append("\r\n<elements>");
  83.         for(Plant plant : list){
  84.             bufXML.append(plant.toXMLString());
  85.         }
  86.        
  87.         bufXML.append("\r\n</elements>");
  88.        
  89.         FileOutputStream fosXML = new FileOutputStream("C:\\file.xml");
  90.         fosXML.write(bufXML.toString().getBytes());
  91.         fosXML.close();
  92.     }
  93.    
  94.     public static void saveToCSV(List<Plant> list) throws IOException{
  95.         StringBuffer bufCSV = new StringBuffer();
  96.        
  97.         for(Plant plant : list){
  98.             bufCSV.append(plant.toCSVString());
  99.         }
  100.        
  101.         FileOutputStream fosCSV = new FileOutputStream("C:\\file.csv");
  102.         fosCSV.write(bufCSV.toString().getBytes());
  103.         fosCSV.close();
  104.     }
  105.     public static String readFromXML (String filename)throws IOException{
  106.         String text="";
  107.        
  108.         Scanner in = new Scanner(new File("C:\\file.xml"));
  109.         while(in.hasNext())
  110.         text += in.nextLine() + "\r\n";
  111.         in.close();
  112.         char color[] = new char[text.indexOf("</color>")-7 - text.indexOf("<color>")];
  113.  
  114.         text.getChars(text.indexOf("<color>")+7, text.indexOf("</color>"), color, 0);
  115.  
  116.         System.out.println(color);
  117.         char name[] = new char[text.indexOf("</name>")-6 - text.indexOf("<name>")];
  118.  
  119.         text.getChars(text.indexOf("<name>")+6, text.indexOf("</name>"), name, 0);
  120.  
  121.         System.out.println(name);
  122.         char timeOfLife[] = new char[text.indexOf("</timeOfLife>")-12 - text.indexOf("<timeOfLife>")];
  123.  
  124.         text.getChars(text.indexOf("<timeOfLife>")+12, text.indexOf("</timeOfLife>"), timeOfLife, 0);
  125.  
  126.         System.out.println(timeOfLife);
  127.             if (text.contains("<Flower>"))
  128.                 bflower=true;
  129.             setColor(color);
  130.             setName(name);
  131.             setTimeOfLife(timeOfLife);
  132.             return text;
  133.        
  134.  
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment