当前位置: 首页 > news >正文

go学习之简单项目

项目

文章目录

    • 项目
      • 1.项目开发流程图
      • 2.家庭收支记账软件项目
        • 2)项目代码实现
        • 3)具体功能实现
      • 3.客户信息管理系统
        • 1)项目需求说明
        • 2)界面设计
        • 3)项目框架图
        • 4)流程
        • 5)完成显示客户列表的功能
        • 6)添加客户功能
        • 7)删除客户功能
        • 8)修改客户的功能
        • 9)完整代码的展示如下

1.项目开发流程图

在这里插入图片描述

2.家庭收支记账软件项目

1)需求说明

  • 模拟实现基于文本界面的《家庭记账软件》

  • 该软件能够记录家庭的收入、支出,并能够打印收支明细表

  • 项目采用分级菜单的方式,主菜单如下:

    --------家庭收支记账软件-------1.收支明细2.登记收入3.登记支出4.退出请选择(1-4):
2)项目代码实现

实现基本功能(先使用面向过程,后面改成面向对象)

编写文件TestMyAccount.go 完成基本功能

  1. 功能1:先完成可以显示主菜单,并且可以退出
  2. 功能2:完成可以显示明细和登记收入的功能
  3. 功能3:完成了登记支出的功能
3)具体功能实现

功能1:先完成可以显示主菜单,并且可以退出

思路分析:给出的界面完成,主菜单的显示,当用户输入4的时候就退出

package main
import ("fmt"
)func main(){//声明一个变量,保存接收用户输入的选项key := ""//声明一个变量,控制是否退出for循环loop := true//显示这个主菜单for {fmt.Println("--------家庭收支记账软件---------")fmt.Println("         1.收支明细")fmt.Println("         2.登记收入")fmt.Println("         3.登记支出")fmt.Println("         4.退出软件")fmt.Print("请选择(1-4)")fmt.Scanln(&key)switch key {case "1" :fmt.Println("1.收支明细")case "2" :fmt.Println("2.登记收入")case "3" :fmt.Println("3.登记支出")case "4" :loop = false	default :fmt.Println("请输入正确的选项")			}if !loop {break}}fmt.Println("你退出了家庭记账软件的使用")
}

功能2:完成可以显示明细和登记收入的功能

思路分析:

1.因为需要显示明细,我们定义一个变量details string来记录

2.还需要定义变量来记录余额(balance),每次支出的收支的金额(money),以及收支说明(note)

走代码

    //声明一个变量统计余额balance := 10000.0//每次收支的金额money := 0.0//每次收支的说明note := ""//收支的详情//当有收支发生的时候,就对details进行拼接处理details := "收支\t账户余额\t收支金额\t说明"case的操作
case "2" :fmt.Println("本次收入金额:")fmt.Scanln(&money)balance += money //修改账户余额fmt.Println("本次收入的说明:")fmt.Scanln(&note)//将这个收入情况,拼接到details变量当中details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balance,money,note)

功能3完成登记支出的功能

思路分析:登记支出的功能和登记收入的功能类似做一些修改即可

case "3" :fmt.Println("本次支出的金额:")fmt.Scanln(&money)//这里需要做出一个必要的判断if money > balance {fmt.Println("余额不足")break}balance -=moneyfmt.Println("本次的支出说明:")fmt.Scanln(&note)details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)

项目改进

1.用户输入4时,给出提示"你确定要退出吗?y/n",必须输入正确的y/n,否则循环输入指令,直到输入y或者n

case "4" :fmt.Println("您确定要退出吗? y/n")choice :=" "for {fmt.Scanln(&choice)if choice == "y" || choice == "n"{ //输了y/n就break出去break}fmt.Println("您的输入有误请重新输入 y/n")}if choice == "y" {loop = false	}

2.当没有任何收支明细时,提示“当前没有收支明细。。。来一笔把!”

case "1" :fmt.Println("------------当前收支明细记录--------")if flag {fmt.Println(details)}else{fmt.Println("您当前没有支出记录,来一笔吧!")}

3.在支出时,判断余额是否够,并给出相应的提示

case "3" :fmt.Println("本次支出的金额:")fmt.Scanln(&money)//这里需要做出一个必要的判断if money > balance {fmt.Println("余额不足")break}balance -=moneyfmt.Println("本次的支出说明:")fmt.Scanln(&note)details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)flag = true

面向过程的家庭记账收支软件全部代码

package main
import ("fmt"
)func main(){//声明一个变量,保存接收用户输入的选项key := ""//声明一个变量,控制是否退出for循环loop := true//声明一个变量统计余额balance := 10000.0//每次收支的金额money := 0.0//每次收支的说明note := ""//定义一个变量记录是否有收支的行为flag := false//收支的详情//当有收支发生的时候,就对details进行拼接处理details := "收支\t账户余额\t收支金额\t说明"//显示这个主菜单for {fmt.Println("\n--------家庭收支记账软件---------")fmt.Println("         1.收支明细")fmt.Println("         2.登记收入")fmt.Println("         3.登记支出")fmt.Println("         4.退出软件")fmt.Print("请选择(1-4)")fmt.Scanln(&key)switch key {case "1" :fmt.Println("------------当前收支明细记录--------")if flag {fmt.Println(details)}else{fmt.Println("您当前没有支出记录,来一笔吧!")}case "2" :fmt.Println("本次收入金额:")fmt.Scanln(&money)balance += money //修改账户余额fmt.Println("本次收入的说明:")fmt.Scanln(&note)//将这个收入情况,拼接到details变量当中details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balance,money,note)flag = truecase "3" :fmt.Println("本次支出的金额:")fmt.Scanln(&money)//这里需要做出一个必要的判断if money > balance {fmt.Println("余额不足")break}balance -=moneyfmt.Println("本次的支出说明:")fmt.Scanln(&note)details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)flag = truecase "4" :fmt.Println("您确定要退出吗? y/n")choice :=" "for {fmt.Scanln(&choice)if choice == "y" || choice == "n"{ //输了y/n就break出去break}fmt.Println("您的输入有误请重新输入 y/n")}if choice == "y" {loop = false	}default :fmt.Println("请输入正确的选项")	}if !loop {break}}fmt.Println("你退出了家庭记账软件的使用")
}

4.将面向过程的代码改为面向对象的方法编写myFamilyAccount.go,并使用testMyFamilyAccount.go去完成测试。

思路分析

把记账软件的功能封装到一个结构体中,然后调用该结构体的方法来实现记账,显示明细就可以了,结构体的名字为FamilyAccount

再通过main方法中创建一个结构体FamilyAccount实例,实现记账即可

代码实现,代码不需要重新写,只需要引用上侧代码

package objectTestAcc
import ("fmt"
)type FamilyAccount struct {//声明必须字段//声明一个字段,保存接收用户输入的选项key string//声明一个字段,控制是否退出for循环loop bool//声明一个字段统计余额balance float64//每次收支的金额money float64//每次收支的说明note string//定义一个字段记录是否有收支的行为flag bool//收支的详情//当有收支发生的时候,就对details进行拼接处理details string
}
//编写一个构造方法返回一个FamilyAccount实例 
func NewFamilyAccount() *FamilyAccount {return &FamilyAccount{key : "",loop : true,balance : 10000.0,money : 0.0,note : "",flag : false,details :  "收支\t账户余额\t收支金额\t说明",}
}//将显示明细写成一个方法
func (this *FamilyAccount) ShowDetails(){fmt.Println("------------当前收支明细记录--------")if this.flag {fmt.Println(this.details)}else{fmt.Println("您当前没有支出记录,来一笔吧!")}
}//将登记收入写成一个方法和*FamilyAccount绑定
func (this *FamilyAccount) Income(){fmt.Println("本次收入金额:")fmt.Scanln(&this.money)this.balance += this.money //修改账户余额fmt.Println("本次收入的说明:")fmt.Scanln(&this.note)//将这个收入情况,拼接到details变量当中this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v",this.balance,this.money,this.note)this.flag = true
}
//将支出也绑定到一个方法当中
func (this *FamilyAccount) Pay(){fmt.Println("本次支出的金额:")fmt.Scanln(&this.money)//这里需要做出一个必要的判断if this.money > this.balance {fmt.Println("余额不足")}this.balance -=this.moneyfmt.Println("本次的支出说明:")fmt.Scanln(&this.note)this.details += fmt.Sprintf("\n支出\t%v\t%v\t%v",this.balance,this.money,this.note)this.flag = true
}//将退出系统写成一个方法
func (this *FamilyAccount) exit(){fmt.Println("您确定要退出吗? y/n")choice :=" "for {fmt.Scanln(&choice)if choice == "y" || choice == "n"{ //输了y/n就break出去break}fmt.Println("您的输入有误请重新输入 y/n")}if choice == "y" {this.loop = false	}
}//为该结构体绑定相应的方法
//显示主菜单
func (this *FamilyAccount) MainMenu(){for {fmt.Println("\n--------家庭收支记账软件---------")fmt.Println("         1.收支明细")fmt.Println("         2.登记收入")fmt.Println("         3.登记支出")fmt.Println("         4.退出软件")fmt.Print("请选择(1-4)")fmt.Scanln(&this.key)switch this.key {case "1" :this.ShowDetails()case "2" :this.Income()case "3" :this.Pay()case "4" :this.exit()	default :fmt.Println("请输入正确的选项")	}if !this.loop {break}}
}
建立一个main方法
package main
import ("fmt""go_code/project/objectTestAcc"
)func main() {fmt.Println("这个是面向对象的方式完成")objectTestAcc.NewFamilyAccount().MainMenu()}

3.客户信息管理系统

1)项目需求说明

模拟实现基于文本界面的《客户信息管理软件》

该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表 多个对象协同工作

2)界面设计

在这里插入图片描述

添加客户界面

在这里插入图片描述

修改客户界面

在这里插入图片描述

删除客户界面

在这里插入图片描述

客户列表的界面

在这里插入图片描述

3)项目框架图

在这里插入图片描述

4)流程

功能说明

当用户运行程序,可以看到主菜单,当输入5时,可以退出该软件

思路分析

编写customerView.go另外可以把customer.go和customerDervice.go协商

代码实现

customerManager/model/customer.go

package model
// import (
// 	"fmt"
// )
//声明一个customer结构体,表示一个客户信息
type Customer struct {Id intName stringGender stringAge intPhone stringEmail string
}//编写一个工厂模式,返回一个Customer的实例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,}}

customerManagerservice/customerService.go

package service
import ("go_code/project/customerManager/model"
)//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {customers []model.Customer//声明一个字段,表示当前切片含有多少客户//该字段后面,还可以作为新客户的id+1customerNum int
}

customerManager/view/customerView.go

package main
import ("fmt"
)type customerView struct {//定义必要字段key string //接收用户输入loop bool //是否循环显示菜单}//显示主菜单
func (this *customerView) mainView() {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":fmt.Println("添加客户")case "2":fmt.Println("修改客户")case "3":fmt.Println("删除客户")case "4":fmt.Println("客户列表")case "5":this.loop = falsedefault :fmt.Println("你的输入有误,请重新输入...")						}if !this.loop {break}}fmt.Println("你退出了客户关系管理系统的使用")
}
func main() {//在主函数中,创建一个customerView并运行显示主菜单...customerView := customerView{key : "",loop : true,	}//显示主菜单customerView.mainView()
}
5)完成显示客户列表的功能

思路分析

在这里插入图片描述

代码实现

customerManager/model/customer.go

package model
import ("fmt"
)
//声明一个customer结构体,表示一个客户信息
type Customer struct {Id intName stringGender stringAge intPhone stringEmail string
}//编写一个工厂模式,返回一个Customer的实例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,}
}
//增加了这个方法
//返回用户的信息,格式化的字符串
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
} 

customerManagerservice/customerService.go

package service
import ("go_code/project/customerManager/model"
)//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {customers []model.Customer//声明一个字段,表示当前切片含有多少客户//该字段后面,还可以作为新客户的id+1customerNum int
}//编写一个方法,可以返回一个*customerService实例
func NewCustomerService() *CustomerService {//为了可以看到客户在切片中,我们初始化一个客户customerService := &CustomerService{}customerService.customerNum = 1customer := 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
}

customerManager/view/customerView.go

package main
import ("fmt""go_code/project/customerManager/service"
)type customerView struct {//定义必要字段key string //接收用户输入loop bool //是否循环显示菜单//增加一个字段customerServicecustomerService   *service.CustomerService
}//显示所有的客户信息
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.Printf("\n--------客户列表完成------------\n\n")
}//显示主菜单
func (this *customerView) mainView() {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":fmt.Println("添加客户")case "2":fmt.Println("修改客户")case "3":fmt.Println("删除客户")case "4":this.list()case "5":this.loop = falsedefault :fmt.Println("你的输入有误,请重新输入...")						}if !this.loop {break}}fmt.Println("你退出了客户关系管理系统的使用")
}
func main() {//在主函数中,创建一个customerView并运行显示主菜单...customerView := customerView{key : "",loop : true,	}//完成对customerView结构体的customerService字段的初始化customerView.customerService = service.NewCustomerService()//显示主菜单customerView.mainView()
}
6)添加客户功能

功能说明

在这里插入图片描述

思路分析

在这里插入图片描述

代码实现

需要编写CustomerView和customerService,Customer类

规定,新添加的学院的id就是他是第几个加入的

customerManager/model/customer.go

//编写一个工厂模式,返回二种Customer的实例方法,不带id
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,}
}

customerManagerservice/customerService.go

增加一个方法
//添加客户到customer切片中
func (this *CustomerService) Add(customer model.Customer) bool{//我们确定一个分配id的规则,就是添加的顺序this.customerNum ++customer.Id = this.customerNumthis.customers = append(this.customers,customer)return true
}

customerManager/view/customerView.go

编写一个add方法调用servic蹭的Add()
//得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {fmt.Println("------------添加客户------------")fmt.Println("姓名:")name := ""fmt.Scanln(&name)fmt.Println("性别:")gender := ""fmt.Scanln(&gender)fmt.Println("年龄:")age := 0fmt.Scanln(&age)fmt.Println("电话号码:")phone := ""fmt.Scanln(&phone)fmt.Println("电邮:")email := ""fmt.Scanln(&email)//构建一个新的Customer实例//注意:id号没有让用户输入,id号是唯一的,让系统分配即可customer := model.NewCustomer2(name,gender,age,phone,email)//调用if this.customerService.Add(customer) {fmt.Println("------------添加完成------------")}else{fmt.Println("------------添加失败------------")}
}
下面的switch方法也要改一下
case "1":this.add()
7)删除客户功能

功能说明

在这里插入图片描述

思路分析

需要编写CustomerView和CustomerService

在这里插入图片描述

代码实现

customerManager/model/customer.go:无变化

customerManagerservice/customerService.go

增加了这两个方法,一个删除一个查找id
//根据id删除客户(从切片中删除)
func (this *CustomerService) Delete(id int )bool {index :=this.FindById(id)//如果index ==-1说明没有这个客户if index== -1 {return false}//如何从切片中删除一个元素this.customers = append(this.customers[:index],this.customers[index+1:]...)return true}//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {//默认为-1index := -1//遍历this.customers切片for i :=0;i < len(this.customers);i++ {if this.customers[i].Id ==id {//找到了index = i}}return index
}

customerManager/view/customerView.go

增加这个方法
//得到用户输入的id删除该id对应的客户
func (this *customerView) delete() {fmt.Println("------------删除客户------------")fmt.Println("请选择待删除的客户编号(-1退出):")id :=-1fmt.Scanln(&id)if id == -1 {return //放弃删除操作}fmt.Println("确认是否删除(Y/N): ")choice := ""fmt.Scanln(&choice)if choice == "y" || choice == "Y" {//调用service中的delete方法if this.customerService.Delete(id) {fmt.Println("------------删除成功------------")}else{fmt.Println("------------删除失败,输入的id号不存在------------")}}
}

8)完善退出确认功能

功能说明:

要求用户在退出时提示“是否退出(Y/N),用户必须输入y/n否则循环提示

思路分析:需编写CustomerView

代码实现

在customerManager/view/customerView.go增加这个方法

//退出软件
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}
}
然后在switch中修改一下
case "5":this.exit()
8)修改客户的功能

功能说明:根据id进行对客户的修改操作

思路:依旧在customerService和customerView中进行编写操作

代码实现

customerManagerservice/customerService.go

//根据id进行修改客户信息的操作
func (this *CustomerService) Update(customer model.Customer) bool {index :=this.FindById(customer.Id)//如果index ==-1说明没有这个客户if index== -1 {return false}//将customer插入到指定的位置并对customers进行更新操作,就将原来位置的customer用一个新的customer进行替换操作this.customers = append(append(this.customers[:index],customer),this.customers[index+1:]...)return true
}//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {//默认为-1index := -1//遍历this.customers切片for i :=0;i < len(this.customers);i++ {if this.customers[i].Id ==id {//找到了index = i}}return index
}

customerManager/view/customerView.go

