forked from steger/pr3-ws202526
|
|
||
|---|---|---|
| .. | ||
| 00-values.go | ||
| 01-variables.go | ||
| 02-constants.go | ||
| 03-for.go | ||
| 04-if-else.go | ||
| 05-switch.go | ||
| 06-arrays.go | ||
| 07-pointers.go | ||
| README.md | ||
| calculator.go | ||
| inventory.go | ||
README.md
Go By Example
Assignment 1 - Simple Calculator
Objective
Write a Go program that performs basic mathematical operations (addition, subtraction, multiplication, and division) based on user input.
Steps
- Define constants for valid operations (add,subtract,multiply,divide).
- Use variables to store user inputs and computed results.
- Use a loop to allow the user to perform multiple calculations until they choose to exit.
- Validate inputs using statements (e.g., handle division by zero).
- 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
- Define product categories (e.g., Electronics, Groceries, Clothes) using
constandiota. - Store a fixed list of initial product names in an array.
- Convert the array into a slice so the inventory can grow or shrink dynamically.
- Use a map to associate product names with details like quantity, price, and category.
- 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.