14 Scala 实现客户信息管理系统

客户信息管理系统

1. AppStart

package com.guli.chapter15.app

import com.guli.chapter15.customerView.CustomerView

object AppStart {
  def main(args: Array[String]): Unit = {
    (new CustomerView).meau()
  }
}

2. CustomerView

package com.guli.chapter15.customerView

import com.guli.chapter15.customerService.CustomerService

import scala.io.StdIn

class CustomerView {
  var customerService = new CustomerService()

  def meau(): Unit = {
    var flag = true
    do {
      println("-----------------客户信息管理软件-----------------")
      println("                  1 添 加 客 户                  ")
      println("                  2 修 改 客 户                  ")
      println("                  3 删 除 客 户                  ")
      println("                  4 客 户 列 表                  ")
      println("                  5 查 询 客 户                   ")
      println("                  6 退      出                  ")
      println("请选择(1-6):                  ")

      var num = StdIn.readInt()

      num match {
        case 1 => addClient()
        case 2 => updateCli()
        case 3 => deleteCli()
        case 4 => showList()
        case 5 => searchCli()
        case 6 => exitApp()
      }
    } while (flag)

    // 退出程序
    def exitApp(): Unit = {
      print("确认是否退出Y/N:")
      val r = StdIn.readLine()
      if (r.equals("Y") || r.equals("y")) {
        flag = false
        println("成功退出程序!Bye~~")
      }
    }
  }

  // 显示客户列表
  def showList(): Unit = {
    println("编号\t\t" + "姓名\t\t" + "性别\t\t" + "年龄\t\t" + "电话\t\t" + "邮箱")
    customerService.showList(customerService.customers)
  }

  // 添加客户
  def addClient(): Unit = {
    customerService.addClient()
  }

  // 删除客户
  def deleteCli() {
    print("请输入要删除的客户编号(-1退出):")
    val id = StdIn.readInt()
    if (id == -1) {
      return
    }
    val index = customerService.findIndexById(id)
    if (index != -1) {
      print("确认是否删除Y/N:")
      val r = StdIn.readLine()
      if (r.equals("Y") || r.equals("y")) {
        customerService.deleteCli(index)
        println("删除成功!")
        return
      }
    }
    println("删除失败!")
  }

  // 修改客户信息
  def updateCli(): Unit = {
    print("请输入要修改的客户的编号:")
    val id = StdIn.readInt()
    val index = customerService.findIndexById(id)
    if (index == -1) {
      print("查无此人!")
      return
    }
    customerService.updateCli(index)
  }

  // 查询客户
  def searchCli(): Unit = {
    print("请选择查询方式(1:根据id , 2:根据名字)")
    val way = StdIn.readInt()
    customerService.serchCli(way)
  }

}

3. CustomerService

package com.guli.chapter15.customerService

import com.guli.chapter15.customer.Customer

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.io.StdIn

class CustomerService {
  var customers = new ListBuffer[Customer]()
  var id = 0
  // customers.append(new Customer(1,"zgl",'男',23,"180","zgl@163.com"))

  // 显示客户列表
  def showList(customers: ListBuffer[Customer]): Unit = {
    for (item <- customers) {
      println(item)
    }
  }

  // 添加客户
  def addClient(): Unit = {
    id += 1
    print("请输入客户姓名:")
    val name = StdIn.readLine()
    print("请输入客户性别:")
    val sex = StdIn.readChar()
    print("请输入客户年龄:")
    val age = StdIn.readShort()
    print("请输入客户电话:")
    val tel = StdIn.readLine()
    print("请输入客户邮箱:")
    val mail = StdIn.readLine()
    customers.append(new Customer(id, name, sex, age, tel, mail))
  }

  // 查找客户
  def findIndexById(id: Int): Int = {
    var index = 0
    for (item <- customers) {
      if (item.id == id) return index
    }
    -1
  }

  // 删除客户
  def deleteCli(index: Int): Unit = {
    customers.remove(index)
  }

  // 修改客户信息
  def updateCli(index: Int) {

    val cus = customers(index)
    print(s"姓名(${cus.name}):")
    val name = StdIn.readLine()
    if (name != "") cus.name = name

    print(s"性别(${cus.sex}):")
    val sex = StdIn.readLine()
    if (sex != "") cus.sex = sex.toCharArray()(0)

    print(s"年龄(${cus.age}):")
    val age = StdIn.readLine()
    if (age != "") cus.age = age.toShort

    print(s"电话(${cus.tel}):")
    val tel = StdIn.readLine()
    if (tel != "") cus.tel = tel

    print(s"邮箱(${cus.mail}):")
    val mail = StdIn.readLine()
    if (mail != "") cus.mail = mail

    customers.update(index, cus)
    println("修改成功!")
  }

  // 查询客户
  def serchCli(way: Int) {
    // 根据id查询
    if (way == 1) {
      print("请输入id:")
      val id = StdIn.readInt()
      for (item <- customers) {
        if (item.id == id) {
          println("编号\t\t" + "姓名\t\t" + "性别\t\t" + "年龄\t\t" + "电话\t\t" + "邮箱")
          println(item)
        }
      }
    } else if (way == 2) {
      // 根据姓名查询
      print("请输入名字:")
      val name = StdIn.readLine()
      for (item <- customers) {
        if (item.name.contains(name)) {
          println("编号\t\t" + "姓名\t\t" + "性别\t\t" + "年龄\t\t" + "电话\t\t" + "邮箱")
          println(item)
        }
      }
    }
    else {
      println("输入有误!")
    }

  }
}

4. Customer

package com.guli.chapter15.customer

class Customer {
  var id: Int = _
  var name: String = _
  var sex: Char = _
  var age: Short = _
  var tel: String = _
  var mail: String = _

  def this(id: Int, name: String, sex: Char, age: Short, tel: String, mail: String) {
    this
    this.id = id
    this.name = name
    this.sex = sex
    this.age = age
    this.tel = tel
    this.mail = mail
  }

  override def toString: String = {
    this.id + "\t\t" + this.name + "\t\t" + this.sex + "\t\t" + this.age + "\t\t" + this.tel + "\t\t" + this.mail
  }

}
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
 这本书绝不轻易放过每个知识点,全书包含有大量习题,要求你自己实现 Scala 标准库或者 Scalaz 中的既有功能。所以,当你读完本书,做完习题后,虽然你的应用开发能力并不会直接提升,但你会体会到构建函数式语言和框架时的难点和取舍,从而增进你的框架开发和语言设计的能力。   ——ThoughtWorks Lead Consultant 杨博   这本书所讲授的,正是基于 Scala 的函数式编程基础。基于 Scheme、Haskell 等老牌函数式语言的传统教材的问题在于,相关语言的语法和思维方式与读者现有的知识体系迥异,容易造成较为陡峭的入门门槛。此外,由于这些语言本身的实际应用机会不多,初学者也难以在实战中获得宝贵的直觉和经验。而在 Scala 的帮助下,这本书并不要求你抛开现有的思维方式另起炉灶,它所做的更像是为你现有的思维方式添砖加瓦,从而令你如虎添翼。   ——Spark committer from Databricks 连城   尽管函数式编程在近十多年用得越来越多,但市面上介绍其高阶特性的书却并不多。这本书在这方面是个重要的补充,它不仅仅面向 Scala 程序员,同样面向用任何编程语言开发的程序员,只要你充满好奇心。   ——挖财网首席架构师 王宏江   “让你洞察计算的本质。”   ——Martin Odersky, Scala的作者   “Scala和Java8开发者的函数式编程指南!”   ——William E. Wheeler, TekSystems   “本书向你展示了提升Scala技能的方法和理念,它已超过‘更好的Java’。”   ——Fernando Dobladez, Code54   “里面的练习有些挑战,很有趣,对你在真实世界中使用它很有益。”   ——Chris Nauroth, Hortonworks   “边干边学,而非只是阅读。”   ——Douglas Alan、Eli和Edythe L. Broad,哈佛和麻省理工学院
