模拟斗地主和 学生管理系统 IO 版

1、模拟斗地主

public class PlayCards {
    public static void main(String[] args) {

        String[] color = {"黑桃", "梅花", "方片", "红桃"};
        String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        ArrayList cards = new ArrayList<>();

        //洗牌
        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < num.length; j++) {
                cards.add(color[i] + num[j]);
            }
        }
        cards.add("大王");
        cards.add("小王");

        Collections.shuffle(cards);

        ArrayList 林志玲 = new ArrayList<>();
        ArrayList 林心如 = new ArrayList<>();
        ArrayList 舒淇 = new ArrayList<>();

        //留下3张底牌
        for (int i = 0; i < cards.size() - 3; i++) {
            if (i % 3 == 0) {
                林志玲.add(cards.get(i));
            } else if (i % 3 == 1) {
                林心如.add(cards.get(i));
            } else if (i % 3 == 2) {
                舒淇.add(cards.get(i));
            }
        }

        System.out.println("林志玲:" + 林志玲);
        System.out.println("林心如:" + 林心如);
        System.out.println("舒淇:" + 舒淇);

        System.out.println("底牌:");
        for (int i = cards.size() - 3; i < cards.size(); i++) {
            System.out.println(cards.get(i));
        }
    }
}

2、学生管理系统 IO 版本

先在项目的根目录下创建 student.txt,用户存储用户输入的信息。

学生类:

    public class Student {
        
        private String id;
        private String name;
        private String age;
        private String address;
        
        // 空参和有参构造方法
        // getter、setter方法
        
    }


系统程序:

    public class StudentManagerIO {
        public static void main(String[] args) throws IOException {
    
            String fileName = "student.txt";
    
            //为了使主程序回到这里,使用循环
            while (true) {
                //学生管理系统主界面
                System.out.println("----------欢迎来到学生管理系统----------");
                System.out.println("1 查看所有学生");
                System.out.println("2 添加学生");
                System.out.println("3 删除学生");
                System.out.println("4 修改学生");
                System.out.println("5 退出系统");
                System.out.println("请输入你的选择:");
    
                //创建键盘录入对象
                Scanner scanner = new Scanner(System.in);
                String choiceString = scanner.nextLine();
    
                //使用 Switch语句
                switch (choiceString) {
                    case "1":
                        //查看所有学生
                        findAllStudent(fileName);
                        break;
                    case "2":
                        //添加学生
                        addStudent(fileName);
                        break;
                    case "3":
                        //删除学生
                        deleteStudent(fileName);
                        break;
                    case "4":
                        //修改学生
                        updateStudent(fileName);
                        break;
                    case "5":
                        //退出
                        System.out.println("谢谢你的使用");
                        System.exit(0); //JVM退出
                    default:
                        System.out.println("你的输入有误,请重新选择");
                }
            }
        }
    
        //从文件中读取数据到集合
        private static void readData(String fileName, ArrayList array) throws IOException {
    
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
    
            String line;
            while ((line = reader.readLine()) != null) {
                String[] datas = line.split(",");
                Student student = new Student();
                student.setId(datas[0]);
                student.setName(datas[1]);
                student.setAddress(datas[2]);
                student.setAddress(datas[3]);
    
                array.add(student);
            }
    
            reader.close();
        }
    
        //把集合中的数据写入文件
        private static void writeData(String fileName, ArrayList array) throws IOException {
    
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    
            for (int i = 0; i < array.size(); i++) {
                Student student = array.get(i);
                StringBuffer sb = new StringBuffer();
                sb.append(student.getId()).append(",").append(student.getName()).append(",")
                        .append(student.getAge()).append(",").append(student.getAddress());
    
                writer.write(sb.toString());
                writer.newLine();
                writer.flush();
            }
    
            writer.close();
        }
    
        //修改学生
        public static void updateStudent(String fileName) throws IOException {
    
            ArrayList array = new ArrayList<>();
            readData(fileName, array);
    
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你要修改的学生的学号:");
            String id = sc.nextLine();
    
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    index = i;
                    break;
                }
            }
    
            if (index == -1) {
                System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");
            } else {
                System.out.println("请输入学生新姓名:");
                String name = sc.nextLine();
                System.out.println("请输入学生新年龄:");
                String age = sc.nextLine();
                System.out.println("请输入学生新居住地:");
                String address = sc.nextLine();
    
                Student student = new Student();
                student.setId(id);
                student.setName(name);
                student.setAge(age);
                student.setAddress(address);
    
                array.set(index, student);
                writeData(fileName, array);
                System.out.println("修改成功!!!");
            }
        }
    
        //删除学生
        public static void deleteStudent(String fileName) throws IOException {
    
            ArrayList array = new ArrayList<>();
            readData(fileName, array);
    
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入需要删除的学生学号:");
            String id = scanner.nextLine();
    
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    index = i;
                    break;
                }
            }
    
            if (index == -1) {
                System.out.println("不好意思,你要删除的学生信息不存在,请重新选择");
            } else {
                array.remove(index);
                writeData(fileName, array);
                System.out.println("删除成功!!");
            }
        }
    
        //添加学生信息
        public static void addStudent(String fileName) throws IOException {
    
            ArrayList array = new ArrayList<>();
            //从文件中读取数据到集合
            readData(fileName, array);
    
            Scanner scanner = new Scanner(System.in);
            String id;
    
            while (true) {
                System.out.println("请输入学生学号:");
                id = scanner.nextLine();
                //判断学号是否被占用
                boolean flag = isExist(array, id);
                if (flag) {
                    System.out.println("对不起,你输入的学号被占用,请重新输入");
                } else {
                    break;
                }
            }
    
            System.out.println("请输入学生姓名:");
            String name = scanner.nextLine();
            System.out.println("请输入学生年龄:");
            String age = scanner.nextLine();
            System.out.println("请输入学生住址:");
            String address = scanner.nextLine();
    
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
    
            array.add(student);
            writeData(fileName, array);
            System.out.println("添加成功!!");
        }
    
        //判断学号是否被占用
        private static boolean isExist(List array, String id) {
            for (int i = 0; i < array.size(); i++) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    return true;
                }
            }
            return false;
        }
    
        //查看所有学生
        public static void findAllStudent(String fileName) throws IOException {
    
            ArrayList array = new ArrayList<>();
            readData(fileName, array);
    
            if (array.size() == 0) {
                System.out.println("不好意思,暂时没有学生信息可供查询,请重新选择");
                return;
            }
    
            System.out.println("学号\t姓名\t年龄\t居住地");
            array.forEach(student ->
                    System.out.println(student.getId() + "\t" + student.getName() + "\t" +
                            student.getAddress() + "\t" + student.getAddress()
                    )
            );
        }
    }

你可能感兴趣的:(模拟斗地主和 学生管理系统 IO 版)