golang实现书籍管理系统

栏目: Go · 发布时间: 6年前

内容简介:author:shuaibing.huo@gmail.com

author:shuaibing.huo@gmail.com

package main
import (
    "fmt"
    "os"
)
//使用函数实现一个简单的图书管理系统
//每本书有书名、作者、价格、上架信息
//用户可以在控制台添加书籍、修改书籍信息、打印所有书籍列表

//需求分析
//0. 定义结构体
type book struct{
    title string
    author string
    price float32
    publish bool
}
//1. 打印菜单
func showmenu(){
    fmt.Println("欢迎登陆BMS!")
    fmt.Println("1.添加书籍")
    fmt.Println("2.修改书籍")
    fmt.Println("3.展示所有书籍")
    fmt.Println("4.退出")
}

func userInput() *book {
    var (
        title string
        author string
        price float32
        publish bool
        )
    fmt.Println("请根据提示输入相关内容")
    fmt.Print("请输入书名:")
    fmt.Scanln(&title)
    fmt.Print("请输入作者:")
    fmt.Scanln(&author)
    fmt.Print("请输入价格:")
    fmt.Scanln(&price)
    fmt.Print("请输入是否上架(true|false):")
    fmt.Scanln(&publish)
    fmt.Println(title,author,price,publish)
    book := newbook(title,author,price,publish)
    return book
}
//2. 等待用户输入菜单选项
//定义一个book指针的切片,用来存储所有书籍
var allbooks = make([]*book,0,200)

//定义一个创建新书的构造函数
func newbook(title,author string, price float32, publish bool) *book{
    return &book{
        title: title,
        author: author,
        price: price,
        publish: publish,
    }
}
//3. 添加书籍的函数
func addbook(){
    var (
        title string
        author string
        price float32
        publish bool
    )
    fmt.Println("请根据提示输入相关内容")
    fmt.Print("请输入书名:")
    fmt.Scanln(&title)
    fmt.Print("请输入作者:")
    fmt.Scanln(&author)
    fmt.Print("请输入价格:")
    fmt.Scanln(&price)
    fmt.Print("请输入是否上架(true|false):")
    fmt.Scanln(&publish)
    fmt.Println(title,author,price,publish)
    book := newbook(title,author,price,publish)
    for _, b := range allbooks{
        if b.title == book.title{
            fmt.Printf("《%s》这本书已经存在",book.title)
            return
        }
    }
    allbooks = append(allbooks,book)
    fmt.Println("添加书籍成功!")
}
//4. 修改书籍的函数
func updatebook(){
    book := userInput()
    for index, b := range allbooks{
        if b.title == book.title{
            allbooks[index] = book
            fmt.Printf("书名:《%s》更新成功!",book.title)
            return 
        }
    }
    fmt.Printf("书名:《%s》不存在!", book.title )
}
//5. 展示书籍的函数
func showbook(){
    if len(allbooks) == 0 {
        fmt.Println("啥也么有")
    }
    for _, b := range allbooks {
        fmt.Printf("《%s》作者:%s 价格:%.2f 是否上架销售: %t\n",b.title,b.author,b.price,b.publish)
    }
}
//6. 退出 os.Exit(0)

func main(){
    for {
        showmenu()
        var option int
        fmt.Scanln(&option)
        switch option {
        case 1:
            addbook()
        case 2:
            updatebook()
        case 3:
            showbook()
        case 4:
            os.Exit(0)
        }
    }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

P3P Web隐私

P3P Web隐私

克劳娜著、技桥译 / 克劳娜 / 清华大学出版社 / 2004-5 / 45.0

自万维网络中出现商业站点以来,基于Web的商业需求和用户的隐私权利之间就存在着不断的斗争。Web开发者们需要收集有关用户的信息,但是他们也需要表示出对用户隐私的尊重。因此隐私偏好工程平台,或者称之为P3P,就作为满足双方利益的技术应运而生了。 P3P由万维网协会研制,它为Web用户提供了对自己公开信息的更多的控制。支持P3P的Web站点可以为浏览者声明他们的隐私策略。支持P3P的浏览......一起来看看 《P3P Web隐私》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具