Scala 学生信息管理系统可以分为以下模块: 1. 学生信息录入模块:包括学生基本信息的录入,如姓名、性别、年龄、班级等; 2. 学生信息查询模块:可以按照学生的姓名、学号、班级等信息进行查询,同时支持模糊查询; 3. 学生成绩录入模块:包括学生的各项考试成绩的录入,如数学、英语、物理等; 4. 学生成绩查询模块:可以按照学生的姓名、学号、班级、科目等信息进行查询,同时支持成绩范围的筛选; 5. 数据库管理模块:对学生信息和成绩信息进行增、删、改、查等操作,同时支持数据备份和还原; 6. 用户管理模块:对系统用户进行管理,包括添加、删除、修改等操作,同时支持用户权限分级。 以下是一个简单的 Scala 学生信息管理系统实现: ```scala import scala.io.StdIn // 定义学生类 case class Student(name: String, id: Int, age: Int, gender: String, grade: String) // 定义成绩类 case class Score(id: Int, math: Double, english: Double, physics: Double) object StudentSystem { // 存储学生信息和成绩信息的两个 Map var students = Map[Int, Student]() var scores = Map[Int, Score]() // 显示菜单 def showMenu(): Unit = { println("=============================") println("| 1. 添加学生信息 |") println("| 2. 添加学生成绩 |") println("| 3. 查看学生信息 |") println("| 4. 查看学生成绩 |") println("| 5. 修改学生信息 |") println("| 6. 修改学生成绩 |") println("| 7. 删除学生信息 |") println("| 8. 删除学生成绩 |") println("| 9. 数据备份 |") println("| 10. 数据还原 |") println("| 11. 退出系统 |") println("=============================") } // 添加学生信息 def addStudent(): Unit = { print("请输入学生姓名:") val name = StdIn.readLine() print("请输入学生学号:") val id = StdIn.readInt() print("请输入学生年龄:") val age = StdIn.readInt() print("请输入学生性别:") val gender = StdIn.readLine() print("请输入学生班级:") val grade = StdIn.readLine() val student = Student(name, id, age, gender, grade) students += (id -> student) println("学生信息添加成功!") } // 添加学生成绩 def addScore(): Unit = { print("请输入学生学号:") val id = StdIn.readInt() print("请输入数学成绩:") val math = StdIn.readDouble() print("请输入英语成绩:") val english = StdIn.readDouble() print("请输入物理成绩:") val physics = StdIn.readDouble() val score = Score(id, math, english, physics) scores += (id -> score) println("学生成绩添加成功!") } // 查看学生信息 def viewStudent(): Unit = { print("请输入要查看的学生学号:") val id = StdIn.readInt() students.get(id) match { case Some(student) => println(student) case None => println("没有找到该学生!") } } // 查看学生成绩 def viewScore(): Unit = { print("请输入要查看的学生学号:") val id = StdIn.readInt() scores.get(id) match { case Some(score) => println(score) case None => println("没有找到该学生成绩!") } } // 修改学生信息 def modifyStudent(): Unit = { print("请输入要修改的学生学号:") val id = StdIn.readInt() students.get(id) match { case Some(student) => print("请输入学生姓名:") val name = StdIn.readLine() print("请输入学生年龄:") val age = StdIn.readInt() print("请输入学生性别:") val gender = StdIn.readLine() print("请输入学生班级:") val grade = StdIn.readLine() val newStudent = student.copy(name = name, age = age, gender = gender, grade = grade) students += (id -> newStudent) println("学生信息修改成功!") case None => println("没有找到该学生!") } } // 修改学生成绩 def modifyScore(): Unit = { print("请输入要修改的学生学号:") val id = StdIn.readInt() scores.get(id) match { case Some(score) => print("请输入数学成绩:") val math = StdIn.readDouble() print("请输入英语成绩:") val english = StdIn.readDouble() print("请输入物理成绩:") val physics = StdIn.readDouble() val newScore = score.copy(math = math, english = english, physics = physics) scores += (id -> newScore) println("学生成绩修改成功!") case None => println("没有找到该学生成绩!") } } // 删除学生信息 def deleteStudent(): Unit = { print("请输入要删除的学生学号:") val id = StdIn.readInt() students.get(id) match { case Some(student) => students -= id scores -= id println("学生信息删除成功!") case None => println("没有找到该学生!") } } // 删除学生成绩 def deleteScore(): Unit = { print("请输入要删除的学生学号:") val id = StdIn.readInt() scores.get(id) match { case Some(score) => scores -= id println("学生成绩删除成功!") case None => println("没有找到该学生成绩!") } } // 数据备份 def backup(): Unit = { val backupStudents = students val backupScores = scores println("数据备份成功!") } // 数据还原 def restore(): Unit = { students = backupStudents scores = backupScores println("数据还原成功!") } // 主函数 def main(args: Array[String]): Unit = { var flag = true while (flag) { showMenu() print("请选择操作:") val choice = StdIn.readInt() choice match { case 1 => addStudent() case 2 => addScore() case 3 => viewStudent() case 4 => viewScore() case 5 => modifyStudent() case 6 => modifyScore() case 7 => deleteStudent() case 8 => deleteScore() case 9 => backup() case 10 => restore() case 11 => flag = false case _ => println("输入有误,请重新输入!") } } } } ``` 以上代码只是一个简单的实现,可以根据实际需求进行扩展和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值