-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd.js
77 lines (70 loc) · 1.67 KB
/
std.js
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
const fs= require('fs')
///////////////////////add method//////////
const addStdData=(id,namestd,subject,grade,comment)=>{
const stdData=loadStdData();
const repatedId=stdData.filter((stdid)=>{
return stdid.id==id
})
if(repatedId.length===0){
stdData.push({
id,
namestd,
subject,
grade,
comment
});
saveStdData(stdData);
console.log("save sucess")
}
else{
console.log("repated id,insert again")
}
}
const loadStdData=()=>{
try{
const bufferData= fs.readFileSync('std.json').toString();
return JSON.parse(bufferData)
}
catch(e){
return []
}
}
const saveStdData=(stdData)=>{
const saveData=JSON.stringify(stdData);
fs.writeFileSync('std.json',saveData)
}
//////////////////remove method
const removestd=(id)=>{
const stdData=loadStdData();
const primaryStd=stdData.filter((std)=>{
return std.id!==id
})
saveStdData(primaryStd);
console.log('remove std')
}
///////////////////read method
const readstd=(id)=>{
const stdData=loadStdData();
const std=stdData.find((ele)=>{
return ele.id===id;
});
if(std){
console.log( 'std id= '+std.id, 'std name '+std.namestd)
}
else{
console.log('this id not found,insert again')
}
}
////////////////////list method//////////////
const list=()=>{
const stdData=loadStdData();
stdData.forEach(std => {
console.log('student name is '+std.namestd,' subject is '+ std.subject +' grade is '+ std.grade)
});
}
module.exports={
addStdData,
removestd,
readstd,
list
}