forked from steger/pr3-ws202526
go basics
parent
aeff92b82f
commit
60bc9586d1
|
|
@ -0,0 +1,84 @@
|
||||||
|
# Go By Example
|
||||||
|
<https://gobyexample.com/>
|
||||||
|
|
||||||
|
|
||||||
|
# Assignment 1 - Simple Calculator
|
||||||
|
|
||||||
|
## Objective
|
||||||
|
Write a Go program that performs basic mathematical operations (addition, subtraction, multiplication, and division) based on user input.
|
||||||
|
|
||||||
|
## Steps
|
||||||
|
|
||||||
|
1. Define constants for valid operations (*add*,*subtract*,*multiply*,*divide*).
|
||||||
|
2. Use variables to store user inputs and computed results.
|
||||||
|
3. Use a loop to allow the user to perform multiple calculations until they choose to exit.
|
||||||
|
4. Validate inputs using statements (e.g., handle division by zero).
|
||||||
|
5. Use a statement to perform the selected mathematical operation.
|
||||||
|
|
||||||
|
## Example Output
|
||||||
|
```
|
||||||
|
Enter operation (add, subtract, multiply, divide, exit): add
|
||||||
|
Enter the first number: 5
|
||||||
|
Enter the second number: 3
|
||||||
|
Result: 8
|
||||||
|
|
||||||
|
Enter operation (add, subtract, multiply, divide, exit): divide
|
||||||
|
Enter the first number: 10
|
||||||
|
Enter the second number: 0
|
||||||
|
Error: Division by zero is not allowed!
|
||||||
|
|
||||||
|
Enter operation (add, subtract, multiply, divide, exit): exit
|
||||||
|
Goodbye!
|
||||||
|
```
|
||||||
|
|
||||||
|
## Hint
|
||||||
|
The program can use the `fmt.Scan` function to accept user input.
|
||||||
|
|
||||||
|
# Assignment 2 - Inventory Management System
|
||||||
|
|
||||||
|
## Objective
|
||||||
|
Create a program that simulates an inventory management system for a store.
|
||||||
|
|
||||||
|
## Steps
|
||||||
|
1. Define product categories (e.g., Electronics, Groceries, Clothes) using `const` and `iota`.
|
||||||
|
2. Store a fixed list of initial product names in an **array**.
|
||||||
|
3. Convert the array into a **slice** so the inventory can grow or shrink dynamically.
|
||||||
|
4. Use a **map** to associate product names with details like quantity, price, and category.
|
||||||
|
5. Implement the following **functions**:
|
||||||
|
- *AddProduct*: Add a new product to the inventory.
|
||||||
|
- *RemoveProduct*: Remove a product from the inventory.
|
||||||
|
- *DisplayInventory*: Display all products with their details.
|
||||||
|
- *UpdateQuantity*: Update the quantity of a product.
|
||||||
|
|
||||||
|
## Example Output
|
||||||
|
```
|
||||||
|
Welcome to the Inventory Manager!
|
||||||
|
|
||||||
|
Initial Inventory:
|
||||||
|
1. Laptop - Electronics (Price: $1000, Quantity: 5)
|
||||||
|
2. Apples - Groceries (Price: $2, Quantity: 50)
|
||||||
|
3. T-shirt - Clothes (Price: $10, Quantity: 20)
|
||||||
|
|
||||||
|
Adding a new product: Phone (Electronics, Price: $800, Quantity: 10)
|
||||||
|
|
||||||
|
Updated Inventory:
|
||||||
|
1. Laptop - Electronics (Price: $1000, Quantity: 5)
|
||||||
|
2. Apples - Groceries (Price: $2, Quantity: 50)
|
||||||
|
3. T-shirt - Clothes (Price: $10, Quantity: 20)
|
||||||
|
4. Phone - Electronics (Price: $800, Quantity: 10)
|
||||||
|
|
||||||
|
Updating quantity for Apples: New Quantity = 30
|
||||||
|
|
||||||
|
Removing product: T-shirt
|
||||||
|
|
||||||
|
Final Inventory:
|
||||||
|
1. Laptop - Electronics (Price: $1000, Quantity: 5)
|
||||||
|
2. Apples - Groceries (Price: $2, Quantity: 30)
|
||||||
|
3. Phone - Electronics (Price: $800, Quantity: 10)
|
||||||
|
```
|
||||||
|
|
||||||
|
- Use **arrays** to store initial product information.
|
||||||
|
- Use **slices** to dynamically manage items in the inventory.
|
||||||
|
- Use **maps** to track product details (e.g., quantity or price).
|
||||||
|
- Define **functions** for adding, removing, and displaying products.
|
||||||
|
- Use **enums** (via constants) to represent product categories.
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
//TODO: implement according to README.md
|
||||||
|
|
||||||
|
//the following code just demonstrates how to use fmt.Scan
|
||||||
|
|
||||||
|
var str string
|
||||||
|
fmt.Print("Please enter a string: ")
|
||||||
|
fmt.Scan(&str)
|
||||||
|
|
||||||
|
var x float64
|
||||||
|
fmt.Print("Please enter a float: ")
|
||||||
|
fmt.Scan(&x)
|
||||||
|
|
||||||
|
fmt.Printf("You entered '%s' and %f\n", str, x)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Product struct {
|
||||||
|
Name string
|
||||||
|
Price float64
|
||||||
|
Quantity int
|
||||||
|
Category string //TODO: use enum instead
|
||||||
|
}
|
||||||
|
|
||||||
|
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
|
||||||
|
//TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeProduct(inventory *[]Product, name string) {
|
||||||
|
//TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
||||||
|
//TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayInventory(inventory []Product) {
|
||||||
|
//TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
|
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"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display initial inventory
|
||||||
|
displayInventory(inventory)
|
||||||
|
|
||||||
|
// Add a new product
|
||||||
|
addProduct(&inventory, "Phone", 800, 10, "Electronics")
|
||||||
|
|
||||||
|
// Display updated inventory
|
||||||
|
displayInventory(inventory)
|
||||||
|
|
||||||
|
// Update the quantity of an existing product
|
||||||
|
updateQuantity(&inventory, "Apples", 30)
|
||||||
|
|
||||||
|
// Remove a product
|
||||||
|
removeProduct(&inventory, "T-shirt")
|
||||||
|
|
||||||
|
// Display final inventory
|
||||||
|
displayInventory(inventory)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue