Created
November 21, 2022 05:45
-
-
Save muhammadqazi/e2cea9ceb013ef62d54357d89879a6cd to your computer and use it in GitHub Desktop.
mid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.nio.charset.StandardCharsets; | |
import java.util.ArrayList; | |
public class Midterm { | |
private final String student_file; | |
public Midterm(String student_file) { | |
this.student_file = student_file; | |
} | |
public void display() { | |
File file = new File(student_file); | |
if (file.exists()) { | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
String student; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
System.out.println( | |
"ID: \t" + arr[0] + "\t " + | |
"Name: \t" + String.format("%-30s", arr[1].strip()) + "\t " + | |
"CGPA: \t" + arr[2] + "\t " + | |
"DOB: \t" + arr[3] + " \t" + | |
"Gender: " + arr[4] | |
); | |
} | |
fr.close(); | |
br.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
System.out.println("File not found."); | |
} | |
public void displayGender(String gender) { | |
File file = new File(student_file); | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
String student; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
if (arr[4].equals(gender)) { | |
System.out.println( | |
"ID: \t" + arr[0] + "\t " + | |
"Name: \t" + String.format("%-30s", arr[1].strip()) + "\t " + | |
"CGPA: \t" + arr[2] + "\t " + | |
"DOB: \t" + arr[3] + " \t" + | |
"Gender: " + arr[4] | |
); | |
} | |
} | |
fr.close(); | |
br.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void displayById(long id) { | |
File file = new File(student_file); | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
String student; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
if (Integer.parseInt(arr[0]) == id) { | |
System.out.println( | |
"ID: \t" + arr[0] + "\t " + | |
"Name: \t" + String.format("%-30s", arr[1].strip()) + "\t " + | |
"CGPA: \t" + arr[2] + "\t " + | |
"DOB: \t" + arr[3] + " \t" + | |
"Gender: " + arr[4] | |
); | |
} | |
} | |
fr.close(); | |
br.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void insert(long id, String name, double cgpa, String dob, String gender) { | |
File file = new File(student_file); | |
try { | |
FileWriter fw = new FileWriter(file, true); | |
fw.write(id + "," + String.format("%-30s", name) + "," + cgpa + "," + dob + "," + gender + System.getProperty("line.separator")); | |
fw.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void stats() { | |
File file = new File(student_file); | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
int males = 0; | |
int females = 0; | |
double mcgpa = 0.0; | |
double fcgpa = 0.0; | |
String student; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
if (arr[4].equals("M")) | |
males++; | |
mcgpa += Double.parseDouble(arr[2]); | |
if (arr[4].equals("F")) | |
females++; | |
fcgpa += Double.parseDouble(arr[2]); | |
} | |
String str = males + " males cgpa is " + mcgpa / males + "\n" + females + " females cgpa is " + fcgpa / females; | |
System.out.println(str); | |
fr.close(); | |
br.close(); | |
FileWriter fw = new FileWriter("exam.txt"); | |
fw.write(str); | |
fw.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void delete(long id) { | |
File file = new File(student_file); | |
if (file.exists()) { | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
ArrayList<String> newList = new ArrayList<>(); | |
String student; | |
boolean found = false; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
if (Integer.parseInt(arr[0]) != id) { | |
newList.add(student); | |
} | |
if (Integer.parseInt(arr[0]) == id) { | |
found = true; | |
} | |
} | |
fr.close(); | |
br.close(); | |
if (found) { | |
FileWriter fw = new FileWriter(student_file); | |
for (String s : newList) { | |
fw.write(s + System.getProperty("line.separator")); | |
} | |
fw.close(); | |
} else { | |
System.out.println("Student not found."); | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
public void modify(long id, double value) { | |
File file = new File(student_file); | |
try { | |
RandomAccessFile rf = new RandomAccessFile(file, "rw"); | |
byte[] std_id = new byte[4]; | |
byte[] record = new byte[55]; | |
String id_str = null; | |
String record_str = null; | |
boolean found = false; | |
while (rf.getFilePointer() <= rf.length() - 2) { | |
rf.read(std_id, 0, 4); | |
id_str = new String(std_id); | |
if (id_str.equals(String.valueOf(id))) { | |
rf.seek(rf.getFilePointer() + 32); | |
rf.write(String.valueOf(value).getBytes(StandardCharsets.UTF_8)); | |
rf.seek(rf.getFilePointer() - 40); | |
rf.read(record, 0, 53); | |
record_str = new String(record); | |
found = true; | |
} | |
if (found) | |
break; | |
rf.seek(rf.getFilePointer() + 50); | |
} | |
rf.close(); | |
System.out.println(record_str); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void deleteByRandomAccess(long id) { | |
File file = new File(student_file); | |
try { | |
RandomAccessFile rf = new RandomAccessFile(file, "rw"); | |
boolean found = false; | |
byte[] std_id = new byte[4]; | |
byte[] record = new byte[54]; | |
String id_Str = null; | |
String record_str = null; | |
ArrayList<String> newList = new ArrayList<>(); | |
while (rf.getFilePointer() <= rf.length() - 2) { | |
rf.read(std_id, 0, 4); | |
id_Str = new String(std_id); | |
if (!id_Str.equals(String.valueOf(id))) { | |
rf.seek(rf.getFilePointer() - 4); | |
rf.readLine(); | |
rf.seek(rf.getFilePointer() - 54); | |
rf.read(record, 0, 54); | |
record_str = new String(record); | |
newList.add(record_str); | |
} | |
rf.seek(rf.getFilePointer() + 50); | |
} | |
rf.close(); | |
RandomAccessFile rf2 = new RandomAccessFile(file, "rw"); | |
for (String s : newList) { | |
rf2.write(s.getBytes(StandardCharsets.UTF_8)); | |
} | |
rf2.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void changeExam(long id, String exam, int point) { | |
File file = new File("exam.txt"); | |
try { | |
RandomAccessFile rf = new RandomAccessFile(file, "rw"); | |
byte[] std_id = new byte[8]; | |
byte[] record = new byte[51]; | |
String id_str = null; | |
String record_str = null; | |
boolean found = false; | |
long pos = 0; | |
switch (exam) { | |
case "mt": | |
pos = 32; | |
break; | |
case "qz": | |
pos = 35; | |
break; | |
case "asg": | |
pos = 38; | |
break; | |
case "fin": | |
pos = 41; | |
break; | |
} | |
while (rf.getFilePointer() <= rf.length() - 2) { | |
rf.read(std_id, 0, 8); | |
id_str = new String(std_id); | |
if (id_str.equals(String.valueOf(id))) { | |
rf.seek(rf.getFilePointer() + pos); | |
rf.write(String.valueOf(point).getBytes(StandardCharsets.UTF_8)); | |
found = true; | |
} | |
if (found) | |
break; | |
rf.seek(rf.getFilePointer() + 44); | |
} | |
rf.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public void calculateGrades(String input, String output) { | |
File file = new File(input); | |
if (file.exists()) { | |
try { | |
FileReader fr = new FileReader(file); | |
BufferedReader br = new BufferedReader(fr); | |
String student; | |
String id = null, grade = null; | |
double total = 0.0; | |
long mid = 0, quiz = 0, asgn = 0, fin = 0; | |
while ((student = br.readLine()) != null) { | |
String[] arr = student.split(","); | |
id = arr[0]; | |
mid += Integer.parseInt(arr[2]); | |
quiz += Integer.parseInt(arr[3]); | |
asgn += Integer.parseInt(arr[4]); | |
fin += Integer.parseInt(arr[5]); | |
} | |
fr.close(); | |
br.close(); | |
total = mid * 0.25 + quiz * 0.20 + asgn * 0.10 + fin * 0.45; | |
if (total >= 85) { | |
grade = "A"; | |
} else if (total < 85) { | |
grade = "B"; | |
} else { | |
grade = "F"; | |
} | |
FileWriter fw = new FileWriter(output); | |
fw.write("ID : " + id + "," + "Total : " + total + "," + "Grade : " + grade); | |
fw.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
public void writingWithBufferWriter() { | |
File file = new File("test.txt"); | |
try { | |
FileWriter fw = new FileWriter(file); | |
BufferedWriter bw = new BufferedWriter(fw); | |
bw.write("20170001,20170002,20170003,20170004,20170005,20170006,20170007,20170008,20170009,20170010"); | |
bw.newLine(); | |
bw.write("mt,mt,mt,mt,mt,mt,mt,mt,mt,mt"); | |
bw.newLine(); | |
bw.write("qz,qz,qz,qz,qz,qz,qz,qz,qz,qz"); | |
bw.newLine(); | |
bw.write("asg,asg,asg,asg,asg,asg,asg,asg,asg,asg"); | |
bw.newLine(); | |
bw.write("fin,fin,fin,fin,fin,fin,fin,fin,fin,fin"); | |
bw.newLine(); | |
bw.write("0,0,0,0,0,0,0,0,0,0"); | |
bw.newLine(); | |
bw.write("0,0,0,0,0,0,0,0,0,0"); | |
bw.newLine(); | |
bw.write("0,0,0,0,0,0,0,0,0,0"); | |
bw.newLine(); | |
bw.write("0,0,0,0,0,0,0,0,0,0"); | |
bw.newLine(); | |
bw.close(); | |
fw.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment