数据结构项目

时间:2016-11-23 19:02:43

标签: java data-structures tree fxml

我用FXML创建了接口..这个项目用于数据结构课程..但实际上我不能将我的数据结构知识与java面向对象混合,任何帮助?!

购物中心(商场)在全国范围内有许多分支机构需要进行计算机化,如下所述:   - 商场有一位经理。   - 商场有四种类型的员工(经理 - 分行经理 - 收银员 - 客户服务)存储在一个文件中。   - 商场有许多产品类别,每个类别都有许多子类别,每个子类别都有许多产品存储在一个文件中。

`

package tree;

import java.util.ArrayList;
import java.util.List;

public class Category {


  private List<Category> children = new ArrayList<Category>();
  private Category parent;
  private String name;

  // private constructor
  private Category(Category parent, String name) { 
    this.parent = parent; 
    this.name = name;
  }

`

1 个答案:

答案 0 :(得分:0)

我建议您阅读有关树数据结构的内容,可能是维基百科:https://en.wikipedia.org/wiki/Tree_%28data_structure%29或数据结构课程的材料(您也可以在youtube上找到很好的信息)。

我将提供一些示例代码,我希望您能够完成理解树数据结构的原理。我假设不同级别的人员将由树中的不同级别表示。

    package tree;

import java.util.ArrayList;
import java.util.List;

public class Category {


  private List<Category> children = new ArrayList<Category>();

  public List<Category> getChildren() {
    return children;
  }

private Category parent;
  private String name;

  public String getName() {
    return name;
}

// private constructor
  private Category(Category parent, String name) { 
    this.parent = parent; 
    this.name = name;
  }

  public void addChild(Category childIm){
      this.children.add(childIm);
  }

  public static void main(String[] gwarrR){
      //tree starts with root node
      Category manager = new Category(null, "Harold");

      //then you add child nodes to the root node
      Category branchManagerA = new Category(manager, "branchDude A");
      Category branchManagerB = new Category(manager, "branchDude B");
      Category branchManagerC = new Category(manager, "branchDude C");

      manager.addChild(branchManagerA);
      manager.addChild(branchManagerB);
      manager.addChild(branchManagerC);

      //Java 8 style print contents of managers children
      manager.getChildren().forEach((child)-> {System.out.println(child.getName());});

      // and print the managers name
      System.out.println(manager.getName());

      // managers children may have children themselves
      branchManagerA.addChild(new Category(branchManagerA, "le cashier"));

      Category cashier2 = new Category(branchManagerB, "cashier2");

      branchManagerB.addChild(cashier2);

      //Java 8 style print contents of managers childrens children
      manager.getChildren().forEach((child)-> {

          System.out.println(child.getName() + " has children:");

          child.getChildren().forEach((childrensChild)-> {

          System.out.println(childrensChild.getName());});

      });
}
}

这会产生:

branchDude A
branchDude B
branchDude C
Harold
branchDude A has children:
le cashier
branchDude B has children:
cashier2
branchDude C has children:

所以你保证从收银员级别继续,好吗? =)

好的,我希望这会有所帮助。 ^^ - d