//修改客户的操作
func (this *customerView) update() {fmt.Println("------------修改客户------------")fmt.Println("请选择修改客户的编号(-1的话就退出): ")id := -1fmt.Scanln(&id)if id == -1 {return}fmt.Println("姓名:")name := ""fmt.Scanln(&name)fmt.Println("性别:")gender := ""fmt.Scanln(&gender)fmt.Println("年龄:")age := 0fmt.Scanln(&age)fmt.Println("电话号码:")phone := ""fmt.Scanln(&phone)fmt.Println("电邮:")email := ""fmt.Scanln(&email)//构建一个新的Customer实例//注意:id号没有让用户输入,id号是唯一的,让系统分配即可customer := model.NewCustomer(id,name,gender,age,phone,email)//调用if this.customerService.Update(customer) {fmt.Println("------------修改成功------------")}else{fmt.Println("------------修改失败------------")}
}

再外加一个简单的登录操作使得项目更加完善

在customerManager/view/customerView.go中进行编写

//简单登录功能的时间
func (this *customerView) Login (){account :=""pwd :=""for {fmt.Println("请输入账号: ")fmt.Scanln(&account)fmt.Println("请输入密码")fmt.Scanln(&pwd)if account == "7758258" && pwd =="111"{fmt.Println("恭喜你!正在进入系统!")break}fmt.Println("您的输入的账号或者密码有误,请重新输入: ")	   	}this.mainView()
}
func main() {//在主函数中,创建一个customerView并运行显示主菜单...customerView := customerView{key : "",loop : true,	}//完成对customerView结构体的customerService字段的初始化customerView.customerService = service.NewCustomerService()//显示主菜单customerView.Login()
}
9)完整代码的展示如下

customerManager/model/customer.go

package model
import ("fmt"
)
//声明一个customer结构体,表示一个客户信息
type Customer struct {Id intName stringGender stringAge intPhone stringEmail string
}//编写一个工厂模式,返回一个Customer的实例
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
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
} 

customerManagerservice/customerService.go

package service
import ("go_code/project/customerManager/model"
)//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {customers []model.Customer//声明一个字段,表示当前切片含有多少客户//该字段后面,还可以作为新客户的id+1customerNum int
}//编写一个方法,可以返回一个*customerService实例
func NewCustomerService() *CustomerService {//为了可以看到客户在切片中,我们初始化一个客户customerService := &CustomerService{}customerService.customerNum = 1customer := 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
}//添加客户到customer切片中
//必须要用指针的方式,保证一直用的都是一个CustomerService
func (this *CustomerService) Add(customer model.Customer) bool{//我们确定一个分配id的规则,就是添加的顺序this.customerNum ++customer.Id = this.customerNumthis.customers = append(this.customers,customer)return true
}//根据id删除客户(从切片中删除)
func (this *CustomerService) Delete(id int )bool {index :=this.FindById(id)//如果index ==-1说明没有这个客户if index== -1 {return false}//如何从切片中删除一个元素this.customers = append(this.customers[:index],this.customers[index+1:]...)return true}//根据id进行修改客户信息的操作
func (this *CustomerService) Update(customer model.Customer) bool {index :=this.FindById(customer.Id)//如果index ==-1说明没有这个客户if index== -1 {return false}//将customer插入到指定的位置并对customers进行更新操作,就将原来位置的customer用一个新的customer进行替换操作this.customers = append(append(this.customers[:index],customer),this.customers[index+1:]...)return true
}//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {//默认为-1index := -1//遍历this.customers切片for i :=0;i < len(this.customers);i++ {if this.customers[i].Id ==id {//找到了index = i}}return index
}

customerManager/view/customerView.go

