# SoA Course In class practice Mar29 2022
###### tags: `Python3` `SoA` `MCUT`
Using Python programming language to design programs and complete requirements.
## Requirements:
1. data structures
1.1 numbers
1.2 strings
1.3 lists
1.4 dictionary
2. control of flows
2.1 if, elif
2.2 for
3. File I/O
4. Exceptions
4.1 try
4.2 except
5. Classes(functions)
5.1 inheritance
## Program design - Grocery store
The program use to create a file and delete a file, and handling a error.
### Main operations:
* Flow control : Mode selector, select mode you want to do, 1 for create goods, 2 for read data, 3 for exit, 4 for delete data, if you enter non-numeric data, it will raise ValueError and print error message.
* Create data : First enter name then price, then will create a dictionary, the name is key, and price is value, and the programs will continue let you enter next items until you hit 'n', while you enter 'n' the program will using File I/O to create a file in your device, named file.txt and the data type is JSON.
* Read data : The program will try to read the data named file.txt and print data content, if the file is not exited, they will raise a IOError and print the error message.
* Exit : Break the if-else state to terminate program.
* Delete data : The program will try to remove data then print done message, if IOError occurred, it will print error message.
### Flow chart:
````flow
st=>start: Start
e=>end: Terminate program
op1=>operation: create data
op2=>operation: define dictionary name :grocery,
key:'goods', value:'price'
op3=>operation: try :read file
op4=>operation: try :delete file
op5=>operation: file deleted
cond1=>condition: numeric number?
cond2=>condition: is input == 1
create data?
cond3=>condition: is input == 2
read file?
cond4=>condition: is input == 3
terminate program?
cond5=>condition: is input == 4
delete data?
cond6=>condition: stop enter data?
cond7=>condition: is file exist?
cond8=>condition: Error occurred
when deleting data?
io1=>inputoutput: try : enter mode 1~4 numertic
io2=>inputoutput: print error message "Numeric Only"
io3=>inputoutput: enter data key & value
io4=>inputoutput: store dictionary to file
io5=>inputoutput: print file content
io6=>inputoutput: print
error message
"File might not exist"
io7=>inputoutput: print
error message
"File might not exist"
st->op2->io1->cond1
cond2(yes,bottom)->io3->cond6
cond1(no,right)->io2(top)->io1
cond1(yes,bottom)->cond2(no,right)->cond3(no,right)->cond5(no,right)->cond4(no,right)->io1
cond6(no,left)->io3
cond6(yes,bottom)->io4(right)->io1
cond3(yes,bottom)->op3->cond7(yes,bottom)->io5(right)->io1
cond7(no,right)->io6(top)->io1
cond4(yes,bottom)->e
cond5(yes,bottom)->op4->cond8(no,right)->io1
cond8(yes,bottom)->io7(right)->io1
````
## Code:
``` Python
#Modified by Tsun-Ping Chen (MCUT) 2022.Mar29
import json
import os
grocery:{
'goods':'price'
}
def main():
while 1:
s = store()
try:
mod = int(input("Select mode, 1 for create goods, 2 for read data , 3 for exit, 4 for delete data :"))
if mod == 1:
s.createdata()
elif mod == 2:
s.readfile()
elif mod == 3:
break
elif mod == 4:
s.deletedata()
except ValueError:
print("Numeric only")
class filedata:
def __init__(self):
pass
def readfile(self):
try:
fo = open('file.txt', "r")
print("Name of the file: ", fo.name)
for line in fo.readlines():
print(line)
fo.close()
except IOError:
print("IOERROR OCCOURED")
def createdata(self):
goods = input("Enter name of goods: ")
price = input("Enter price of goods: ")
grocery = {goods: price}
for x in 'content':
x = input("continue? Y/n")
if x == 'n':
break
goods = input("Enter name of goods: ")
price = input("Enter price of goods: ")
grocery[goods] = price
with open('file.txt', 'w') as file:
file.write(json.dumps(grocery))
def deletedata(self):
try:
os.remove("file.txt")
print("data is deleted")
except IOError:
print("File might not exist")
class store(filedata):
def __init__(self):
pass
if __name__ == "__main__":
# execute only if run as a script
main()
```
## References materials:
* [Python 3 tutorials - tutorialspoint](https://www.tutorialspoint.com/python3/python_classes_objects.htm)
* [Python 3 tutorials - Geeks for Geeks](https://www.geeksforgeeks.org/python-3-input-function/)