用compose+ktor实现前后端分离计算器

用compose+ktor实现前后端分离计算器

这个作业属于哪个课程https://bbs.csdn.net/forums/ssynkqtd-05
这个作业要求在哪里https://bbs.csdn.net/topics/617377308
这个作业的目标实现前后端分离计算器
其他参考文献

0.PSP表格

PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划3035
• Estimate• 估计这个任务需要多少时间3035
Development开发14001165
• Analysis• 需求分析 (包括学习新技术)6055
• Design Spec• 生成设计文档6070
• Design Review• 设计复审3040
• Coding Standard• 代码规范 (为目前的开发制定合适的规范)3020
• Design• 具体设计3330
• Coding• 具体编码10001000
• Code Review• 代码复审2388
• Test• 测试(自我测试,修改代码,提交修改)4599
Reporting报告11100
• Test Repor• 测试报告34187
• Size Measurement• 计算工作量2389
• Postmortem & Process Improvement Plan• 事后总结, 并提出过程改进计划4399
合计12302010

1.成品展示

1.基础计算器功能

(1)基础运算
请添加图片描述

(2)清零回退
请添加图片描述

(3)错误提示
在这里插入图片描述

(4)历史记录
在这里插入图片描述

2.利率计算器

在这里插入图片描述

2.设计实现过程

1.前端

前端采用compose构建ui,基于mvi架构,

2.后端

后端采用ktor,exposed

ktor是由kotlin语言开发的轻量级后端框架,exposed是用kotlin编写的链接数据库的框架

3.数据库

数据库使用mysql数据库,MySQL 数据库易于使用,功能强大,支持触发器、存储的程序和可以更新的视图,受到了 Web 开发人员的青睐。

3.关键代码说明

ktor后端实现

routing {
        get("/") {
            call.respondText("Hello World!")
        }
        post("/calculate") {
            val response = call.receiveParameters()
            val calculationFormula = response["calculationFormula"] ?: ""
            val result = response["result"] ?: ""
            transaction(db){
                val stPete = Calculate.insert {
                    it[Calculate.calculationFormula] = calculationFormula
                    it[Calculate.result] = result
                } get Calculate.id
            }
            call.respond(
                mapOf<String,String>(
                    "state" to "ok"
                )
            )
        }
        get ("/history"){
            var stPete : List<History> = listOf()
            transaction(db){
                stPete = Calculate.selectAll().toList().map {
                    History(it[Calculate.result],it[Calculate.calculationFormula])
                }
            }
            call.respond(mapOf("list" to stPete))

        }

        get("/depositInterestRate"){
            call.respond(
                mapOf(
                    "data" to depositInterestRateWithClass
                )
            )
        }

        get("/interestRate"){
            call.respond(
                interestRate
            )
        }
    }

ktor前端请求发起

val response : HistoryResponse = client.get("http://127.0.0.1:8082/history").body()
        list.clear()
        response.list.forEach {
            list.add(Pair(it.calculationFormula,it.result))
        }

exposed规定数据库表

object Calculate : Table() {
    val id: Column<Int> = integer("id").autoIncrement()
    val calculationFormula: Column<String> = varchar("calculationFormula", 50)
    val result : Column<String> = varchar("result", 50 )
    override val primaryKey = PrimaryKey(id, name = "PK_Calculate_ID")
}


exposed连接mysql

 val db = Database.connect("jdbc:mysql://localhost:3306/test", driver = "com.mysql.cj.jdbc.Driver", user = "root", password = "***********")

代码仓库

后端

前端

4.心路历程和收获

由于ktor和exposed的简单操作,这次作业并不是很难,但是由于第一次并没有规划前后端分离,所以改了不少的代码,而且写了许多不优雅的代码,只能说仅仅实现了功能吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值