lottocounter.java

来自「Usefull sample codes for Java. Containt 」· Java 代码 · 共 69 行

JAVA
69
字号
import java.io.*;
import java.util.*;
import nu.xom.*;

public class LottoCounter {
    Document document;
    Element root;
    File logFile;
    
    public LottoCounter() throws IOException, ParsingException {
        Builder builder = new Builder();
        logFile = new File("lotto.log");
        if (!logFile.exists()) {
            // create a new XML document
            root = new Element("log");
            document = new Document(root);
            writeDocument();
        }
        // load the XML document from a file
        document = builder.build(logFile);
        root = document.getRootElement();
    }
    
    public boolean sendResult(int plays, int win3, int win4, int win5, int win6) 
        throws IOException, ParsingException {
            
        // create the result element
        Element result = new Element("result");
        
        // create subelements for each value
        Element playElement = new Element("plays");
        playElement.appendChild("" + plays);
        Element win3Element = new Element("win3");
        win3Element.appendChild("" + win3);
        Element win4Element = new Element("win4");
        win4Element.appendChild("" + win4);
        Element win5Element = new Element("win5");
        win5Element.appendChild("" + win5);
        Element win6Element = new Element("win6");
        win6Element.appendChild("" + win6);
        Element dateElement = new Element("date");
        Date now = new Date();
        dateElement.appendChild(now.toString());
        
        // add subelements to result
        result.appendChild(playElement);
        result.appendChild(win3Element);
        result.appendChild(win4Element);
        result.appendChild(win5Element);
        result.appendChild(win6Element);
        result.appendChild(dateElement);
        
        // add the result to the root
        root.appendChild(result);
                       
        // save the document
        writeDocument();
        return true;
   }
   
   // write the document to a file
   private void writeDocument() throws IOException {
        FileOutputStream fileStream = new FileOutputStream(logFile);
        Serializer writer = new Serializer(fileStream);
        writer.setIndent(2);
        writer.write(document);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?