Scala项目-客户信息管理系统

[toc]

## Scala项目-客户信息管理系统

### 1. 项目开发流程

![项目开发流程](assets/项目开发流程.png)

### 2. 项目需求分析

> 1. 模拟实现基于文本界面的《客户信息管理系统》。
> 2. 该系统Scala能够实现对客户对象的插入、修改和删除、显示,查询(用ArrayBuffer或者ListBuffer实现),并能够打印客户明细表。
> 3. 我们使用ListBuffer实现!

### 3. 项目界面

#### 主界面

![image-20210404210936216](assets/image-20210404210936216.png)

#### 添加客户界面

![image-20210404211059823](assets/image-20210404211059823.png)

#### 修改客户界面

![image-20210404211240175](assets/image-20210404211240175.png)

#### 删除客户界面

![image-20210404211433588](assets/image-20210404211433588.png)

#### 查询客户界面

![image-20210404211519874](assets/image-20210404211519874.png)

### 4. 项目框架图

> 程序框架图:设计系统有多少个文件,以及文件(一般来说,一个文件对应一个类)之间的调用关系,可以帮助程序员实现模块的设计(清晰),便于程序员之间对项目交流分析(业务,优化,设计方案)

![程序框架图](assets/程序框架图.png)

### 5. 项目功能实现

> 根据项目框架图依次实现功能模块

#### 数据层Customer

~~~~scala
package com.atguigu.chapter15.dao

/**
* @Date 2021/4/4 18:49
* @Version 10.21
* @Author DuanChaojie
*/
class Customer {
/**
* 客户信息
*/
var id: Int = _
var age: Int = _
var name: String = _
var gender: Char = _
var phone: String = _
var email: String = _

/**
* 辅助构造器
* @param id
* @param age
* @param name
* @param gender
* @param phone
* @param email
*/
def this(id: Int, age: Int, name: String, gender: Char, phone: String, email: String) {
this
this.id = id
this.age = age
this.name = name
this.gender = gender
this.phone = phone
this.email = email
}

/**
* 辅助构造器
* @param age
* @param name
* @param gender
* @param phone
* @param email
*/
def this(age: Int, name: String, gender: Char, phone: String, email: String) {
this
this.age = age
this.name = name
this.gender = gender
this.phone = phone
this.email = email
}

/**
* 重写toString方法
* @return
*/
override def toString: String = {
this.id + "\t\t" + this.age + "\t\t" + this.name + "\t\t" + this.gender + "\t\t" + this.phone + "\t\t" + this.email
}
}
~~~~

#### 业务逻辑层CustomerService