package main
import ("fmt""go_code/project/customerManager/service""go_code/project/customerManager/model"
)type customerView struct {//定义必要字段key string //接收用户输入loop bool //是否循环显示菜单//增加一个字段customerServicecustomerService   *service.CustomerService
}//显示所有的客户信息
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.Printf("\n--------客户列表完成------------\n\n")
}//得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {fmt.Println("------------添加客户------------")fmt.Println("姓名:")name := ""fmt.Scanln(&name)fmt.Println("性别:")gender := ""fmt.Scanln(&gender)fmt.Println("年龄:")age := 0fmt.Scanln(&age)fmt.Println("电话号码:")phone := ""fmt.Scanln(&phone)fmt.Println("电邮:")email := ""fmt.Scanln(&email)//构建一个新的Customer实例//注意:id号没有让用户输入,id号是唯一的,让系统分配即可customer := model.NewCustomer2(name,gender,age,phone,email)//调用if this.customerService.Add(customer) {fmt.Println("------------添加完成------------")}else{fmt.Println("------------添加失败------------")}
}//修改客户的操作
func (this *customerView) update() {fmt.Println("------------修改客户------------")fmt.Println("请选择修改客户的编号(-1的话就退出): ")id := -1fmt.Scanln(&id)if id == -1 {return}fmt.Println("姓名:")name := ""fmt.Scanln(&name)fmt.Println("性别:")gender := ""fmt.Scanln(&gender)fmt.Println("年龄:")age := 0fmt.Scanln(&age)fmt.Println("电话号码:")phone := ""fmt.Scanln(&phone)fmt.Println("电邮:")email := ""fmt.Scanln(&email)//构建一个新的Customer实例//注意:id号没有让用户输入,id号是唯一的,让系统分配即可customer := model.NewCustomer(id,name,gender,age,phone,email)//调用if this.customerService.Update(customer) {fmt.Println("------------修改成功------------")}else{fmt.Println("------------修改失败------------")}
}//得到用户输入的id删除该id对应的客户
func (this *customerView) delete() {fmt.Println("------------删除客户------------")fmt.Println("请选择待删除的客户编号(-1退出):")id :=-1fmt.Scanln(&id)if id == -1 {return //放弃删除操作}fmt.Println("确认是否删除(Y/N): ")choice := ""for {fmt.Scanln(&choice)if choice == "y" || choice == "Y" || choice =="n" || choice =="N"{break}fmt.Println("您的输入有误请重新输入(Y/N): ")}if choice == "y" || choice == "Y" {//调用service中的delete方法if this.customerService.Delete(id) {fmt.Println("------------删除成功------------")}else{fmt.Println("------------删除失败,输入的id号不存在------------")}} else{this.mainView()}
}//退出软件
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 (this *customerView) mainView() {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.update()case "3":this.delete()case "4":this.list()case "5":this.exit()default :fmt.Println("你的输入有误,请重新输入...")						}if !this.loop {break}}fmt.Println("你退出了客户关系管理系统的使用")
}//简单登录功能的时间
func (this *customerView) Login (){account :=""pwd :=""for {fmt.Println("请输入账号: ")fmt.Scanln(&account)fmt.Println("请输入密码")fmt.Scanln(&pwd)if account == "7758258" && pwd =="111"{fmt.Println("恭喜你!正在进入系统!")break}fmt.Println("您的输入的账号或者密码有误,请重新输入: ")	   	}this.mainView()
}
func main() {//在主函数中,创建一个customerView并运行显示主菜单...customerView := customerView{key : "",loop : true,	}//完成对customerView结构体的customerService字段的初始化customerView.customerService = service.NewCustomerService()//显示主菜单customerView.Login()
}

10)项目展示

1.登录

在这里插入图片描述

2.客户列表

在这里插入图片描述

3.添加客户

在这里插入图片描述

4.修改客户

在这里插入图片描述

5.删除客户

在这里插入图片描述

6.退出

在这里插入图片描述

http://www.lryc.cn/news/235663.html

相关文章:

  • 代码随想录二刷 | 数组 | 总结篇
  • go test 命令详解
  • 【Mysql学习笔记】1 - Mysql入门
  • sentinel 网关
  • 常见面试题-MySQL的Explain执行计划
  • SpringBoot静态资源配置
  • Java拼图
  • Linux 怎样通过win 远程桌面连接链接Linux后台服务器的可视化图形界面
  • Java 实现随机图形
  • java 读写文件的代码。
  • 如何使用贝锐花生壳内网穿透远程访问JupyterNotebook?
  • 文本向量化
  • java--贪吃蛇
  • 录制第一个jmeter性能测试脚本2(http协议)
  • pip命令大全
  • Redis篇---第二篇
  • 【LeetCode刷题日志】232.用栈实现队列
  • 单元测试实战(二)Service 的测试
  • LabVIEW和NIUSRP硬件加快了认知无线电开发
  • 嵌入式软件工程师面试题——2025校招社招通用(十六)
  • 白盒测试之测试用例设计方法
  • 在CentOS 7上关闭SELinux
  • 基于单片机温湿度PM2.5报警系统
  • OpenHarmony系统编译环境
  • 二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
  • 通过制作llama_cpp的docker镜像在内网离线部署运行大模型
  • JavaScript 异步编程
  • linux课程第一课------命令的简单的介绍
  • XLua热更新框架原理和代码实战
  • Hive客户端hive与beeline的区别