简易ATM机
自动柜员机,即ATM,是指银行在不同地点设置一种小型机器,利用一张信用卡大小的胶卡上的磁带记录客户的基本户口资料(通常就是银行卡),让客户可以通过机器进行提款、存款、转账等银行柜台服务,大多数客户都把这种自助机器称为自动提款机。本小节将模拟一个简易ATM机,实现其存款、取款、查询余额等功能。
需求分析
开发一个单机版简单的ATM机程序,暂不涉及数据库相关操作,实现只有一个用户的查询余额、取款、存款、退出功能。
在ATM程序运行之前,准备好一个账户名、账户对应的密码和账户对应的余额。程序运行提示输入密码,输入用户输入密码,并进行验证。如果输入正确则显示用户选择菜单(1.查询余额 2.存钱 3.取钱 4.退出)。
代码实现
代码示例:【23-1-1】
目录结构:
atm
|
—— controllers
| |
—— account.go
|
—— main.go
文件man.go是该ATM项目的入口文件,主要通过入口main函数实现用户密码验证、显示用户选择菜单和调用查询余额、存钱、取钱、退出等功能,代码详情如下。
package main
import (
"atm/controllers"
"log"
"net/http"
)
func main() {
http.HandleFunc("/query", controllers.Query)
http.HandleFunc("/withdraw", controllers.Withdraw)
http.HandleFunc("/deposit", controllers.Deposit)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Panic(err)
}
}
account.go文件实现用户操作相关的业务逻辑。文件中的Account结构体用于存储用户共有的属性,Withdraw方法用于用户的取款操作,Deposit方法用于用户的存款操作,Query方法用于用户的查询操作,代码详情如下。
package controllers
import (
"encoding/json"
"net/http"
"strconv"
)
type Account struct {
UserName string
Password string
Amounts float64
}
var account = Account{UserName: "oldboy", Password: "123456", Amounts: 10000}
func Base(w http.ResponseWriter, r *http.Request) (user Account, code int, err string) {
if r.Method != "POST" {
err, code = "Method Not Allowed", http.StatusMethodNotAllowed
return
}
userName, password := r.PostFormValue("user_name"), r.PostFormValue("password")
money, _ := strconv.ParseFloat(r.PostFormValue("money"), 64)
if userName == "" || password == "" {
err, code = "fail parameter error", http.StatusBadRequest
return
}
user = Account{UserName: userName, Password: password, Amounts: money}
return
}
type ResponseJson struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
}
func SendOK(w http.ResponseWriter, res ResponseJson) {
w.WriteHeader(http.StatusOK)
bytes, _ := json.Marshal(res)
w.Write(bytes)
}
// 取款
func Withdraw(w http.ResponseWriter, r *http.Request) {
user, code, err := Base(w, r)
if err != "" {
http.Error(w, err, code)
return
}
if user.Amounts <= 0 || user.Amounts > account.Amounts {
http.Error(w, "fail parameter error", http.StatusBadRequest)
return
}
if user.UserName == account.UserName && user.Password == account.Password {
account.Amounts -= user.Amounts
res := ResponseJson{Code: 200, Data: map[string]interface{}{
"user": account.UserName,
"amounts": account.Amounts,
}, Message: "取款成功!"}
SendOK(w, res)
}
}
// 存款
func Deposit(w http.ResponseWriter, r *http.Request) {
user, code, err := Base(w, r)
if err != "" {
http.Error(w, err, code)
return
}
if user.Amounts <= 0 {
http.Error(w, "fail parameter error", http.StatusBadRequest)
return
}
if user.UserName == account.UserName && user.Password == account.Password {
account.Amounts += user.Amounts
res := ResponseJson{Code: 200, Data: map[string]interface{}{
"user": account.UserName,
"amounts": account.Amounts,
}, Message: "存款成功!"}
SendOK(w, res)
}
}
// 查询
func Query(w http.ResponseWriter, r *http.Request) {
user, code, err := Base(w, r)
if err != "" {
http.Error(w, err, code)
return
}
if user.UserName == account.UserName && user.Password == account.Password {
res := ResponseJson{Code: 200, Data: map[string]interface{}{
"user": account.UserName,
"amounts": account.Amounts,
}, Message: "查询成功!"}
SendOK(w, res)
}
}
查询:
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" -d 'user\_name=oldboy&password=123456' "http://127.0.0.1:8080/query"
存钱:
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" -d 'user\_name=oldboy&password=123456&money=1000' "http://127.0.0.1:8080/deposit"
取钱:
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" -d 'user\_name=oldboy&password=123456&money=1000' "http://127.0.0.1:8080/withdraw"
老男孩教育专注Linux云计算运维工程师、Python全栈+人工智能、Python自动化运维开发、、数据分析、新媒体运营、MySQLDBA开发、K8S微服务、等互联网课程培训。欢迎对it行业感兴趣的朋友们来公司考察及学习。
推荐阅读: