-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_management.go
More file actions
82 lines (74 loc) · 2.01 KB
/
Copy pathinventory_management.go
File metadata and controls
82 lines (74 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"bufio"
"os"
"strconv"
"strings"
)
type Item struct {
Name string
Price float64
Stock int
}
var inventory []Item
func addItem(name string, price float64, stock int) {
inventory = append(inventory, Item{Name: name, Price: price, Stock: stock})
}
func viewItems() {
if len(inventory) == 0 {
fmt.Println("No items to display.")
return
}
for _, item := range inventory {
fmt.Printf("Name: %s, Price: $%.2f, Stock: %d\n", item.Name, item.Price, item.Stock)
}
}
func deleteItem(name string) {
for i, item := range inventory {
if item.Name == name {
inventory = append(inventory[:i], inventory[i+1:]...)
fmt.Printf("Item %s deleted.\n", name)
return
}
}
fmt.Printf("Item %s not found.\n", name)
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("\nInventory Management System:")
fmt.Println("1. Add Item")
fmt.Println("2. View Items")
fmt.Println("3. Delete Item")
fmt.Println("4. Exit")
fmt.Print("Enter your choice: ")
scanner.Scan()
choice, _ := strconv.Atoi(scanner.Text())
switch choice {
case 1:
fmt.Print("Enter item name: ")
scanner.Scan()
name := scanner.Text()
fmt.Print("Enter item price: ")
scanner.Scan()
price, _ := strconv.ParseFloat(scanner.Text(), 64)
fmt.Print("Enter item stock: ")
scanner.Scan()
stock, _ := strconv.Atoi(scanner.Text())
addItem(name, price, stock)
case 2:
viewItems()
case 3:
fmt.Print("Enter item name to delete: ")
scanner.Scan()
name := scanner.Text()
deleteItem(name)
case 4:
fmt.Println("Exiting...")
return
default:
fmt.Println("Invalid choice. Please try again.")
}
}
}