• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Scala实现登录商品界面,指定退货商品,指定退货数量,在浏览商品时库存有增加2.完善商品出库操作 ,判断库存是否足够,假如不够就剩余库存的商品全部卖出,完善退出登录逻辑,增加修改密码

武飞扬头像
咖啡杯.
帮助1

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

显示管理员登陆界面函数

  1.  
    def showLogin() = {
  2.  
    // 管理员登录界面
  3.  
    println("***********************************")
  4.  
    println("* *")
  5.  
    println("* CMS商品管理系统 *")
  6.  
    println("* *")
  7.  
    println("* 请选择操作(输入操作对应的数字): *")
  8.  
    println("* 1. 管理员登录 *")
  9.  
    println("* 0. 退出系统 *")
  10.  
    println("* *")
  11.  
    println("***********************************")
  12.  
    }

显示管理员操作界面函数

  1.  
    def showOperation(username: String) = {
  2.  
    println("***********************************")
  3.  
    println("* *")
  4.  
    println(s"* 欢迎您,$username! ^_^ *")
  5.  
    println("* *")
  6.  
    println("* 请选择操作(输入操作对应的数字): *")
  7.  
    println("* 1. 浏览商品信息 *")
  8.  
    println("* 2. 商品入库操作 *")
  9.  
    println("* 3. 商品出库操作 *")
  10.  
    println("* 4. 商品退货操作 *")
  11.  
    println("* 5. 商品下架 *")
  12.  
    println("* 6. 退出登录 *")
  13.  
    println("* 7. 修改用户密码 *")
  14.  
    println("* 0. 退出系统 *")
  15.  
    println("* *")
  16.  
    println("***********************************")
  17.  
    }
学新通

浏览商品信息

  1.  
    def showProducts(products: List[Product]) = {
  2.  
    if (products.isEmpty) {
  3.  
    println("目前没有库存商品。")
  4.  
    } else {
  5.  
    products.foreach(println)
  6.  
    }
  7.  
    }

商品入库操作函数

  1.  
    def addProduct(products: List[Product]) = {
  2.  
    val pName = readLine("入库商品名称:")
  3.  
    val pPrice = readLine("入库商品单价:").toFloat
  4.  
    val pQuantity = readLine("入库商品数量:").toInt
  5.  
    // 构造一个商品实体
  6.  
    val product = Product(pName, pPrice, pQuantity)
  7.  
    // 将该商品加入到集合中(入库操作)
  8.  
    products.: (product)
  9.  
    }

商品出库操作函数

  1.  
    def subProduct(products: List[Product]) = {
  2.  
    val cpName = readLine("出库商品名称:")
  3.  
    val cpQuantity = readLine("出库商品数量:").toInt
  4.  
    products.map {
  5.  
    // 篮球,奶,手机
  6.  
    case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity > cpQuantity => Product(pName, pPrice, pQuantity - cpQuantity)//若出库数量小于库存则 减去出库数量
  7.  
    case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity <= cpQuantity => Product(pName, pPrice, pQuantity - pQuantity) //若出库数量大于等于库存则 库存全部出售
  8.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
  9.  
     
  10.  
     
  11.  
    /* // 使用模式匹配
  12.  
    products.map {
  13.  
    // 篮球,奶,手机
  14.  
    case Product(pName, pPrice, pQuantity) if pName == cpName => Product(pName, pPrice, pQuantity - cpQuantity)
  15.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)*/
  16.  
    }
  17.  
    }
学新通

商品退货操作

  1.  
    def removeProduct(products: List[Product]) = {
  2.  
    val tpName = readLine("退货商品名称:")
  3.  
    val tpQuantity = readLine("退货商品数量:").toInt
  4.  
    products.map {
  5.  
    // 篮球,奶,手机
  6.  
    case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity tpQuantity) //判断要退货的名字是否符合输入名字 若true则增加对应数量tpQuantity
  7.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
  8.  
    // 使用模式匹配
  9.  
    /* products.filter {
  10.  
    case Product(pName, _, _) if pName == tpName => false
  11.  
    case Product(pName, _, _) => true*/
  12.  
    }
  13.  
    }

商品下架操作

  1.  
    def deleteProduct(products:List[Product]) = {
  2.  
    val tpName = readLine("下架商品名称:")
  3.  
    // 使用模式匹配
  4.  
    products.filter {
  5.  
    case Product(pName, _, _) if pName == tpName => false
  6.  
    case Product(pName, _, _) => true
  7.  
    }
  8.  
    }

判断新密码是否包含去大小写字母

  1.  
    def isSpecialCharacterTable(string: String): Boolean = {
  2.  
    if (string.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
  3.  
    return false
  4.  
    }
  5.  
    else {
  6.  
    return true
  7.  
    }
  8.  
    }

设置管理员账号和密码以及修改密码及选择的商品操作

  1.  
    def main(args: Array[String]) {
  2.  
    // 内置管理员信息(账号和密码)
  3.  
    val adminName = "admin"
  4.  
    var adminPwd = "123456"
  5.  
    // 定义一个列表,用来存储所有的商品信息
  6.  
    var products: List[Product] = Nil // 初始为空
  7.  
    // 任务1
  8.  
    // 管理员登录界面
  9.  
    showLogin()
  10.  
    val op = readLine("\n请选择操作:")
  11.  
    if (op == "1") {
  12.  
    println("\n您选择管理员登录")
  13.  
    // 任务2
  14.  
    // 管理员登录操作
  15.  
    var flag = true // 这是一个标志变量。true:继续输入账号和密码;false:结束登录过程
  16.  
    while (flag) {
  17.  
    val username = readLine("请输入账号:")
  18.  
    val userpwd = readLine("请输入密码:")
  19.  
    // 判断用户输入的账号和密码是否正确
  20.  
    if (username == adminName && userpwd == adminPwd) {
  21.  
    flag = false // 如果登录成功,则改变标志变量的值
  22.  
    // 再定义一个控制管理员操作的标志变量
  23.  
    var opFlag = true
  24.  
    while (opFlag) {
  25.  
    // 管理员操作界面
  26.  
    showOperation(username)
  27.  
    // 任务3
  28.  
    // 选择操作选项
  29.  
    val op2 = readLine("请选择您的操作(1-7):")
  30.  
    // 使用简单模式匹配
  31.  
    op2 match {
  32.  
    case "1" =>
  33.  
    println("您选择浏览商品信息。当前库存商品有:")
  34.  
    showProducts(products)
  35.  
    case "2" =>
  36.  
    println("您选择商品入库操作。请按以下提示输入要入库的商品信息:")
  37.  
    // 将该商品加入到集合中(入库操作)
  38.  
    products = addProduct(products)
  39.  
    case "3" =>
  40.  
    println("您选择商品出库操作。请选择要出库的商品名称和数量:")
  41.  
    // 出库操作
  42.  
    products = subProduct(products)
  43.  
    case "4" =>
  44.  
    println("您选择商品退货操作。请选择要出库的商品名称和数量:")
  45.  
    // 退货操作
  46.  
    products = removeProduct(products)
  47.  
    case "5" =>
  48.  
    println("您选择商品下架操作。请选择要下架的商品名称和数量:")
  49.  
    //退货操作
  50.  
    products = deleteProduct(products)
  51.  
    case "6" =>
  52.  
    println("您选择注销登录")
  53.  
    opFlag = false
  54.  
    flag = true
  55.  
    //退出登录
  56.  
    showLogin()
  57.  
    case "7" =>
  58.  
    println("您选择修改登陆密码。注意:(密码长度不少于8位,必须包含大小写字母和特殊符号。)")
  59.  
    var newPwd: String = readLine("请输入修改后的密码:")
  60.  
    var asdfasfd = true
  61.  
     
  62.  
    while (asdfasfd){
  63.  
    println("所输入的密码不符合规则,请重新输入")
  64.  
    newPwd = readLine("请输入新的密码:")
  65.  
    val aaaaa :Boolean = isDigit(newPwd) & isSpecialCharacterTable(newPwd) & newPwd.length >= 8
  66.  
    //println(aaaaa)
  67.  
    if (aaaaa){
  68.  
    asdfasfd = false
  69.  
    }
  70.  
    }
  71.  
    adminPwd = newPwd
  72.  
    println("密码更新成功!!!")
  73.  
    println("新密码为:" adminPwd)
  74.  
     
  75.  
    case "0" =>
  76.  
    println("您选择退出CMS系统")
  77.  
    System.exit(0)
  78.  
    case _ =>
  79.  
    println("您的选择不正确。请重新选择正确的操作(1-7)!\n")
  80.  
    }
  81.  
    }
  82.  
    } else {
  83.  
    println("账号或密码错误!请重新登录。\n")
  84.  
    }
  85.  
    }
  86.  
    } else if (op == "0") {
  87.  
    println("\n欢迎再次登录!")
  88.  
    System.exit(0)
  89.  
    } else {
  90.  
    println("\n您选择的操作不正确!")
  91.  
    }
  92.  
    }
  93.  
    }
  94.  
     
学新通

完整代码:

  1.  
    package com.scalaDemo
  2.  
    import sun.security.util.Password
  3.  
    import scala.io.StdIn.readLine
  4.  
    object cms5 {
  5.  
    // 定义一个case class类,用来表示商品信息
  6.  
    case class Product(pName: String, pPrice: Float, pQuantity: Int)
  7.  
    // 显示管理员登录界面的函数
  8.  
    def showLogin() = {
  9.  
    // 管理员登录界面
  10.  
    println("***********************************")
  11.  
    println("* *")
  12.  
    println("* CMS商品管理系统 *")
  13.  
    println("* *")
  14.  
    println("* 请选择操作(输入操作对应的数字): *")
  15.  
    println("* 1. 管理员登录 *")
  16.  
    println("* 0. 退出系统 *")
  17.  
    println("* *")
  18.  
    println("***********************************")
  19.  
    }
  20.  
    // 显示管理员操作界面的函数
  21.  
    def showOperation(username: String) = {
  22.  
    println("***********************************")
  23.  
    println("* *")
  24.  
    println(s"* 欢迎您,$username! ^_^ *")
  25.  
    println("* *")
  26.  
    println("* 请选择操作(输入操作对应的数字): *")
  27.  
    println("* 1. 浏览商品信息 *")
  28.  
    println("* 2. 商品入库操作 *")
  29.  
    println("* 3. 商品出库操作 *")
  30.  
    println("* 4. 商品退货操作 *")
  31.  
    println("* 5. 商品下架 *")
  32.  
    println("* 6. 退出登录 *")
  33.  
    println("* 7. 修改用户密码 *")
  34.  
    println("* 0. 退出系统 *")
  35.  
    println("* *")
  36.  
    println("***********************************")
  37.  
    }
  38.  
    // 浏览商品信息函数
  39.  
    def showProducts(products: List[Product]) = {
  40.  
    if (products.isEmpty) {
  41.  
    println("目前没有库存商品。")
  42.  
    } else {
  43.  
    products.foreach(println)
  44.  
    }
  45.  
    }
  46.  
    // 商品入库操作函数
  47.  
    def addProduct(products: List[Product]) = {
  48.  
    val pName = readLine("入库商品名称:")
  49.  
    val pPrice = readLine("入库商品单价:").toFloat
  50.  
    val pQuantity = readLine("入库商品数量:").toInt
  51.  
    // 构造一个商品实体
  52.  
    val product = Product(pName, pPrice, pQuantity)
  53.  
    // 将该商品加入到集合中(入库操作)
  54.  
    products.: (product)
  55.  
    }
  56.  
    // 商品出库操作函数
  57.  
    def subProduct(products: List[Product]) = {
  58.  
    val cpName = readLine("出库商品名称:")
  59.  
    val cpQuantity = readLine("出库商品数量:").toInt
  60.  
    products.map {
  61.  
    // 篮球,奶,手机
  62.  
    case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity > cpQuantity => Product(pName, pPrice, pQuantity - cpQuantity)//若出库数量小于库存则 减去出库数量
  63.  
    case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity <= cpQuantity => Product(pName, pPrice, pQuantity - pQuantity) //若出库数量大于等于库存则 库存全部出售
  64.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
  65.  
     
  66.  
     
  67.  
    /* // 使用模式匹配
  68.  
    products.map {
  69.  
    // 篮球,奶,手机
  70.  
    case Product(pName, pPrice, pQuantity) if pName == cpName => Product(pName, pPrice, pQuantity - cpQuantity)
  71.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)*/
  72.  
    }
  73.  
    }
  74.  
    // 商品退货操作
  75.  
    def removeProduct(products: List[Product]) = {
  76.  
    val tpName = readLine("退货商品名称:")
  77.  
    val tpQuantity = readLine("退货商品数量:").toInt
  78.  
    products.map {
  79.  
    // 篮球,奶,手机
  80.  
    case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity tpQuantity) //判断要退货的名字是否符合输入名字 若true则增加对应数量tpQuantity
  81.  
    case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
  82.  
    // 使用模式匹配
  83.  
    /* products.filter {
  84.  
    case Product(pName, _, _) if pName == tpName => false
  85.  
    case Product(pName, _, _) => true*/
  86.  
    }
  87.  
    }
  88.  
    // 商品下架操作
  89.  
    def deleteProduct(products:List[Product]) = {
  90.  
    val tpName = readLine("下架商品名称:")
  91.  
    // 使用模式匹配
  92.  
    products.filter {
  93.  
    case Product(pName, _, _) if pName == tpName => false
  94.  
    case Product(pName, _, _) => true
  95.  
    }
  96.  
    }
  97.  
    //判断新密码是否包含数字和大小写英文
  98.  
    def isDigit(string: String): Boolean = {
  99.  
    if (string.replaceAll("[\u4e00-\u9fa5]*\\d*-*_*\\s*", "").length() == 0) {
  100.  
    false
  101.  
    }
  102.  
    else true
  103.  
    }
  104.  
    //判断新密码是否包含特殊字符
  105.  
    def isSpecialCharacterTable(string: String): Boolean = {
  106.  
    if (string.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
  107.  
    return false
  108.  
    }
  109.  
    else {
  110.  
    return true
  111.  
    }
  112.  
    }
  113.  
    def main(args: Array[String]) {
  114.  
    // 内置管理员信息(账号和密码)
  115.  
    val adminName = "admin"
  116.  
    var adminPwd = "123456"
  117.  
    // 定义一个列表,用来存储所有的商品信息
  118.  
    var products: List[Product] = Nil // 初始为空
  119.  
    // 任务1
  120.  
    // 管理员登录界面
  121.  
    showLogin()
  122.  
    val op = readLine("\n请选择操作:")
  123.  
    if (op == "1") {
  124.  
    println("\n您选择管理员登录")
  125.  
    // 任务2
  126.  
    // 管理员登录操作
  127.  
    var flag = true // 这是一个标志变量。true:继续输入账号和密码;false:结束登录过程
  128.  
    while (flag) {
  129.  
    val username = readLine("请输入账号:")
  130.  
    val userpwd = readLine("请输入密码:")
  131.  
    // 判断用户输入的账号和密码是否正确
  132.  
    if (username == adminName && userpwd == adminPwd) {
  133.  
    flag = false // 如果登录成功,则改变标志变量的值
  134.  
    // 再定义一个控制管理员操作的标志变量
  135.  
    var opFlag = true
  136.  
    while (opFlag) {
  137.  
    // 管理员操作界面
  138.  
    showOperation(username)
  139.  
    // 任务3
  140.  
    // 选择操作选项
  141.  
    val op2 = readLine("请选择您的操作(1-7):")
  142.  
    // 使用简单模式匹配
  143.  
    op2 match {
  144.  
    case "1" =>
  145.  
    println("您选择浏览商品信息。当前库存商品有:")
  146.  
    showProducts(products)
  147.  
    case "2" =>
  148.  
    println("您选择商品入库操作。请按以下提示输入要入库的商品信息:")
  149.  
    // 将该商品加入到集合中(入库操作)
  150.  
    products = addProduct(products)
  151.  
    case "3" =>
  152.  
    println("您选择商品出库操作。请选择要出库的商品名称和数量:")
  153.  
    // 出库操作
  154.  
    products = subProduct(products)
  155.  
    case "4" =>
  156.  
    println("您选择商品退货操作。请选择要出库的商品名称和数量:")
  157.  
    // 退货操作
  158.  
    products = removeProduct(products)
  159.  
    case "5" =>
  160.  
    println("您选择商品下架操作。请选择要下架的商品名称和数量:")
  161.  
    //退货操作
  162.  
    products = deleteProduct(products)
  163.  
    case "6" =>
  164.  
    println("您选择注销登录")
  165.  
    opFlag = false
  166.  
    flag = true
  167.  
    //退出登录
  168.  
    showLogin()
  169.  
    case "7" =>
  170.  
    println("您选择修改登陆密码。注意:(密码长度不少于8位,必须包含大小写字母和特殊符号。)")
  171.  
    var newPwd: String = readLine("请输入修改后的密码:")
  172.  
    var asdfasfd = true
  173.  
     
  174.  
    while (asdfasfd){
  175.  
    println("所输入的密码不符合规则,请重新输入")
  176.  
    newPwd = readLine("请输入新的密码:")
  177.  
    val aaaaa :Boolean = isDigit(newPwd) & isSpecialCharacterTable(newPwd) & newPwd.length >= 8
  178.  
    //println(aaaaa)
  179.  
    if (aaaaa){
  180.  
    asdfasfd = false
  181.  
    }
  182.  
    }
  183.  
    adminPwd = newPwd
  184.  
    println("密码更新成功!!!")
  185.  
    println("新密码为:" adminPwd)
  186.  
     
  187.  
    case "0" =>
  188.  
    println("您选择退出CMS系统")
  189.  
    System.exit(0)
  190.  
    case _ =>
  191.  
    println("您的选择不正确。请重新选择正确的操作(1-7)!\n")
  192.  
    }
  193.  
    }
  194.  
    } else {
  195.  
    println("账号或密码错误!请重新登录。\n")
  196.  
    }
  197.  
    }
  198.  
    } else if (op == "0") {
  199.  
    println("\n欢迎再次登录!")
  200.  
    System.exit(0)
  201.  
    } else {
  202.  
    println("\n您选择的操作不正确!")
  203.  
    }
  204.  
    }
  205.  
    }
  206.  
     
  207.  
     
  208.  
     
学新通

运行结果:

学新通

学新通 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgabeai
系列文章
更多 icon
同类精品
更多 icon
继续加载