Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 管理教员中学生的应用程序_Java - Fatal编程技术网

Java 管理教员中学生的应用程序

Java 管理教员中学生的应用程序,java,Java,正如标题所说,我必须制作一个管理学生的应用程序。学生被组织成小组。学生必须从一个文本文件中书写,每个文本文件都要分组。如果我限制每组30名学生,那么对于60名学生,我将分为两组。作为Java新手,我真的不知道在接触到30名学生后如何增加组id,或者,我不知道一种方法。我在想地图之类的东西。这就是我到目前为止所做的。有什么建议吗 学生班级: public class Student { private String firstName; private String lastName; priva

正如标题所说,我必须制作一个管理学生的应用程序。学生被组织成小组。学生必须从一个文本文件中书写,每个文本文件都要分组。如果我限制每组30名学生,那么对于60名学生,我将分为两组。作为Java新手,我真的不知道在接触到30名学生后如何增加组id,或者,我不知道一种方法。我在想地图之类的东西。这就是我到目前为止所做的。有什么建议吗

学生班级:

public class Student {
private String firstName;
private String lastName;
private int age;
private String CNP;
private int grade;
private boolean leader;

public Student(String firstName, String lastName, int age, String CNP, int grade, boolean leader) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.CNP = CNP;
    this.grade = grade;
    this.leader = leader;
}
}

教员班:

public class Faculty {
private int groupIterator;
private List<Student> list;

public Faculty() throws Exception {
    list = new ArrayList<Student>();
    Scanner fileIn = new Scanner(new File("---"));

    String line;
    String firstName;
    String lastName;
    int age;
    String CNP;
    int grade;
    boolean leader;

    while(fileIn.hasNextLine()) {
        line = fileIn.nextLine();
        firstName = line;
        line = fileIn.nextLine();
        lastName = line;
        line = fileIn.nextLine();
        age = Integer.parseInt(line);  
        line = fileIn.nextLine();
        CNP = line;
        line = fileIn.nextLine();
        grade = Integer.parseInt(line); 
        line = fileIn.nextLine();
        leader = Boolean.parseBoolean(line);  
        list.add(new Student(firstName,lastName,age,CNP,grade,leader));
    }

    fileIn.close();
}
公共课教师{
私有整数组迭代器;
私人名单;
公共教师()抛出例外{
列表=新的ArrayList();
Scanner fileIn=new Scanner(新文件(“--”);
弦线;
字符串名;
字符串lastName;
智力年龄;
字符串CNP;
国际等级;
布尔前导;
while(fileIn.hasNextLine()){
line=fileIn.nextLine();
firstName=行;
line=fileIn.nextLine();
lastName=行;
line=fileIn.nextLine();
年龄=整数.parseInt(行);
line=fileIn.nextLine();
CNP=直线;
line=fileIn.nextLine();
等级=整数.parseInt(行);
line=fileIn.nextLine();
leader=Boolean.parseBoolean(行);
添加(新学生(名字、姓氏、年龄、CNP、年级、领导));
}
fileIn.close();
}

目前,您的代码混淆了
教员所扮演的角色。它似乎代表了一个班级的老师和学生。但是,它也有解析新生所需的代码。这将是一个糟糕的设计选择

实现这一点的最佳OOP方法是建立一个
注册者
类。这基本上代表了将学生安置在教师身边的学校实体。它将解析将学生安置在
教员
类中的文件

教员
课程应该有一个方法
isClassFull
students.size>=30
),该方法将用于确定新学生是否需要新的
教员
,因为先前的
教员
已满。然后,注册官将创建一个新教员并重复该过程

最终结果将是一个
注册者
,拥有
教员
,最多30名
学生

由于这似乎是一个学生练习,我将把实现留给您,但希望这能澄清如何在代码中表示这个概念。

学生:

public class Student {
private String firstName;
private String lastName;
private int age;
private String CNP;
private int grade;
private boolean leader;
private int group;

public Student(String firstName, String lastName, int age, String CNP, int grade, boolean leader, int group) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.CNP = CNP;
    this.grade = grade;
    this.leader = leader;
    this.group = group;
}
教员:

public class Faculty {
private int groupIterator;
private List<Student> list;

public Faculty() throws Exception {
    list = new ArrayList<Student>();
    Scanner fileIn = new Scanner(new File("---"));

    String line;
    String firstName;
    String lastName;
    int age;
    String CNP;
    int grade;
    boolean leader;

    while(fileIn.hasNextLine()) {
        line = fileIn.nextLine();
        firstName = line;
        line = fileIn.nextLine();
        lastName = line;
        line = fileIn.nextLine();
        age = Integer.parseInt(line);  
        line = fileIn.nextLine();
        CNP = line;
        line = fileIn.nextLine();
        grade = Integer.parseInt(line); 
        line = fileIn.nextLine();
        leader = Boolean.parseBoolean(line);  
        // Here's the key bit - we now pass in the group id based on the list size
        list.add(new Student(firstName,lastName,age,CNP,grade,leader,len(list)/30));
    }

    fileIn.close();
}
公共课教师{
私有整数组迭代器;
私人名单;
公共教师()抛出例外{
列表=新的ArrayList();
Scanner fileIn=new Scanner(新文件(“--”);
弦线;
字符串名;
字符串lastName;
智力年龄;
字符串CNP;
国际等级;
布尔前导;
while(fileIn.hasNextLine()){
line=fileIn.nextLine();
firstName=行;
line=fileIn.nextLine();
lastName=行;
line=fileIn.nextLine();
年龄=整数.parseInt(行);
line=fileIn.nextLine();
CNP=直线;
line=fileIn.nextLine();
等级=整数.parseInt(行);
line=fileIn.nextLine();
leader=Boolean.parseBoolean(行);
//这里是关键位-我们现在根据列表大小传入组id
列表。添加(新学生(名字、姓氏、年龄、CNP、年级、领导、len(列表)/30);
}
fileIn.close();
}

在这里,我没有看到任何您尝试合并组的想法。“private int groupIterator”是我看到的唯一提到的组。你有没有尝试过添加组的概念?你想要的最终结果是什么?现在你将得到一个学生对象列表。你到底想要什么?如果你只想在每个Person对象中添加一个“group”字段,你可以在添加每个Person对象之前查看列表的大小在上。您可以执行(len(list)/30)给自己一个组ID,前30个添加的学生的组ID为“0”,后30个学生的组ID为“1”。这就是您想要/需要的吗?只需向Person构造函数添加一个“group”参数并传入len(list)/30.很抱歉解释得不好,我不知道该如何表述。我只有一个教员班,教员班包含一个小组列表(学生以小组形式组织).我只想把每一个学生都从一篇课文中写出来,放在教员的列表中。我的问题是:如果我的课文文件中有45名学生,那么30名学生将在该教员的一个小组中,另外15名学生将在第二个小组中,依此类推,对于90名学生,我将有3个小组。这就是我需要知道的全部,如何做到这一点?