Golang面向对象小案例(未连接数据库)

简介: Golang面向对象小案例(未连接数据库)

customerManage/model/customer.go

package model
import (
  "fmt"
)
type Customer struct{
  Id    int
  Name  string
  Gender  string
  Age   int
  Phone string
  Email string
}
//通过工厂模式,返回一个Customer的实例  ===>被NewCustomerService调用
func NewCustomer(id int,name string,gender string,age int,
  phone string,email string)Customer{
    return Customer{
      Id : id,
      Name : name,
      Gender : gender,
      Age : age,
      Phone : phone,
      Email : email, 
    }
}
//通过工厂模式,返回一个Customer的实例 不提供id==>Add调用
func NewCustomer2(name string,gender string,age int,
    phone string,email string)Customer{
      return Customer{
        Name : name,
        Gender :gender,
        Age : age,
        Phone : phone,
        Email : email,
      }
}
//返回用户信息,格式化的字符串
func (this Customer)GetInfo() string{
  info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t",this.Id,this.Name,this.Gender,
  this.Age,this.Phone,this.Email)
  return info
}

customerManage/service/customerService.go

package service
import (
  "fmt"
  "oopproject/customerManage/model"
  "strconv"
)
//增删改查
//定义一个结构体,记录不同的用户及登录用户的数量
type CustomerService struct{
  customers     []model.Customer
  customerNum   int
}
//通过工程模式来创建创建 *CustomerService
func NewCustomerService() *CustomerService{
  customerService := &CustomerService{}
  customerService.customerNum = 1
  customer :=model.NewCustomer(1,"张三","男",20,"112","zs@sohu.com")
  customerService.customers = append(customerService.customers,customer)
  return customerService
}
//获取客户切片
func (this *CustomerService)List()[]model.Customer{
  return this.customers
}
//添加用户到customers中
func (this *CustomerService)Add(customer model.Customer)bool{
  this.customerNum++
  customer.Id = this.customerNum
  this.customers = append(this.customers,customer)
  return true
}
//根据id查找用户的在切片中的下标
func (this *CustomerService)FindById(id int)int{
  index := -1
  //遍历切片
  for i:=0;i<len(this.customers);i++{
    if this.customers[i].Id == id{
      index = i
    }
  }
  return index
}
//从customers删除用户
func (this *CustomerService)Delete(id int)bool{
  index := this.FindById(id)
  if index == -1{ //没有找到
    return false   //无法删除
  }
  //从切片删除该用户
  this.customers = append(this.customers[:index],this.customers[index+1:]...)  //作闭右开
  return true
}
//从customers修改指定用户
func (this *CustomerService)Revise(id int)bool{
  index := this.FindById(id)
  if index == -1{
    return false
  }
  fmt.Printf("姓名(%v):<回车表示不做修改>\n",this.customers[index].Name)
  name  := ""
  fmt.Scanln(&name)
  if name != ""{ 
    this.customers[index].Name = name
  }
  fmt.Printf("性别(%v):<回车表示不做修改>\n",this.customers[index].Gender)
  gender := ""
  fmt.Scanln(&gender)
  if gender != ""{ //\r
    this.customers[index].Gender = gender
  }else{
  }
  fmt.Printf("年龄(%v):<回车表示不做修改>\n",this.customers[index].Age)
  age := ""
  fmt.Scanln(&age)
  //func Atoi(s string) (i int, err error)
  if age != ""{ //\r
    this.customers[index].Age,_= strconv.Atoi(age)
  }
  fmt.Printf("电话(%v):<回车表示不做修改>\n",this.customers[index].Phone)
  phone := ""
  fmt.Scanln(&phone)
  if phone != ""{ //\r
    this.customers[index].Phone = phone
  }
  fmt.Printf("邮箱(%v):<回车表示不做修改>\n",this.customers[index].Email)
  email := ""
  fmt.Scanln(&email)
  if phone != ""{ //\r
    this.customers[index].Email = email
  }
  return true
}

customerManage/view/customerView.go

package main
import (
  "fmt"
  "oopproject/customerManage/service"
  "oopproject/customerManage/model"
)
//定义显示菜单
type customerView struct{
  //定义必要字段
  key   string
  loop  bool
  //获取服务器端
  customerService *service.CustomerService
}
func (this *customerView)mainMenu(){
  for{
    fmt.Println("-------------------------------客户信息管理软件-----------------------")
    fmt.Println("                                1 添 加 客 户")
    fmt.Println("                                2 修 改 客 户")
    fmt.Println("                                3 删 除 客 户")
    fmt.Println("                                4 客 户 列 表")
    fmt.Println("                                5 退       出")
    fmt.Println("请选择(1-5):")
    fmt.Scanln(&this.key)
    switch this.key{
    case "1":
      this.add()
    case "2":
      this.revise()
    case "3":
      this.delete()
    case "4":
      this.list()
    case "5":
      this.exit()
    default:
      fmt.Println("你的输入有误,请重新输入...")
    }
    if !this.loop{
      break
    }
  }
  fmt.Println("你退出了客户关系管理系统...")
}
//显示所有客户信息
func (this *customerView)list(){
  //首先,获取到当前所有用户的客户信息
  customers := this.customerService.List()
  //显示
  fmt.Println("------------客户列表-----------------")
  fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
  //遍历用户切片
  for i:=0;i<len(customers);i++{
    fmt.Println(customers[i].GetInfo())
  }
  fmt.Println("\n-----------------客户列表完成------------------\n\n")
}
//添加新用户
func (this *customerView)add(){
  fmt.Println("---------------添加客户-----------------")
  fmt.Println("姓名:")
  name := ""
  fmt.Scanln(&name)
  fmt.Println("性别:")
  gender := ""
  fmt.Scanln(&gender)
  fmt.Println("年龄:")
  age := 0
  fmt.Scanln(&age)
  fmt.Println("电话:")
  phone := ""
  fmt.Scanln(&phone)
  fmt.Println("邮件:")
  email := ""
  fmt.Scanln(&email)
  //创建一个model.Customer实例
  customer := model.NewCustomer2(name,gender,age,phone,email)
  if this.customerService.Add(customer){
    fmt.Println("------------------添加成功------------------")
  }else{
    fmt.Println("------------------添加失败------------------")
  }
}
//删除用户
func (this *customerView)delete(){
  fmt.Println("-------------------删除用户----------------")
  fmt.Println("请选择待删除客户的编号(-1)退出:")
  id := -1
  fmt.Scanln(&id)
  if id == -1{
    return    //放弃删除操作
  }
  fmt.Println("确定是否删除(Y/y):")
  choice := ""
  fmt.Scanln(&choice)
  if choice == "y" || choice == "Y"{
    if this.customerService.Delete(id){
      fmt.Println("------------------删除成功--------------")
    }else{
      fmt.Println("------------------删除失败,用户不存在--------------")
    }
  }
}
//修改指定id的信息
func (this *customerView)revise(){
  fmt.Println("------------------------修改用户------------------")
  fmt.Println("请选择待删除用户的编号(-1退出):")
  id := -1
  fmt.Scanln(&id)
  if id == -1{
    return    //放弃删除操作
  }
  //创建一个model.Customer实例
  if this.customerService.Revise(id){
    fmt.Println("------------------修改成功------------------")
  }else{
    fmt.Println("------------------修改失败,用户不存在------------------")
  }
}
//退出程序
func (this *customerView)exit(){
  fmt.Println("确定是否退出(y/n):")
  for{
    fmt.Scanln(&this.key)//可以重新定义的变量
    if this.key == "y" || this.key == "Y" || this.key == "n" || this.key == "N"{
      break
    } 
    fmt.Println("你的输入有误,确认是否退出(y/n):")
  }
  if this.key == "Y" || this.key == "y"{
    this.loop = false
  }
}
func main(){
  customerView := &customerView{
    key : "",
    loop : true,
  }
  //初始化customerService
  customerView.customerService = service.NewCustomerService()
  //显示主菜单功能
  customerView.mainMenu()
}
相关文章
|
1月前
|
SQL 关系型数据库 MySQL
探索Gorm - Golang流行的数据库ORM框架
探索Gorm - Golang流行的数据库ORM框架
|
4天前
|
JSON 前端开发 JavaScript
SSMP整合案例第五步 在前端页面上拿到service层调数据库里的数据后列表
SSMP整合案例第五步 在前端页面上拿到service层调数据库里的数据后列表
9 2
|
4天前
|
关系型数据库 MySQL 数据库
关系型数据库MySQL开发要点之多表设计案例详解代码实现
关系型数据库MySQL开发要点之多表设计案例详解代码实现
11 2
|
4天前
|
关系型数据库 MySQL 数据库
MySQL数据库开发之多表查询数据准备及案例实操
MySQL数据库开发之多表查询数据准备及案例实操
16 1
|
16天前
|
SQL 关系型数据库 MySQL
mysqldiff - Golang 针对 MySQL 数据库表结构的差异 SQL 工具
Golang 针对 MySQL 数据库表结构的差异 SQL 工具。https://github.com/camry/mysqldiff
50 7
|
1月前
|
SQL 数据库
数据库数据恢复—sqlserver数据库分区空间不足导致故障的数据恢复案例
数据库数据恢复环境: 某品牌r520服务器,服务器中有7块SAS硬盘,这7块硬盘组建了一组2盘raid1阵列和一组5盘raid5阵列,raid1阵列存储空间安装操作系统,raid5阵列存储空间存放数据。服务器上部署sql server数据库,数据库存放在C盘。 数据库故障: 工作人员发现服务器的C盘容量即将耗尽,于是将sql server数据库路径指向D盘,在D盘生成了一个.ndf文件。一个多星期后,sql server数据库出现故障,连接失效,无法正常附加查询。
数据库数据恢复—sqlserver数据库分区空间不足导致故障的数据恢复案例
|
21天前
|
SQL 关系型数据库 MySQL
MySQL数据库——索引(3)-索引语法(创建索引、查看索引、删除索引、案例演示),SQL性能分析(SQL执行频率,慢查询日志)
MySQL数据库——索引(3)-索引语法(创建索引、查看索引、删除索引、案例演示),SQL性能分析(SQL执行频率,慢查询日志)
21 2
|
22天前
|
SQL 存储 关系型数据库
MySQL数据库案例实战教程:数据类型、语法与高级查询详解
MySQL数据库案例实战教程:数据类型、语法与高级查询详解
35 3
|
1月前
|
SQL 存储 小程序
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
数据库数据恢复环境: 5块硬盘组建一组RAID5阵列,划分LUN供windows系统服务器使用。windows系统服务器内运行了Sql Server数据库,存储空间在操作系统层面划分了三个逻辑分区。 数据库故障: 数据库文件丢失,主要涉及3个数据库,数千张表。数据库文件丢失原因未知,不能确定丢失的数据库文件的存放位置。数据库文件丢失后,服务器仍处于开机状态,所幸未写入大量数据。
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
|
6天前
|
SQL 前端开发 数据可视化
数据库开发关键之与DQL查询语句有关的两个案例
数据库开发关键之与DQL查询语句有关的两个案例
8 0