2.24 Go之信息管理系统

2.24 Go之信息管理系统

信息管理系统

基于文本界面的客户关系管理软件,该软件可以实现对客户的插入、修改和删除,并且可以打印客户信息明细表

组成模块

  • 主模块--->customerView--->菜单的显示和处理用户操作

  • 管理模块--->customerService--->提供增、删、改、查功能

  • 用户结构体--->封装用户信息

项目目录结构

 

项目代码

Model层

package model

import "fmt"

/*
1、定义customer结构体
2、提供工厂构建结构体实例的函数
3、提供类似java的toString方法,获取结构体信息
*/
type Customer struct {
   Id int
   Name string
   Sex string
   Age int
   Phone string
   Email string
}

// 提供工厂构建customer结构体的函数
func NewCustomerByAll(id int, name string, sex string, age int, phone string, email string) Customer {
   // 返回结构体信息
   return Customer{
       Id: id,
       Name: name,
       Sex: sex,
       Age: age,
       Phone: phone,
       Email: email,
  }
}

func NewCustomerByPart(name string, sex string, age int, phone string, email string) Customer {
   // 返回结构体
   return Customer{
       Name: name,
       Sex: sex,
       Age: age,
       Phone: phone,
       Email: email,
  }
}

// 提供获取结构体信息的函数
func (this Customer) GetInfo() string {
   info := fmt.Sprintf("%v\t\t %v\t %v\t\t %v\t\t %v\t %v\t", this.Id, this.Name, this.Sex, this.Age, this.Phone, this.Email)
   return info
}

Serview层

package service

import (
   "GoInformationSys/InformationSys/model"
)

/*
提供对customer结构体的操作
增、删、改、查
*/

// 提供一个结构体,里面包含当前切片
type CustomerService struct {
   // 声明一个字段,表示当前切片含有多少个客户--->用户结构体切片
   customers []model.Customer

   // 用户id
   customerNum int
}

// 返回customerservice结构体指针,展示默认的数据
func NewCustomerService() *CustomerService {
   // 初始化一个默认的客户
   customerService := &CustomerService{}
   // 设置默认用户id
   customerService.customerNum = 1

   // 设置默认用户信息--->调用customer包下的new函数
   customer := model.NewCustomerByAll(1, "JunkingBoy", "Man", 22, "18154223156", "Lucifer@test.com")
   // 将用户信息放到customerService切片中
   customerService.customers = append(customerService.customers, customer)
   // 返回customerService结构体
   return customerService
}

// 获取customer切片的函数
func (this CustomerService) List() []model.Customer {
   return this.customers
}

// 添加客户到customers切片中
func (this *CustomerService) Add(customer model.Customer) bool {
   // 首先customerService的id自增
   this.customerNum++
   // 将传入的customerService的id设置为customer的id
   customer.Id = this.customerNum
   // 将形参放到切片中
   this.customers = append(this.customers, customer)
   return true
}

// 根据id查找客户--->没查到返回-1
func (this *CustomerService) FindById(id int) int {
   index := -1
   if id > 0 {
       // 循环遍历切片
       for i := 0; i < len(this.customers); i++ {
           if this.customers[i].Id == id {
               // 有该客户,找到该客户,返回对应的id
               index = i
          }
      }
  }
   return index
}

// 根据id删除客户
func (this *CustomerService) Delete(id int) bool {
   if 0 < id {
       targetId := this.FindById(id)
       if targetId != -1 {
           // 从切片中删除元素
           this.customers = append(this.customers[:targetId], this.customers[targetId+1:]...)
           return true
      }
  }
   return false
}

View层

package main

import (
   "GoInformationSys/InformationSys/model"
   "GoInformationSys/InformationSys/service"
   "fmt"
)

/*
定义结构体面向用户
*/
type customerView struct {
   // 接收用户输入
   key string
   // 是否循环展示主菜单--->推出时就不需要
   load bool
   // 添加字段customerService
   customerService *service.CustomerService
}

// 显示客户信息
func (this *customerView) list() {
   // 获取已经在客户列表的数据(在切片中)--->调用customerService实现的list函数
   customers := this.customerService.List()
   // 显示内容
   fmt.Println("---------------------------Customers List---------------------------")
   fmt.Println("ID\t\tNAME\t\tGENDER\t\tAGE\t\tPHONE\t\tEMAIL")
   // 循环获取拿到的客户列表信息并且打印
   for i := 0; i < len(customers); i++ {
       // 调用customer包下的getinfo函数获取信息
       fmt.Println(customers[i].GetInfo())
  }
   fmt.Printf("\n-------------------------Customers List Finish-------------------------\n\n")
}

// 添加客户的函数
func (this *customerView) add() {
   fmt.Println("---------------------Add Customer---------------------")
   // 姓名
   name := ""
   for {
       fmt.Println("Name:")
       fmt.Scanln(&name)
       if "" != name {
           break
      }
       fmt.Println("Empty Name, Please Try Again!")
  }

   // 性别
   sex := ""
   for {
       fmt.Println("Sex:")
      fmt.Scanln(&sex)
       if "" != sex && sex == "Man" || sex == "Woman" || sex == "man" || sex == "woman" {
           break
      }
       fmt.Println("Sex Error, Please Try Again!")
  }

   // 年龄
   age := 0
   for {
       fmt.Println("Age:")
       fmt.Scanln(&age)
       if age > 0 && age < 120 {
           break
      }
       fmt.Println("Age Error, Please Try Again!")
  }

   // 电话号码
   phone := ""
   for {
       fmt.Println("Phone Number:")
       fmt.Scanln(&phone)
       if "" != phone && len(phone) == 11 {
           break
      }
       fmt.Println("Phone Number Error, Please Try Again!")
  }

   // 邮箱
   email := ""
   for {
       fmt.Println("Email:")
       fmt.Scanln(&email)
       if "" != email {
           break
      }
       fmt.Println("Empty Email, Please Try Again!")
  }

   //构建一个新的Customer实例--->调用customer包下提供的构造函数
   //注意: id号,没有让用户输入,id是唯一的,需要系统分配
   customer := model.NewCustomerByPart(name, sex, age, phone, email)

   // 调用service包下定义的添加函数
   if this.customerService.Add(customer) {
       fmt.Println("---------------------Add Successfully---------------------")
  }else {
       fmt.Println("---------------------Add Fail, Can not Find The Customer By Id---------------------")
  }
}

// 获取用户输入的id并且对客户列表的id进行删除
func (this *customerView) delete() {
   fmt.Println("---------------------Delete Customer---------------------")
   fmt.Print("Select the customer number to be deleted(-1Exit):")
   id := -1
   fmt.Scanln(&id)
   if id != -1 && id != 1 {
       fmt.Println("Confirm deletion(Y/N):")
       choose := ""
       fmt.Scanln(&choose)
       switch choose {
       case "Y":
           // 调用customerService下的delete函数
           judge := this.customerService.Delete(id)
           if judge {
               fmt.Println("---------------------Delete Successfully---------------------")
          }else {
               fmt.Println("---------------------Delete Fail, Can not Find The Customer By Id---------------------")
          }
       case "y":
           // 调用customerService下的delete函数
           judge := this.customerService.Delete(id)
           if judge {
               fmt.Println("---------------------Delete Successfully---------------------")
          }else {
               fmt.Println("---------------------Delete Fail, Can not Find The Customer By Id---------------------")
          }
       case "N":
           break
       case "n":
           break
       default:
           fmt.Println("Unknow Input,Please Try Again!")
      }
  }else if id == 1 {
       fmt.Println("Can Not Delete This Data!")
  }
}

// 退出软件
func (this *customerView) exit() {
   fmt.Println("Confirm Exit(Y/N):")
   for {
       fmt.Scanln(&this.key)
       //switch this.key {
       //case "Y":
       //   break
       //case "y":
       //   break
       //case "N":
       //   break
       //case "n":
       //   break
       //default:
       //   fmt.Println("输入有误,确认是否退出(Y/N)")
       //}
       if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n" {
           break
      }

       fmt.Print("Error Input, Confirm Exit(Y/N):")
  }

   if this.key == "Y" || this.key == "y" {
       this.load = false
  }
}

// 显示主菜单
func (this *customerView) mainMenu() {
   for {
       fmt.Println("-----------------Information System-----------------")
       fmt.Println("                 1 Add Customer")
       fmt.Println("                 2 Modify Customer")
       fmt.Println("                 3 Delete Customer")
       fmt.Println("                 4 Customers List")
       fmt.Println("                 5 Exit System")
       fmt.Print("Please Input What You Want(1-5):")

       fmt.Scanln(&this.key)
       switch this.key {
       case "1":
           this.add()
       case "2":
           fmt.Println("Modify Customer Information")
       case "3":
           this.delete()
       case "4":
           this.list()
       case "5":
           this.exit()
       default:
           fmt.Println("Input Error, Please Try Again!")
           continue
      }

       // 结构体customerView的load属性不为true直接退出
       if !this.load {
           break
      }
  }
   fmt.Println("Exit System!")
}

// 函数调用
func main() {
   //在main函数中,创建一个customerView,并运行显示主菜单..
   customerView := customerView{
       key : "",
       load : true,
  }
   //这里完成对customerView结构体的customerService字段的初始化
   customerView.customerService = service.NewCustomerService()
   if customerView.load == true {
       //显示主菜单..
       customerView.mainMenu()
  }
}
posted @   俊king  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示