用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

查找 增加 删除

2021-12-12 作者: GuanTou举报

[java]代码库

package textstudent;
import java.io.Serializable;
public class Student implements Serializable {
    private String id;
    private String name;
    private int eng;
    private int math;
    private int comp;
    private int sum;

    public Student(String id,String name,int eng,int math,int comp){
        this.id=id;
        this.name=name;
        this.eng=eng;
        this.math=math;
        this.comp=comp;
        sum();
    }

    public Student(Student s){
        this.id= s.id;
        this.name= s.name;
        this.eng=s.eng;
        this.math=s.math;
        this.comp=s.comp;
        sum();
    }
    public void setId(String id){
        this.id=id;
    }

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

    public void setEng(int eng) {
        this.eng = eng;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public void setComp(int comp) {
        this.comp = comp;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getEng() {
        return eng;
    }

    public int getMath() {
        return math;
    }

    public int getComp() {
        return comp;
    }

    public int getSum() {
        return sum;
    }

    void sum(){
        this.sum=eng+math+comp;
    }
    public String toString(){
        return getId()+"\t"+getName()+"\t"+getEng()+"\t"
                +getMath()+"\t"+getComp()+"\t"+getSum();
    }
    public boolean equals(Object x){
        if(this.getClass()!=x.getClass())
            return false;
        Student b=(Student) x;
        return (this.getId().equals(b.getId()));
    }

    public int compare(Student A){
        if(this.getSum()>A.getSum())
            return 1;
        else if(this.getSum()==A.getSum())
            return 0;
        else
            return -1;
    }
}
----------------------------------------------------------------------------
package textstudent;

public class StudentClass {
    private String name;
    static int capacity=40;
    private Student students[];
    private int size;

    public StudentClass(String name, int size){
        this.name=name;
        this.size=size;
        students=new Student[capacity];
    }

    public String getName() {
        return name;
    }

    public static int getCapacity() {
        return capacity;
    }

    public Student[] getStudents() {
        return students;
    }

    public int getsize() {
        return size;
    }

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

    public static void setCapacity(int capacity) {
        StudentClass.capacity = capacity;
    }

    public void setSize(int size) {
        if(size>capacity){
            System.out.println("size为"+size+",不能超过"+capacity);
            return;
        }
        this.size=size;
    }

    public void setStudents(Student... students) {
        for(int i=0;i<size;i++){
            this.students[i]=new Student(students[i]);
        }
        this.size=students.length;
    }
    public String toString(){
        String s;
        s="班级"+name+"\t"+"容量"+capacity+"\t"+"实际人数"+size+"\n\n";
        s=s+"学号"+"\t"+"姓名"+"\t"+"英语"+"\t"+"数学"+"\t"+"计算机"+"\t"+"总成绩\n";
        for(int i=0;i<size;i++){
            s=s+students[i].getId()+"\t"+students[i].getName()+"\t"+students[i].getEng()+"\t"+students[i].getMath()
                    +"\t"+students[i].getComp()+"\t"+students[i].getSum()+"\n";
        }
        return s;
    }
    //查找
    public int find(String id){
        for(int i=0;i<size;i++) {
            if (students[i].getId().equals(id))
                return i;
        }
        return -1;
    }
    //增加   
    public boolean add(Student aStudent){
        if(size==capacity)
            return false;
        if(find(aStudent.getId())>=0)
            return false;
        this.students[size]=new Student(new String(aStudent.getId()),new String(aStudent.getName()),
                aStudent.getEng(),aStudent.getMath(),aStudent.getComp());
        size++;
        return true;
    }
    //删除
    public boolean del(Student aStudent){
        int pos=find(aStudent.getId());
        if(pos==-1)
            return false;
        for(int i=pos+1;i<size;i++){
            students[i-1]=students[i];
        }
        size--;
        return true;
    }

    public boolean del(String id){
        int pos=find(id);
        if (pos==-1)
            return false;
        for(int i=pos+1;i<size;i++){
            students[i-1]=students[i];
        }
        size--;
        return true;
    }
}
--------------------------------------------------------
package textstudent;
import java.io.*;
public class Keyboard {
	static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
	public static int getInteger() {
		try {
			return(Integer.valueOf(inputStream.readLine().trim()).intValue());
		}
		catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	public static String getString() {
		try {
			return(inputStream.readLine());
		}
		catch (IOException e) {
			return "0";
		}
	}
}

-----------------------------------------------------------------
package textstudent;
import java.io.*;
public class Tester2 {
    public static void main(String[] args) throws IOException {
        Student students[] = new Student[5];
        //从文件stu.ser中读出学生信息
        try {
            FileInputStream fi = new FileInputStream("stu.ser");
            ObjectInputStream si = new ObjectInputStream(fi);
            for (int i = 0; i < 5; i++) {
                students[i] = (Student) si.readObject();
            }
            si.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        StudentClass aClass = new StudentClass("软件 0201", 5);
        aClass.setStudents(students);
        System.out.println(aClass);
        //查找 增加 删除测试
        for (int i = 0; i < 2; i++){
            System.out.println("输入查找的学号");
            String id = Keyboard.getString();
            if(aClass.find(id)<0){
                System.out.println("不存在!");
            }
            else{
                System.out.println("存在!");
            }
        }
        for(int i=0;i<2;i++){
            Student aStudent = getAStudent(6+i);
            if(aClass.add(aStudent)){
                System.out.println("添加成功");
            }
            else{
                System.out.println("不能添加");
            }
        }

        for(int i=0;i<2;i++){
            System.out.println("输入要删除的学号");
            String id = Keyboard.getString();
            if(aClass.del(id)){
                System.out.println("已删除");
            }else{
                System.out.println("不存在");
            }
        }
        System.out.println(aClass);
    }
    public static Student getAStudent(int i){
        Student studenti;
        System.out.println("输入第"+i+"个学生的信息:");
        System.out.print("学号:");
        String id = Keyboard.getString();
        System.out.print("姓名:");
        String name = Keyboard.getString();
        System.out.print("英语成绩:");
        int eng = Keyboard.getInteger();
        System.out.print("数学成绩:");
        int math = Keyboard.getInteger();
        System.out.print("计算机成绩:");
        int comp = Keyboard.getInteger();
        studenti = new Student(id,name,eng,math,comp);
        return studenti;
    }
}








[代码运行效果截图]


查找 增加 删除


网友评论    (发表评论)

共8 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...