zoukankan      html  css  js  c++  java
  • 面向对象案例-学生信息管理系统V1.1

    1.学生类

    package com.qfedu.student.entity;
    
    /**
     * 学生类实体
     * 
     * @author GGGXXC
     *
     */
    public class Student {
        private int id;
        private String name;
        private int age;
        private char gender;
        private float score;
        
        
    
        public Student() {
        }
    
        public Student(String name, int age, char gender, float score) {
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.score = score;
        }
    
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public char getGender() {
            return gender;
        }
    
        public void setGender(char gender) {
            this.gender = gender;
        }
    
        public float getScore() {
            return score;
        }
    
        public void setScore(float score) {
            this.score = score;
        }
        
        
        /**
         * 
         * 先留着??????????????
         * 使用System.out.println打印展示Student类对象时
         * 是直接自动调用toString方法,展示该方法返回String字符串内容
         */
        
        @Override
        public String toString() {
            return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score
                    + "]";
        }
        
        
    }

    StudentManager类

    package com.qfedu.student.manager;
    
    import java.util.Scanner;
    
    import com.qfedu.student.entity.Student;
    
    public class StudentManager {
    
        /**
         * 私有化学生数组,数组不能对外任意公开,有且只针对于当前管理类使用 提供了两种构造方法,有参,无参 因暂时不能确定数组容量大小,暂时将数组初始化为null
         */
        private Student[] allStus = null;
    
        /**
         * 设置Student类的默认容量大小 类内调用 设为private属性 常量 设为final属性
         * 
         * 后期可能会创建较多的StudentManager对象,如果不加static每个对象就都会创建这个变量 加上static后所有对象共享这一个
         * 
         * private static final int DEFAULT_CAPACITY = 10;
         */
        private static final int DEFAULT_CAPACITY = 10;
    
        /**
         * 设置数组最大容量值,
         * 
         * private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
         */
        private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    
        /**
         * 当前数组中学生个数 采用成员变量修饰:因为后期可能涉及多个对象,这个变量不能让所有对象共有
         * 采用private修饰符修饰,因为只在类内操作,避免不必要的访问
         */
        private int size = 0;
    
        Scanner sc = new Scanner(System.in);
        boolean flagSort = false;
    
        /**
         * 学生id计数变量
         */
        private int count = 0;// 学生初始数量
    
        /**
         * 无参构造方法创建学生数组,如果用户未指定数组大小,则设为DEFAULT_CAPACITY
         */
        public StudentManager() {
            allStus = new Student[DEFAULT_CAPACITY];
        }
    
        /**
         * 有参构造方法,用用户传入的数组设置数组初始大小 对用户输入的数据进行检查,如果用户输入的初始大小小于0,或者大于int的最大值减去8,则报错
         * 
         * 现阶段针对异常先退出程序????????????????
         * 
         * @param initCapacity
         */
        public StudentManager(int initCapacity) {
            if (initCapacity < 0 || initCapacity > DEFAULT_CAPACITY) {
                /* 此处异常先退出程序 */
                System.exit(0);
            }
            allStus = new Student[DEFAULT_CAPACITY];
        }
    
        // 创建增加学生方法add
        // 权限修饰符采用public 方式 因为增加学生的操作要在类外进行
        // 传入学生对象
        // 返回值采用boolean类型,如果增加成功则返回true,否则返回false
    
        public boolean add(Student student) {
            // 在增加之前先判断数组大小
    
            if (size == allStus.length) {
                grow(size + 1);
            }
            student.setId(count + 1);
            count += 1;
            allStus[size] = student;
            size += 1;
            return true;
        }
    
        private void grow(int minCapacity) {
    
            // 1.获取原数组的大小
            int oldCapacity = allStus.length;
    
            // 2.计算得到新数组的大小,按1.5倍计算
    
            int newCapacity = oldCapacity + oldCapacity / 2;
    
            // 3.判断新数组长度是否满足最小容量
            if (minCapacity > newCapacity) {
                newCapacity = minCapacity;
            }
    
            // 4.判断当前容量是否超出MAX_ARRAY_SIZE
            // 超出的话目前先报异常
            if (newCapacity > MAX_ARRAY_SIZE) {
                System.exit(0);
            }
    
            // 5.创建新数组
            Student[] temp = new Student[newCapacity];
    
            // 6.数据拷贝
            for (int i = 0; i < oldCapacity; i++) {
                temp[i] = allStus[i];
    
            }
    
            // 7.使用新数组保存就数组
            allStus = temp;
    
        }
    
        /**
         * 根据id查找对应学生
         * 
         * @param id 要查找的学生id
         * @return 如果找到返回学生对象,否则返回null
         */
        public Student get(int id) {
            int index = findIndexById(id);
    
            return index > 0 ? allStus[index] : null;
        }
    
        /**
         * 类内私有方法,用于根据指定id查找对应下标位置
         * 
         * @param id 指定的id号
         * @return 返回对应下标,返回大于等于0表示已经找到,返回-1表示未找到
         */
        private int findIndexById(int id) {
            int temp = -1;
            for (int i = 0; i < size; i++) {
                if (id == allStus[i].getId()) {
                    temp = i;
                    break;
                }
            }
            return temp;
        }
    
        /**
         * 删除对应id的学生
         * 
         * @param id要删除的学生id
         * @return 删除成功返回true,否则返回false
         */
    
        public boolean remove(int id) {
            int index = findIndexById(id);
    
            if (-1 == index) {
                System.out.println("Not Found");
                return false;
            }
    
            /*
             * 如果id的值不是-1,则表示找到了需要删除的元素
             * 
             */
            for (int i = index; i < size - 1; i++) {
                allStus[i] = allStus[i + 1];
            }
            // 最后一个元素赋值为null
            allStus[size - 1] = null;
    
            // 有效元素-1
            size -= 1;
    
            return true;
        }
    
        /**
         * 展示学生数组
         */
        public void showAllStudents() {
            for (int i = 0; i < size; i++) {
                System.out.println(allStus[i]);
            }
        }
    
        // 0509;
    
        public void modify(int id) {
            Student stu = get(id);
    
            System.out.println("请输入要修改的内容:");
    
            System.out.println("1.修改姓名  2.修改年龄 3.修改性别 4.修改分数 5.退出");
    
            int choice = sc.nextInt();
            sc.nextLine();// 是不是所有的nextInt处都要加?
    
            while (choice != 5) {
                switch (choice) {
    
                case 1:
                    System.out.println("请输入学生姓名");
                    String name = sc.next();
                    stu.setName(name);
                    break;
                case 2:
                    System.out.println("请输入学生年龄");
                    int age = sc.nextInt();
                    stu.setAge(age);
                    break;
    
                case 3:
                    System.out.println("请输入学生性别");
                    char gender = sc.next().charAt(0);
                    stu.setGender(gender);
                    break;
                case 4:
                    System.out.println("请输入学生分数");
                    float score = sc.nextFloat();
                    stu.setScore(score);
                    break;
                case 5:
                    System.out.println("退出");
                    break;
                default:
                    System.out.println("输入错误");
                    break;
    
                }
    
                System.out.println("请输入要修改的内容:");
    
                System.out.println("1.修改姓名  2.修改年龄 3.修改性别 4.修改分数 5.退出");
                choice = sc.nextInt();
            }
    
        }
    
        /**
         * 根据学生对象的成绩进行排序
         */
        public void sortSelect() {
            System.out.println("请选择: 
    1.升序排序	2.降序排序");
            int choice = sc.nextInt();
    
            switch (choice) {
            case 1:
                flagSort = true;
                break;
            case 2:
                flagSort = false;
                break;
            }
            sort(flagSort);
        }
    
        /**
         * 排序算法
         * 
         * @param flagSort true升序排序,false降序排序
         */
        private void sort(boolean flagSort) {
            Student[] stusTemp = new Student[size];
            for (int i = 0; i < size; i++) {
                stusTemp[i] = allStus[i];
            }
    
            for (int i = 0; i < size - 1; i++) {
    
                int index = i;
    
                for (int j = i + 1; j < size; j++) {
    
                    if (flagSort) {
                        if (stusTemp[index].getScore() > stusTemp[j].getScore()) {
                            index = j;
                        }
                    } else {
                        if (stusTemp[index].getScore() < stusTemp[j].getScore()) {
                            index = j;
                        }
                    }
                }
                if (i != index) {
                    Student temp = stusTemp[i];
                    stusTemp[i] = stusTemp[index];
                    stusTemp[index] = temp;
                }
            }
    
            for (int i = 0; i < size; i++) {
                System.out.println(stusTemp[i]);
            }
        }
    
        /**
         * 提示用户输入信息,生成学生对象
         */
        public void gene() {
    
            System.out.println("请输入姓名:");
            String name = sc.next();
    
            System.out.println("请输入年龄:");
            int age = sc.nextInt();
            System.out.println("请输入性别:");
            char gender = sc.next().charAt(0);
            System.out.println("请输入分数:");
            float score = sc.nextFloat();
    
            Student stuTemp = new Student(name, age, gender, score);
            add(stuTemp);
        }
    
        /**
         * 展示菜单 简化主方法界面
         */
        public void showMenu() {
            Scanner sc = new Scanner(System.in);
            int choice = -1;
            do {
                System.out.println();
                System.out.println("1.插入学生  2.删除学生  3.修改学生  4.查找学生  5.分数排序  6.展示学生  7.退出");
                System.out.print("请输入你的操作:");
                choice = sc.nextInt();
                switch (choice) {
                case 1:
                    gene();
                    break;
                case 2:
                    System.out.print("请输入你要删除的id:");
                    int id = sc.nextInt();
                    remove(id);
                    break;
                case 3:
                    System.out.print("请输入你要修改的id:");
                    id = sc.nextInt();
    
                    modify(id);
                    break;
                case 4:
                    System.out.print("请输入你要查找的id:");
                    id = sc.nextInt();
                    Student stu = get(id);
                    System.out.println(stu);
                    break;
                case 5:
                    sortSelect();
                    break;
                case 6:
                    showAllStudents();
                    break;
                case 7:
                    System.out.println("退出");
                    sc.close();
                    return;
                default:
                    System.out.println("输入错误");
                    break;
                }
            } while (choice != 7);
        }
    
        /**
         * 初始化学生信息
         */
        public void initial() {
            Student student = new Student("张0", 18, '男', 90F);
    
            add(student);
    
            add(new Student("张1", 26, '男', 91F));
            add(new Student("张2", 26, '男', 92F));
            add(new Student("张3", 26, '男', 93F));
            add(new Student("张4", 26, '男', 94F));
            add(new Student("张5", 26, '男', 95F));
            add(new Student("张6", 26, '男', 96F));
            add(new Student("张7", 26, '男', 97F));
            add(new Student("张8", 26, '男', 98F));
            add(new Student("张9", 26, '男', 99F));
    
        }
    }

    MainProject类

    package com.qfedu.student.mainproject;
    
    import java.util.Scanner;
    //import java.util.Arrays;
    //System.out.println(Arrays.toString(ns))
    
    import com.qfedu.student.entity.Student;
    import com.qfedu.student.manager.StudentManager;
    
    /**
     * @author GGGXXC
     *
     */
    public class MainProject {
        public static void main(String[] args) {
            
            StudentManager stuMeth = new StudentManager();
            
            stuMeth.initial();
            
            stuMeth.showMenu();
            
    
        }
    }
  • 相关阅读:
    FileUpload组件
    国际化
    dbutils
    BeanUtils
    c3p0连接池]
    JDBC代码模板
    JDBC基础与连接sql2012
    JSP以及JSP解析原理
    Tomcat使用,部署
    JAVA---反射
  • 原文地址:https://www.cnblogs.com/raising/p/12861343.html
Copyright © 2011-2022 走看看