面向对象实践

1、 JAVA多线程实验–哲学家就餐问题

package caopeng.javase.小学期;
/*
    要求筷子只能被一个人拿起,当筷子被拿起时,那么要准备拿筷子的人只能进入等待
 */
public class Chopstick {
    private boolean use = false;
    // 每个筷子弄一个下标,便于调试
    private int index;

    public Chopstick() {
    }

    public Chopstick(int index) {
        this.index = index;
    }

    /**
     *   synchronized 声明在实例方法上是锁住了 this
     *   当筷子被别的人用了就等待
     */
    public synchronized void take() throws InterruptedException {
        while(use)
            wait();
        use =true;
    }

    /**
     *  当筷子用完了,就唤醒等待的人
     */
    public synchronized void drop(){
        use = false;
        notify();
    }

    @Override
    public String toString() {
        return "第 " + index+ " 号筷子";
    }

    public boolean getUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }
}
 package caopeng.javase.小学期;

import java.util.Random;

/*
    哲学家应该有两个方法,一个是思考,一个是拿筷子准备吃面
        对于思考时间和吃面时间我希望能弄一个随机数.思考和吃面的时间在一定的范围内随机取值
        哲学家先后拿左手和右手边的筷子
 */
public class Philosopher implements Runnable{
    private Chopstick letf;
    private Chopstick right;
    private int index;

    public Philosopher() {
    }

    public Philosopher(Chopstick letf, Chopstick right, int index) {
        this.letf = letf;
        this.right = right;
        this.index = index;
    }

