增强scala代码健壮性

原来的源代码CMS5在下面链接中

链接:https://pan.baidu.com/s/1gYk_nGQsW7g4lelKZaCang 
提取码:SgAb

val adminName = "admin"
var adminPwd = "123456"

在原有的CMS5代码中完善一下条件,使得代码更加完整

1.增加商品退货操作

条件:指定退货商品,指定退货数量,在浏览商品时库存有增加

2.完善商品出库操作

条件:判断库存是否足够,假如不够就剩余库存的商品全部卖出

3.完善退出登录逻辑问题

条件: 在 退出登录 后,应该返回上一层级 showLogin()

4.增加 修改管理员密码功能。

条件:密码长度不少于8位,必须包含大小写字母和特殊符号。

1、退货是指购买者退回商品,所以在浏览是库存有增加

退货部分代码

def tuihuoProduce(products: List[Product]) = {
    val tpName = readLine("退货商品名称:")
    val tpSum:Int = readLine("退货商品数量:").toInt
    products.map {
      case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity + tpSum)
      case Product(pName, pPrice, pQuantity)                    => Product(pName, pPrice, pQuantity)
    }.map {
      case Product(pName, pPrice, pQuantity) if pQuantity < 0   => Product(pName, pPrice, 0)
      case Product(pName, pPrice, pQuantity)               => Product(pName, pPrice, pQuantity)
    }

  }

2、出库中要实现判断库存是否足够,假如不够就剩余库存的商品全部卖出

出库部分代码

 // 商品出库操作函数
  def subProduct(products: List[Product]) = {
    val cpName = readLine("出库商品名称:")
    val cpQuantity = readLine("出库商品数量:").toInt
    // 使用模式匹配
    products.map {
      //          篮球,奶,手机
      case Product(pName, pPrice, pQuantity) if pName == cpName => Product(pName, pPrice, pQuantity - cpQuantity)
      case Product(pName, pPrice, pQuantity)                    => Product(pName, pPrice, pQuantity)
    }.map {
      case Product(pName, pPrice, pQuantity) if pQuantity < 0   => Product(pName, pPrice, 0)
      case Product(pName, pPrice, pQuantity)               => Product(pName, pPrice, pQuantity)
    }

  }

 3、在退出登录后,应该返回上一层级 showLogin()

case "7" =>
                println("您已退出登录")
                opFlag = false
                flag = true
                xun()
              case "0" =>
                println("您选择退出CMS系统")
                System.exit(0)

4、密码长度不少于8位,必须包含大小写字母和特殊符号

修改密码部分代码

就是普普通通的条件判断

case "6" =>
                val d = """([A-Z]+)""".r
                val x = """([a-z]+)""".r
                val t = """([!@#$%&*]+)""".r
                var bz=true
                while (bz) {
                  val mima = readLine("请输入新密码:")
                  if (d.findAllMatchIn(mima).size > 0 & x.findAllMatchIn(mima).size > 0 & t.findAllMatchIn(mima).size > 0 & mima.size >= 8) {
                    adminPwd = mima
                    println("修改成功")
                    bz = false
                  } else {
                    println("你的输入有误,请重新输入")
                  }
                }

修改后的源代码 

import scala.io.StdIn.readLine

object CMS {

  // 内置管理员信息(账号和密码)
  val adminName = "admin"
  var adminPwd = "123456"

  // 定义一个列表,用来存储所有的商品信息
  var products: List[Product] = Nil // 初始为空

  // 定义一个case class类,用来表示商品信息
  case class Product(pName: String, pPrice: Float, pQuantity: Int)

  // 显示管理员登录界面的函数
  def showLogin() = {
    // 管理员登录界面
    println("*************************************")
    println("*                                   *")
    println("*         CMS商品管理系统             *")
    println("*                                   *")
    println("* 请选择操作(输入操作对应的数字):       *")
    println("* 1. 管理员登录                       *")
    println("* 0. 退出系统                         *")
    println("*                                   *")
    println("*************************************")
  }

  // 显示管理员操作界面的函数
  def showOperation(username: String) = {
    println("*************************************")
    println("*                                    *")
    println(s"*       欢迎您,$username! ^_^        *")
    println("*                                    *")
    println("* 请选择操作(输入操作对应的数字):       *")
    println("* 1. 浏览商品信息                      *")
    println("* 2. 商品入库操作                      *")
    println("* 3. 商品出库操作                      *")
    println("* 4. 商品下架操作                      *")
    println("* 5. 商品退货操作                      *")
    println("* 6. 更改密码                        *")
    println("* 7. 退出登录                         *")
    println("* 0. 退出系统                         *")
    println("*                                   *")
    println("*************************************")
  }

  // 浏览商品信息函数
  def showProducts(products: List[Product]) = {
    if (products.isEmpty) {
      println("目前没有库存商品。")
    } else {
      products.foreach(println)
    }
  }

  // 商品入库操作函数
  def addProduct(products: List[Product]) = {
    val pName = readLine("入库商品名称:")
    val pPrice = readLine("入库商品单价:").toFloat
    val pQuantity = readLine("入库商品数量:").toInt

    // 构造一个商品实体
    val product = Product(pName, pPrice, pQuantity)
    // 将该商品加入到集合中(入库操作)
    products.:+(product)
  }

  // 商品出库操作函数
  def subProduct(products: List[Product]) = {
    val cpName = readLine("出库商品名称:")
    val cpQuantity = readLine("出库商品数量:").toInt
    // 使用模式匹配
    products.map {
      //          篮球,奶,手机
      case Product(pName, pPrice, pQuantity) if pName == cpName => Product(pName, pPrice, pQuantity - cpQuantity)
      case Product(pName, pPrice, pQuantity)                    => Product(pName, pPrice, pQuantity)
    }.map {
      case Product(pName, pPrice, pQuantity) if pQuantity < 0   => Product(pName, pPrice, 0)
      case Product(pName, pPrice, pQuantity)               => Product(pName, pPrice, pQuantity)
    }

  }

  // 商品下架操作
  def removeProduct(products: List[Product]) = {
    val tpName = readLine("下架商品名称:")

    // 使用模式匹配
    products.filter {
      case Product(pName, _, _) if pName == tpName => false
      case Product(pName, _, _)                    => true
    }
  }

//  商品退货操作
  def tuihuoProduce(products: List[Product]) = {
    val tpName = readLine("退货商品名称:")
    val tpSum:Int = readLine("退货商品数量:").toInt
    products.map {
      case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity + tpSum)
      case Product(pName, pPrice, pQuantity)                    => Product(pName, pPrice, pQuantity)
    }.map {
      case Product(pName, pPrice, pQuantity) if pQuantity < 0   => Product(pName, pPrice, 0)
      case Product(pName, pPrice, pQuantity)               => Product(pName, pPrice, pQuantity)
    }

  }
  def main(args: Array[String]) {
    xun()
  }
  def xun(): Unit ={


    // 任务1
    // 管理员登录界面
    showLogin()

    val op = readLine("\n请选择操作:")

    if (op == "1") {
      println("\n您选择管理员登录")

      // 任务2
      // 管理员登录操作
      var flag = true // 这是一个标志变量。true:继续输入账号和密码;false:结束登录过程

      while (flag) {

        val username = readLine("请输入账号:")
        val userpwd = readLine("请输入密码:")

        // 判断用户输入的账号和密码是否正确
        if (username == adminName && userpwd == adminPwd) {

          flag = false // 如果登录成功,则改变标志变量的值

          // 再定义一个控制管理员操作的标志变量
          var opFlag = true

          while (opFlag) {
            // 管理员操作界面
            showOperation(username)

            // 任务3
            // 选择操作选项
            val op2 = readLine("请选择您的操作(1-7):")

            // 使用简单模式匹配
            op2 match {
              case "1" =>
                println("您选择浏览商品信息。当前库存商品有:")
                showProducts(products)

              case "2" =>
                println("您选择商品入库操作。请按以下提示输入要入库的商品信息:")
                // 将该商品加入到集合中(入库操作)
                products = addProduct(products)

              case "3" =>
                println("您选择商品出库操作。请选择要出库的商品名称和数量:")
                // 出库操作
                products = subProduct(products)

              case "4" =>
                println("您选择商品下架操作。请选择要出库的商品名称:")
                // 下架操作
                products = removeProduct(products)

              case "5" =>
                println("您选择商品退货操作。请选择要出库的商品名称和数量:")
                // 退货操作
                products = tuihuoProduce(products)

              case "6" =>
                val d = """([A-Z]+)""".r
                val x = """([a-z]+)""".r
                val t = """([!@#$%&*]+)""".r
                var bz=true
                while (bz) {
                  val mima = readLine("请输入新密码:")
                  if (d.findAllMatchIn(mima).size > 0 & x.findAllMatchIn(mima).size > 0 & t.findAllMatchIn(mima).size > 0 & mima.size >= 8) {
                    adminPwd = mima
                    println("修改成功")
                    bz = false
                  } else {
                    println("你的输入有误,请重新输入")
                  }
                }
              case "7" =>
                println("您已退出登录")
                opFlag = false
                flag = true
                xun()
              case "0" =>
                println("您选择退出CMS系统")
                System.exit(0)
              case _ =>
                println("您的选择不正确。请重新选择正确的操作(1-5)!\n")
            }
          }
        } else {
          println("账号或密码错误!请重新登录。\n")
        }
      }
    } else if (op == "0") {
      println("\n欢迎再次登录!")
      System.exit(0)
    } else {
      println("\n您选择的操作不正确!")
    }
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值