Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9.  
  10. type product struct {
  11. id int
  12. name string
  13. stock int
  14. }
  15.  
  16. type order struct {
  17. id int
  18. name string
  19. idProduct int
  20. quantity int
  21. }
  22.  
  23. type user struct {
  24. username string
  25. password string
  26. }
  27.  
  28. func main() {
  29. var products []product
  30. var users []user
  31. var orders []order
  32.  
  33. var isLogin = false
  34.  
  35. var input, idOrder, idProduct, quantity, action, id int
  36. var username, password, name string
  37.  
  38. for !isLogin {
  39.  
  40. displayGuestMenu()
  41.  
  42. _, _ = fmt.Scanf("%d", &action)
  43.  
  44. if action == 1 {
  45. isLogin = login(users)
  46. } else if action == 2 {
  47. fmt.Println("Masukkan username:")
  48. _, _ = fmt.Scanf("%s", &username)
  49. fmt.Println("Masukkan password:")
  50. _, _ = fmt.Scanf("%s", &password)
  51.  
  52. register(username, password, &users)
  53. } else if action == 0 {
  54. fmt.Println("Thank you for using i-Shope! :)")
  55. break
  56. }
  57. }
  58.  
  59. for isLogin {
  60. displayMenu()
  61.  
  62. _, _ = fmt.Scanf("%d", &input)
  63.  
  64. switch input {
  65. case 0:
  66. fmt.Println("Thank you for using i-Shope! :)")
  67. isLogin = false
  68. break
  69. case 1:
  70. showListProduct(products)
  71. break
  72. case 2:
  73. fmt.Println("Masukkan id product:")
  74. _, _ = fmt.Scanf("%d", &id)
  75. showProductById(id, products)
  76. break
  77. case 3:
  78. addProduct(&products)
  79. break
  80. case 4:
  81. fmt.Println("Masukkan id product:")
  82. _, _ = fmt.Scanf("%d", &id)
  83. updateProductById(id, &products)
  84. break
  85. case 5:
  86. fmt.Println("Masukkan id product:")
  87. _, _ = fmt.Scanf("%d", &id)
  88. deleteProductById(id, &products)
  89. break
  90. case 6:
  91. fmt.Println("Masukkan order sesuai format: idOrder userName idProduct Quantity")
  92. _, _ = fmt.Scanf("%d %s %d %d", &idOrder, &name, &idProduct, &quantity)
  93. orderProduct(order{idOrder, name, idProduct, quantity}, &products, &orders)
  94. break
  95. default:
  96. fmt.Println("Menu tidak dikenali, coba cek lagi!")
  97. break
  98. }
  99. }
  100. }
  101.  
  102. func displayGuestMenu() {
  103. fmt.Println("Welcome to i-Shope :)")
  104. fmt.Println("Guest menu:")
  105. fmt.Println("1. Login")
  106. fmt.Println("2. Register")
  107. fmt.Println("0. Cancel")
  108. }
  109.  
  110. func displayMenu() {
  111. fmt.Println("[======================]")
  112. fmt.Println("List menu: ")
  113. fmt.Println("1.] -> Show list product")
  114. fmt.Println("2.] -> Show product by id")
  115. fmt.Println("3.] -> Add product")
  116. fmt.Println("4.] -> Update product")
  117. fmt.Println("5.] -> Delete product")
  118. fmt.Println("6.] -> Order product")
  119. fmt.Println("0.] -> [[ Exit Program ]]")
  120. fmt.Println("[======================]")
  121. }
  122.  
  123. func orderProduct(order order, products *[]product, orders *[]order) {
  124. for i, p := range *products {
  125. if p.id == order.idProduct {
  126. if p.stock >= 1 && p.stock >= order.quantity {
  127. (*products)[i] = product{p.id, p.name, p.stock - order.quantity}
  128. *orders = append(*orders, order)
  129. printOrders(orders)
  130. fmt.Println("Terimakasih, anda sudah berhasil melakukan pemesanan terhadap product kami :)")
  131. } else {
  132. fmt.Println("Maaf, stock sudah habis atau tidak mencukupi!")
  133. }
  134. return
  135. }
  136. }
  137. }
  138.  
  139. func printOrders(orders *[]order) {
  140. fmt.Println("Berhasil di order:")
  141. for _, o := range *orders {
  142. fmt.Println("ID: ", o.id)
  143. fmt.Println("Username: ", o.name)
  144. fmt.Println("ID Product: ", o.idProduct)
  145. fmt.Println("Quantity: ", o.quantity)
  146. fmt.Println("=======================")
  147. }
  148. }
  149.  
  150. func deleteProductById(id int, products *[]product) {
  151. for i, p := range *products {
  152. if p.id == id {
  153. *products = append((*products)[:i], (*products)[i+1:]...)
  154. showListProduct(*products)
  155. return
  156. }
  157.  
  158. if i == len(*products) - 1 {
  159. fmt.Println("Product not found!")
  160. }
  161. }
  162. }
  163.  
  164. func updateProductById(id int, products *[]product) {
  165. newName := ""
  166. newStock := 0
  167.  
  168. fmt.Println("Masukkan nama terbaru product")
  169. consoleReader := bufio.NewReader(os.Stdin)
  170. newName, _ = consoleReader.ReadString('\n')
  171.  
  172. fmt.Println("Masukkan jumlah stock")
  173. _, _ = fmt.Scanf("%d", &newStock)
  174.  
  175. for i, p := range *products {
  176. if p.id == id {
  177. (*products)[i] = product{id, newName, newStock}
  178. showListProduct(*products)
  179. return
  180. }
  181.  
  182. if i == len(*products) - 1 {
  183. fmt.Println("Product not found!")
  184. }
  185. }
  186. }
  187.  
  188. func addProduct(products *[]product) {
  189. var id, stock int
  190. name := ""
  191.  
  192. fmt.Println("Masukkan id product")
  193. _, _ = fmt.Scanf("%d", &id)
  194. fmt.Println("Masukkan nama product")
  195.  
  196. consoleReader := bufio.NewReader(os.Stdin)
  197. name, _ = consoleReader.ReadString('\n')
  198.  
  199. fmt.Println("Masukkan jumlah stock")
  200. _, _ = fmt.Scanf("%d", &stock)
  201.  
  202. *products = append(*products, product{id, name, stock})
  203. showListProduct(*products)
  204. }
  205.  
  206. func showProductById(id int, products []product) {
  207. if len(products) == 0 {
  208. fmt.Println("There's no product yet!")
  209. }
  210.  
  211. for i, p := range products {
  212. if p.id == id {
  213. fmt.Println("ID: ", p.id)
  214. fmt.Println("Stock: ", p.stock)
  215. fmt.Println("Name: ", p.name)
  216. return
  217. }
  218.  
  219. if i == len(products) - 1 {
  220. fmt.Println("Product not found!")
  221. }
  222. }
  223. }
  224.  
  225. func showListProduct(products []product) {
  226. if len(products) == 0 {
  227. fmt.Println("There's no product yet, please add a product before")
  228. return
  229. }
  230.  
  231. fmt.Println("List products:")
  232. for _, p := range products {
  233. fmt.Println("ID: ", p.id)
  234. fmt.Println("Stock: ", p.stock)
  235. fmt.Println("Name: ", p.name)
  236. }
  237. }
  238.  
  239. func checkCredential(username string, password string, users []user) bool {
  240. for _, u := range users {
  241. if strings.Compare(username, u.username) == 0 && strings.Compare(password, u.password) == 0 {
  242. return true
  243. }
  244. }
  245.  
  246. return false
  247. }
  248.  
  249. func login(users []user) bool {
  250. var username, password string
  251.  
  252. fmt.Println("You must login to access this program.")
  253.  
  254. fmt.Println("Username: ")
  255. _, _ = fmt.Scanf("%s", &username)
  256. fmt.Println("Password: ")
  257. _, _ = fmt.Scanf("%s", &password)
  258.  
  259. if checkCredential(username, password, users) {
  260. return true
  261. }
  262.  
  263. return false
  264. }
  265.  
  266. func register(username string, password string, users *[]user) {
  267. *users = append(*users, user{username, password})
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement