forked from steger/pr3-ws202526
85 lines
2.9 KiB
Markdown
85 lines
2.9 KiB
Markdown
# 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.
|