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()
}
相关文章
|
7月前
|
存储 Oracle 关系型数据库
服务器数据恢复—光纤存储上oracle数据库数据恢复案例
一台光纤服务器存储上有16块FC硬盘,上层部署了Oracle数据库。服务器存储前面板2个硬盘指示灯显示异常,存储映射到linux操作系统上的卷挂载不上,业务中断。 通过storage manager查看存储状态,发现逻辑卷状态失败。再查看物理磁盘状态,发现其中一块盘报告“警告”,硬盘指示灯显示异常的2块盘报告“失败”。 将当前存储的完整日志状态备份下来,解析备份出来的存储日志并获得了关于逻辑卷结构的部分信息。
|
7月前
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
10月前
|
SQL 数据库 数据安全/隐私保护
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
|
5月前
|
SQL 关系型数据库 MySQL
Mysql数据恢复—Mysql数据库delete删除后数据恢复案例
本地服务器,操作系统为windows server。服务器上部署mysql单实例,innodb引擎,独立表空间。未进行数据库备份,未开启binlog。 人为误操作使用Delete命令删除数据时未添加where子句,导致全表数据被删除。删除后未对该表进行任何操作。需要恢复误删除的数据。 在本案例中的mysql数据库未进行备份,也未开启binlog日志,无法直接还原数据库。
|
7月前
|
Oracle 关系型数据库 数据库
数据库数据恢复—服务器异常断电导致Oracle数据库报错的数据恢复案例
Oracle数据库故障: 某公司一台服务器上部署Oracle数据库。服务器意外断电导致数据库报错,报错内容为“system01.dbf需要更多的恢复来保持一致性”。该Oracle数据库没有备份,仅有一些断断续续的归档日志。 Oracle数据库恢复流程: 1、检测数据库故障情况; 2、尝试挂起并修复数据库; 3、解析数据库文件; 4、导出并验证恢复的数据库文件。
|
10月前
|
SQL 存储 分布式数据库
分布式存储数据恢复—hbase和hive数据库数据恢复案例
分布式存储数据恢复环境: 16台某品牌R730xd服务器节点,每台服务器节点上有数台虚拟机。 虚拟机上部署Hbase和Hive数据库。 分布式存储故障: 数据库底层文件被误删除,数据库不能使用。要求恢复hbase和hive数据库。
378 12
|
8月前
|
存储 Oracle 关系型数据库
oracle数据恢复—oracle数据库执行错误truncate命令的数据恢复案例
oracle数据库误执行truncate命令导致数据丢失是一种常见情况。通常情况下,oracle数据库误操作删除数据只需要通过备份恢复数据即可。也会碰到一些特殊情况,例如数据库备份无法使用或者还原报错等。下面和大家分享一例oracle数据库误执行truncate命令导致数据丢失的数据库数据恢复过程。
|
10月前
|
关系型数据库 MySQL 大数据
大数据新视界--大数据大厂之MySQL 数据库课程设计:MySQL 数据库 SQL 语句调优的进阶策略与实际案例(2-2)
本文延续前篇,深入探讨 MySQL 数据库 SQL 语句调优进阶策略。包括优化索引使用,介绍多种索引类型及避免索引失效等;调整数据库参数,如缓冲池、连接数和日志参数;还有分区表、垂直拆分等其他优化方法。通过实际案例分析展示调优效果。回顾与数据库课程设计相关文章,强调全面认识 MySQL 数据库重要性。为读者提供综合调优指导,确保数据库高效运行。
|
10月前
|
NoSQL MongoDB 数据库
数据库数据恢复——MongoDB数据库服务无法启动的数据恢复案例
MongoDB数据库数据恢复环境: 一台Windows Server操作系统虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 管理员在未关闭MongoDB服务的情况下拷贝数据库文件。将MongoDB数据库文件拷贝到其他分区后,对MongoDB数据库所在原分区进行了格式化操作。格式化完成后将数据库文件拷回原分区,并重新启动MongoDB服务。发现服务无法启动并报错。
|
存储 SQL 关系型数据库
服务器数据恢复—云服务器上mysql数据库数据恢复案例
某ECS网站服务器,linux操作系统+mysql数据库。mysql数据库采用innodb作为默认存储引擎。 在执行数据库版本更新测试时,操作人员误误将在本来应该在测试库执行的sql脚本在生产库上执行,导致生产库上部分表被truncate,还有部分表中少量数据被delete。
310 25

推荐镜像

更多