【蒟蒻的Java代码记录系列】学生信息管理系统

该博客展示了如何使用Java读取文件中的学生信息,进行异常处理,实现Comparable接口进行内部排序,以及Comparator接口进行外部排序。同时,还包含字符串处理方法,如检查汉字、计算字符串宽度等。示例中详细定义了Student类,并提供了输入提示、显示格式化及文件读写功能。
摘要由CSDN通过智能技术生成

1.从文件中读取数据

File f=new File(......);Scanner sc=new Scanner(f);

2.异常处理

throws Exception

3.Comparable接口内置比较器 public int compareTo(Student s)

4.Comparator接口外置比较器(排序辅助类)public int compare(Student a,Student b)

import java.util.Scanner;
import java.time.LocalDate;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

class StringFormat{
    public static boolean isChinese(char c){//判断是否为汉字
        Character.UnicodeBlock ub=Character.UnicodeBlock.of(c);
        if(ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS||
           ub==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS||
           ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A||
           ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B||
           ub==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION||
           ub==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS||
           ub==Character.UnicodeBlock.GENERAL_PUNCTUATION)
        return true;
        return false;
    }

    public static int length(String s){//求s的实际占用宽度
        char[] chAr=s.toCharArray();
        int c=0;
        for(char x: chAr) c=(isChinese(x))?c+2:c+1;
        return c;
    }

    public static String repeat(char c,int n){ //将字符c重复n次
        if(n<=0) return null;
        String s="";
        for(int i=0;i<n;i++) s=s+c;
        return s;
    }

    public static String stringHead(String s,int len){//取s中从头开始的len个字符宽度
        char[] chAr=s.toCharArray();
        int c=0,i=0;
        for(;i<chAr.length&&c<len;i++) c=(isChinese(chAr[i]))?c+2:c+1;
        return s.substring(0,i);
    }

    public static String formatL(String s,int n,char c){
        //左对齐,将s补充成长度n的字符串,c是填充字符,补充在右部
        int len=length(s);
        if(len>=n) return stringHead(s,n);
        return s+repeat(c,n-len);
    }
}

class Student implements Comparable<Student>{
    String ID,name;
    char sex;
    boolean partyMember;
    double math,chinese;
    LocalDate birthday;

    static void titleHint(){//输入格式提示
        System.out.print("\n请输入一组学生,输入Ctrl+Z结束,格式为:");
        System.out.print("\n学号 姓名 性别 出生日期 党员 数学 语文,例如:");
        System.out.print("\n001 张三 M 2000-01-01 true 84.2 93.7\n");
    }

    static void showTitle(){
        System.out.print(" 学号   姓名   性别  出生日期  党员   数学  语文\n");
        System.out.print("----------------------------------------------\n");
    }

    void read(Scanner sc){//读取数据
        ID=sc.next(); name=sc.next();
        sex=sc.next().charAt(0);
        String bd=sc.next();
        birthday=LocalDate.parse(bd);
        partyMember=sc.nextBoolean();
        math=sc.nextDouble(); chinese=sc.nextDouble();
    }

    public String toString(){//自定义输出
        String id,xm,xb,dy,sx,yw;
        id=String.format("%-5s",ID);
        xm=StringFormat.formatL(name,8,' ');
        xb=(sex=='F'||sex=='f')?"女":((sex=='M'||sex=='m')?"男":" ? ");
        dy=(partyMember==true)?"共产党员":"非党员  ";
        sx=String.format("%-6.2f",math);
        yw=String.format("%-6.2f",chinese);
        return " "+id+" "+xm+" "+xb+" "+birthday+" "+dy+" "+sx+" "+yw;
    }

    public int compareTo(Student s){//会被Arrays.sort()自动调用
        if(this.ID.compareTo(s.ID)<0) return -1;
        else if(this.ID.equals(s.ID)) return 0;
        else return 1;
    }
}

class SortByMath implements Comparator<Student>{
    public int compare(Student a,Student b){
        if(a.math>b.math) return -1;
        else if(a.math<b.math) return 1;
        else return 0;
    }
}

class BanJi{
    final int maxN; Student[] st; int renShu;

    BanJi(int max){maxN=max; st=new Student[max]; renShu=0;}
    
    void add(Student s){//追加学生到数组内
        st[renShu]=s; renShu++;
    } 

    void readFromFile(File f) throws Exception{
        Scanner sc=new Scanner(f);
        sc.nextLine(); sc.nextLine(); //吃掉前两行的标题
        Student s;
        while(sc.hasNext()==true){
            s=new Student();
            s.read(sc);
            add(s);
        }
    }

    void append(){//输入一批学生
        Student.titleHint();
        Scanner sc=new Scanner(System.in);
        Student s;
        while(sc.hasNext()==true){
            s=new Student();
            s.read(sc);
            add(s);
        }
    }

    void show(){
        Student.showTitle();
        for(int i=0;i<renShu;i++){
            System.out.println(st[i]);
        }
        System.out.println("班级中共有 "+renShu+" 人。");
    }

    void sort(){
        Arrays.sort(st,0,renShu);
    }

    void sortByMath(){
        Arrays.sort(st,0,renShu,new SortByMath());
    }
}

class App{
    public static void main(String[] agrs) throws Exception{
        BanJi bj=new BanJi(50);
        File f;
        System.out.println("从文件读取数据······");
        f=new File("StudentInfo.txt");
        bj.readFromFile(f);
        System.out.println("班级信息如下:"); bj.show();
        System.out.print("\n按学号排序后:\n"); bj.sort(); bj.show();
        System.out.println("按数学成绩排序后"); bj.sortByMath(); bj.show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl AC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值