forked from steger/pr3-ws202627
Calcu und Inventory
parent
f335aace32
commit
e220ea6031
|
|
@ -0,0 +1,3 @@
|
|||
module gitty.informatik.th-mannheim.de/steger/pr3-sose2026/go
|
||||
|
||||
go 1.26.0
|
||||
|
|
@ -1,40 +1,66 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
Name string
|
||||
Price float64
|
||||
Quantity int
|
||||
Category string //TODO: use enum instead
|
||||
Category int //TODO: use enum instead
|
||||
}
|
||||
|
||||
const(
|
||||
Electronics int = iota
|
||||
Groceries
|
||||
Clothes
|
||||
)
|
||||
|
||||
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
|
||||
//TODO: implement
|
||||
var l int = inventory.len
|
||||
inventory = append(inventory, name, price, quantity, category)
|
||||
}
|
||||
|
||||
func removeProduct(inventory *[]Product, name string) {
|
||||
//TODO: implement
|
||||
inventory = inventory.delete(name)
|
||||
}
|
||||
|
||||
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
||||
//TODO: implement
|
||||
sliceInventory[name].quantity = newQuantity;
|
||||
}
|
||||
|
||||
func displayInventory(inventory []Product) {
|
||||
//TODO: implement
|
||||
for _, p := range sliceInventory{
|
||||
fmt.Println(karte(sliceInventory[p]))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
inventory := []Product{
|
||||
{Name: "Laptop", Price: 1000, Quantity: 5, Category: "Electronics"},
|
||||
{Name: "Apples", Price: 2, Quantity: 50, Category: "Groceries"},
|
||||
{Name: "T-shirt", Price: 10, Quantity: 20, Category: "Clothes"},
|
||||
{Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics},
|
||||
{Name: "Apples", Price: 2, Quantity: 50, Category: Groceries},
|
||||
{Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes},
|
||||
}
|
||||
var arrInventory = [3]string{"Laptop", "Apples", "T-shirt"}
|
||||
var sliceInventory = arrInventory[:]
|
||||
|
||||
karte := map[string]Product{
|
||||
"Laptop": {Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics},
|
||||
"Apples": {Name: "Apples", Price: 2, Quantity: 50, Category: Groceries},
|
||||
"T-shirt": {Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes},
|
||||
|
||||
}
|
||||
|
||||
// Display initial inventory
|
||||
displayInventory(inventory)
|
||||
|
||||
// Add a new product
|
||||
addProduct(&inventory, "Phone", 800, 10, "Electronics")
|
||||
addProduct(&inventory, "Phone", 800, 10, Electronics)
|
||||
|
||||
// Display updated inventory
|
||||
displayInventory(inventory)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
m := make(map[string]int)
|
||||
|
||||
m["k1"] = 7
|
||||
m["k2"] = 13
|
||||
|
||||
fmt.Println("map:", m)
|
||||
|
||||
v1 := m["k1"]
|
||||
fmt.Println("v1:", v1)
|
||||
|
||||
v3 := m["k3"]
|
||||
fmt.Println("v3:", v3)
|
||||
|
||||
fmt.Println("len:", len(m))
|
||||
|
||||
delete(m, "k2")
|
||||
fmt.Println("map:", m)
|
||||
|
||||
clear(m)
|
||||
fmt.Println("map:", m)
|
||||
|
||||
_, prs := m["k2"]
|
||||
fmt.Println("prs:", prs)
|
||||
|
||||
n := map[string]int{"foo": 1, "bar": 2}
|
||||
fmt.Println("map:", n)
|
||||
|
||||
n2 := map[string]int{"foo": 1, "bar": 2}
|
||||
if maps.Equal(n, n2) {
|
||||
fmt.Println("n == n2")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
var s []string
|
||||
fmt.Println("uninit:", s, s == nil, len(s) == 0)
|
||||
|
||||
s = make([]string, 3)
|
||||
fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
|
||||
|
||||
s[0] = "a"
|
||||
s[1] = "b"
|
||||
s[2] = "c"
|
||||
fmt.Println("set:", s)
|
||||
fmt.Println("get:", s[2])
|
||||
|
||||
fmt.Println("len:", len(s))
|
||||
|
||||
s = append(s, "d")
|
||||
s = append(s, "e", "f")
|
||||
fmt.Println("apd:", s)
|
||||
|
||||
c := make([]string, len(s))
|
||||
copy(c, s)
|
||||
fmt.Println("cpy:", c)
|
||||
|
||||
l := s[2:5]
|
||||
fmt.Println("sl1:", l)
|
||||
|
||||
l = s[:5]
|
||||
fmt.Println("sl2:", l)
|
||||
|
||||
l = s[2:]
|
||||
fmt.Println("sl3:", l)
|
||||
|
||||
t := []string{"g", "h", "i"}
|
||||
fmt.Println("dcl:", t)
|
||||
|
||||
t2 := []string{"g", "h", "i"}
|
||||
if slices.Equal(t, t2) {
|
||||
fmt.Println("t == t2")
|
||||
}
|
||||
|
||||
twoD := make([][]int, 3)
|
||||
for i := range 3 {
|
||||
innerLen := i + 1
|
||||
twoD[i] = make([]int, innerLen)
|
||||
for j := range innerLen {
|
||||
twoD[i][j] = i + j
|
||||
}
|
||||
}
|
||||
fmt.Println("2d: ", twoD)
|
||||
}
|
||||
Loading…
Reference in New Issue