项目需求分析
模拟实现基于文本界面的《客户信息管理软件》。
该软件scala能够实现对客户对象的插入,修改,删除,显示,查询(用ArrayBuffer或者ListBuffer实现),并能够打印客户明细表。
主界面:
添加客户:
修改客户:
删除客户:
客户列表:
建包:
下面开始:
编写 Customer 类
显示主菜单和完成退出软件功能:
在view 包中建类:CustomerView
在app 包中建立Object CustomerCrm
显示客户列表:
1.编写一个方法,返回当前系统的客户
2.客户放在内存中 ==》 集合 =》可变集合 =》ArrayBuffer/ListBuffer
CustomService 中添加list() 方法
在 CustomerView 中添加listCustomers()方法,显示用户列表
重写Customer 的toString 方法
添加客户
CustomerService中:
CustomerView中:
Customer中:
添加了辅助构造器,没有传入id
因为id 在CustomerService 中进行了传入。在CustomerView中就不需要传入了
删除客户:
CustomerService:
查找有没有该用户:
删除:
CustomerView:
修改用户:
这里是我自己写的,很low,只能通过用户id 修改年龄
CustomerService:
CustomerView:
下面贴上代码:
CustomerCrm:
package Project.app
import Project.view.CustomerView
object CustomerCrm {
def main(args: Array[String]): Unit = {
new CustomerView().mainMenu()
}
}
Customer
package Project.bean
class Customer {
//属性
var id: Int = _
var name: String = _
var gender: Char = _
var age: Short = _
var tel: String = _
var email: String = _
//设计辅助构造器1
def this(id: Int, name: String, gender: Char, age: Short, tel: String, email: String) {
this
this.id = id
this.name = name
this.gender = gender
this.age = age
this.tel = tel
this.email = email
}
//设计辅助构造器2,添加用户时可以不使用id
def this(name: String, gender: Char, age: Short, tel: String, email: String) {
this
this.name = name
this.gender = gender
this.age = age
this.tel = tel
this.email = email
}
override def toString: String = {
this.id + "\t \t" + this.name + "\t \t" + this.gender + "\t \t" + this.age + "\t \t" + this.tel + "\t \t" + this.email
}
}
CustomerService
package Project.customerservice
import Project.bean.Customer
import scala.collection.mutable.ListBuffer
import scala.io.StdIn
import util.control.Breaks._
class CustomerService {
var customerNum = 1
val customers = ListBuffer(
new Customer(1, "tom", '男', 18, "123456", "[email protected]"))
def list(): ListBuffer[Customer] = {
this.customers
}
def addCustomer(customer: Customer)={
customerNum += 1
customer.id = customerNum //此处将customer 的id 传入,实现递增效果
customers.append(customer)
}
def deleteCustomer(id: Int): Boolean={
val index = findIndexById(id)
if (index != -1){
customers.remove(index)
true
} else {
false
}
}
//根据id 找到index
def findIndexById(id: Int)={
var index = -1
breakable{
for(i <- 0 until customers.length){
if (customers(i).id ==id){
index = i
break()
}
}
}
index
}
def update()={
//可以通过模式匹配来完善更多
println("请输入你想修改的用户id")
val id = StdIn.readInt()
val index = findIndexById(id)
println("请输入你想修改的年龄:")
var newAge = StdIn.readShort()
customers(index).age = newAge
}
}
CustomerView
package Project.view
import Project.bean.Customer
import Project.customerservice.CustomerService
import scala.io.StdIn
class CustomerView {
var loop = true //定义一个循环变量,控制退出
var key = ' ' //定义一个key ,控制操作
var customerService= new CustomerService()
//主界面
def mainMenu()={
do{
println("-------------------客户信息管理软件-------------------")
println(" 1 添 加 客 户")
println(" 2 修 改 客 户")
println(" 3 删 除 客 户")
println(" 4 客 户 列 表")
println(" 5 退 出")
print("请输入你的操作:")
key =StdIn.readChar()
key match {
case '1' => this.add()
case '2' => this.update()
case '3' => this.delete()
case '4' => this.list()
case '5' =>
println("退 出")
}
}while(loop)
println("退出了系统")
}
//显示客户列表
def list(): Unit={
println()
println("-------------------客户列表-------------------")
println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
//for 遍历获取客户信息
val customers = customerService .list()
for (customer <- customers){
//重写customer的toString方法
println(customer)
}
println("-----------------客户列表完成-----------------")
}
//添加用户
def add()={
println("-------------------添加客户-------------------")
//此处没有传入id,CustomerService的addCustomer方法中传入了id
print("姓名:")
val name = StdIn.readLine()
print("性别")
val gender = StdIn.readChar()
print("年龄")
val age = StdIn.readShort()
print("电话")
val tel = StdIn.readLine()
print("邮箱")
val email = StdIn.readLine()
//使用的是辅助构造器2
val customer = new Customer(name,gender,age,tel,email)
customerService.addCustomer(customer)
println("-------------------添加成功-------------------")
}
//修改用户
def update()={
customerService.update()
println("修改用户成功")
}
//删除用户
def delete(): Any ={
println("-------------------删除用户-------------------")
println("请选择待删除用户编号(-1退出)")
val id = StdIn.readInt()
if (id == -1) {
println("没有删除用户")
return
}
println("确认是否删除(y/n)")
val choice = StdIn.readChar().toLower
if (choice == 'y'){
if (customerService.deleteCustomer(id)){
println("删除用户成功")
return
}
}
println("删除没有完成")
}
}
观看韩顺平老师的Scala 笔记