如何向方法添加代码

时间:2015-04-22 08:30:22

标签: java arrays json api parsing

您好我想问一下如何将代码功能转换为主要运行的方法!基本上我可以从主要激活工作方法。我是编程新手,所以我在掌握一切方面遇到了一些麻烦

    package bla_bla;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.*;
    import javax.swing.text.html.parser.Element;

        import org.codehaus.jackson.map.ObjectMapper;
        import org.jsoup.Jsoup;
        import org.jsoup.nodes.Document;
        import org.jsoup.select.Elements;
        import org.jsoup.select.Evaluator.Id;
        import org.json.*;

        import argo.saj.InvalidSyntaxException;

        public class supermonkey {

        private static ArrayList<BugsList> bugsList;    
        private static ArrayList<BugsList> bugbug;  


        public static void main(String args[]) throws IOException {

            bugsList = new ArrayList<BugsList>();
            bugbug = new ArrayList<BugsList>();
            Document doc = Jsoup.connect("https://bugzilla.mozilla.org/rest/bug?product=Input&f1=bug_mentor&o1=isnotempty").get();
            String rawData = doc.body().text();
        //  System.out.println(title);
            JSONObject obj = new JSONObject(rawData);

        //  System.out.println(obj);

            System.out.println(obj.get("bugs"));

            JSONArray jsonMainArr = new JSONArray(obj.get("bugs").toString());
            for (int i = 0; i < jsonMainArr.length(); i++) {  // **line 2**
                 JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
                 JSONObject assigned = childJSONObject.getJSONObject("assigned_to_detail");
              //   JSONObject assigned2 = childJSONObject.getJSONObject("assigned_to_detail");


                 int id = assigned.getInt("id");
                 BugsList bug = new BugsList();
                 BugsList bug2 = new BugsList();
                 bug.setId(id);



                 String severity = childJSONObject.getString("severity");
                 String resolution = childJSONObject.getString("resolution");
                 String summary = childJSONObject.getString("summary");
                 String component = childJSONObject.getString("component");
                 bug.setSeverity(severity);
                 bug.setResolution(resolution);
                 bug.setSummary(summary);
                 bug.setComponent(component);
                 bugsList.add(bug);

                 // String severity = assigned.getString();
        //       System.out.println("sss  "+ assigned);

            }



            getComponent("Code Quality");
        //  getSeverity(524276);
        //  getResolution(524276);
        //  getSummary(524276);
        }
           public static void getSeverity(int id){
               for(int i =0;i<bugsList.size(); i++){
                   if(bugsList.get(i).getId() == id){
                       System.out.println("The id exists in the list " + bugsList.get(i).getSeverity());
                   }
               }
           } 

           public static void getResolution(int id){
               for(int i =0;i<bugsList.size(); i++){
                   if(bugsList.get(i).getId() == id){
                       System.out.println("The id exists in the list and The resolution is" + bugsList.get(i).getResolution());
                   }
               }
           }  

           public static void getSummary(int id){
               for(int i =0;i<bugsList.size(); i++){
                   if(bugsList.get(i).getId() == id){
        System.out.println("The comp.. exists in the list and The summary is   " + bugsList.get(i).getSummary());
                   }
               }
           }
            // Current used method
           public static ArrayList<BugsList> getComponent(String component){
               for(int i =0;i<bugsList.size(); i++){
                   if(bugsList.get(i).getComponent().equals(component)){
                       System.out.println("(Code Quality) component contains summary  " + bugsList.get(i).getSummary() +" /The resolution is " +                 
        bugsList.get(i).getResolution() + "  /Severity is " + bugsList.get(i).getSeverity());
                       bugbug.add(bugsList.get(i));
                   }
               }
               return bugbug;
           }   


        }

  package bla_bla;

public class BugsList {

    private String severity;
    private int id; 
    private String resolution;
    private String summary;
    private String component;

    public String getComponent() {
        return component;
    }
    public void setComponent(String component) {
        this.component = component;
    }
    public String getSummary() {
        return summary;
    }
    public void setSummary(String summary) {
        this.summary = summary;
    }
    public String getResolution() {
        return resolution;
    }
    public void setResolution(String resolution) {
        this.resolution = resolution;
    }
    public String getSeverity() {
        return severity;
    }
    public void setSeverity(String severity) {
        this.severity = severity;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }   
}

1 个答案:

答案 0 :(得分:0)

我想我理解你的问题。您将代码编写到main函数中,而不是解决方案。 你的主要功能应该是这样的:

    public class Supermonkey {

    private  ArrayList<BugsList> bugsList;
    private  ArrayList<BugsList> bugbug;

    public Supermonkey(){
        bugsList = new ArrayList<BugsList>();
        bugbug = new ArrayList<BugsList>();
        Document doc = Jsoup
                .connect(
                        "https://bugzilla.mozilla.org/rest/bug?product=Input&f1=bug_mentor&o1=isnotempty")
                .get();
        String rawData = doc.body().text(); // ... partial code add everything about initialisation of your instance

你的Class是Supermonkey,你创建了一个实例调用supermonkey(有时人们使用mySupermonkey)。这个类只有一个实例可以使用。

然后在构造函数方法中,您可以创建所有内容以构建实例supermonkey:

        // Current used method
    public  ArrayList<BugsList> getComponent(String component) {
        for (int i = 0; i < bugsList.size(); i++) {
            if (bugsList.get(i).getComponent().equals(component)) {
                System.out
                        .println("(Code Quality) component contains summary  "
                                + bugsList.get(i).getSummary()
                                + " /The resolution is "
                                + bugsList.get(i).getResolution()
                                + "  /Severity is "
                                + bugsList.get(i).getSeverity());
                bugbug.add(bugsList.get(i));
            }
        }
        return bugbug;
    }

然后你可以有一个这个Class Supermonkey的方法

public static void main(String args[])

此方法是来自supermonkey.getComponent("componennt name "); BugsList

的来电

另外我想更改类BugItem的名称似乎是{{1}},可能是?