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()
}
相关文章
|
1月前
|
SQL 数据库
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
SQL Server附加数据库出现错误823,附加数据库失败。数据库没有备份,无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有:数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面、断电或者文件系统问题导致页面丢失。
95 12
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
|
10天前
|
Java 数据库
案例一:去掉数据库某列中的所有英文,利用java正则表达式去做,核心:去掉字符串中的英文
这篇文章介绍了如何使用Java正则表达式从数据库某列中去除所有英文字符。
28 15
|
1月前
|
Oracle 关系型数据库 数据库
数据库数据恢复—Oracle数据库文件出现坏块的数据恢复案例
打开oracle数据库报错“system01.dbf需要更多的恢复来保持一致性,数据库无法打开”。 数据库没有备份,无法通过备份去恢复数据库。用户方联系北亚企安数据恢复中心并提供Oracle_Home目录中的所有文件,急需恢复zxfg用户下的数据。 出现“system01.dbf需要更多的恢复来保持一致性”这个报错的原因可能是控制文件损坏、数据文件损坏,数据文件与控制文件的SCN不一致等。数据库恢复工程师对数据库文件进一步检测、分析后,发现sysaux01.dbf文件损坏,有坏块。 修复并启动数据库后仍然有许多查询报错,export和data pump工具使用报错。从数据库层面无法修复数据库。
数据库数据恢复—Oracle数据库文件出现坏块的数据恢复案例
|
27天前
|
Oracle 关系型数据库 数据库
Oracle数据恢复—异常断电导致Oracle数据库数据丢失的数据恢复案例
Oracle数据库故障: 机房异常断电后,Oracle数据库启库报错:“system01.dbf需要更多的恢复来保持一致性,数据库无法打开”。数据库没有备份,归档日志不连续。用户方提供了Oracle数据库的在线文件,需要恢复zxfg用户的数据。 Oracle数据库恢复方案: 检测数据库故障;尝试挂起并修复数据库;解析数据文件。
|
28天前
|
存储 数据挖掘 数据库
服务器数据恢复—raid磁盘故障导致数据库数据损坏的数据恢复案例
存储中有一组由3块SAS硬盘组建的raid。上层win server操作系统层面划分了3个分区,数据库存放在D分区,备份存放在E分区。 RAID中一块硬盘的指示灯亮红色,D分区无法识别;E分区可识别,但是拷贝文件报错。管理员重启服务器,导致离线的硬盘上线开始同步数据,同步还没有完成就直接强制关机了,之后就没有动过服务器。
|
1月前
|
Prometheus Cloud Native Go
Golang语言之Prometheus的日志模块使用案例
这篇文章是关于如何在Golang语言项目中使用Prometheus的日志模块的案例,包括源代码编写、编译和测试步骤。
35 3
Golang语言之Prometheus的日志模块使用案例
|
10天前
|
Oracle 关系型数据库 数据库
oracle数据恢复—Oracle数据库文件损坏导致数据库打不开的数据恢复案例
打开oracle数据库时报错,报错信息:“system01.dbf需要更多的恢复来保持一致性,数据库无法打开”。急需恢复zxfg用户下的数据。 出现上述报错的原因有:控制文件损坏、数据文件损坏、数据文件与控制文件的SCN不一致等。数据恢复工程师对数据库文件做进一步检测分析后发现sysaux01.dbf文件有坏块。修复sysaux01.dbf文件,启动数据库依然有许多查询报错。export和data pump工具无法使用,查询告警日志并分析报错,确认发生上述错误的原因就是sysaux01.dbf文件损坏。由于该文件损坏,从数据库层面无法修复数据库。由于system和用户表空间的数据文件是正常的,
|
1月前
|
Go
Golang语言基本数据类型默认值及字符串之间互相转换案例
这篇文章讲解了Golang语言中基本数据类型的默认值、类型转换的概述以及整型、浮点型、字符串之间的相互转换案例,包括如何将基本数据类型转换为字符串类型和字符串类型转换为基本数据类型,以及字符串与字节切片之间的转换。
17 2
|
1月前
|
Go
Golang语言数据类型分类及进制转换案例
这篇文章详细介绍了Go语言中数据类型的分类、进制转换的概念和实例,以及数字字面量语法,还涉及了原码、反码和补码的相关知识。
17 0
Golang语言数据类型分类及进制转换案例
|
1月前
|
Go
Golang的time.NewTicker周期性定时器使用案例
这篇文章介绍了Golang中time包的NewTicker函数如何创建周期性定时器,并通过两个示例展示了如何使用定时器以及如何停止定时器。
51 1