Golang学习笔记 主要就是练练手
流程控制 面对对象 切片熟练使用
问题 方法(this *Customer)与(this Customer)的区别
import (
"fmt"
)
func main() {
key := ""
loop := true
balance := 10000.0
money := 0.0
note := ""
details := "收支\t账户金额\t收支金额\t说 明"
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 收支明细--------------")
fmt.Println(details)
case "2":
fmt.Println("本次收入金额:")
fmt.Scanln(&money)
fmt.Println("本次收入说明:")
fmt.Scanln(¬e)
balance += money
details += fmt.Sprintf("\n收入\t%v\t%v\t\t%v", balance, money, note)
case "3":
fmt.Println("3 登记支出--------------")
fmt.Println("本次支出金额:")
fmt.Scanln(&money)
if money > balance {
fmt.Println("钱不够啊,你怎么支出")
break
}
balance -= money
fmt.Println("本次支出说明:")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n支出\t%v\t%v\t\t%v", balance, money, note)
case "4":
fmt.Println("4 退出软件--------------")
loop = false
default:
fmt.Println("输入正确选项--------------")
}
if !loop {
break
}
}
fmt.Println("软件退出--------------")
}
面向对象
type FamilyAccout struct {
key string
loop bool
balance float64
money float64
note string
details string //"收支\t账户金额\t收支金额\t说 明"
}
func NewFamilyAccount() *FamilyAccout {
return &FamilyAccout{
key: "",
loop: true,
balance: 10000.0,
money: 0.0,
note: "",
details: "收支\t账户金额\t收支金额\t说 明",
}
}
func (this *FamilyAccout) showDetails() {
fmt.Println("1 收支明细--------------")
fmt.Println(this.details)
}
func (this *FamilyAccout) noteIncome() {
fmt.Println("本次收入金额:")
fmt.Scanln(&this.money)
fmt.Println("本次收入说明:")
fmt.Scanln(&this.note)
this.balance += this.money
this.details += fmt.Sprintf("\n收入\t%v\t%v\t\t%v", this.balance, this.money, this.note)
}
func (this *FamilyAccout) noteOutcome() {
fmt.Println("3 登记支出--------------")
fmt.Println("本次支出金额:")
fmt.Scanln(&this.money)
if this.money > this.balance {
fmt.Println("钱不够啊,你怎么支出")
} else
{
// 钱够可以支出
this.balance -= this.money
fmt.Println("本次支出说明:")
fmt.Scanln(&this.note)
this.details += fmt.Sprintf("\n支出\t%v\t%v\t\t%v", this.balance, this.money, this.note)
}
}
func (this *FamilyAccout) exit() {
fmt.Println("4 ----退出软件---------")
this.loop = false
}
//绑定相应方法
func (this *FamilyAccout) MainMenue() {
for {
fmt.Println("--------收支记账软件-----")
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.noteIncome()
case "3":
this.noteOutcome()
case "4":
this.exit()
default:
fmt.Println("输入正确选项--------------")
}
if !this.loop {
break
}
}
fmt.Println("软件退出--------------")
}
调用
import "go_code/project1/account/utils"
func main() {
account := utils.NewFamilyAccount()
account.MainMenue()
}
package model
import "fmt"
type Customer struct {
Id int
Name string
Gender string
Age int
Phone string
Email string
}
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 {
return fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v", this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
}
func NewCustomerNoID( name string, gender string, age int, phone string, email string) Customer {
return Customer{
Name: name,
Gender: gender,
Age: age,
Phone: phone,
Email: email,
}
}
package service
import "go_code/project1/CRM/model"
type CustomerService struct {
customer []model.Customer //切片
customerNum int
autoInrecment int
}
func NewCustomerService() *CustomerService {
coustomerService := &CustomerService{
}
coustomerService.customerNum = 1
coustomerService.autoInrecment = 1
coustomer := model.NewCustomer(1, "zhangsan", "man", 20, "911", "[email protected]")
coustomerService.customer = append(coustomerService.customer, coustomer)
return coustomerService
}
func (this *CustomerService) List() []model.Customer {
return this.customer
}
func (this *CustomerService) Add(customer model.Customer) {
//确定一个分配ID的规则 定义一个自增
this.customerNum++
this.autoInrecment++
customer.Id = this.autoInrecment
this.customer = append(this.customer, customer)
}
func (this *CustomerService) Delete(id int) bool {
index := this.FindById(id)
if index == -1 {
return false
}
//从切片中删除元素
this.customer = append(this.customer[:index], this.customer[index+1:]...)
return true
}
func (this *CustomerService) UpdateById(customer model.Customer) {
for i := 0; i < len(this.customer); i++ {
if this.customer[i].Id == customer.Id {
this.customer[i] = customer
}
}
}
func (this *CustomerService) FindById(id int) int {
index := -1
for i := 0; i < len(this.customer); i++ {
if this.customer[i].Id == id {
index = i
}
}
return index
}
package main
import (
"fmt"
"go_code/project1/CRM/model"
"go_code/project1/CRM/service"
)
type customerView struct {
key string
loop bool
costomerService *service.CustomerService
}
func (this *customerView) list() {
customers := this.costomerService.List()
fmt.Println("-------------------客户列表-----------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
for i := 0; i < len(customers); i++ {
fmt.Println(customers[i].GetInfo())
}
fmt.Println("-------------------客户列表-----------------")
}
func (this *customerView) add() {
fmt.Println("-------------------添加客户-----------------")
name := ""
fmt.Println("姓名:")
fmt.Scanln(&name)
gender := ""
fmt.Println("性别:")
fmt.Scanln(&gender)
age := 0
fmt.Println("年龄:")
fmt.Scanln(&age)
phone := ""
fmt.Println("电话:")
fmt.Scanln(&phone)
email := ""
fmt.Println("邮件:")
fmt.Scanln(&email)
//ID号自动生成
customer := model.NewCustomerNoID(name, gender, age, phone, email)
this.costomerService.Add(customer)
if true {
fmt.Println("-------------------添加成功-----------------")
}
}
func (this *customerView) delete() {
fmt.Println("-------------------删除客户-----------------")
fmt.Println("选择删除客户编号:(-1取消删除)")
id := -1
fmt.Scanln(&id)
if id == -1 {
return
}
fmt.Println("请确定是否删除(y/n)")
choice := ""
fmt.Scanln(&choice)
if choice == "y" {
b := this.costomerService.Delete(id)
if b == true {
fmt.Println("!--------删除成功------")
} else {
fmt.Println("!-------删除失败-------")
}
} else {
fmt.Println("您选择取消删除")
}
}
func (this *customerView) update() {
fmt.Println("-------------------修改客户-----------------")
fmt.Println("请输入修改的用户id")
id := -1
reput:
fmt.Scanln(&id)
byId := this.costomerService.FindById(id)
if byId == -1 {
fmt.Println("用户不存在,请重新输入")
goto reput
}
fmt.Println("请重新输入修改用户的信息")
name := ""
fmt.Println("姓名:")
fmt.Scanln(&name)
gender := ""
fmt.Println("性别:")
fmt.Scanln(&gender)
age := 0
fmt.Println("年龄:")
fmt.Scanln(&age)
phone := ""
fmt.Println("电话:")
fmt.Scanln(&phone)
email := ""
fmt.Println("邮件:")
fmt.Scanln(&email)
customer := model.NewCustomer(id, name, gender, age, phone, email)
this.costomerService.UpdateById(customer)
fmt.Println("修改成功")
}
func (cv *customerView) mainMenu() {
for {
fmt.Println("-------------客户关系管理系统---------------")
fmt.Println("\t\t\t\t1 添加客户")
fmt.Println("\t\t\t\t2 修改客户")
fmt.Println("\t\t\t\t3 删除客户")
fmt.Println("\t\t\t\t4 客户列表")
fmt.Println("\t\t\t\t5 退 出")
fmt.Println("请输入")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
fmt.Println("添加客户")
cv.add()
case "2":
fmt.Println("修改用户")
cv.update()
case "3":
cv.delete()
fmt.Println("删除用户")
case "4":
fmt.Println("客户列表")
cv.list()
case "5":
cv.loop = false
default:
fmt.Printf("有问题,重新输入")
}
if ! cv.loop {
break
}
}
}
func main() {
//在主函数中创建customer实例
view := customerView{
key: "", loop: true}
view.costomerService = service.NewCustomerService()
view.mainMenu()
}