|
| 1 | +// Project : New Wave Stock [No Inputs] |
| 2 | +// Written by: Mason Z |
| 3 | +// Date: Sept 20, 2024 |
| 4 | +// Description: Non-input version. Original. - Kinda Rudimetary: Basic Demonstration of Dictionary's in Swift |
| 5 | + |
| 6 | +/* TASK |
| 7 | +1 - Add a product – The user will enter a product name and quantity. Make sure that they don’t accidently overwrite a product that is already in the database. |
| 8 | + |
| 9 | +2 - Add a quantity to an existing product – The user will enter a product name and quantity. This quantity should be added to the existing stock. The user should be notified if they enter an invalid product name. |
| 10 | + |
| 11 | +3 - Remove a quantity from an existing product – The user will enter a product name and quantity. This quantity should be subtracted from the existing stock. The user should be notified if they enter an invalid product name or an invalid quantity. |
| 12 | + |
| 13 | +4 - Total number of items in stock – Print the total quantity of items that New Wave Computers™ has in stock. |
| 14 | + |
| 15 | +5 - List all items that are in stock – Print a nicely formatted table of all items currently in stock, sorted from items that have the highest quantity down to items that have the lowest quantity. |
| 16 | +*/ |
| 17 | + |
| 18 | +import Foundation |
| 19 | + |
| 20 | +// variables |
| 21 | +var stock = ["hard drives": 502, "software": 7, "wPhones": 24, "wPens": 700532] |
| 22 | + |
| 23 | + |
| 24 | +// because input on xcode doesn't work, this is the version where input is disabled and inputs are substituded with consts |
| 25 | + |
| 26 | +// menu - defunct |
| 27 | +print("************************************************************") |
| 28 | +print("welcome to new wave computer systems - inputs off") |
| 29 | +print("") |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +// #1 |
| 34 | +print("1 - Add Product") |
| 35 | +print("For this purpose, we will add wTablets with a quantity of 2, as it is a new product") |
| 36 | +print("") |
| 37 | + |
| 38 | +if let product = stock["wTablets"] { |
| 39 | + print("ERROR: ALREADY EXISTS. CANNOT EXISTING ITEM AS NEW ITEM TO DATABASE") // in this casse, this error will not occur |
| 40 | +} else { |
| 41 | + stock["wTablets"] = 2 |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +// #2 |
| 47 | +print("2 - Add a Quantity to an Existing Product") |
| 48 | +print("For This Purpose, 8 will added to hard drives. It shall equal 510 after.") |
| 49 | +print("") |
| 50 | + |
| 51 | +if let productQuantity = stock["hard drives"] { |
| 52 | + stock["hard drives"] = productQuantity + 8 |
| 53 | +} else { |
| 54 | + print("that product doesn't exist") // in this case, this error will not occur |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +// #3 |
| 61 | +print("3 - Remove a Quantity to an Existing Product") |
| 62 | +print("For This Purpose, 5 will removed from software. It shall equal 2 after.") |
| 63 | +print("") |
| 64 | + |
| 65 | +if let productQuantity = stock["software"] { |
| 66 | + stock["software"] = productQuantity - 5 |
| 67 | +} else { |
| 68 | + print("that product doesn't exist") // in this casse, this error will not occur |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +// #4 |
| 74 | +var totalStock = 0 |
| 75 | +for quantity in stock.values { |
| 76 | + totalStock+=quantity |
| 77 | +} |
| 78 | +print("4 - Total # of Items in Stock = \(totalStock)") |
| 79 | +print("") |
| 80 | + |
| 81 | +// #5 |
| 82 | +print("5 - All Items in Stock Report") |
| 83 | +print("") |
| 84 | + |
| 85 | +print ("Product Quantity") |
| 86 | +for (product, quantity) in stock { |
| 87 | + print ("\(product) \(quantity)") |
| 88 | +} |
| 89 | +print("") |
| 90 | + |
| 91 | +print("************************************************************") |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments