定义一个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 && pQuantity > cpQuantity => Product(pName, pPrice, pQuantity - cpQuantity)//若出库数量小于库存则 减去出库数量
case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity <= cpQuantity => Product(pName, pPrice, pQuantity - pQuantity) //若出库数量大于等于库存则 库存全部出售
case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
}
}
商品退货操作
def removeProduct(products: List[Product]) = {
val tpName = readLine("退货商品名称:")
val tpQuantity = readLine("退货商品数量:").toInt
products.map {
// 篮球,奶,手机
case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity + tpQuantity) //判断要退货的名字是否符合输入名字 若true则增加对应数量tpQuantity
case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
// 使用模式匹配
}
}
商品下架操作
def deleteProduct(products:List[Product]) = {
val tpName = readLine("下架商品名称:")
// 使用模式匹配
products.filter {
case Product(pName, _, _) if pName == tpName => false
case Product(pName, _, _) => true
}
}
判断新密码是否包含去大小写字母
def isSpecialCharacterTable(string: String): Boolean = {
if (string.replaceAll("[u4e00-u9fa5]*[a-z]*[A-Z]*\d*-*_*\s*", "").length() == 0) {
return false
}
else {
return true
}
}
设置管理员账号和密码以及修改密码及选择的商品操作
def main(args: Array[String]) {
// 内置管理员信息(账号和密码)
val adminName = "admin"
var adminPwd = "123456"
// 定义一个列表,用来存储所有的商品信息
var products: List[Product] = Nil // 初始为空
// 任务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 = deleteProduct(products)
case "6" =>
println("您选择注销登录")
opFlag = false
flag = true
//退出登录
showLogin()
case "7" =>
println("您选择修改登陆密码。注意:(密码长度不少于8位,必须包含大小写字母和特殊符号。)")
var newPwd: String = readLine("请输入修改后的密码:")
var asdfasfd = true
while (asdfasfd){
println("所输入的密码不符合规则,请重新输入")
newPwd = readLine("请输入新的密码:")
val aaaaa :Boolean = isDigit(newPwd) & isSpecialCharacterTable(newPwd) & newPwd.length >= 8
//println(aaaaa)
if (aaaaa){
asdfasfd = false
}
}
adminPwd = newPwd
println("密码更新成功!!!")
println("新密码为:" + adminPwd)
case "0" =>
println("您选择退出CMS系统")
System.exit(0)
case _ =>
println("您的选择不正确。请重新选择正确的操作(1-7)!n")
}
}
} else {
println("账号或密码错误!请重新登录。n")
}
}
} else if (op == "0") {
println("n欢迎再次登录!")
System.exit(0)
} else {
println("n您选择的操作不正确!")
}
}
}
完整代码:
package com.scalaDemo
import sun.security.util.Password
import scala.io.StdIn.readLine
object cms5 {
// 定义一个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 && pQuantity > cpQuantity => Product(pName, pPrice, pQuantity - cpQuantity)//若出库数量小于库存则 减去出库数量
case Product(pName, pPrice, pQuantity) if pName == cpName && pQuantity <= cpQuantity => Product(pName, pPrice, pQuantity - pQuantity) //若出库数量大于等于库存则 库存全部出售
case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
}
}
// 商品退货操作
def removeProduct(products: List[Product]) = {
val tpName = readLine("退货商品名称:")
val tpQuantity = readLine("退货商品数量:").toInt
products.map {
// 篮球,奶,手机
case Product(pName, pPrice, pQuantity) if pName == tpName => Product(pName, pPrice, pQuantity + tpQuantity) //判断要退货的名字是否符合输入名字 若true则增加对应数量tpQuantity
case Product(pName, pPrice, pQuantity) => Product(pName, pPrice, pQuantity)
// 使用模式匹配
}
}
// 商品下架操作
def deleteProduct(products:List[Product]) = {
val tpName = readLine("下架商品名称:")
// 使用模式匹配
products.filter {
case Product(pName, _, _) if pName == tpName => false
case Product(pName, _, _) => true
}
}
//判断新密码是否包含数字和大小写英文
def isDigit(string: String): Boolean = {
if (string.replaceAll("[u4e00-u9fa5]*\d*-*_*\s*", "").length() == 0) {
false
}
else true
}
//判断新密码是否包含特殊字符
def isSpecialCharacterTable(string: String): Boolean = {
if (string.replaceAll("[u4e00-u9fa5]*[a-z]*[A-Z]*\d*-*_*\s*", "").length() == 0) {
return false
}
else {
return true
}
}
def main(args: Array[String]) {
// 内置管理员信息(账号和密码)
val adminName = "admin"
var adminPwd = "123456"
// 定义一个列表,用来存储所有的商品信息
var products: List[Product] = Nil // 初始为空
// 任务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 = deleteProduct(products)
case "6" =>
println("您选择注销登录")
opFlag = false
flag = true
//退出登录
showLogin()
case "7" =>
println("您选择修改登陆密码。注意:(密码长度不少于8位,必须包含大小写字母和特殊符号。)")
var newPwd: String = readLine("请输入修改后的密码:")
var asdfasfd = true
while (asdfasfd){
println("所输入的密码不符合规则,请重新输入")
newPwd = readLine("请输入新的密码:")
val aaaaa :Boolean = isDigit(newPwd) & isSpecialCharacterTable(newPwd) & newPwd.length >= 8
//println(aaaaa)
if (aaaaa){
asdfasfd = false
}
}
adminPwd = newPwd
println("密码更新成功!!!")
println("新密码为:" + adminPwd)
case "0" =>
println("您选择退出CMS系统")
System.exit(0)
case _ =>
println("您的选择不正确。请重新选择正确的操作(1-7)!n")
}
}
} else {
println("账号或密码错误!请重新登录。n")
}
}
} else if (op == "0") {
println("n欢迎再次登录!")
System.exit(0)
} else {
println("n您选择的操作不正确!")
}
}
}
运行结果:



