Last active
December 26, 2023 01:13
-
-
Save rkalit/d8d3e35078e95b05c7c45adf47df8866 to your computer and use it in GitHub Desktop.
A simple console App that can do simple CRUD (Create Read Update Delete) written in Go language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Code by rkalit | |
| */ | |
| package main | |
| import( | |
| "fmt" | |
| "bufio" | |
| "os" | |
| ) | |
| type product struct { | |
| id int | |
| name string | |
| stock int | |
| } | |
| var products []product | |
| func login() bool{ | |
| var username, password string | |
| fmt.Println("=============Login=============") | |
| fmt.Print("Username: ") | |
| fmt.Scan(&username) | |
| fmt.Print("Password: ") | |
| fmt.Scan(&password) | |
| if(username == "admin" && password == "admin"){ | |
| fmt.Println("==========Login Succes==========") | |
| return true | |
| } else { | |
| fmt.Println("Incorrect Username or Password, please re-login!") | |
| return false | |
| } | |
| } | |
| func listMenu(){ | |
| 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("0. Exit") | |
| fmt.Print("Enter your choice: ") | |
| } | |
| func menu() { | |
| var choose int | |
| listMenu() | |
| for { | |
| fmt.Scan(&choose) | |
| if choose == 0 { | |
| fmt.Println("Thank You, exiting.....") | |
| break | |
| } else { | |
| switch choose { | |
| case 1: | |
| showListProduct(products) | |
| fmt.Println("press 'Enter' to back to menu....") | |
| bufio.NewReader(os.Stdin).ReadBytes('\n') | |
| listMenu() | |
| case 2: | |
| var id int | |
| fmt.Println() | |
| fmt.Print("Enter Product ID :") | |
| fmt.Scan(&id) | |
| fmt.Println("\nInfo for Product with ID ", id) | |
| showProductById(id, products) | |
| fmt.Println("press 'Enter' to back to menu....") | |
| bufio.NewReader(os.Stdin).ReadBytes('\n') | |
| listMenu() | |
| case 3: | |
| fmt.Println("Add a new product") | |
| addProduct(&products) | |
| fmt.Println() | |
| fmt.Println("press 'Enter' to back to menu....") | |
| bufio.NewReader(os.Stdin).ReadBytes('\n') | |
| listMenu() | |
| case 4: | |
| var id int | |
| fmt.Print("Enter Product Id: ") | |
| fmt.Scan(&id) | |
| updateProductById(id, &products) | |
| fmt.Println() | |
| fmt.Println("press 'Enter' to back to menu....") | |
| bufio.NewReader(os.Stdin).ReadBytes('\n') | |
| listMenu() | |
| case 5: | |
| var id int | |
| fmt.Println() | |
| fmt.Print("Enter Product Id: ") | |
| fmt.Scan(&id) | |
| deleteProductById(id, &products) | |
| fmt.Println() | |
| fmt.Println("press 'Enter' to back to menu....") | |
| bufio.NewReader(os.Stdin).ReadBytes('\n') | |
| listMenu() | |
| default: | |
| fmt.Println("Re-enter your choice!") | |
| listMenu() | |
| } | |
| } | |
| } | |
| } | |
| /*** Start of CRUD Function***/ | |
| func showListProduct(products []product) { | |
| // Untuk setiap product p dengan index i pada array products | |
| for _, p := range products { | |
| fmt.Println("ID :", p.id) | |
| fmt.Println("Name :", p.name) | |
| fmt.Println("Stock :", p.stock) | |
| fmt.Println() | |
| } | |
| } | |
| func showProductById(id int, products []product) { | |
| for _, p := range products { | |
| if p.id == id { | |
| fmt.Println("ID :", p.id) | |
| fmt.Println("Name :", p.name) | |
| fmt.Println("Stock :",p.stock) | |
| fmt.Println() | |
| } | |
| } | |
| } | |
| func addProduct(products *[]product) { | |
| var id int | |
| consoleReader := bufio.NewReader(os.Stdin) | |
| var stock int | |
| fmt.Println() | |
| fmt.Print("Enter Product Id : ") | |
| fmt.Scan(&id) | |
| fmt.Print("Enter Product Name : ") | |
| name, _ := consoleReader.ReadString('\n') //accept the whitespace | |
| fmt.Print("Enter Product Stock : ") | |
| fmt.Scan(&stock) | |
| product_ := product{id, name, stock} | |
| *products = append(*products, product_) | |
| } | |
| func updateProductById(id int, products *[]product) { | |
| var newStock int | |
| fmt.Println() | |
| fmt.Print("Enter the New quantity of the Product: ") | |
| fmt.Scan(&newStock) | |
| for i, p := range *products { | |
| if p.id == id { | |
| (*products)[i] = product{id, p.name, newStock} | |
| } | |
| } | |
| } | |
| func deleteProductById(id int, products *[]product) { | |
| for i, p := range *products { | |
| if p.id == id { | |
| *products = append((*products)[:i], (*products)[i+1:]...) | |
| } | |
| } | |
| } | |
| /*** End of CRUD Function ***/ | |
| func DitenunApp() { | |
| for !login() { | |
| login() | |
| break | |
| } | |
| // Example data | |
| products = append(products, product{id : 1, name : "Ulos 1", stock: 25}) | |
| products = append(products, product{id : 2, name : "Ulos 2", stock: 15}) | |
| menu() | |
| } | |
| func main() { | |
| DitenunApp() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment