-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shop.sh
executable file
·161 lines (134 loc) · 3.6 KB
/
Shop.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /bin/bash
# ==========================BEGIN Welcomming =====================
read -p "Welcome to Store!\nWhat is your name ? " NAME;sleep 1
echo -e "Okay Dear $NAME , Here is List Of Options\n"
# ===========================End Welcomming =======================
# ==============A Dictionary for goods and their price============
declare -A prices
prices=( ["Pen"]="8000" ["NoteBook"]="20000" ["Pencil"]="6000" ["Paper"]="90000" ["Done"]=0 )
#============Show what are there in the Store ====================
function menu {
for key in "${!prices[@]}";
do
printf "%-10s ==> %s\n" $key "${prices[$key]}"
done
echo -e "\n"
}
# ==========Here a function add items to goods list==============
declare -A goods
Total_price=0
Totla_number=0
function ask {
# take number of good
read -p "How Manay Do You Need ? " number
# multiple number of goods and their prices
good_price=$(($number * ${prices[$1]}))
# add goods price and their name to a dictionary
goods[$1]=$(echo -e "$number $1 \tPrice is $good_price")
let Total_number+=number
let Total_price+=good_price
}
# ===========This function is for chceking price===================
function price {
# 5 % discount to total price if its up to 100000
if [ $Total_price -ge 100000 ];then
discount=$(echo "$Total_price * 0.05"|bc -l)
Total_price=$(echo "$Total_price - $discount" | bc -l)
printf "\nYour total Price for %d goods with 5%% discount is: %.0f $\n" $Total_number $Total_price
else
printf "\nYour total Price for %d goods is: %.0f $\n" $Total_number $Total_price
fi
}
# ========== a function to check list is empty or not! ==============
function check {
if [ ${#goods[@]} == 0 ];then
echo "You've Chosen Nothing!"
return $(false)
else
return $(true)
fi
}
# ================ a function to show chosen stuffs =================
function show_list {
if check;then
echo -e "$NAME's Receipt : "
for key in "${!goods[@]}";
do
echo "-----------------------------------------------"
printf "${goods[$key]}\n"
done
fi
}
# =============== main function ======================================
function main1 {
# show goods menu and sleep for 1 second
menu;sleep 1
# here user sould choose goods
select item1 in "${!prices[@]}"
do
case $item1 in
"Paper")
ask "Paper"
;;
"NoteBook")
ask "NoteBook"
;;
"Pen")
ask "Pen"
;;
"Pencil")
ask "Pencil"
;;
"Done")
if check;then
break
fi
;;
*)
echo "No No! You Have To Choose One Of The Above! You chose $REPLY"
esac
done
}
# ================ This function is for operators =========================
function main2 {
# a list of operators user can choose
operator_menu=("Show" "Help" "Quit")
function operators {
echo """
Show ==> Show what have you chosen far now!
Help ==> Show This Help Menu!
Quit ==> Quit from store
Buy ==> You can Choose again
"""
}
select item2 in "${operator_menu[@]}"
do
case $item2 in
"Show")
show_list
;;
"Help")
operators
;;
"Quit")
echo "Hav a Nica Day,dude!"
break
;;
*)
echo "No No! You Have To Choose One Of The Above! You chose $REPLY"
esac
done
}
# Running functions
main1
main2
result1="$(show_list)"
result2="$(price)"
# write Results in a file
echo -e """
$(date +"%A %d %b %Y At %r")\n
$result1
$result2\n
Have A Nice Day!
GoodBy!
""" > /home/$USER/Receipt.log