    @Override
    public void run() {
        try {
            think();
            eat();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public  void eat() throws InterruptedException {
        letf.take();
        System.out.println(this + " 已经获得" + this.letf);
        right.take();
        System.out.println(this + " 已经获得" + this.right);
        System.out.println(this + " 开始吃面");
        Thread.sleep(time());
        letf.drop();
        right.drop();
        System.out.println(this + " 吃完面了");
    }

    public void think() throws InterruptedException {
        System.out.println(this + " 开始思考");
        Thread.sleep(0);
        System.out.println(this + " 思考结束");
    }

    public Long time(){
        Random random = new Random();
        Long time = Long.valueOf(random.nextInt(5));
        return time;
    }

    @Override
    public String toString() {
        return "第 " + index+ " 位哲学家";
    }
}

 package caopeng.javase.小学期;

public class TTT {
    public static void main(String[] args) {
        Chopstick[] chopsticks = new Chopstick[5];
        for(int i=0 ; i< 5 ;i++) {
            chopsticks[i] = new Chopstick(i);
        }
        Philosopher philosopher1 = new Philosopher(chopsticks[4],chopsticks[0],0);
        Philosopher philosopher2 = new Philosopher(chopsticks[0],chopsticks[1],1);
        Philosopher philosopher3 = new Philosopher(chopsticks[1],chopsticks[2],2);
        Philosopher philosopher4 = new Philosopher(chopsticks[2],chopsticks[3],3);
        Philosopher philosopher5 = new Philosopher(chopsticks[3],chopsticks[4],4);
        Thread thread1 = new Thread(philosopher1);
        Thread thread2 = new Thread(philosopher2);
        Thread thread3 = new Thread(philosopher3);
        Thread thread4 = new Thread(philosopher4);
        Thread thread5 = new Thread(philosopher5);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

(2)

package caopeng.javase.小学期;
/*
    要求筷子只能被一个人拿起,当筷子被拿起时,那么要准备拿筷子的人只能进入等待
 */
public class Chopstick {
    private boolean use = false;
    // 每个筷子弄一个下标,便于调试
    private int index;

    public Chopstick() {
    }

    public Chopstick(int index) {
        this.index = index;
    }

    /**
     *   synchronized 声明在实例方法上是锁住了 this
     *   当筷子被别的人用了就等待
     */
    public synchronized void take() throws InterruptedException {
        while(use)
            wait();
        use =true;
    }

    /**
     *  当筷子用完了,就唤醒等待的人
     */
    public synchronized void drop(){
        use = false;
        notify();
    }

    @Override
    public String toString() {
        return "第 " + index+ " 号筷子";
    }

    public boolean getUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }
}
package caopeng.javase.小学期;

import java.util.Random;


/*
    哲学家应该有两个方法,一个是思考,一个是拿筷子准备吃面
        对于思考时间和吃面时间我希望能弄一个随机数.思考和吃面的时间在一定的范围内随机取值
        哲学家先后拿左手和右手边的筷子

    每个哲学家必须确定自己左右手的筷子都可用的时候,才能同时拿起两只筷子进餐,吃完之后同时放下两只筷子
 */
public class PhilosopherTest implements Runnable{
    private Chopstick letf;
    private Chopstick right;
    private int index;

    public PhilosopherTest() {
    }

    public PhilosopherTest(Chopstick letf, Chopstick right, int index) {
        this.letf = letf;
        this.right = right;
        this.index = index;
    }

    @Override
    public void run() {
        try {
            think();
            eat();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public  void eat() throws InterruptedException {
        if ( !letf.getUse() && !right.getUse()){
            letf.take();
            System.out.println(this + " 已经获得" + this.letf);
            right.take();
            System.out.println(this + " 已经获得" + this.right);
            System.out.println(this + " 开始吃面");
            Thread.sleep(time());
            letf.drop();
            right.drop();
            System.out.println(this + " 吃完面了");
        }
        else {
            Thread.sleep(3 * 1000);
            eat();
        }
    }

    public void think() throws InterruptedException {
        System.out.println(this + " 开始思考");
        Thread.sleep(0);
        System.out.println(this + " 思考结束");
    }

    public Long time(){
        Random random = new Random();
        Long time = Long.valueOf(random.nextInt(5));
        return time;
    }

    @Override
    public String toString() {
        return "第 " + index+ " 位哲学家";
    }
}

 package caopeng.javase.小学期;

public class TTTTT {
    public static void main(String[] args) {
        Chopstick[] chopsticks = new Chopstick[5];
        for(int i=0 ; i< 5 ;i++) {
            chopsticks[i] = new Chopstick(i);
        }
        PhilosopherTest philosopher1 = new PhilosopherTest(chopsticks[4],chopsticks[0],0);
        PhilosopherTest philosopher2 = new PhilosopherTest(chopsticks[0],chopsticks[1],1);
        PhilosopherTest philosopher3 = new PhilosopherTest(chopsticks[1],chopsticks[2],2);
        PhilosopherTest philosopher4 = new PhilosopherTest(chopsticks[2],chopsticks[3],3);
        PhilosopherTest philosopher5 = new PhilosopherTest(chopsticks[3],chopsticks[4],4);
        Thread thread1 = new Thread(philosopher1);
        Thread thread2 = new Thread(philosopher2);
        Thread thread3 = new Thread(philosopher3);
        Thread thread4 = new Thread(philosopher4);
        Thread thread5 = new Thread(philosopher5);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

在这里插入图片描述
在这里插入图片描述

2、 同学档案管理系统

package caopeng.javase.小学期;

import java.util.Objects;

public class Student implements Comparable<Student>{
    // 学生属性
    private String no;
    private String name;
    private String age;
    private String classno;

    // 构造方法
    public Student() {
    }

    public Student(String no, String name, String age, String classno) {
        this.no = no;
        this.name = name;
        this.age = age;
        this.classno = classno;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                age == student.age &&
                Objects.equals(name, student.name) &&
                Objects.equals(classno, student.classno);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name, age, classno);
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", classno='" + classno + '\'' +
                '}';
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getClassno() {
        return classno;
    }

    public void setClassno(String classno) {
        this.classno = classno;
    }



    /**
     *    放入TreeSet里面要写compareTo方法
     * @param o
     * @return
     */
    @Override
    public int compareTo(Student o) {
        return Integer.parseInt(this.getNo()) - Integer.parseInt(o.getNo());
    }
}
 package caopeng.javase.小学期;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

//A、信息录入,包括记录的追加和插入;
//B、信息删除;
//C、信息修改;
//D、信息排序和查询;
//E、信息的保存和加载;
//F、简单的说明和帮助。
public class Student_Management_System {
    // 向文件写入学生信息
    public static void write(ArrayList<Student> students, String filename) throws IOException {
        // 缓冲流,不需要准备char数组
        BufferedWriter bw=new BufferedWriter(new FileWriter(filename));
        for(int i=0;i< students.size();i++) {
            Student student =students.get(i);
            StringBuilder sb=new StringBuilder();
            sb.append(student.getNo()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getClassno()).append(",");
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        //  避免空指针异常
        if(bw != null){
            bw.close();
        }
    }

    // 从文件中读取学生信息
    /**
     * 此方法的目的是将文件中的学生信息全部导入学生集合中
     * @param students   学生集合
     * @param filename    文件名
     * @throws IOException
     */
    public static void read(ArrayList<Student> students,String filename) throws IOException {
        BufferedReader br=new BufferedReader(new FileReader(filename));
        String info = null;
        while((info = br.readLine()) != null){
            String[] infos =info.split(",");
            Student student = new Student();
            student.setNo(infos[0]);
            student.setName(infos[1]);
            student.setAge(infos[2]);
            student.setClassno(infos[3]);
            students.add(student);
        }
        if (br != null){
            br.close();
        }
    }

    /**
     *  先使用read方法,将文件内所有学生信息放入 students 学生集合中,然后遍历集合,看集合中的学生的学号是否与新同学的学号重复,重复即错误
     *  没有重复的话就输入学生信息,再将其追加到集合当中,最后写入文件
     * @param students   学生集合
     * @param filename    文件名
     * @throws IOException
     */
    // 添加学生信息
    public static void addStudentInfo(ArrayList<Student> students,String filename) throws IOException {
        read(students,filename);
        Scanner scanner =new Scanner(System.in);
        boolean flag = false;
        // 遍历原文件,看是否存在相同的学号,学号相同,认为学生相同
        System.out.println("请输入学生学号");
        String no = scanner.next();
        // 避免第一次出现空指针异常
        if (students.size() != 0){
            for(Student student : students){
                if (student.getNo().equals(no)){
                    // 有相等的学号
                    flag = true;
                    break;
                }
            }
        }
        if (flag){
            System.out.println("你输入的学号有问题,与之前的学生重复了,请重新输入");
        }else {
            System.out.println("请输入学生姓名");
            String name = scanner.next();
            System.out.println("请输入学生年龄");
            String age = scanner.next();
            System.out.println("请输入学生班级");
            String classNo = scanner.next();
            Student student = new Student(no,name,age,classNo);
            students.add(student);
            write(students,filename);
        }
    }

    /**
     * 删除的实现是依靠 集合的 remove 方法,通过遍历比较学生的学号,得到学生对象的下标
     * @param students
     * @param filename
     * @throws IOException
     */
    // 删除学生信息
    public static void delStudentInfo(ArrayList<Student> students,String filename) throws IOException {
        read(students,filename);
        Scanner scanner =new Scanner(System.in);
        boolean flag = false;
        System.out.println("请输入你要删除的学生的学号");
        // 遍历原文件,看是否存在此学号
        System.out.println("请输入学生学号");
        String no = scanner.next();
        int index = 0;
        int num = 0;
        for (;index <= students.size();index ++ ){
            if (students.get(index).getNo().equals(no)){
                flag = true;
                num = index;
                break;
            }
        }
        if (flag){
            students.remove(num);
        }else {
            System.out.println("删除失败,没有这个学生");
        }
        write(students,filename);
        System.out.println("删除成功");
    }


    /**
     * 此方法只能查询一个学生的信息
     * @param students
     * @param filename
     * @throws IOException
     */
    // 查看某个学生的信息
    public static void getOneStudentInfo(ArrayList<Student> students,String filename) throws IOException {
        read(students,filename);
        Scanner scanner = new Scanner(System.in);
        boolean flag = false;
        System.out.println("输入你要查找的学生的学号");
        String no = scanner.next();
        int index = 0;
        int num = 0;
        for (;index <= students.size();index ++ ){
            if (students.get(index).getNo().equals(no)){
                flag = true;
                num = index;
                break;
            }
        }
        if (flag){
            System.out.println(students.get(num));
        }else {
            System.out.println("查询错误,没有这个学生");
        }
    }

    /**
     * 先将文集内的学生信息导入ArrayList中,再遍历ArrayList,将信息导入TreeSet中,TreeSet集合能根据compareto 方法进行排序
     * @param students
     * @param filename
     * @throws IOException
     */
    // 查看所有学生的信息
    public static void getAllStudent(ArrayList<Student> students,String filename) throws IOException {
        TreeSet<Student> treeSet = new TreeSet<>();
        read(students,filename);
        for (Student student : students){
            treeSet.add(student);
        }
        for (Student student : treeSet){
            System.out.println(student);
        }
    }


    // 修改学生信息(学号不能改动)
    public static void updateStudentInfo(ArrayList<Student> students,String filename) throws IOException {
        read(students,filename);
        Scanner scanner = new Scanner(System.in);
        boolean flag = false;
        // 遍历原文件,看是否存在此学号
        System.out.println("输入你要修改信息的学生的学号");
        String no = scanner.next();
        int index = 0;
        int num = 0;
        for (;index <= students.size();index ++ ){
            if (students.get(index).getNo().equals(no)){
                flag = true;
                num = index;
                break;
            }
        }
        if (flag){
            System.out.println("请输入修改后的学生姓名");
            String name = scanner.next();
            System.out.println("请输入输入修改后的学生年龄");
            String age = scanner.next();
            System.out.println("请输入输入修改后的学生班级");
            String classNo = scanner.next();
            Student student = students.get(num);
            student.setAge(age);
            student.setName(name);
            student.setClassno(classNo);
            students.set(num,student);
        }else {
            System.out.println("学号输入错误,无此学生");
        }
        write(students,filename);
    }
}
 package caopeng.javase.小学期;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> students = new ArrayList<>();
        String filename = "C:\\Users\\A556U\\JavaSE曹鹏\\javase作业\\src\\caopeng\\javase\\小学期\\学生信息";
        System.out.println("-----------------欢迎来到苏科大学生管理系统-------------------");
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println();
            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("输入数字 6 表示退出此系统");
            System.out.println("------------------------------------------");
            System.out.println();
            System.out.println("现在可以输入数字:");
            int num = scanner.nextInt();

            switch(num){
                case 1:
                    Student_Management_System.addStudentInfo(students,filename);
                    students.clear();
                    break;
                case 2:
                    Student_Management_System.delStudentInfo(students,filename);
                    students.clear();
                    break;
                case 3   :
                    Student_Management_System.getOneStudentInfo(students,filename);
                    students.clear();
                    break;
                case 4:
                    Student_Management_System.getAllStudent(students,filename);
                    students.clear();
                    break;
                case 5:
                    Student_Management_System.updateStudentInfo(students,filename);
                    students.clear();
                    break;
                case 6:
                    System.out.println("数据已保存,欢迎下次使用");
                    return;
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3、 设计一个简单的数据库(sqlserver或access),含有表student

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package javase.小学期;

import java.sql.*;

public class JDBCTest {
    public static void main(String[] args) {
        //JDBC编程六步
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet =null;
        try {
            // 获取驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            String url = "jdbc:mysql://localhost:3306/testdatabase";
            String username = "root";
            String password = "???";
            connection = DriverManager.getConnection(url,username,password);
            // 获取数据库操作对象
            statement = connection.createStatement();
            // 执行sql语句
            String sql = "select name,score from student";
            resultSet = statement.executeQuery(sql);
            // 处理结果集
            while (resultSet.next()){
                String name = resultSet.getNString("name");
                String score = resultSet.getString("score");
                System.out.println("姓名: "+name + " 分数: "+score);
            }
            // 关闭资源
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }if (connection != null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值