项目开发流程

小程序1

package utils

import (
	//"flag"
	"fmt"
)
type FamilyAccount struct {
	//声明字段
	key   string
	//声
	loop bool
	//
	balance float64
	money float64
	note string
	flag bool
	details string
}
//显示明细做成方法
func (n *FamilyAccount)showdetails(){
			if n.flag{
				fmt.Println(n.details)
			}else {
				fmt.Println("还没有存款来一笔吧")
			}
}
func (n *FamilyAccount)income(){
			fmt.Printf("输入收入金额: ")
			fmt.Scanln(&n.money)
			n.balance+=n.money
			fmt.Printf("输入收入说明: ")
			fmt.Scanln(&n.note)
			n.details += fmt.Sprintf("收入\t%v\t\t%v\t\t%v\n",n.balance,n.money,n.note)
			n.flag=true
}
func (n *FamilyAccount)pay(){
		fmt.Printf("输入支出金额: ")
		fmt.Scanln(&n.money)
		if n.money< n.balance{
			n.balance-=n.money
		}else{
			fmt.Println("你发的存款不够你这次消费")
			//return
		}
		if n.balance<=0{
			n.flag=false
		}

		fmt.Printf("输入支出说明: ")
		fmt.Scanln(&n.note)
		n.details += fmt.Sprintf("支出\t%v\t\t%v\t\t%v\n",n.balance,n.money,n.note)
}
func (n *FamilyAccount)exit(){
	var b string
	fmt.Printf("是否真要退出选择y/n:")
	for {
		fmt.Scanln(&b)
		if b=="y"|| b == "n"{
			break
		}
		fmt.Println("输入有误")
	}
	if b == "y"{
		n.loop= false
	}

}

func (n *FamilyAccount)MainMenu (){
			for n.loop{
			 		fmt.Println("-----------------------家庭收支记账----------------")
		     		fmt.Println("                         1.收支明细")
					fmt.Println("                         2.登记收入")
					fmt.Println("                         3.登记支出")
					fmt.Println("                         4.退出    ")
					fmt.Printf("请选择1-4: ")
					//声明变量接收用户
					//var t string
					fmt.Scanln(&n.key)
					switch n.key {
						case "1":
							n.showdetails()
						case "2":
							n.income()

						case "3":
							n.pay()
						case "4":
						n.exit()
				}
	fmt.Println("退出这个记账软件的使用")
}
}
func NewFamilyAccount() *FamilyAccount{
	return &FamilyAccount{
		key: "",
		loop: true,
		balance: 10000,
		money: 0.0,
		note: "",
		flag: false,
		details: "收支\t账户金额\t收支金额\t说明\n",
	}
}
-------------------------------------------------------------
package main

import "src01/go_code/src/chapter13/familyaccunt/utils"
func main(){
	t :=utils.NewFamilyAccount()
	t.MainMenu()
}

  示例2

package model

import "fmt"

//声明Customer结构体,表示一个客户信息
type Customer struct{
	Id int
	Nane 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,
		Nane: name,
		Gender: gender,
		Age: age,
		Phone: phone,
		Email: email,
	}
} 
func NewCustomer2(name string,gender string,age int,phone string,email string) Customer{
	return Customer{
		//Id: id,
		Nane: 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 \n",this.Id,this.Nane,this.Gender,this.Age,this.Phone,this.Email)
}

  第二个文件

package service

import (
	"src01/go_code/src/chapter14/customerManage/model"
)

//该结构体完成对customer的操作包括增删改查
type CustomerService struct{
	coustmers []model.Customer
	//声明一个字段表示当前切片含有多少个客户
	//该字段后面,也可以作为新客户id 号
	coustmerNum int
}
func NewCustomerService() *CustomerService{
	customerservice :=&CustomerService{}
	customerservice.coustmerNum=1
	customer:=model.NewCustomer(customerservice.coustmerNum,"张三","男",32,"苹果", "123@qq.com")
	customerservice.coustmers=append(customerservice.coustmers,customer)
	return customerservice
}
func (this *CustomerService)List()[]model.Customer{
	return this.coustmers
}
func (this *CustomerService)Add(Customer model.Customer) bool{
	//确定id 规则
	this.coustmerNum++
	Customer.Id=this.coustmerNum
	this.coustmers=append(this.coustmers, Customer)
	return true
}
func (this *CustomerService)Delete(v int) bool{
	index :=this.FindByld(v)
	r:=false
	if index == -1{
		// fmt.Println("删除ID不存在")
		return r
	}
	if index == len(this.coustmers) {
		this.coustmers = this.coustmers[:len(this.coustmers)-1] //删除最后一个元素
		r = true
	}else if index == 0 {
		this.coustmers = this.coustmers[1:] //删除开头1个元素
		r= true
	}else {
		this.coustmers= append(this.coustmers[:index],this.coustmers[index+1:]...) //删除中间一个元素
		r= true
	}
	return r
}
//根据id查索引
func (this *CustomerService) FindByld(id int) int{
	//var t int 
	var b bool
	for i,v:=range this.coustmers{
		if v.Id==id{
			id = i
			b= true
		}
	}
	if b {
		return id
	}else {
		return -1
	}
}
//判断输入ID号是否存在
func (this *CustomerService) FindID(id int) bool{
	//var t int 
	var b bool
	for i,v:=range this.coustmers{
		if v.Id==id{
			id = i
			b= true
		}
	}
	return b
}
//修改 需要传入的参数 用户名及ID
func (this *CustomerService)Revise(Customer model.Customer,id int){
	//确定id 规则
	Customer.Id=id
	index :=this.FindByld(id)
	this.coustmers[index]=Customer
}

  main 文件

package main

import (
	"fmt"
	"src01/go_code/src/chapter14/customerManage/model"
	"src01/go_code/src/chapter14/customerManage/service"
)
type coustmerView struct{
	key string
	loop bool
	coustmerService *service.CustomerService
}
//显示主菜单
func (cv *coustmerView) mainMenu(){
	t1:
	for{
		//
		fmt.Println("------------------------------客户信息管理系统-----------------------------")
		fmt.Println("                              1. 添 加 客 户")
		fmt.Println("                              2. 修 改 客 户")
		fmt.Println("                              3. 删 除 客 户")
		fmt.Println("                              4. 客 户 列 表")
		fmt.Println("                              5. 退       出")
		fmt.Printf("请选择(1-5):")
		fmt.Scanln(&cv.key)
		switch cv.key{
		
		case "1":
			cv.add()
		case "2":
			cv.revise()
		case "3":
			cv.delete()
		case "4":
			cv.list()
		case "5":
			t2:
			fmt.Println("请选择是否退出")
			v := ""
			fmt.Scanln(&v)
			if v == "y" || v == "Y" {
				break t1
			}else if v == "n"|| v == "N" {
				goto t1
			}else {
				goto t2
			}
		}
	}
	fmt.Println("退出客户关系管理")
}
func (this *coustmerView)list(){
	//获取到当前所有客户信息
	customers := this.coustmerService.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 *coustmerView)add(){
	fmt.Println("----------------------------添加客户------------------")
	fmt.Printf("姓名")
	name :=""
	fmt.Scanln(&name)
	fmt.Printf("性别")
	gender :=""
	fmt.Scanln(&gender)
	fmt.Printf("年龄")
	age :=0
	fmt.Scanln(&age)
	fmt.Printf("电话")
	phone :=""
	fmt.Scanln(&phone)
	fmt.Printf("邮件")
	email :=""
	fmt.Scanln(&email)
	//coustmer:= model.Customer{Nane: name,Gender: gender,Age:age,Phone:phone,Email: email }
	coustmer := model.NewCustomer2(name,gender,age,phone,email)
	//this.coustmerService.Add(coustmer)
	if this.coustmerService.Add(coustmer){
	fmt.Println("-----------------------------------添加完成--------------------------------")
	}else {
		fmt.Println("-----------------------------------添加失败--------------------------------")	
	}
}


func (this *coustmerView)revise(){
	fmt.Println("----------------------------修改客户------------------")
	fmt.Printf("输入要修改的用户ID ")
	var t int
	fmt.Scanln(&t)
	if !this.coustmerService.FindID(t){
		fmt.Println("ID不存在")
		return
	}
		fmt.Printf("姓名")
		name :=""
		fmt.Scanln(&name)
		fmt.Printf("性别")
		gender :=""
		fmt.Scanln(&gender)
		fmt.Printf("年龄")
		age :=0
		fmt.Scanln(&age)
		fmt.Printf("电话")
		phone :=""
		fmt.Scanln(&phone)
		fmt.Printf("邮件")
		email :=""
		fmt.Scanln(&email)
		coustmer := model.NewCustomer2(name,gender,age,phone,email)
		this.coustmerService.Revise(coustmer,t)
	fmt.Println("-----------------------------------修改完成--------------------------------")

}

func (this *coustmerView)delete(){
	fmt.Println("----------------------------删除客户------------------")
	t :=-1
	fmt.Printf("输入要删除的用户ID(-1退出): ")
	fmt.Scanln(&t)
	if t == -1 {
		return
	}
	fmt.Printf("是否真的要删除(y/n)")
	s := ""
	fmt.Scanln(&s)
	
	if s == "y"{
		if this.coustmerService.Delete(t){
			fmt.Println("-----------------------删除完成-----------------------")
		}else{
			fmt.Println("删除ID不存在")
		}
	}
	
	if this.coustmerService.Delete(t){

	}

}

func main(){
	coustmerView :=coustmerView{
		key: "",
		loop: true,
	}
	coustmerView.coustmerService=service.NewCustomerService()
	coustmerView.mainMenu()

}

  

posted @ 2026-03-22 18:43  烟雨楼台,行云流水  阅读(0)  评论(0)    收藏  举报