|
| 1 | +/*AARTI RATHI |
| 2 | +My website - https://aartirathi17.herokuapp.com/ */ |
| 3 | + |
| 4 | +/*Problem Statement : |
| 5 | +
|
| 6 | +Implement a program for maintaining a database of student records using Files.Student has Student_id,name, Roll_no, Class, marks and address. Display the data for few students. |
| 7 | +1. Create Database |
| 8 | +2. Display Database |
| 9 | +3. Delete Records |
| 10 | +4. Update Record |
| 11 | +5. Search Record */ |
| 12 | + |
| 13 | +---------------------------------------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +package assign8 |
| 16 | +import java.io.*; |
| 17 | +import java.io.File; |
| 18 | +import java.util.*; |
| 19 | +public class Student |
| 20 | +{ |
| 21 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 22 | + |
| 23 | + public void addRecords() throws IOException |
| 24 | + { |
| 25 | + // Create or Modify a file for Database |
| 26 | + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("st.txt",true))); |
| 27 | + String studentname, address, stu_class; |
| 28 | + int studentid, rollno; |
| 29 | + float marks; |
| 30 | + String s; |
| 31 | + boolean addMore = false; |
| 32 | + // Read Data |
| 33 | + do |
| 34 | + { |
| 35 | + System.out.print("Enter Student's Name: "); |
| 36 | + studentname = br.readLine(); |
| 37 | + |
| 38 | + System.out.print("Enter Student Id: "); |
| 39 | + studentid = Integer.parseInt(br.readLine()); |
| 40 | + |
| 41 | + System.out.print("Enter Roll no: "); |
| 42 | + rollno = Integer.parseInt(br.readLine()); |
| 43 | + |
| 44 | + System.out.print("Enter Address: "); |
| 45 | + address = br.readLine(); |
| 46 | + |
| 47 | + System.out.print("Enter Class: "); |
| 48 | + stu_class = br.readLine(); |
| 49 | + |
| 50 | + System.out.print("Enter Marks : "); |
| 51 | + marks = Float.parseFloat(br.readLine()); |
| 52 | + // Print to File |
| 53 | + pw.println(studentname+" "+studentid+" "+rollno+" "+address+" "+stu_class+" "+marks); |
| 54 | + System.out.print("\nRecords added successfully !\n\nDo you want to add more records ? (y/n) : "); |
| 55 | + s = br.readLine(); |
| 56 | + if(s.equalsIgnoreCase("y")) |
| 57 | + { |
| 58 | + addMore = true; |
| 59 | + System.out.println(); |
| 60 | + } |
| 61 | + else |
| 62 | + addMore = false; |
| 63 | + } |
| 64 | + while(addMore); |
| 65 | + pw.close(); |
| 66 | + } |
| 67 | + |
| 68 | + public void readRecords() throws IOException |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + // Open the file |
| 73 | + BufferedReader file = new BufferedReader(new FileReader("st.txt")); |
| 74 | + String name; |
| 75 | + int i=1; |
| 76 | + // Read records from the file |
| 77 | + while((name = file.readLine()) != null) |
| 78 | + { |
| 79 | + System.out.println(name); |
| 80 | + System.out.println(""); |
| 81 | + } |
| 82 | + file.close(); |
| 83 | + } |
| 84 | + catch(FileNotFoundException e) |
| 85 | + { |
| 86 | + System.out.println("\nERROR : File not Found !!!"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void searchRecords() throws IOException |
| 91 | + { |
| 92 | + try |
| 93 | + { |
| 94 | + // Open the file |
| 95 | + BufferedReader file = new BufferedReader(new FileReader("st.txt")); |
| 96 | + String name; |
| 97 | + int flag=0; |
| 98 | + Scanner sc=new Scanner(System.in); |
| 99 | + System.out.print("Enter an id of the student you want to search: "); |
| 100 | + String searchname=sc.next(); |
| 101 | + // Read records from the file |
| 102 | + while((name = file.readLine()) != null) |
| 103 | + { |
| 104 | + |
| 105 | + String[] line = name.split(" "); |
| 106 | + //System.out.println(line[0]); |
| 107 | + if(searchname.equalsIgnoreCase(line[1])){ |
| 108 | + System.out.println("Record found"); |
| 109 | + System.out.println(name); |
| 110 | + System.out.println(""); |
| 111 | + flag=1; |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + if(flag==0) |
| 117 | + System.out.println("Record not found"); |
| 118 | + file.close(); |
| 119 | + } |
| 120 | + catch(FileNotFoundException e) |
| 121 | + { |
| 122 | + System.out.println("\nERROR : File not Found !!!"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public void deleteRecords() throws IOException |
| 127 | + { |
| 128 | + try |
| 129 | + { |
| 130 | + // Open the file |
| 131 | + BufferedReader file1 = new BufferedReader(new FileReader("st.txt")); |
| 132 | + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("st1.txt",true))); |
| 133 | + String name; |
| 134 | + int flag=0; |
| 135 | + Scanner sc=new Scanner(System.in); |
| 136 | + System.out.print("Enter the name of the student you want to delete: "); |
| 137 | + String searchname=sc.next(); |
| 138 | + // Read records from the file |
| 139 | + while((name = file1.readLine()) != null) |
| 140 | + { |
| 141 | + |
| 142 | + String[] line = name.split(" "); |
| 143 | + //System.out.println(line[0]); |
| 144 | + if(!searchname.equalsIgnoreCase(line[0])) |
| 145 | + { |
| 146 | + |
| 147 | + pw.println(name); |
| 148 | + flag=0; |
| 149 | + |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + System.out.println("Record found"); |
| 154 | + flag=1; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + file1.close(); |
| 159 | + pw.close(); |
| 160 | + File delName = new File("st.txt"); |
| 161 | + File oldName = new File("st1.txt"); |
| 162 | + File newName = new File("st.txt"); |
| 163 | + if(delName.delete()) |
| 164 | + System.out.println("deleted successfully"); |
| 165 | + else |
| 166 | + System.out.println("Error"); |
| 167 | + |
| 168 | + |
| 169 | + if (oldName.renameTo(newName)) |
| 170 | + System.out.println("Renamed successfully"); |
| 171 | + else |
| 172 | + System.out.println("Error"); |
| 173 | + |
| 174 | + } |
| 175 | + catch(FileNotFoundException e) |
| 176 | + { |
| 177 | + System.out.println("\nERROR : File not Found !!!"); |
| 178 | + } |
| 179 | + } |
| 180 | + public void updateRecords() throws IOException |
| 181 | + { |
| 182 | + try |
| 183 | + { |
| 184 | + // Open the file |
| 185 | + BufferedReader file1 = new BufferedReader(new FileReader("st.txt")); |
| 186 | + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("st1.txt",true))); |
| 187 | + String name; |
| 188 | + int flag=0; |
| 189 | + Scanner sc=new Scanner(System.in); |
| 190 | + System.out.print("Enter the name of the student you want to update: "); |
| 191 | + String searchname=sc.next(); |
| 192 | + // Read records from the file |
| 193 | + while((name = file1.readLine()) != null && flag==0) |
| 194 | + { |
| 195 | + |
| 196 | + String[] line = name.split(" "); |
| 197 | + //System.out.println(line[0]); |
| 198 | + if(!searchname.equalsIgnoreCase(line[0])) |
| 199 | + { |
| 200 | + |
| 201 | + pw.println(name); |
| 202 | + flag=0; |
| 203 | + |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + System.out.println("Record found"); |
| 208 | + System.out.println("Enter updated marks:"); |
| 209 | + Float number = sc.nextFloat(); |
| 210 | + pw.println(line[0]+" "+line[1]+" "+line[2]+" "+line[3]+" "+line[4]+" "+number); |
| 211 | + flag=1; |
| 212 | + } |
| 213 | + |
| 214 | + } |
| 215 | + file1.close(); |
| 216 | + pw.close(); |
| 217 | + File delName = new File("st.txt"); |
| 218 | + File oldName = new File("st1.txt"); |
| 219 | + File newName = new File("st.txt"); |
| 220 | + if(delName.delete()) |
| 221 | + System.out.println("record updated successfully"); |
| 222 | + else |
| 223 | + System.out.println("Error"); |
| 224 | + |
| 225 | + |
| 226 | + if (oldName.renameTo(newName)) |
| 227 | + System.out.println("Renamed successfully"); |
| 228 | + else |
| 229 | + System.out.println("Error"); |
| 230 | + } |
| 231 | + catch(FileNotFoundException e) |
| 232 | + { |
| 233 | + System.out.println("\nERROR : File not Found !!!"); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + public void clear(String filename) throws IOException |
| 238 | + { |
| 239 | + // Create a blank file |
| 240 | + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(filename))); |
| 241 | + pw.close(); |
| 242 | + System.out.println("\nAll Records cleared successfully !"); |
| 243 | + } |
| 244 | + |
| 245 | + public void Menu() throws IOException |
| 246 | + { |
| 247 | + int ch,in; |
| 248 | + System.out.println(""); |
| 249 | + do |
| 250 | + { |
| 251 | + System.out.println("[1] Create Database"); |
| 252 | + System.out.println("[2] Display Database"); |
| 253 | + System.out.println("[3] Clear Database"); |
| 254 | + System.out.println("[4] Modify Database"); |
| 255 | + System.out.println("[5] Search Database\n"); |
| 256 | + System.out.println("Enter your choice(enter 0 if exit)"); |
| 257 | + |
| 258 | + ch = Integer.parseInt(br.readLine()); |
| 259 | + System.out.println(""); |
| 260 | + |
| 261 | + if(ch==1) |
| 262 | + { |
| 263 | + addRecords(); |
| 264 | + } |
| 265 | + |
| 266 | + else if(ch==2) |
| 267 | + { |
| 268 | + readRecords(); |
| 269 | + } |
| 270 | + |
| 271 | + else if(ch==3) |
| 272 | + { |
| 273 | + clear("st.txt"); |
| 274 | + } |
| 275 | + |
| 276 | + |
| 277 | + else if(ch==4) |
| 278 | + { |
| 279 | + System.out.println("[1] DELETE records?\n[2] UPDATE records?"); |
| 280 | + in = Integer.parseInt(br.readLine()); |
| 281 | + if(in==1) |
| 282 | + { |
| 283 | + deleteRecords(); |
| 284 | + } |
| 285 | + else if(in==2) |
| 286 | + { |
| 287 | + updateRecords(); |
| 288 | + } |
| 289 | + else |
| 290 | + { |
| 291 | + System.out.println("Wrong Input :( "); |
| 292 | + } |
| 293 | + |
| 294 | + } |
| 295 | + |
| 296 | + else if(ch==5) |
| 297 | + { |
| 298 | + searchRecords(); |
| 299 | + } |
| 300 | + |
| 301 | + else if(ch!=0) |
| 302 | + { |
| 303 | + System.out.print("\nINVALID INPUT!!\n"); |
| 304 | + } |
| 305 | + } |
| 306 | + while(ch!=0); |
| 307 | + } |
| 308 | + |
| 309 | + public static void main(String args[]) throws IOException |
| 310 | + { |
| 311 | + Student call = new Student(); |
| 312 | + Call.Menu(); |
| 313 | + System.out.print("\nOPERATION COMPLETED!!! Thank you :) "); |
| 314 | + } |
| 315 | +} |
0 commit comments