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

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

案例介绍:

环境:vscode ubantu18.04

案例界面:

 

 

 

//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
}
//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
}
//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()
}
目录
打赏
0
0
0
0
1
分享
相关文章
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
分布式存储数据恢复—hbase和hive数据库数据恢复案例
分布式存储数据恢复环境: 16台某品牌R730xd服务器节点,每台服务器节点上有数台虚拟机。 虚拟机上部署Hbase和Hive数据库。 分布式存储故障: 数据库底层文件被误删除,数据库不能使用。要求恢复hbase和hive数据库。
58 12
大数据新视界--大数据大厂之MySQL 数据库课程设计:MySQL 数据库 SQL 语句调优的进阶策略与实际案例(2-2)
本文延续前篇,深入探讨 MySQL 数据库 SQL 语句调优进阶策略。包括优化索引使用,介绍多种索引类型及避免索引失效等;调整数据库参数,如缓冲池、连接数和日志参数;还有分区表、垂直拆分等其他优化方法。通过实际案例分析展示调优效果。回顾与数据库课程设计相关文章,强调全面认识 MySQL 数据库重要性。为读者提供综合调优指导,确保数据库高效运行。
数据库数据恢复——MongoDB数据库服务无法启动的数据恢复案例
MongoDB数据库数据恢复环境: 一台Windows Server操作系统虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 管理员在未关闭MongoDB服务的情况下拷贝数据库文件。将MongoDB数据库文件拷贝到其他分区后,对MongoDB数据库所在原分区进行了格式化操作。格式化完成后将数据库文件拷回原分区,并重新启动MongoDB服务。发现服务无法启动并报错。
数据库编程:在PHP环境下使用SQL Server的方法。
看看你吧,就像一个调皮的小丑鱼在一片广阔的数据库海洋中游弋,一路上吞下大小数据如同海中的珍珠。不管有多少难关,只要记住这个流程,剩下的就只是探索未知的乐趣,沉浸在这个充满挑战的数据库海洋中。
51 16
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
84 19
服务器数据恢复—云服务器上mysql数据库数据恢复案例
某ECS网站服务器,linux操作系统+mysql数据库。mysql数据库采用innodb作为默认存储引擎。 在执行数据库版本更新测试时,操作人员误误将在本来应该在测试库执行的sql脚本在生产库上执行,导致生产库上部分表被truncate,还有部分表中少量数据被delete。
103 25
数据库数据恢复—SQL Server报错“错误 823”的数据恢复案例
SQL Server数据库附加数据库过程中比较常见的报错是“错误 823”,附加数据库失败。 如果数据库有备份则只需还原备份即可。但是如果没有备份,备份时间太久,或者其他原因导致备份不可用,那么就需要通过专业手段对数据库进行数据恢复。
虚拟化数据恢复—误还原快照导致虚拟机上数据库丢失的数据恢复案例
虚拟化数据恢复环境&故障: vmfs文件系统,存储的数据是SqlServer数据库及其他办公文件。 工作人员误将快照还原,导致了SqlServer数据库数据的丢失,需要恢复原来的SqlServer数据库文件。
99 22
数据库数据恢复——MySQL简介和数据恢复案例
MySQL数据库数据恢复环境&故障: 本地服务器,安装的windows server操作系统。 操作系统上部署MySQL单实例,引擎类型为innodb,表空间类型为独立表空间。该MySQL数据库没有备份,未开启binlog。 人为误操作,在用Delete命令删除数据时未添加where子句进行筛选导致全表数据被删除,删除后未对该表进行任何操作。
下一篇
oss创建bucket
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等