使用自定义LinkedList类的工资单系统

时间:2014-03-23 22:28:54

标签: java class generics linked-list

我正在使用链接列表编写Payroll应用程序。目的是读取多个文件并将其内容存储在链接列表中。然后程序将使用另一个类从链接到另一个文本文件输出所需的数据。稍后,文件将替换为数据库表。

我的TestReadFile类在链表中读取数据和存储,然后应该读取它。 LinkedListClass和LinkedListNode类应该是通用的。

我想将每个部门的对象存储在单独的对象中,然后将这些对象放在链表中,并且在需要时我可以从链表中检索对象。

任何帮助将不胜感激! 非常感谢

package payroll;
public class Department implements iDepartment
{
    private int deptID;
    private String deptName, deptLoc;

    public Department()
    {
        deptID = 0;
        deptName = "";
        deptLoc = "";
    }

    public Department(int deptID, String deptName, String deptLoc)
    {
        setDeptID(deptID);
        setDepName(deptName);
        setDeptLoc(deptLoc);
    }

    public void setDeptID(int deptID)
    {
        this.deptID = deptID;
    }

    public void setDepName(String deptName)
    {
        this.deptName = deptName;
    }

    public void setDeptLoc(String deptLoc)
    {
        this.deptLoc = deptLoc;
    }

    public int getDeptID()
    {
        return deptID;
    }

    public String getDeptName()
    {
        return deptName;
    }

    public String getDeptLoc()
    {
        return deptLoc;
    }
}

package payroll;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class TestReadFile 
{
    private static Scanner inFile;    
    private static Department department;
    public static void main(String[] args) throws FileNotFoundException
    {
        inFile = new Scanner(new FileReader("Department.in"));

        LinkedListClass dept = new LinkedListClass();        

        inFile.useDelimiter(";");
//        while(inFile.hasNextLine())
//        {            
            department = new Department();
            department.setDeptID(inFile.nextInt());
            department.setDepName(inFile.next());
            department.setDeptLoc(inFile.next());            
            dept.addLast(department);                        
//        }
//

//      dept.outputObject();    
        System.out.println("Department ID: " + dept.outputObject().toString());             
    }
}

public class LinkedListClass<T> implements iLinkedListClass<T>
{
    private LinkedListNode<T> head, tail, trav, p;
    private int count;


    public LinkedListClass()
    {
        head = null;
        tail = null;
        count = 0;
    }

    public void addFirst(T data)
    {
        p = new LinkedListNode<T>(data, null);
        p.setLink(head);
        head = p;
    }

    public void addLast(T data)
    {
        p = new LinkedListNode<T>(data, null);

        if(head == null)
        {
            head = p;
            tail = p;
        }
        else
        {
            tail.setLink(p);
            tail = p;
        }
    }

    public void removePosition(int pos)
    {
        LinkedListNode<T> prev;
        count = 0;

        if(pos == 0)
        {
            head = head.getLink();
        }

        else if(pos < size())
        {
            prev = head;
            for (int i = 1; i <= (pos - 1); i++)
            {
                prev = prev.getLink();
            }
            prev.setLink(prev.getLink().getLink());

            //if last one to be deleted
            if (prev.getLink() == null)
            {
                tail = prev;
            }
        }
    }

    public void print()
    {
        trav = head;

        while(trav != null)
        {
            System.out.println("Value: " + trav.getInfo());
            trav = trav.getLink();
        }
    }

    public int size()
    {
        trav = head;
        count = 0;

        while(trav != null)
        {
            count++;
            trav = trav.getLink();
        }
        return count;
    }

    public boolean search(T data)
    {
        boolean found = false;
        trav = head;

        while(trav != null)
        {
            if(trav.getLink() != null)
            {
                found = true;
            }
            trav = trav.getLink();
        }
        if(found)
        {
            System.out.println("Value searched is: " + data);
        }
        else
        {
            System.out.println("Value does not exist in the list");
        }
        return found;
    }

    public void insert(T data, int pos)
    {
        trav = head;

        if((pos <= size()) && (pos > 0))
        {
            count = 1;
            while(count <= pos)
            {
                if(count == pos)
                {
                    p = new LinkedListNode(data , null);
                    p.setLink(trav.getLink());
                    trav.setLink(p);
                }
                trav = trav.getLink();
                count++;
            }
        }
        else if(pos <= 0)
        {
            addFirst(data);
        }
        else if(pos > size())
        {
            addLast(data);
        }        
    }

    public int position(T data)
    {
        int pos = -1;
        trav = head;
        while(trav != null)
        {
            if(trav.getInfo() != null)
            {
                return pos;
            }
            pos++;
            trav = trav.getLink();
        }
        return -1;
    }    

    public T outputObject()
    {
        trav = head;
        return trav.getInfo();
    }
}


package payroll;
public class LinkedListNode<T>
{
    private T data;
    private LinkedListNode<T> link;

    public LinkedListNode(T info, LinkedListNode<T> link)
    {
        this.data = info;
        this.link = link;
    }

    public void setInfo(T info)
    {
        this.data = info;
    }

    public void setLink(LinkedListNode<T> link)
    {
        this.link = link;
    }

    public T getInfo()
    {
        return this.data;
    }

    public LinkedListNode<T> getLink()
    {
        return this.link;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该通过您将创建的类(对象)来解决您的问题,而不是您想要执行的程序性工作。示例:在您的情况下,您似乎拥有一个由一个或多个部门组成的公司。所以..创建一个公司类,并给它一个方法来读取等等的部门。

例如,请考虑以下代码。

主要类

package com.example.accounting;
import java.util.LinkedList;
public class Main {
// Object fields
private static Main me;
private Company company;
private LinkedList<Department> departments;

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    me = new Main();
    me.doWork(args);

}

private void doWork(String[] args) {
    //TODO accept file name as an argument?!

    company = new Company();
    company.addDepartment("Department.in");
    company.listDepartments();

    departments = company.getDepartments();
}
}

公司类

package com.example.accounting;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.Scanner;

public class Company {
// Object fields
LinkedList<Department> departments = null;

public Company() {
    departments = new LinkedList<Department>();
}

public boolean addDepartment(String file) {
    Scanner source = null;

    try {
        source = new Scanner(new FileReader(file));
    } catch (FileNotFoundException e) {
        System.out.println("Error trying to read " + file + ": " + e.getMessage());
        e.printStackTrace();
        return false;
    }
    source.useDelimiter(";");

    while(source.hasNextLine()) {
        int departmentId = source.nextInt();
        String departmentName = source.next();
        String departmentLocation = source.next();
        departments.add(new Department(departmentId, departmentName, departmentLocation));
    }
    source.close();
    return true;
}

public LinkedList<Department> getDepartments() {
    return departments;
}

public void listDepartments() {

    for (Department dept : departments) {
        System.out.println(" " + dept.getDeptId() + ". " + dept.getDeptName() + ", " + dept.getDeptLoc());
    }
}
}

系类

package com.example.accounting;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.Scanner;

公共类公司{     //对象字段     LinkedList departments = null;

public Company() {
    departments = new LinkedList<Department>();
}

public boolean addDepartment(String file) {
    Scanner source = null;

    try {
        source = new Scanner(new FileReader(file));
    } catch (FileNotFoundException e) {
        System.out.println("Error trying to read " + file + ": " + e.getMessage());
        e.printStackTrace();
        return false;
    }
    source.useDelimiter(";");

    while(source.hasNextLine()) {
        int departmentId = source.nextInt();
        String departmentName = source.next();
        String departmentLocation = source.next();
        departments.add(new Department(departmentId, departmentName, departmentLocation));
    }
    source.close();
    return true;
}

public LinkedList<Department> getDepartments() {
    return departments;
}

public void listDepartments() {

    for (Department dept : departments) {
        System.out.println(" " + dept.getDeptId() + ". " + dept.getDeptName() + ", " + dept.getDeptLoc());
    }
}
}