一棵苹果树的主干上有2个分支,一个分支上结了10个苹果,另一个分支上结了8个苹果,苹果每斤4元钱。请用组合模式组织苹果树的结构,当用户发现有新的分支或新的苹果时,不必修改计算苹果树的重量和苹果价值的代码。
import java.util.*;
public interface TreeComponent{
public void add(TreeComponent node);
public void remove( TreeComponent node);
public TreeComponent getChild(int index);
public Iterator<TreeComponent> getAllChildren();
public boolean isLeaf();
public double getweight();
}
import java.util.Iterator;
import java.util.LinkedList;
public class Apple implements TreeComponent{
LinkedList<TreeComponent> list;
double weight;
String name;
Apple(String name , double weight){
this.name = name;
this.weight = weight;
list = new LinkedList<TreeComponent>();
}
@Override
public void add(TreeComponent node) {}
@Override
public void remove(TreeComponent node) {}
@Override
public TreeComponent getChild(int index) {
return null;
}
@Override
public Iterator<TreeComponent> getAllChildren() {
return null;
}
@Override
public boolean isLeaf() {
return true;
}
@Override
public double getweight() {
return weight;
}
public String toString(){
return name;
}
}
import java.util.Iterator;
import java.util.LinkedList;
public class TreeBody implements TreeComponent{
LinkedList<TreeComponent>list;
double weight;
String name;
public TreeBody(String name,double weight) {
this.name=name;
this.weight=weight;
list=new LinkedList<TreeComponent>();
}
@Override
public void add(TreeComponent node) {
list.add(node);
}
@Override
public void remove(TreeComponent node) {
list.remove(node);
}
@Override
public TreeComponent getChild(int index) {
return list.get(index);
}
@Override
public Iterator<TreeComponent> getAllChildren() {
return list.iterator();
}
@Override
public boolean isLeaf() {
return false;
}
@Override
public double getweight() {
return weight;
}
public String toString(){
return name;
}
}
import java.util. *;
public class Computer{
public static double computerWeight(TreeComponent node){
double weightSum = 0;
if(node.isLeaf() == true){
weightSum = weightSum + node.getweight();
}
if(node.isLeaf() == false){
weightSum = weightSum + node.getweight();
Iterator<TreeComponent> iterator = node.getAllChildren();
while(iterator. hasNext()){
TreeComponent p= iterator.next();
weightSum = weightSum + computerWeight(p);
}
}
return weightSum;
}
public static double computerValue(TreeComponent node, double unit){
double appleWorth= 0;
if(node.isLeaf()==true){
appleWorth= appleWorth +node.getweight() * unit;
}
if(node.isLeaf()== false){
Iterator<TreeComponent> iterator = node. getAllChildren();
while(iterator.hasNext()){
TreeComponent p= iterator.next();
appleWorth = appleWorth +computerValue(p,unit);
}
}
return appleWorth;
}
public static String getAllChildrenName(TreeComponent node){
StringBuffer mess = new StringBuffer();
if(node.isLeaf()== true){
mess.append(" "+node.toString());
}
if(node.isLeaf()==false){
mess.append(" " + node.toString());
Iterator<TreeComponent>iterator = node.getAllChildren();
while(iterator.hasNext()){
TreeComponent p= iterator.next();
mess. append(getAllChildrenName(p));
}
}
return new String(mess);
}
}
import javax. swing.*;
import javax.swing.tree. *;
import javax.swing. event. * ;
import java.awt. *;
public class Application extends JFrame implements TreeSelectionListener{
TreeComponent mainBody,branchOne,branchTwo,apple[];
DefaultMutableTreeNode trunk,branch1,branch2,leaf[];
JTree tree;
final static int MAX=18;
JTextArea text;
public Application(){
mainBody = new TreeBody("树干",786);
trunk = new DefaultMutableTreeNode(mainBody);
branchOne = new TreeBody("树枝",45);
branch1 = new DefaultMutableTreeNode(branchOne);
branchTwo = new TreeBody("树枝",25);
branch2 = new DefaultMutableTreeNode(branchTwo);
apple = new Apple[MAX];
leaf = new DefaultMutableTreeNode[MAX];
for(int i=0;i<MAX;i++){
apple[i]=new Apple("苹果",0.25);
leaf[i]=new DefaultMutableTreeNode(apple[i]);
}
mainBody. add(branchOne);
trunk.add(branch1);
mainBody.add(branchTwo);
trunk.add(branch2);
for(int i=0;i<=7;i++){
branchOne.add(apple[i]);
branch1.add(leaf[i]);
}
for(int i=8;i<MAX;i++){
branchTwo.add(apple[i]);
branch2.add(leaf[i]);
}
tree = new JTree(trunk);
tree.addTreeSelectionListener(this);
text = new JTextArea(20,20);
text.setFont(new Font("宋体", Font. BOLD,12));
text.setLineWrap(true);
setLayout(new GridLayout(1,2));
add(new JScrollPane(tree));
add(new JScrollPane(text));
setBounds(70,80,460,320);
setDefaultCloseOperation(JFrame. DISPOSE_ON_CLOSE);
setVisible(true);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
text.setText(null);
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
TreeComponent treeComponent=(TreeComponent)node.getUserObject();
String allName=Computer.getAllChildrenName(treeComponent);
double weight=Computer.computerWeight(treeComponent);
String mess = null;
if(treeComponent.isLeaf())
mess =allName+"的重量:\n" + weight +"公斤";
else
mess = allName +"加在一起的重量:\n" + weight+"公斤";
text. append(mess+ "\n");
double unit = 4;
double value = Computer.computerValue(treeComponent, unit);
String name = treeComponent.toString();
if(treeComponent.isLeaf())
mess = name +"的价值("+ unit+"元/kg)" + value + "元";
else
mess = name+"所结苹果的价值("+ unit+"元/kg)" +value+"元";
text.append("n" +mess);
}
public static void main(String args[]){
new Application();
}
}