从Filewriter中的另一个类调用GUI中的另一个函数

时间:2018-11-16 15:53:21

标签: java filewriter bufferedwriter

当按下“生成报告”时,将生成一个文本文件。它将具有标签1的标题,日期和时间(下面已经有所有代码)以及用户在jTextArea中以“活动名称,持续时间,父母+活动名称,持续时间,父母”的形式输入的活动名称和活动持续时间。分隔符是+ DisplayGUI.java

else if(str.equals("Generate Report"))
    {

            try {
                String filename = JOptionPane.showInputDialog("Please input a filename:");
                FileWriter fw = new FileWriter("D:\\" + filename + ".txt");
                BufferedWriter buffer = new BufferedWriter(fw);
                String title="Title: "+lb1.getText();
                DateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss");
                Date date = new Date();
                buffer.write(title);
                buffer.newLine();
                buffer.newLine();
                buffer.write("\nDate and Time of Creation: "+dateFormat.format(date));

                buffer.newLine();
                buffer.newLine();
                buffer.write("\nList of activities & their duration:"); //STILL NEED TO WRITE CODE FOR THIS





                buffer.newLine();
                buffer.newLine();
                buffer.write("List of all paths with the activity names and total duration:");
                buffer.newLine();             
                String txtArea=ta1.getText();
                String[] txtArray= txtArea.split("\n");
                for(int i=0;i<txtArray.length;i++)
                {
                 buffer.write(txtArray[i]);
                 buffer.newLine();
                }
               buffer.close();
               ta1.setText("Report Generated");



                buffer.close();
            }
            catch (Exception e) {
                System.out.println(e);
            }
    }

Node.java //此类可查看用户输入的每个节点的活动名称,持续时间和父级

  import java.util.ArrayList;

public class Node {

private String activityName;
private int duration;
private ArrayList<String> parents = new ArrayList<String>();
private boolean end;

public Node() {
    this.activityName = "ExampleActivity";
    this.duration = 1;
    this.parents.add("null");
    this.end = false;
}
public Node(String activityName, int duration, ArrayList<String> parents) {
    this.activityName = activityName;
    this.duration = duration;
    this.parents = (ArrayList<String>) parents.clone();
    this.end = false;
}
public String getActivityName() {
    return activityName;
}
public void setActivityName(String activityName) {
    this.activityName = activityName;
}
public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}
public ArrayList<String> getParents() {
    return parents;
}
public void setParents(ArrayList<String> parents) {
    for(int i=0;i<parents.size();i++) {
        this.parents.add(parents.get(i));
    }
}
public boolean getEnd() {
    return end;
}
public void setEnd( ArrayList<Node> nodes  ){
    for( Node node : nodes ) {
        if( node.parents.contains( this.activityName ) ) return;
    }
    this.end = true;
}
@Override
public String toString() {
    return "Node [activityName=" + activityName + ", duration=" + duration
            + ", parents=" + parents + ", end=" + end + "]";
}
}

NodeBuilder.java //此类构建每个节点

import java.util.ArrayList;
import java.util.Scanner;

public class NodeBuilder {
    ArrayList<Node> nodes = new ArrayList<Node>();

ArrayList<String> parents = new ArrayList<String>();

public int add(String input) {

        //Receive user node input
        String activityName;
                    int duration;

        //Setting up new Scanner and delimiter
        Scanner s = new Scanner(input);
        s.useDelimiter(",");

        //Set node variables with user input
        if(s.hasNext()) {
            activityName = s.next();
            //System.out.println(activityName);
        } else
            return(-1);

        if(s.hasNextInt()) {
            duration = s.nextInt();
            //System.out.println(duration);

        } else
            return(-1);


        while (s.hasNext()) {
            parents.add(s.next());
        }

        Node myNode = new Node(activityName, duration, parents);
        nodes.add(myNode);
        parents.clear();

        //System.out.println(parents.size());
        //System.out.println(nodes.size());

        //reset parents ArrayList


        //Closing Scanner
        s.close();
        return(0);

    }


            public String computeNetwork(boolean onlyCrits) {
                String pathsString = "";

                for(Node node : nodes) {
                        node.setEnd( nodes );
                }

                //Error checking
                int errors = 0;
                for(int x=0;x<nodes.size();x++) {
                        boolean haskids = hasKids(nodes.get(x),nodes);
                        if(haskids==false && nodes.get(x).getParents().isEmpty()) {
                                errors = 1;
                                return("ERROR: One or more nodes are not connected to the other nodes. \nPlease reset and enter a new sequence of nodes that are connected.");

                        }
                }

                if(errors == 0) {
                        pathsString = NetworkBuilder.networkBuilder(nodes, onlyCrits);
                }
                return(pathsString);
            }
    public String changeDuration(boolean onlyCrits, String activity, int newDuration){



            int changed = 0;
            for(int j=0;j<nodes.size();j++) {
                if(nodes.get(j).getActivityName().equals(activity) && changed == 0) {
                    nodes.get(j).setDuration(newDuration);
                    changed = 1;
                }
            }
            return(NetworkBuilder.networkBuilder(nodes, onlyCrits));


    }


static boolean hasKids(Node node, ArrayList<Node> nodes) {
    for(int i=0;i<nodes.size();i++) {
        if(nodes.get(i).getParents().contains(node.getActivityName())) return true;
    }
    return false;
}
}

0 个答案:

没有答案