-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.go
90 lines (83 loc) · 1.83 KB
/
assignment.go
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
package main
import "fmt"
var m map[string]int;
func add(key string, val int) {
m[key] = val;
}
func retrieve(key string) int {
val, ok := m[key];
if !ok {
return -1;
} else {
return val;
}
}
func length() int {
return len(m);
}
func remove(key string) {
delete(m, key);
}
func printAll() {
for key, value := range m {
fmt.Printf("%q: %d\n", key, value)
}
}
func main() {
var temp string;
var val int;
var loop bool;
var a int;
loop = true;
m = make(map[string]int);
for loop {
fmt.Print("\n1. Add a Person\n2. Check a Person's age\n3. Change a Person's age\n4. Check number of people added\n5. Delete a record\n6. View Entries\n7. Exit\nEnter your choice: ");
fmt.Scan(&a);
switch a {
case 1:
fmt.Print("Enter Person's name: ");
fmt.Scanln(&temp);
fmt.Print("Enter Person's age: ");
fmt.Scanln(&val);
add(temp, val);
case 2:
fmt.Print("Enter Person's name: ");
fmt.Scanln(&temp);
t := retrieve(temp);
if t != -1 {
fmt.Println("Person's age is: ", t);
} else {
fmt.Println("Person not found");
}
case 3:
fmt.Print("Enter Person's name: ");
fmt.Scanln(&temp);
t := m[temp];
if t != 0 {
fmt.Print("Enter Person's age: ");
fmt.Scanln(&val);
add(temp, val);
} else {
fmt.Println("Person not found");
}
case 4:
fmt.Println("Number of people added: ", length());
case 5:
fmt.Print("Enter Person's name: ");
fmt.Scanln(&temp);
t := m[temp];
if t != 0 {
remove(temp);
} else {
fmt.Println("Person not found");
}
case 6:
printAll();
case 7:
fmt.Println();
loop = false;
default:
fmt.Println("\n\nInvalid Input\n");
}
}
}