起因:
看摊的时候心血来潮,并且可以巩固知识点就写了。
注意:
1.
该系统不要乱输入,因为我没有做输入格式的检验(是我懒吗?不!我这是任性)
2.
在管理员,地摊和摊主三者的联系不怎么好,不喜勿喷,要脸。
3
效果自己运行。
这里写目录标题
代码
package main import ( "bufio" "fmt" "io" "math/rand" "os" "strconv" "strings" "time" ) type DiTan struct { MasterName string AdministratorName string // 摊位的范围 单位平方米 Scope float32 // 年租的价格 Price float32 // 所在位置 Location string // 所在集市 Bazaar string // 到租年限 EndTime string } type People struct { Username string Password string name string Sex string } type Master struct { People // 电话号码 Number string } type Administrator struct { People // 电话号码 Number string // 管理集市 Bazaars []string } func MasterRegister(master *Master) bool { // 如果没有文档存在,则新创一个文档 path := "master.txt" _, err := os.Stat(path) if err == nil || os.IsExist(err) { // 文档存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0666) if err != nil { fmt.Println("追加写入文档信息失败,err =",err) return false } defer file.Close() if UserIsExist("master",master.Username) { writer := bufio.NewWriter(file) writer.WriteString(master.Username+","+master.Password+","+master.name+","+master.Sex+","+master.Number+"\n") writer.Flush() } else { return false } } else { // 文档不存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println("创建文件并写入失败,err =",err) return false } defer file.Close() if UserIsExist("master",master.Username) { writer := bufio.NewWriter(file) writer.WriteString(master.Username+","+master.Password+","+master.Sex+","+master.Number+"\n") writer.Flush() } else { return false } } return true } func AdministratorRegister(administrator *Administrator) bool { // 如果没有文档存在,则新创一个文档 path := "administrator.txt" _, err := os.Stat(path) if err == nil || os.IsExist(err) { // 文档存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0666) if err != nil { fmt.Println("追加写入文档信息失败,err =",err) return false } defer file.Close() if UserIsExist("administrator",administrator.Username) { writer := bufio.NewWriter(file) writer.WriteString(administrator.Username+","+administrator.Password+","+administrator.name+","+administrator.Sex+","+administrator.Number+","+SliceToString(administrator.Bazaars)+"\n") writer.Flush() } else { return false } } else { // 文档不存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println("创建文件并写入失败,err =",err) return false } defer file.Close() if UserIsExist("administrator",administrator.Username) { writer := bufio.NewWriter(file) writer.WriteString(administrator.Username+","+administrator.Password+","+administrator.name+","+administrator.Sex+","+administrator.Number+","+SliceToString(administrator.Bazaars)+"\n") writer.Flush() } else { return false } } return true } func creationDiTan(tan *DiTan) bool { // 如果没有文档存在,则新创一个文档 path := "diTan.txt" _, err := os.Stat(path) if err == nil || os.IsExist(err) { // 文档存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0666) if err != nil { fmt.Println("追加写入文档信息失败,err =",err) return false } defer file.Close() if diTanIsExit(tan.Location) { writer := bufio.NewWriter(file) writer.WriteString(tan.MasterName+","+tan.AdministratorName+ "," + FloatToString(tan.Scope) + "," +FloatToString(tan.Price)+","+ tan.Location+"," + tan.Bazaar + "," + tan.EndTime + "\n") writer.Flush() } else { return false } } else { // 文档不存在 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println("创建文件并写入失败,err =",err) return false } defer file.Close() if diTanIsExit(tan.Location) { writer := bufio.NewWriter(file) writer.WriteString(tan.MasterName+","+tan.AdministratorName+"," + FloatToString(tan.Scope) + "," +FloatToString(tan.Price)+","+ tan.Location+"," + tan.Bazaar + "," + tan.EndTime + "\n") writer.Flush() } else { return false } } return true } // 判断用户是否存在 func UserIsExist(sort,username string) bool { file, err := os.Open(sort+".txt") // 检查是否有错误产生,如果有则抛出错误。 if err != nil { fmt.Println("打开文件错误:",err) } defer file.Close() reader := bufio.NewReader(file) flag := true for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") if split[0] == username{ flag = false fmt.Println("用户已存在") break } } return flag } func diTanIsExit(location string) bool { file, err := os.Open("diTan.txt") if err != nil { fmt.Println("打开文件错误:",err) } defer file.Close() reader := bufio.NewReader(file) flag := true for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") if split[6] == location { flag = false fmt.Println("摊位已被租用") break } } return flag } func DeleteUser(sort string,username string) bool { file, err := os.Open(sort+".txt") if err != nil { fmt.Println("打开文件错误:",err) } reader := bufio.NewReader(file) flag := false open, err := os.OpenFile("temp.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") if split[0] == username { flag = true fmt.Println("用户"+username+"已删除") continue } writer := bufio.NewWriter(open) writer.WriteString(readString) writer.Flush() } file.Close() open.Close() // 删除原本的文件 err = os.Remove(sort + ".txt") if err != nil { fmt.Println("删除用户文件失败,err=",err) } // 拷贝临时文件 readerFile, err := os.OpenFile(sort+".txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) f, _ := os.Open("temp.txt") defer readerFile.Close() buf := make([]byte,1024) for { n, err := f.Read(buf) if err != nil && err != io.EOF{ fmt.Println("拷贝文件读时发生错误,err:",err) return false } if n==0 { break } if _,err := readerFile.Write(buf[:n]);err != nil { fmt.Println("拷贝文件写时发生错误,err:",err) return false } } // 删除临时文件 f.Close() err = os.Remove("temp.txt") if err != nil { fmt.Println("删除临时文件失败,err=",err) } return flag } func DeleteDiTan(tan *DiTan) bool { file, err := os.Open("diTan.txt") if err != nil { fmt.Println("打开文件错误:",err) } reader := bufio.NewReader(file) flag := false open, err := os.OpenFile("temp.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") if split[4] == tan.Location && split[5] == tan.Bazaar { flag = true continue } writer := bufio.NewWriter(open) writer.WriteString(readString) writer.Flush() } file.Close() open.Close() // 删除原本的文件 err = os.Remove("diTan.txt") if err != nil { fmt.Println("删除用户文件失败,err=",err) } // 拷贝临时文件 readerFile, err := os.OpenFile("diTan.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) f, _ := os.Open("temp.txt") defer readerFile.Close() buf := make([]byte,1024) for { n, err := f.Read(buf) if err != nil && err != io.EOF{ fmt.Println("拷贝文件读时发生错误,err:",err) return false } if n==0 { break } if _,err := readerFile.Write(buf[:n]);err != nil { fmt.Println("拷贝文件写时发生错误,err:",err) return false } } // 删除临时文件 f.Close() err = os.Remove("temp.txt") if err != nil { fmt.Println("删除临时文件失败,err=",err) } return flag } // 空参构造函数 func newMaster() *Master{ master := &Master{} master.Username = "" master.Password = "" master.Sex = "" master.name = "" master.Number = "" return master } func newAdministrator() *Administrator{ administrator := &Administrator{} administrator.Username = "" administrator.Password = "" administrator.name = "" administrator.Sex = "" administrator.Number = "" administrator.Bazaars = make([]string,0) return administrator } func newDiTan() *DiTan { return &DiTan{ MasterName: "", AdministratorName: "", Scope: 0, Price: 0, Location: "", Bazaar: "", EndTime: "", } } // 各个对象的toString方法 func (m *Master)toString() string { return "摊主姓名:" + m.name +",摊主性别:" + m.Sex+ ",联系电话:" +m.Number } func (a Administrator)toString() string { return "管理员姓名:" + a.name +",管理员性别:"+a.Sex+",管理员所管理的集市:"+SliceToString(a.Bazaars) +",联系电话:"+a.Number } func (d DiTan) toString() string { return "摊主用户名:" + d.MasterName + ",摊位大小:" + FloatToString(d.Scope) + ",年租价格:" + FloatToString(d.Price) + "万,所在集市:" +d.Bazaar + ",所在地址:" +d.Location +",到期时间:"+d.EndTime } // 验证码 func Verification() bool { rand.Seed(time.Now().Unix()) var i int for true { i = rand.Intn(10000) if 9999>=i && i >= 1000 { break } } fmt.Println("验证码为:",i) fmt.Println("请输入验证码:") text := Scan() itoa := strconv.Itoa(i) if text == itoa { return true } return false } // 类型转化 func SliceToString(slice []string) string { var sum string for _, s := range slice { sum += "-" + s + "-," } return sum } func StringToSlice(s string) []string { if s == "" { return nil } var str []string split := strings.Split(s, "-") for i, s2 := range split { if i%2 != 0 { str = append(str,s2) } } return str } func FloatToString(f float32) string { return strconv.FormatFloat(float64(f), 'f', 6, 64) } func StringToFloat(s string) float32 { float, _ := strconv.ParseFloat(s,32) return float32(float) } func Scan() (text string) { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() return scanner.Text() } // 摊主登录 func masterLoginInterface() (bool,*Master) { fmt.Println("请输入用户名") userName := Scan() fmt.Println("请输入密码") password := Scan() file, err := os.Open("master.txt") if err != nil { fmt.Println("打开文件错误:",err) } defer file.Close() reader := bufio.NewReader(file) for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") master := newMaster() master.Username = split[0] master.Password = split[1] master.name = split[2] master.Sex = split[3] master.Number = split[4] if split[0] == userName && split[1] == password { fmt.Println(userName+"登录成功") return true,master } } return false,nil } // 管理员登录 func administratorLoginInterface() (bool,*Administrator) { fmt.Println("请输入用户名") userName := Scan() fmt.Println("请输入密码") password := Scan() file, err := os.Open("administrator.txt") if err != nil { fmt.Println("打开文件错误:",err) } defer file.Close() reader := bufio.NewReader(file) for { readString, err := reader.ReadString('\n') if err == io.EOF { break } split := strings.Split(readString, ",") if split[0] == userName && split[1] == password { fmt.Println(userName+"登录成功") administrator := newAdministrator() administrator.Username = split[0] administrator.Password = split[1] administrator.name = split[2] administrator.Sex = split[3] administrator.Number = split[4] administrator.Bazaars = StringToSlice(split[5]) return true,administrator } } return false,nil } func main() { fmt.Println("欢迎进入地摊管理系统,请您根据提示进行操作☺") defer fmt.Println("√√退出系统成功√√") for true { fmt.Println("---------------请输入序号---------------") fmt.Println("1.摊主登录") fmt.Println("2.管理员登录") fmt.Println("3.注册") fmt.Println("4.退出") scanner := bufio.NewScanner(os.Stdin) scanner.Scan() text := scanner.Text() switch text { case "1": loginInterface, m := masterLoginInterface() if loginInterface { for true { login := masterLogin(m) if login == 1 { break } else if login == 2 { continue } else if login == 3 { return } } } else { fmt.Println("登录失败") } case "2": b, a := administratorLoginInterface() if b { for true { login := administratorLogin(a) if login == 1 { break } else if login == 2 { continue } else if login == 3 { return } } } else { fmt.Println("登录失败") } case "3": if register() { fmt.Println("用户创建成功") fmt.Println("即将返回主界面") // 休眠3秒再返回 time.Sleep(3000000000) } else { fmt.Println("用户创建失败") } case "4": return default: fmt.Println("输入的序号错误,请重新输入") } } } // 返回 1 是回到主菜单,返回 2 是循环,返回 3 退出 func masterLogin(m *Master) int { fmt.Println("---------------摊主登录成功---------------") fmt.Println("1.查看摊位") fmt.Println("2.摊位缴费") // 这里的撤销摊位是给管理该摊位的管理员发送信息, // 待管理员确认后可以撤销摊位 // 也就是说摊主并没有实权管理摊位 fmt.Println("3.撤销摊位") fmt.Println("4.回到登录界面") fmt.Println("5.退出") newScanner := bufio.NewScanner(os.Stdin) newScanner.Scan() text := newScanner.Text() switch text { case "1": checkDiTan(m.Username) if selectionOperationONE() { fmt.Println("您的权限不足") time.Sleep(1500000000) } case "2": payTheFees(m) time.Sleep(1500000000) case "3": dismissDiTian(m) time.Sleep(1500000000) case "4": return 1 case "5": return 3 default: fmt.Println("您输入的序号错误,请重新输入") return 2 } return 2 } func administratorLogin(a *Administrator) int { fmt.Println("---------------管理员登录成功---------------") fmt.Println("1.查看摊位信息") fmt.Println("2.查看摊主信息") fmt.Println("3.新增摊位") // 这里的撤销摊位是给管理该摊位的管理员发送信息, // 管理员可直接撤销摊位 fmt.Println("4.撤销摊位") fmt.Println("5.回到登录界面") fmt.Println("6.退出") newScanner := bufio.NewScanner(os.Stdin) newScanner.Scan() text := newScanner.Text() switch text { case "1": fmt.Print("请输入您要检查的店主名称:") checkDiTan(Scan()) if selectionOperationONE() { updateDiTan() } case "2": confirmMaster() time.Sleep(1500000000) case "3": if creatDiTan() { fmt.Println("新建地摊成功") } else { fmt.Println("新建地摊失败") } case "4": tan := confirmDiTan() DeleteDiTan(tan) case "5": return 1 case "6": return 3 default: fmt.Println("您输入的序号错误,请重新输入") return 2 } return 2 } // 注册用户的函数 func register() bool { for { fmt.Println("请输入您要注册的角色") fmt.Println("1.摊主") fmt.Println("2.管理员") scan := Scan() if scan == "1" { master := newMaster() fmt.Println("请填写以下基本信息") for { fmt.Print("用户名:") text := Scan() master.Username = text if text != "" { break } fmt.Println("不支持的用户名格式请重新填写") time.Sleep(1000000000) } fmt.Print("密码:") master.Password = Scan() fmt.Print("真实姓名:") master.name = Scan() fmt.Print("性别:") master.Sex = Scan() fmt.Print("电话号码:") master.Number = Scan() for true { if Verification() { fmt.Println("√√验证成功√√") break } else { fmt.Println("××验证失败××") fmt.Println("重新验证") } } if MasterRegister(master) { return true } else { return false } } else if scan == "2" { administrator := newAdministrator() fmt.Println("请填写以下基本信息") fmt.Print("用户名:") administrator.Username = Scan() fmt.Print("密码:") administrator.Password = Scan() fmt.Print("真实姓名:") administrator.name = Scan() fmt.Print("性别:") administrator.Sex = Scan() fmt.Print("电话号码:") administrator.Number = Scan() fmt.Println("输入所管理的摊位:") for true { fmt.Println("???是否添加集市???(输入错误默认退出添加集市)") fmt.Println("1.是√") fmt.Println("2.否×") scan = Scan() if scan == "1" { fmt.Println("请输入集市名称:") administrator.Bazaars = append(administrator.Bazaars,Scan()) } else { break } } for true { if Verification() { fmt.Println("√√验证成功√√") break } else { fmt.Println("××验证失败××") fmt.Println("重新验证") } } return AdministratorRegister(administrator) } else { fmt.Println("您的输入有误,请重新输入:") } } } func creatDiTan() bool { diTan := newDiTan() fmt.Println("请填写摊位基础信息:") fmt.Print("摊主姓名:") diTan.MasterName = Scan() fmt.Print("管理员姓名:") diTan.AdministratorName = Scan() fmt.Print("摊位的大小(单位:平方):") diTan.Scope = StringToFloat(Scan()) fmt.Print("年租价格(单位:爽):") diTan.Price = StringToFloat(Scan()) fmt.Print("所在位置:") diTan.Location = Scan() fmt.Print("所在集市:") diTan.Bazaar = Scan() fmt.Print("到租年限:") diTan.EndTime = Scan() return creationDiTan(diTan) } func checkDiTan(username string) { open, err := os.Open("diTan.txt") if err != nil { fmt.Println("文件打开失败:err=",err) } defer open.Close() reader := bufio.NewReader(open) for { readString, err := reader.ReadString('\n') split := strings.Split(readString, ",") if err == io.EOF { break } if split[0] == username { tan := newDiTan() tan.MasterName = split[0] tan.AdministratorName = split[1] tan.Scope = StringToFloat(split[2]) tan.Price = StringToFloat(split[3]) tan.Location = split[4] tan.Bazaar = split[5] tan.EndTime = split[6] fmt.Println(tan.toString()) } } } /*func checkMaster(username string) { open, err := os.Open("master.txt") if err != nil { fmt.Println("文件打开失败:err=",err) } defer open.Close() reader := bufio.NewReader(open) for { readString, err := reader.ReadString('\n') split := strings.Split(readString, ",") if err == io.EOF { break } if split[0] == username { master := newMaster() master.Username = split[0] master.Password = split[1] master.name = split[2] master.Sex = split[3] master.Number = split[4] fmt.Println(master.toString()) } } }*/ func confirmMaster() *Master{ fmt.Print("请输入摊主用户名:") userName := Scan() open, err := os.Open("master.txt") if err != nil { fmt.Println("文件打开失败:err=",err) } reader := bufio.NewReader(open) master := newMaster() for true { readString, err := reader.ReadString('\n') split := strings.Split(readString, ",") if err == io.EOF { fmt.Println("不存在") break } defer open.Close() if split[0] == userName{ master.Username = split[0] master.Password = split[1] master.name = split[2] master.Sex = split[3] master.Number = split[4] fmt.Println(master.toString()) break } } return master } func confirmDiTan() *DiTan{ fmt.Print("请输入摊位所在集市:") bazaar := Scan() fmt.Print("请输入摊位所在位置:") location := Scan() open, err := os.Open("diTan.txt") if err != nil { fmt.Println("文件打开失败:err=",err) } reader := bufio.NewReader(open) tan := newDiTan() for true { readString, err := reader.ReadString('\n') split := strings.Split(readString, ",") if err == io.EOF { fmt.Println("地摊不存在") break } defer open.Close() if split[4] ==location && split[5] == bazaar { tan.MasterName = split[0] tan.AdministratorName = split[1] tan.Scope = StringToFloat(split[2]) tan.Price = StringToFloat(split[3]) tan.Location = split[4] tan.Bazaar = split[5] tan.EndTime = split[6] break } } return tan } func updateDiTan() { tan := confirmDiTan() DeleteDiTan(tan) flag := true for flag { fmt.Println("请选择您要进行的操作") fmt.Println("1.修改摊位所在位置") fmt.Println("2.摊位管理员转让") fmt.Println("3.到租年限更改") fmt.Println("4.更该摊主") fmt.Println("5.确认更改") scan := Scan() switch scan { case "1": fmt.Print("请输入地摊新位置:") tan.Location = Scan() case "2": fmt.Print("请输入转让管理员的用户名:") tan.AdministratorName = Scan() case "3": fmt.Print("请输入新的到租年限:") tan.EndTime = Scan() case "4": fmt.Print("请输入新摊主的用户名:") tan.AdministratorName = Scan() case "5": creationDiTan(tan) flag = false fmt.Println("更改摊位信息成功") default: fmt.Print("您输入的数据错误请重新输入") time.Sleep(1500000000) } } } func payTheFees(master *Master) { checkDiTan(master.Username) tan := confirmDiTan() fmt.Println("请输入您缴费的钱数") money := Scan() fmt.Println("正在给用户名为{"+tan.AdministratorName+"}的管理员发送信息") time.Sleep(1500000000) fmt.Println(money+"缴费成功") } func selectionOperationONE() bool { for true { fmt.Println("请选择您的操作") fmt.Println("1.进行摊位信息更改") fmt.Println("{回车}返回上一步") scan := Scan() if scan == "1"{ return true } else if scan == "" { return false } else { fmt.Println("您的输入有误,请重新输入") } } return false } func dismissDiTian(master *Master) { checkDiTan(master.Username) tan := confirmDiTan() fmt.Println("正在给用户名为{"+tan.AdministratorName+"}的管理员发送信息") time.Sleep(1500000000) fmt.Println("发送成功") }
码云仓库(自取不谢)
https://gitee.com/west-wood-does-not-live/Stall-management-system.git