此功能表明,如果按下“生成报告”按钮,将生成一个文本文件。它将具有标签1,日期和时间以及活动名称和活动持续时间的标题,该标题由用户在jTextArea中以“活动名称,持续时间,父母+活动名称,持续时间,父母”的形式输入,此处分隔符为+
else if(str.equals("Generate Report"))
{
try {
FileWriter fw = new FileWriter("D:\\Report.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.write("\nDate and Time of Creation: "+dateFormat.format(date));
buffer.newLine();
//ENTER YOUR CODE HERE TO PRINT ALL ACTIVITY NAMES & DURATION
buffer.write("\nList of activities & their duration: " + ));
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 void 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
activityName = "Activity " + "not entered";
if(s.hasNext()) {
duration = s.nextInt();
//System.out.println(duration);
} else
duration = 0;
while (s.hasNext()) {
parents.add(s.next());
}
Node myNode = new Node(activityName, duration, parents);
nodes.add(myNode);
parents.clear();
s.close();
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;
}
}
文本文件的所需输出: https://i.stack.imgur.com/gQBwR.png