分离文本文件的一方以组织文本以获得总数

时间:2016-04-21 01:07:09

标签: java

我无法弄清楚如何将教授的职级分开,以便从以下问题中添加薪水:

  

处理大型数据集大学将员工的工资过帐到   http://cs.armstrong.edu/liang/data/Salary.txt。文件中的每一行   由教师的名字,姓氏,职级和薪水组成   (参见编程练习   12.24)。编写一个程序来显示助理教授,副教授,全职教授和所有教师的总薪水,   分别显示助理教授的平均工资,   副教授,全职教授和所有教师。

输入样本:

FirstName1 LastName1 assistant 79174.73
FirstName2 LastName2 associate 70817.75
FirstName3 LastName3 associate 69619.0
FirstName4 LastName4 full 116992.43
FirstName5 LastName5 full 116761.76
FirstName6 LastName6 full 123743.86
FirstName7 LastName7 assistant 70071.81
FirstName8 LastName8 assistant 67605.92
FirstName9 LastName9 associate 65534.42

我的代码:

import java.io.*;
import java.util.Scanner;

class Faculty implements Comparable<Faculty> {
    public String FirstName;
    public String LastName;
    public double salary;
    public String rank;

    Faculty() {
    }

    Faculty(String FirstName, String LastName, double salary) {
        setFirst(FirstName);
        setLast(LastName);
        setSalary(salary);
    }

    public void setFirst(String FirstName) {
        this.FirstName = FirstName;
    }

    public String getFirst() {
        return FirstName;
    }

    public void setLast(String LastName) {
        this.LastName = LastName;
    }

    public String getLast() {
        return LastName;
    }

    public void setrank() {
        this.rank = rank;
    }

    public int getrank(String rank) {
        if (rank == "full")
            return 0;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public int compareTo(Faculty arg) {
        return Integer.compare(getrank(), arg.getrank());
    }
}

class associate extends Faculty {
    public String associate;

    associate() {
    }

    associate(String FirstName, String LastName, String associate, double salary) {
        super(FirstName, LastName, salary);

    }

    public void setAssociate(String associate) {
        this.associate = associate;
    }

    public String getAssociate() {
        return associate;
    }

    public String compareTo(Faculty arg) {
        return String.compare(getRank(), arg.getRank());
    }
}

class assistant extends Faculty {
    public String assistant;

    assistant() {
    }

    assistant(String FirstName, String LastName, String assistant, double salary) {
        super(FirstName, LastName, salary);
        getAssistant = assistant;
    }

    public void setAssistant(String assistant) {
        this.assistant = assistant;
    }

    public String getAssistant() {
        return assistant;
    }

    public String compareTo(Faculty arg) {
        return String.compare(getRank(), arg.getRank());
    }
}

class full extends Faculty {
    public String full;

    full() {
    }

    full(String FirstName, String LastName, String full, double salary) {
        super(FirstName, LastName, salary);
        getFull = full;
    }

    public void setFull(String full) {
        this.full = full;
    }

    public String getFull() {
        return full;
    }

    public String compareTo(Faculty arg) {
        return String.compare(getRank(), arg.getRank());
    }
}

public class Facmembers {
    Faculty[] fm;

    //input
    public void input() throws FileNotFoundException {
        int count = 0;
        Scanner input = null;
        try {
            input = new Scanner(new File("C:/Users/Fava/Desktop/cps 2231 notes"));
            if (input.hasNextLine()) {
                count = input.nextInt();
            }
            fm = new Faculty[count];
            for (int i = 0; i < count && input.hasNextLine(); i++) {
                Faculty fm;
                String FirstName = input.next();
                String LastName = input.next();
                String ranks = input.next();
                if (rank(ranks)) {
                    ranktype(ranks);
                }
                double salary = input.nextDouble();
            }
        } catch (FileNotFoundException e) {
            if (input != null) {
                System.out.println("File not found.");
                input.close();
            }
        }
    }

    public static boolean rank(String A) {
        String test = A.toLowerCase();
        if (test == "full" || test == "associate" || test = "assistant") {
            return true;
        } else {
            return false;
        }
    }

    public static String ranktype(Faculty mem) {
        if (rank(mem.getrank()) == true) {
            return mem.getrank();
        } else {
            return "Error in rank";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于您没有告诉我们您的代码是否有效或者您是否有任何问题,我会尝试为您提供最容易解决的问题解决问题

使用SQLite

sqlite> create table salary (firstname, lastname, role, amount);
sqlite> .separator " "
sqlite> .import Salary.txt salary
sqlite> select sum(amount) from salary where role = 'assistant';
20246511.91
sqlite> select sum(amount) from salary where role = 'associate';
28844146.58
sqlite> select sum(amount) from salary where role = 'full';
35678051.41
sqlite> select avg(amount) from salary where role = 'assistant';
65949.5501954398
sqlite> select avg(amount) from salary where role = 'associate';
83849.2633139535
sqlite> select avg(amount) from salary where role = 'full';
102229.373667622