Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "strings"
- )
- type product struct {
- id int
- name string
- stock int
- }
- type order struct {
- id int
- name string
- idProduct int
- quantity int
- }
- type user struct {
- username string
- password string
- }
- func main() {
- var products []product
- var users []user
- var orders []order
- var isLogin = false
- var input, idOrder, idProduct, quantity, action, id int
- var username, password, name string
- for !isLogin {
- displayGuestMenu()
- _, _ = fmt.Scanf("%d", &action)
- if action == 1 {
- isLogin = login(users)
- } else if action == 2 {
- fmt.Println("Masukkan username:")
- _, _ = fmt.Scanf("%s", &username)
- fmt.Println("Masukkan password:")
- _, _ = fmt.Scanf("%s", &password)
- register(username, password, &users)
- } else if action == 0 {
- fmt.Println("Thank you for using i-Shope! :)")
- break
- }
- }
- for isLogin {
- displayMenu()
- _, _ = fmt.Scanf("%d", &input)
- switch input {
- case 0:
- fmt.Println("Thank you for using i-Shope! :)")
- isLogin = false
- break
- case 1:
- showListProduct(products)
- break
- case 2:
- fmt.Println("Masukkan id product:")
- _, _ = fmt.Scanf("%d", &id)
- showProductById(id, products)
- break
- case 3:
- addProduct(&products)
- break
- case 4:
- fmt.Println("Masukkan id product:")
- _, _ = fmt.Scanf("%d", &id)
- updateProductById(id, &products)
- break
- case 5:
- fmt.Println("Masukkan id product:")
- _, _ = fmt.Scanf("%d", &id)
- deleteProductById(id, &products)
- break
- case 6:
- fmt.Println("Masukkan order sesuai format: idOrder userName idProduct Quantity")
- _, _ = fmt.Scanf("%d %s %d %d", &idOrder, &name, &idProduct, &quantity)
- orderProduct(order{idOrder, name, idProduct, quantity}, &products, &orders)
- break
- default:
- fmt.Println("Menu tidak dikenali, coba cek lagi!")
- break
- }
- }
- }
- func displayGuestMenu() {
- fmt.Println("Welcome to i-Shope :)")
- fmt.Println("Guest menu:")
- fmt.Println("1. Login")
- fmt.Println("2. Register")
- fmt.Println("0. Cancel")
- }
- func displayMenu() {
- fmt.Println("[======================]")
- fmt.Println("List menu: ")
- fmt.Println("1.] -> Show list product")
- fmt.Println("2.] -> Show product by id")
- fmt.Println("3.] -> Add product")
- fmt.Println("4.] -> Update product")
- fmt.Println("5.] -> Delete product")
- fmt.Println("6.] -> Order product")
- fmt.Println("0.] -> [[ Exit Program ]]")
- fmt.Println("[======================]")
- }
- func orderProduct(order order, products *[]product, orders *[]order) {
- for i, p := range *products {
- if p.id == order.idProduct {
- if p.stock >= 1 && p.stock >= order.quantity {
- (*products)[i] = product{p.id, p.name, p.stock - order.quantity}
- *orders = append(*orders, order)
- printOrders(orders)
- fmt.Println("Terimakasih, anda sudah berhasil melakukan pemesanan terhadap product kami :)")
- } else {
- fmt.Println("Maaf, stock sudah habis atau tidak mencukupi!")
- }
- return
- }
- }
- }
- func printOrders(orders *[]order) {
- fmt.Println("Berhasil di order:")
- for _, o := range *orders {
- fmt.Println("ID: ", o.id)
- fmt.Println("Username: ", o.name)
- fmt.Println("ID Product: ", o.idProduct)
- fmt.Println("Quantity: ", o.quantity)
- fmt.Println("=======================")
- }
- }
- func deleteProductById(id int, products *[]product) {
- for i, p := range *products {
- if p.id == id {
- *products = append((*products)[:i], (*products)[i+1:]...)
- showListProduct(*products)
- return
- }
- if i == len(*products) - 1 {
- fmt.Println("Product not found!")
- }
- }
- }
- func updateProductById(id int, products *[]product) {
- newName := ""
- newStock := 0
- fmt.Println("Masukkan nama terbaru product")
- consoleReader := bufio.NewReader(os.Stdin)
- newName, _ = consoleReader.ReadString('\n')
- fmt.Println("Masukkan jumlah stock")
- _, _ = fmt.Scanf("%d", &newStock)
- for i, p := range *products {
- if p.id == id {
- (*products)[i] = product{id, newName, newStock}
- showListProduct(*products)
- return
- }
- if i == len(*products) - 1 {
- fmt.Println("Product not found!")
- }
- }
- }
- func addProduct(products *[]product) {
- var id, stock int
- name := ""
- fmt.Println("Masukkan id product")
- _, _ = fmt.Scanf("%d", &id)
- fmt.Println("Masukkan nama product")
- consoleReader := bufio.NewReader(os.Stdin)
- name, _ = consoleReader.ReadString('\n')
- fmt.Println("Masukkan jumlah stock")
- _, _ = fmt.Scanf("%d", &stock)
- *products = append(*products, product{id, name, stock})
- showListProduct(*products)
- }
- func showProductById(id int, products []product) {
- if len(products) == 0 {
- fmt.Println("There's no product yet!")
- }
- for i, p := range products {
- if p.id == id {
- fmt.Println("ID: ", p.id)
- fmt.Println("Stock: ", p.stock)
- fmt.Println("Name: ", p.name)
- return
- }
- if i == len(products) - 1 {
- fmt.Println("Product not found!")
- }
- }
- }
- func showListProduct(products []product) {
- if len(products) == 0 {
- fmt.Println("There's no product yet, please add a product before")
- return
- }
- fmt.Println("List products:")
- for _, p := range products {
- fmt.Println("ID: ", p.id)
- fmt.Println("Stock: ", p.stock)
- fmt.Println("Name: ", p.name)
- }
- }
- func checkCredential(username string, password string, users []user) bool {
- for _, u := range users {
- if strings.Compare(username, u.username) == 0 && strings.Compare(password, u.password) == 0 {
- return true
- }
- }
- return false
- }
- func login(users []user) bool {
- var username, password string
- fmt.Println("You must login to access this program.")
- fmt.Println("Username: ")
- _, _ = fmt.Scanf("%s", &username)
- fmt.Println("Password: ")
- _, _ = fmt.Scanf("%s", &password)
- if checkCredential(username, password, users) {
- return true
- }
- return false
- }
- func register(username string, password string, users *[]user) {
- *users = append(*users, user{username, password})
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement