Kotlin实现简单的学生信息管理系统

一、实验内容

根据Android数据存储的内容,综合应用SharedPreferences和SQLite数据库实现一个用户信息管理系统,强化对SharedPreferences的理解的使用,熟练掌握SQLite的操作。要求:

  1. 巩固Android应用开发工具(Eclipse或者AndroidStudio)的常规用法;
  2. 巩固Activity、UI控件的常规用法;
  3. 掌握SharedPpreferences数据存储的使用;
  4. 掌握SQLite数据库及SQLiteOpenHelper的使用。

二、实验步骤

1、页面布局

本次布局提倡从简原则,按照往常习惯,我肯定是创建多个Activity,然后每个Activity设置下页面,分别从主页面跳转到各个页面。既然是实验,那就从简,实现核心的思想就可以了,底层逻辑实现出来,表面内容那不是花时间设计下就行了。言归正传,主页面布局如下,没有任何亮点可言,比较常规,只给Button和TextView都设置了background。

在这里插入图片描述
完整的layout代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/et_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学号"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:padding="10dp"
        android:layout_margin="20dp"
        android:inputType="text"
        android:background="@drawable/et_selector" />
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:padding="10dp"
        android:layout_margin="20dp"
        android:inputType="text"
        android:background="@drawable/et_selector" />
    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:padding="10dp"
        android:layout_margin="20dp"
        android:inputType="text"
        android:background="@drawable/et_selector" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_insert"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="添加"
            android:background="@drawable/btn_selector"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:textColor="@color/black"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="删除"
            android:background="@drawable/btn_selector"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:textColor="@color/black"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp">
        <Button
            android:id="@+id/btn_update"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="修改"
            android:background="@drawable/btn_selector"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:textColor="@color/black"/>
        <Button
            android:id="@+id/btn_query"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="查询"
            android:background="@drawable/btn_selector"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:textColor="@color/black"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/black"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="学号"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="姓名"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="年龄"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/black"/>
    <ListView
        android:id="@+id/lv_stu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

2、数据库

考查对SQLite的熟练程度,其实就是Android提供了一个数据库帮助类,帮我们进行数据库的各种操作。我们要做的就是建库建表,写个增删改查的方法,然后剩下的事情交给系统。这里是学生表的建表语句,一切属性都能用text表示。

    private val CREATE_STUDENT = "create table Student (" +
            "id text primary key," +
            "name text," +
            "age text)"

再看学生类,简直封装的太好了,Kotlin的魅力所在,换做Java又是属性、构造函数、get和set方法。

class Student(val id:String, val name:String, val age:String) {

}

下面看数据库的增删改查操作,所有的操作都是针对数据库的Student表来的,增加、删除和修改都很简单,使用ContentValues添加键值对。查询是最关键的,使用cursor游标一行行遍历表数据,各种约束条件可以自己加,正常全查就完事了。

 val dbHelper = DBHelper(context, "stu.db", 1)
    lateinit var db:SQLiteDatabase

    fun openDB() {
        db = dbHelper.writableDatabase
    }

    fun closeDB() {
        if (db != null) dbHelper.close()
    }
    // 插入学生
    fun insertStudent(stu: Student) {
        val values = ContentValues().apply {
            put("id", stu.id)
            put("name", stu.name)
            put("age", stu.age)
        }
        db.insert("Student", null, values)
    }
    // 删除学生
    fun deleteStudent(stu: Student) {
        db.delete("Student", "id = ?", arrayOf(stu.id))
    }
    // 更新学生
    fun updateStudent(stu: Student) {
        val values = ContentValues()
        values.put("name", stu.name)
        values.put("age", stu.age)
        db.update("Student", values, "id = ?", arrayOf(stu.id))
    }
    // 查询所有学生
    fun queryAllStudent():ArrayList<Student> {
        val cursor = db.query("Student", null, null, null, null, null, null)
        val stuList = ArrayList<Student>()
        if (cursor.moveToFirst()) {
            do {
                val id = cursor.getString(cursor.getColumnIndex("id"))
                val name = cursor.getString(cursor.getColumnIndex("name"))
                val age = cursor.getString(cursor.getColumnIndex("age"))
                val stu = Student(id, name, age)
                stuList.add(stu)
            } while (cursor.moveToNext())
        }
        cursor.close()
        return stuList
    }

3、登录活动

登录活动用的是sharedPreferences,它使用方法非常easy,首先初始化一个sharedPreferences对象,文件名和访问类型自定义。读数据就是调用getString获取键值对,设定个默认值。写数据就是调用sharedPreferences.edit()赋值给editor对象,然后putString读取键值对。还记录了下app的使用次数。

	override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE)
        var editor = sharedPreferences.edit()
        // 得到之前的使用次数,然后每次打开app都加1
        var count = sharedPreferences.getString("count", "0");
        binding.tvCount.text = (count!!.toInt() + 1).toString()
        // 保存键值对到sharedpreferences中
        editor.putString("count", (count!!.toInt() + 1).toString())
        editor.apply()
        binding.btnLogin.setOnClickListener{
            editor.putString("account", binding.etAccount.toString().trim())
            editor.putString("password", binding.etPassword.toString().trim())
            editor.apply()
            Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }

4、增删改查

其实对数据表的增删改查逻辑在StudentDao中已经封装好了,我们在Activity里面也只是调用方法实现界面和数据库的交互罢了。具体的操作逻辑如下:

输入学号、姓名和年龄后点击添加可以添加学生;输入学号点击查询可以查询学生信息,然后点击删除会删除信息,点击修改会修改输入框中的学生信息,最后如果输入的学号不存在而且你点查询了,会显示所有学生的信息,如果存在只会显示该学生的信息。

    override fun onClick(p0: View?) {
        var stuId = binding.etId.text.toString()
        var stuName = binding.etName.text.toString()
        var stuAge = binding.etAge.text.toString()
        var stu = Student(stuId, stuName, stuAge)
        var flag = (studentDao.queryById(stuId) != null)
        when(p0?.id) {
            R.id.btn_insert->{
                if (flag) {
                    Toast.makeText(this, "学生已存在,无法添加", Toast.LENGTH_SHORT).show()
                } else {
                    studentDao.insertStudent(stu)
                    Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show()
                }
            }
            R.id.btn_delete->{
                if (flag) {
                    studentDao.deleteStudent(stu)
                    Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "该学生不存在,无法删除", Toast.LENGTH_SHORT).show()
                }
            }
            R.id.btn_update->{
                if (flag) {
                    studentDao.updateStudent(stu)
                    Toast.makeText(this, "修改成功!", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "该学生不存在,无法修改", Toast.LENGTH_SHORT).show()
                }
            }
            R.id.btn_query->{
                if (flag) {// 如果存在则补全信息
                    binding.etAge.setText(studentDao.queryById(stuId)?.age)
                    binding.etName.setText(studentDao.queryById(stuId)?.name)
                    Toast.makeText(this, "查询到该学生信息", Toast.LENGTH_SHORT).show()
                } else {// 不存在则显示所有学生信息
                    studentList = studentDao.queryAllStudent()
                    adapter = StudentAdapter(this, R.layout.item_student, studentList)
                    binding.lvStu.adapter = adapter
                    Toast.makeText(this, "查询所有学生信息", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

三、运行演示

1、首先进入登录界面,输入账号和密码然后点击登录即可,右上角是使用次数。

在这里插入图片描述

2、进入主界面,我们输入学号、姓名和年龄进行添加学生。

在这里插入图片描述
3、点击添加按钮,添加成功。再依次添加几个学生。

在这里插入图片描述

4、点击查询,此时学号是不存在的,所以就查询了所有学生的信息。

在这里插入图片描述
5、我们输入学号4,然后点击查询,可以看到查询到信息并自动补全了。

在这里插入图片描述
6、修改姓名和年龄,然后再点修改,再点击查询,发现已经修改好了信息。

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

7、我们再查询小益的信息,然后删除小益的信息。
在这里插入图片描述
在这里插入图片描述

四、实验总结

其实学生系统涉及到数据库的操作完全和前面的其他系统相似,真正做起来还是比较繁琐的。哪里有什么容易代码,都是在一个个bug解决中完成的。理论引导实战,光理论只会纸上谈兵,光实践缺少方法论,基础打牢了才能进阶,不然上限不会高,基础决定了你的上限。

五、源码下载

关注公众号《 萌新加油站 》,后台回复: Kotlin学生
🔥源代码已上传CSDN,点击下载源代码🔥

🚀这有你错过的精彩内容🚀
Android Studio实现考试管理系统
Android Studio实现购物商城
Android Studio实现选课系统
Android Studio实现图书管理系统
Android Stduio实现外卖订餐系统
  • 33
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 25
    评论
Kotlin是近年来崛起的一种面向对象编程语言,也可以用来实现学生管理系统学生管理系统是一种常见的管理系统的应用场景,主要实现学生信息存储、查询、添加、修改和删除等基本功能。 在使用Kotlin实现学生管理系统时,我们可以采用MVC(模型视图控制器)设计的模式,将应用程序的不同部分分离开来,以实现松耦合的设计和优秀的可重用性。其中,模型用于处理业务逻辑和数据的存储,在Kotlin中可以使用类和对象来实现;视图用于呈现数据、接收用户输入、显示操作结果,在Kotlin中可以使用控制台或图形用户界面(GUI)来实现;控制器用于处理用户输入,调用模型并将结果返回到视图,Kotlin中可以使用函数来实现。 在Kotlin中,我们可以使用类和对象来存储学生信息,如姓名、性别、年龄、学号、专业、成绩等。使用集合类可以为学生信息提供更好的管理方式,如数组、链表或哈希表等。通过利用Kotlin的继承和接口机制,我们可以实现不同类之间的关系,如学生类继承自人类、管理员类实现管理员接口等,以使程序更具面向对象的特性。 在Kotlin中,我们可以使用条件语句、循环语句、函数和Lambda表达式等来实现学生管理系统的各个功能,例如,查询学生信息时可以通过遍历集合,并使用Lambda表达式来筛选符合条件的学生。同时,Kotlin还提供了许多库和框架,如Kotlinx.Serialization、Ktor和Exposed等,可以为学生管理系统提供更多实现方式和功能。 总之,借助Kotlin的强大功能和面向对象的特性,我们可以轻松实现一个高效、安全、易于维护和扩展的学生管理系统,可以为教育管理、招生、选课、成绩管理等场景提供支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

振华OPPO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值