```scala
package com.atguigu.chapter15.service

import com.atguigu.chapter15.dao.Customer

import scala.collection.mutable.ListBuffer
import scala.io.StdIn
import util.control.Breaks._

/**
* @Date 2021/4/4 18:49
* @Version 10.21
* @Author DuanChaojie
*/
class CustomerService {

val customer1 = new Customer(1, 18, "tom", '男', "14725836995", "tom@qq.com")
val customer2 = new Customer(2, 18, "jac", '男', "15869951472", "jac@qq.com")

var customerInfo = new ListBuffer[Customer]()
var customerId = 3

// 添加数据
customerInfo.append(customer1)
customerInfo.append(customer2)

/**
* 获取顾客信息:
*/
def getCustomerInfo(): Unit = {
println(Console.BLUE + "客户列表:")
println("编号\t\t年龄\t\t姓名\t\t性别\t\t电话\t\t邮箱")
for (customer <- customerInfo) {
println(customer.toString)
}
println("客户列表显示完毕...\n.....................................")
}

/**
* 添加顾客信息
*/
def addCustomerInfo(): Unit = {
println(Console.GREEN + "添加客户:")
println("请依次输入用户信息:")
println("姓名:")
val name = StdIn.readLine()
println("年龄:")
val age = StdIn.readShort()
println("性别:")
val gender = StdIn.readChar()
println("电话:")
val tel = StdIn.readLine()
println("邮箱:")
val email = StdIn.readLine()
//构建对象
val customer = new Customer(customerId, age, name, gender, tel, email)
customerId += 1
customerInfo.append(customer)
println("添加客户信息完成!\n")
}

/**
* 删除顾客信息
*/
def delCustomerInfo(): Unit = {
println(Console.YELLOW + "删除客户操作:")
println("请选择待删除客户编号(-1退出):")
val id = StdIn.readInt()
if (id == -1) {
println("退出删除操作")
return
}
println("确认是否删除(Y/N):")
var choice = ' '
breakable {
do {
choice = StdIn.readChar().toLower
if (choice == 'y' || choice == 'n') {
break()
}
} while (true)
}

if (choice == 'y') {
if (del(id)) {
println("删除完成!")
return
}
}

println("删除失败!")
}

/**
* @param id 根据id删除用户信息
* @return
*/
def del(id: Int): Boolean = {
val index = findIndexById(id)
if (index == -1) {
false
} else {
customerInfo.remove(index)
true
}
}

/**
* 根据id找到customerInfo的index
*
* @param id
* @return
*/
def findIndexById(id: Int) = {
//默认-1,如果找到就改成对应,如果没有找到,就返回-1
var index = -1
// 遍历customerInfo
breakable {
for (i <- 0 until customerInfo.length) {
// 找到
if (customerInfo(i).id == id) {
index = i
break()
}
}
}
index
}

def updateCustomerInfo(): Unit = {
println(Console.CYAN + "修改客户操作:")
println("请选择待修改客户编号(-1退出):")
val id = StdIn.readInt()
if (id == -1) {
println("退出修改操作")
return
}
val index = findIndexById(id)
if (index == -1) {
println("请输入正确的客户id!")
return
} else {
println("请依次输入新的用户信息:")
println("姓名(" + customerInfo(index).name + "):")
customerInfo(index).name = StdIn.readLine()

println("年龄(" + customerInfo(index).age + "):")
customerInfo(index).age = StdIn.readShort()

println("性别(" + customerInfo(index).gender + "):")
customerInfo(index).gender = StdIn.readChar()

println("电话(" + customerInfo(index).phone + "):")
customerInfo(index).phone = StdIn.readLine()

println("邮箱(" + customerInfo(index).email + "):")
customerInfo(index).email = StdIn.readLine()
println("修改用户信息完成!")
return
}
}
}
```

#### ☆界面层CustomerView

```scala
package com.atguigu.chapter15.view

import com.atguigu.chapter15.service.CustomerService

import scala.io.StdIn

/**
* @Date 2021/4/4 18:49
* @Version 10.21
* @Author DuanChaojie
*/
class CustomerView {


var flag = true
private val customerService = new CustomerService()

/**
* 主菜单
*/
def mainMenu(): Unit = {
do {
println(Console.RED + "客户信息管理系统:")
println("\t1 添加客户")
println("\t2 修改客户")
println("\t3 删除客户")
println("\t4 查询客户")
println("\t5 退出系统")
println("\t请输入(1-5)__")

val operNum = StdIn.readInt()
operNum match {
case 1 => customerService.addCustomerInfo()
case 2 => customerService.updateCustomerInfo()
case 3 => customerService.delCustomerInfo()
case 4 => customerService.getCustomerInfo()
case 5 => flag = false
}
} while (flag)
println("已经退出系统....")
}


}
```

#### 程序入口CustomerMain

~~~~scala
/**
* @Date 2021/4/4 18:48
* @Version 10.21
* @Author DuanChaojie
*/
object CustomerMain {

def main(args: Array[String]): Unit = {
// 程序的入口
new CustomerView().mainMenu()
}
}
~~~~

## ☆

上一篇:Scala函数式编程高级


下一篇:Scala并发编程模型Akka