如何处理自定义链接列表中的对象?

时间:2017-08-02 14:58:47

标签: java data-structures linked-list singly-linked-list

我成功制作了一个链表,但现在我无法处理它。我需要将哪些方法添加到我的FoodList类才能处理我的对象?例如,我需要让用户能够选择手动添加食物对象,这样我就可以打印一顿饭了。另外,我不能使用java API中的任何集合类。这一切都必须是定制的。

public static void main(String[] args) {
    FoodList list = new FoodList();
    boolean keepGoing = true;
    int scanResultInt;
    try
    {
        //I/O stream
    FileReader fr = new FileReader("foodlist.txt");
    BufferedReader br = new BufferedReader(fr);
    Scanner scan = new Scanner(br);

        Food hold = new Food();
        while(scan.hasNext()){
            list.add(hold = new Food());
            String str = scan.next();
            //str = scan.next();
            hold.setName(str);
            str = scan.next();
            hold.setGroup(str);
            int cal = scan.nextInt();
            hold.setNumCal(cal);
            double percent = scan.nextDouble();
            hold.setPercentDV(percent);
            list.add(hold);
        }
        //System.out.println("" + list.toString());

        br.close(); //close I/O stream
    }
    catch(IOException e){
        System.err.println("I/O EXCEPTION: " + e.getMessage());
    }
    Scanner scan2 = new Scanner(System.in);
do  {
    System.out.println("---------------------------------------------------------");
    System.out.println("            Welcome to the Parkland Meal Selector"        );
    System.out.println("---------------------------------------------------------");
    System.out.println("Enter the number of the menu option you would like to select:");
    System.out.println("        1) List food database");
    System.out.println("        2) Create meal by manual selection");
    System.out.println("        3) Create meal by random selection");
    System.out.println("        4) Remove foods high in calories");
    System.out.println("        5) Exit");

    scanResultInt = scan2.nextInt();
    switch(scanResultInt) {
        case 1: {
            System.out.println("" + list.toString());
            break;
        }
        case 2: {
            System.out.println("Create-A-Meal Menu\n");
            System.out.println("Enter the name of a food you would like to add:\n");
            String foodWanted = scan2.next();

            /*while( != null){
                if(foodWanted.equals());
            }*/
            /*Food tmp;
            for(tmp = head; tmp != null; tmp = tmp.next)
            {
                result += tmp.f;
            }
            return result;*/
        }
        case 3: {
            System.out.println("Create meal by random selection: \n");
            break;
        }
        case 4: {
            System.out.println("Remove Food High In Calories: \n");
            break;
        }
        case 5: {
            keepGoing = false;
            break;
        }
    }
}
while(keepGoing);
}

这是我的链接列表:

public class FoodList {

    // Class fields
    private FoodNode head;
    private int listCount;

    // Private inner class
    private class FoodNode
    {
        public Food f;
        public FoodNode next;
        public FoodNode(Food f)
        {
            this.f = f;
            this.next = null;
        }
    }


    // Constructor for LinkedList
    public FoodList()
    {
        // Initialize start of the list
        head = null;
        listCount = 0;
    }

    // Add method (adds a reservation to the linked list)
    public void add(Food f)
    {
        // Create a new ReservationNode
        FoodNode node = new FoodNode(f);

        // If this is the first node
        if( head == null )
            head = node;
        else
        {
            FoodNode tmp = head;
            while(tmp.next != null)
                tmp = tmp.next;
            tmp.next = node;
        }
        listCount++
    }
    /*public boolean hasThatFood(String food){
        boolean haveThat = false;
        FoodNode tmp;
        for(tmp = head; tmp != null; tmp = tmp.next)
        {
            if (food == f.getName());
                haveThat = true;
        }
        return haveThat;
    }*/
    /*public boolean hasNext(){
        boolean hasNext = false;
        if(head != null) {
            hasNext = true;
            return hasNext;
        }

    }*/

    @Override
    public String toString() {
        String result = "My Foods:" + '\n';

        // Loop through all the reservation nodes
        FoodNode tmp;
        for(tmp = head; tmp != null; tmp = tmp.next)
        {
            result += tmp.f;
        }
        return result;
    }
}

我的食物课

        public class Food {
    private String name;
    private String group;
    private int numCal;
    private double percentDV;

    public Food() {//String name, String group, int numCal, double percentDV
        /*this.name = name;
        this.group = group;
        this.numCal = numCal;
        this.percentDV = percentDV;*/
        name = "";
        group = "";
        numCal = 0;
        percentDV = 0.0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public int getNumCal() {
        return numCal;
    }

    public void setNumCal(int numCal) {
        this.numCal = numCal;
    }

    public double getPercentDV() {
        return percentDV;
    }

    public void setPercentDV(double percentDV) {
        this.percentDV = percentDV;
    }
    @Override
    public String toString() {
        return "Food{" +
                "name: '" + name + '\'' +
                ", Food Group: '" + group + '\'' +
                ", Calories:  " + numCal +
                ", Daily Percent: " + percentDV +
                '}';
    }
}

我知道这是意大利面条代码,但这是我的最后手段。任何帮助都会受到欢迎!

3 个答案:

答案 0 :(得分:1)

要操作对象,您必须编写自定义Iterator。我想这里打开LinkedList来源并了解其工作原理并不是犯罪行为。

答案 1 :(得分:1)

像这样的东西,你可以在网上找到很多资源,

https://crunchify.com/how-to-implement-a-linkedlist-class-from-scratch-in-java/

这是一个。

(lldb) expr -l objc -O -- 0x7fc795700140
<UIApplication: 0x7fc795700140>

答案 2 :(得分:1)

您已经实现了一些复杂的类。它的内在逻辑不像你的问题那么清楚。因此,几乎任何答案都无法满足您的需求。

如果我愿意,我会尝试使用java核心工具推荐逻辑(不实现以最佳方式实现的类LinkedListArrayList ...)。逻辑应转换为一些结构解决方案。例如:

  • 输入点创建并调用您的stream service来处理提供的输入流;
  • stream handler应该操纵builder;
  • builder结果必须收集到composite;
  • 等......

如果你以更加结构化的方式提供逻辑,你会问更明确的问题指出问题。此外,我相信你的问题将在准备之后消失。

我还建议您熟悉下一个GoF patternsbuilderfactory methodcompositestrategy