1 package customer 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 ) 8 9 type customer struct { 10 Name string 11 Sex string 12 Age int 13 Phone string 14 Email string 15 Address string 16 } 17 18 type CusSlice []customer 19 20 type MainDisplay struct { 21 CusList CusSlice 22 } 23 24 func (self *MainDisplay) SaveJsonToFile() { 25 dir, err := os.Getwd() 26 fmt.Println("当前执行路径:", dir, err) 27 file := dir + "/data.json" 28 var fh *os.File 29 30 fi, _ := os.Stat(file) 31 if fi == nil { 32 fh, _ = os.Create(file) 33 } else { 34 fh, _ = os.OpenFile(file, os.O_RDWR, 0666) 35 } 36 defer fh.Close() 37 38 bytes, _ := json.Marshal(*self) 39 fh.Write(bytes) 40 } 41 42 func (self *MainDisplay) LoadJsonByFile() { 43 dir, err := os.Getwd() 44 fmt.Println("当前执行路径:", dir, err) 45 file := dir + "/data.json" 46 var fh *os.File 47 48 fi, _ := os.Stat(file) 49 if fi == nil { 50 fh, _ = os.Create(file) 51 } else { 52 fh, _ = os.OpenFile(file, os.O_RDWR, 0666) 53 } 54 defer fh.Close() 55 56 bytes := make([]byte, fi.Size()) 57 fh.Seek(0, 0) 58 fh.Read(bytes) 59 json.Unmarshal(bytes, self) 60 61 } 62 63 func (self *MainDisplay) AddCustomer() { 64 var Name string 65 var Sex string 66 var Age int 67 var Phone string 68 var Email string 69 var Address string 70 71 fmt.Print("请输入姓名:") 72 fmt.Scanln(&Name) 73 fmt.Print("请输入性别:") 74 fmt.Scanln(&Sex) 75 fmt.Print("请输入年龄:") 76 fmt.Scanln(&Age) 77 fmt.Print("请输入电话:") 78 fmt.Scanln(&Phone) 79 fmt.Print("请输入邮箱:") 80 fmt.Scanln(&Email) 81 fmt.Print("请输入住址:") 82 fmt.Scanln(&Address) 83 84 self.CusList = append(self.CusList, customer{Name, Sex, Age, Phone, Email, Address}) 85 fmt.Println("添加成功!") 86 fmt.Println() 87 fmt.Println() 88 fmt.Println() 89 } 90 91 func (self *MainDisplay) ModifyCustomer() { 92 var Name string 93 var Sex string 94 var Age int 95 var Phone string 96 var Email string 97 var Address string 98 var id int 99 100 fmt.Print("请输入要修改的序号:") 101 fmt.Scanln(&id) 102 if len(self.CusList) < id { 103 fmt.Println("修改失败!序列号不存在!") 104 fmt.Println() 105 fmt.Println() 106 fmt.Println() 107 return 108 } 109 fmt.Print("请输入修改后的姓名:") 110 fmt.Scanln(&Name) 111 fmt.Print("请输入修改后的性别:") 112 fmt.Scanln(&Sex) 113 fmt.Print("请输入修改后的年龄:") 114 fmt.Scanln(&Age) 115 fmt.Print("请输入修改后的电话:") 116 fmt.Scanln(&Phone) 117 fmt.Print("请输入修改后的邮箱:") 118 fmt.Scanln(&Email) 119 fmt.Print("请输入修改后的住址:") 120 fmt.Scanln(&Address) 121 122 if Name == "" { 123 Name = self.CusList[id-1].Name 124 } 125 if Sex == "" { 126 Sex = self.CusList[id-1].Sex 127 } 128 if Age == 0 { 129 Age = self.CusList[id-1].Age 130 } 131 if Phone == "" { 132 Phone = self.CusList[id-1].Phone 133 } 134 if Email == "" { 135 Email = self.CusList[id-1].Email 136 } 137 if Address == "" { 138 Address = self.CusList[id-1].Address 139 } 140 self.CusList[id-1].Name = Name 141 self.CusList[id-1].Sex = Sex 142 self.CusList[id-1].Age = Age 143 self.CusList[id-1].Phone = Phone 144 self.CusList[id-1].Email = Email 145 self.CusList[id-1].Address = Address 146 147 fmt.Println("修改成功!") 148 fmt.Println() 149 fmt.Println() 150 fmt.Println() 151 } 152 153 func (self *MainDisplay) ShowCustomer() { 154 155 fmt.Printf("%20v%20v%20v%20v%20v%20v%20v\n\n", "Sort", "Name", "Sex", "Age", "Phone", "Email", "Address") 156 for index, val := range self.CusList { 157 fmt.Printf("%20v%20v%20v%20v%20v%20v%20v\n", index+1, val.Name, val.Sex, val.Age, val.Phone, val.Email, val.Address) 158 } 159 fmt.Println() 160 fmt.Println() 161 fmt.Println() 162 } 163 164 func (self *MainDisplay) DeleteCustomer() { 165 var id int 166 167 fmt.Print("请输入要修改的序号:") 168 fmt.Scanln(&id) 169 if len(self.CusList) < id { 170 fmt.Println("修改失败!序列号不存在!") 171 fmt.Println() 172 fmt.Println() 173 fmt.Println() 174 return 175 } 176 choise1 := "" 177 fmt.Printf("是否确认删除'%v'退出?(y/n)", self.CusList[id-1].Name) 178 fmt.Scanln(&choise1) 179 if choise1 == "y" { 180 for i := id; i < len(self.CusList); i++ { 181 self.CusList[i-1] = self.CusList[i] 182 } 183 self.CusList = self.CusList[:len(self.CusList)-2] 184 } 185 fmt.Println() 186 fmt.Println() 187 fmt.Println() 188 189 } 190 191 func (self *MainDisplay) ViewMain() { 192 193 self.LoadJsonByFile() 194 195 mainfor: 196 for { 197 var choise int 198 fmt.Println("-----------------客户信息管理系统-----------------") 199 fmt.Println(" 1. 客 户 列 表") 200 fmt.Println(" 2. 添 加 客 户") 201 fmt.Println(" 3. 修 改 客 户") 202 fmt.Println(" 4. 删 除 客 户") 203 fmt.Println(" 5. 退 出") 204 fmt.Print("清选择(1~5)操作:") 205 fmt.Scanln(&choise) 206 switch choise { 207 case 1: 208 self.ShowCustomer() 209 case 2: 210 self.AddCustomer() 211 case 3: 212 self.ModifyCustomer() 213 case 4: 214 self.DeleteCustomer() 215 case 5: 216 choise1 := "" 217 fmt.Print("是否确认退出?(y/n)") 218 fmt.Scanln(&choise1) 219 if choise1 == "y" { 220 break mainfor 221 } 222 fmt.Println() 223 fmt.Println() 224 fmt.Println() 225 226 default: 227 fmt.Println("输入错误,请重新选择!") 228 fmt.Println() 229 fmt.Println() 230 fmt.Println() 231 } 232 } 233 self.SaveJsonToFile() 234 } 235 236 func CustomerFactory() *MainDisplay { 237 return &MainDisplay{} 238 } 239 240 func TestCustomer() { 241 m := CustomerFactory() 242 m.ViewMain() 243 }
标签:语法,Scanln,CusList,fmt,基础,id,Golang,Println,self From: https://www.cnblogs.com/watermeloncode/p/17707953.html