Scala版客户信息管理系统

基于Scala常规客户信息管理系统实现

Customer.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package giesen.Model  

class Customer {
  // 属性
  var id: Int = _
  var name: String = _
  var gender: Char = _
  var age: Short = _
  var tel: String = _
  var email: String = _

  // 设计一个辅助构造器
  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
  }
  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.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package giesen.Business  

import giesen.Model.Customer
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks.{break, breakable}



class CustomerService {
  var customerNum = 1
  // customers是存放客户的,这里先初始化
  val customers: ArrayBuffer[Customer] =
    ArrayBuffer(new Customer(1, "张三", '男', 20, "10000000000", "test@qq.com"))

  def list(): ArrayBuffer[Customer] = {
    this.customers
  }

  def add(customer: Customer): Boolean = {
    // 设置id
    customerNum += 1
    customer.id = customerNum
    // 加入到customers
    customers += customer
    true
  }
  def delete(id: Int) :Boolean = {
    val index = findIndexById(id)
    if(index != -1){
      // 去删除
      customers.remove(index)
      true
    }else{
      false
    }
  }
  // 根据id找到index
  def findIndexById(id: Int): Int = {
    // 默认-1 找到就改成对应的,找不到就返回-1
    var index = -1
    // 遍历ArrayBuffer
    breakable{
      for (i <- customers.indices) {
        if(customers(i).id == id){
          index = i
          break()
        }
      }
    }
    index
  }
  def findCustomerById(id:Int):Any = {
    val index = findIndexById(id)
    if(index != -1){
      return customers(index)
    }
    false
  }
  def update(id:Int,customer: Customer):Boolean ={
    val index = findIndexById(id)
    if(index != -1){
      val customerFromArray = customers(index)
      if(customer.name != null && customer.name != ""){
        customerFromArray.name = customer.name
      }
      if (customer.gender == '男'  || customer.gender == '女') {
        customerFromArray.gender = customer.gender
      }
      if (customer.age >= 0) {
        customerFromArray.age = customer.age
      }
      if (customer.tel != null && customer.tel != "") {
        customerFromArray.tel = customer.tel
      }
      if (customer.email != null && customer.email != "") {
        customerFromArray.email = customer.email
      }
      return true
    }
    false
  }
}

CustomerView.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package giesen.Interface  

import giesen.Business.CustomerService
import giesen.Model.Customer

import scala.io.StdIn
import scala.util.control.Breaks.{break, breakable}



class CustomerView {
  // 定义一个循环变量,控制是否退出while
  var loop = true
  // 定义一个key,用于接收用户输入的选项
  var key = ' '

  var customerService = new CustomerService()


  def mainMenu(): Unit = {
    do {
      println("-----------------Game customer information management software-----------------")
      println("1 Add a game play guest")
      println("2 Repair and change tour play guest")
      println("3 Delete the game user account")
      println("4 Play guest list")
      println("5 Back out")
      println("Please select (1-5):")
      key = StdIn.readChar()
      key match {
        case '1' => this.add()
        case '2' => this.update()
        case '3' => this.delete()
        case '4' => this.list()
        case '5' => this.quit()
        case _ => println("Please choose again!")
      }
    } while (loop)
  }

  def quit(): Unit = {
    println("Confirm whether to exit (Y/N):")
    var choice = ' '
    breakable {
      while (true) {

        choice = StdIn.readChar().toLower
        if (choice != 'y' || choice != 'n') {
          println("Confirm whether to exit (Y/N):")
        }
        if (choice == 'n') {
          break()
        }
        if (choice == 'y') {
          this.loop = false
          break()
        }
      }
    }
  }


  def list(): Unit = {
    println()
    println("---------------------------Game Client List---------------------------")
    println("Number\tName\tGender\t\tAge\t\tTelephone\t\tMail")
    // for循环遍历
    // 获取customers
    val customers = customerService.list()
    for (customer <- customers) {
      // 重写customer的toString方法
      println(customer)
    }
    println("-------------------------The game client list is complete-------------------------")
  }

  def add(): Unit = {
    println("")
    println("---------------------Add game customers---------------------")
    println("Name:")
    val name = StdIn.readLine()
    println("Gender:")
    val gender = StdIn.readChar()
    println("Age:")
    val age = StdIn.readShort()
    println("Telephone:")
    val tel = StdIn.readLine()
    println("Mail:")
    val email = StdIn.readLine()
    // 构建对象
    val customer = new Customer(name, gender, age, tel, email)
    customerService.add(customer)
    println("---------------------Add to complete---------------------")
  }


  def delete(): Unit = {
    println("")
    println("---------------------Deleting Game Clients---------------------")
    println("Select the customer number of the game to be deleted (-1 Exit):")
    val id = StdIn.readInt()
    if (id == -1) {
      println("---------------------Delete failed---------------------")
      return
    }
    println("Confirm whether to delete (Y/N):")
    var choice = ' '
    breakable {
      do {
        choice = StdIn.readChar().toLower
        if (choice == 'y' || choice == 'n') {
          break()
        }
        println("Confirm whether to delete (Y/N):")
      } while (true)
    }
    if (choice == 'y') {
      if (customerService.delete(id)) {
        println("---------------------Deleting completed---------------------")
        return
      }
    }
    println("---------------------Delete failed---------------------")
  }

  def update(): Unit ={
    println("")
    println("---------------------Modifying Game Clients---------------------")
    println("Select the customer ID of the game you want to modify (-1 Exit):")
    val id = StdIn.readInt()
    if(id == -1){
      println("---------------------Modify the failure---------------------")
      return
    }
    val customerFromArray = customerService.findCustomerById(id)
    if(customerFromArray.isInstanceOf[Boolean]){
      println("---------------------The user does not exist---------------------")
      update()
    }else{
      val customerFromService = customerFromArray.asInstanceOf[Customer]
      var name = ""
      var gender = ' '
      var age : Short = -1
      var tel = ""
      var email = ""
      println(s"Name(${customerFromService.name}):")
      name = StdIn.readLine()
      println(s"Gender:(${customerFromService.gender}):")
      gender = StdIn.readChar()
      println(s"Age:(${customerFromService.age}):")
      age = StdIn.readShort()
      println(s"Telephone:(${customerFromService.tel}):")
      tel = StdIn.readLine()
      println(s"Mail:(${customerFromService.email}):")
      email = StdIn.readLine()
      // 构建对象
      val customer = new Customer(name, gender, age, tel, email)
      val isUpdated = customerService.update(id, customer)
      if(isUpdated){
        println("---------------------Modified to complete---------------------")
        return
      }
    }
    println("---------------------Modify the failure---------------------")
  }
}

CustomerCrm.scala

1
2
3
4
5
6
7
import giesen.Interface.CustomerView  

object CustomerCrm {
  def main(args: Array[String]): Unit = {
    new CustomerView().mainMenu()
  }
}

尾巴