package whatever;
import java.io.BufferedReader;
import java.io.BufferedWriter; //write in the file
import java.io.File; //allows to create a file
import java.io.FileNotFoundException; // Signals that an attempt to open the file
denoted by a specified path name has failed.
import java.io.FileReader; //Reads text from character files using a default buffer
size. Decoding from bytesto characters uses either a specified charsetor the
default charset.
import java.io.FileWriter; //allows to write in the file
import java.io.IOException; //throws input and output errors
import java.io.InputStreamReader;//acts as a bridge between byte streams and
character streams. It reads bytes from an InputStream and converts them into
characters based on a specified character encoding.
import java.io.PrintWriter;//used for writing formatted text to files or other
output destinations, such as the console, in an easy-to-use and efficient manner.
import java.time.LocalDate;//import the date
import java.time.LocalDateTime; //import the date and time
import java.time.LocalTime; //import the time
import java.time.format.DateTimeFormatter; //import the DateTimeFormatter class
class AddStudentRecords {
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); //same function as Scanner(System.in) method that
allows the user to enter value
recordsmenu rm = new recordsmenu(); //creating a object for the class
recordsmenu()
LocalDate C_date = LocalDate.now();
LocalTime C_time = LocalTime.now();
LocalDateTime cDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("MM-dd-yyyy
HH:mm");
String formattedDateTime = cDateTime.format(dateTimeFormat);
//adding Records
public void addingRecords() throws IOException{
//Create or Modify a file for Database
Drive:\\Folder\\FileName.txt
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("C:\\Users\\Khaye\\Documents\\sample\\studentRecords.txt",true)));
//creating a file in a certain path
String name, Class, fname, mname, address, dob, mobNo;
int age;
String s;
boolean addMore = false;
//Read Data
do {
System.out.print("\nEnter Name: ");
name = br.readLine();
System.out.print("Father's Name: ");
fname = br.readLine();
System.out.print("Mother's Name: ");
mname = br.readLine();
System.out.print("Address: ");
address = br.readLine();
System.out.print("Class: ");
Class = br.readLine();
System.out.print("Date of Birth (dd/mm/yy) :");
dob = br.readLine();
System.out.print("Mobile No.: ");
mobNo = br.readLine();
System.out.print("Age: ");
age = Integer.parseInt(br.readLine());
System.out.println("Date: " + C_date);
System.out.println("Time: " + C_time);
System.out.println("Date & Time: " +
formattedDateTime);
//parseInt used to convert a String to an int. It's
commonly used when you need to convert text input that represents a number into an
integer for arithmetic operations, comparisons, or other computations.
//Print to File
pw.println(name);
pw.println(fname);
pw.println(mname);
pw.println(address);
pw.println(Class);
pw.println(dob);
pw.println(mobNo);
pw.println(age);
pw.println(C_date);
pw.println(C_time);
pw.println(formattedDateTime);
System.out.print("\nRecords added successfully! \n\
nDo you want to add more records ? (y/n) :");
s = br.readLine();
if(s.equalsIgnoreCase("y")) {//ignore the cases of
the input
addMore = true; //identify if the user wants to
add more data
System.out.println();
}else {
addMore = false;
}
}while(addMore);
pw.close(); //closes the PrintWriter method.
rm.showMenu(); //calling the showMenu method
recordsmenu class
}//end adding Records
//reading records
public void readingRecords() throws IOException
{
try
{
BufferedReader file = new BufferedReader(new
FileReader("C:\\Users\\Khaye\\Documents\\sample\\studentRecords.txt"));
//read if there is an existing file
String name;
int i=1;
boolean found = false;
// Read records from the file
while((name = file.readLine()) != null)
{
System.out.println("S.No. : " +(i++));
System.out.println("-------------");
System.out.println("\nName: " +name);
System.out.println("Father's Name : "+file.readLine());
System.out.println("Mother's Name : "+file.readLine());
System.out.println("Address: "+file.readLine());
System.out.println("Class: "+file.readLine());
System.out.println("Date of Birth : "+file.readLine());
System.out.println("Mobile. No.:"+file.readLine());
System.out.println("Age: "+Integer.parseInt(file.readLine()));//converts
String to integer
System.out.println("Date: "+file.readLine());
System.out.println("Time: "+file.readLine());
System.out.println("Date & Time: "+file.readLine());
}
if (!found) {
System.out.println("\n---No Record Found!---");
}
file.close(); //close the BufferedReader
rm.showMenu();
}
catch(FileNotFoundException e)
{
System.out.println("\nERROR : File not Found !!!");
}
}//end reading records
//search
public void searchRecord() throws IOException {
System.out.print("\nEnter the name of the student to search: ");
String searchName = br.readLine();
boolean found = false;
try (BufferedReader file = new BufferedReader(new FileReader("C:\\Users\\
Khaye\\Documents\\sample\\studentRecords.txt"))) {
String name;
while ((name = file.readLine()) != null) { //read the whole file
String fname = file.readLine();
String mname = file.readLine();
String address = file.readLine();
String Class = file.readLine();
String dob = file.readLine();
String mobNo = file.readLine();
int age = Integer.parseInt(file.readLine());
String date = file.readLine();
String time = file.readLine();
String dateTime = file.readLine();
if (name.equalsIgnoreCase(searchName)) { //searching if the file
has a data that is same with the searchname.
System.out.println("\nRecord Found!");
System.out.println("Name: " + name);
System.out.println("Father's Name: " + fname);
System.out.println("Mother's Name: " + mname);
System.out.println("Address: " + address);
System.out.println("Class: " + Class);
System.out.println("Date of Birth: " + dob);
System.out.println("Mobile No.: " + mobNo);
System.out.println("Age: " + age);
System.out.println("Date: " + date);
System.out.println("Time: " + time);
System.out.println("Date & Time: " + dateTime);
found = true;
break;
}
}
if (!found) {
System.out.println("\n---No Record Found!---");
}
} catch (NumberFormatException e) {
System.out.println("Error parsing number: " + e.getMessage());
}
rm.showMenu();
}//end searchRecord
//editRecord
public void editRecord() throws IOException {
System.out.print("\nEnter the name of the student to edit: ");
String searchName = br.readLine();
boolean found = false;
File inputFile = new File("C:\\Users\\Khaye\\Documents\\sample\\
studentRecords.txt");
File tempFile = new File("C:\\Users\\Khaye\\Documents\\sample\\
temp.txt"); //creating a temporary file to put all the data temporarily
BufferedReader file = new BufferedReader(new FileReader(inputFile));
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter(tempFile)));
String name;
while ((name = file.readLine()) != null) {
String fname = file.readLine();
String mname = file.readLine();
String address = file.readLine();
String Class = file.readLine();
String dob = file.readLine();
String mobNo = file.readLine();
int age = Integer.parseInt(file.readLine());
String nDate = file.readLine();
String nTime = file.readLine();
String nDateTime = file.readLine();
if (name.equalsIgnoreCase(searchName)) {
found = true;
System.out.println("\nRecord Found!");
System.out.println("Editing Record for: " + name);
// Prompt user for new details
System.out.print("Enter new Name (or press Enter to keep current: "
+ name + "): ");
String newName = br.readLine();
name = newName.isEmpty() ? name : newName;
//isEmpty() method is commonly used to determine whether a String,
Collection, or Map is empty.
//? : ternary operator is a shorthand for an if-else statement.
//if newName is empty returns true, then name remains unchange. if
newName returns false, then name is assigned the value of newName.
System.out.print("Enter new Father's Name (or press Enter to keep
current: " + fname + "): ");
String newFname = br.readLine();
fname = newFname.isEmpty() ? fname : newFname;
System.out.print("Enter new Mother's Name (or press Enter to keep
current: " + mname + "): ");
String newMname = br.readLine();
mname = newMname.isEmpty() ? mname : newMname;
System.out.print("Enter new Address (or press Enter to keep
current: " + address + "): ");
String newAddress = br.readLine();
address = newAddress.isEmpty() ? address : newAddress;
System.out.print("Enter new Class (or press Enter to keep current:
" + Class + "): ");
String newClass = br.readLine();
Class = newClass.isEmpty() ? Class : newClass;
System.out.print("Enter new Date of Birth (or press Enter to keep
current: " + dob + "): ");
String newDob = br.readLine();
dob = newDob.isEmpty() ? dob : newDob;
System.out.print("Enter new Mobile No. (or press Enter to keep
current: " + mobNo + "): ");
String newMobNo = br.readLine();
mobNo = newMobNo.isEmpty() ? mobNo : newMobNo;
System.out.print("Enter new Age (or press Enter to keep current: "
+ age + "): ");
String ageStr = br.readLine();
age = ageStr.isEmpty() ? age : Integer.parseInt(ageStr);
System.out.println("Date: " + nDate);
System.out.println("Time: " + nTime + "): ");
System.out.println("Date & Time: " + nDateTime + "): ");
System.out.println("\nRecord updated successfully!");
}
// Write updated (or original) record to temp file
pw.println(name);
pw.println(fname);
pw.println(mname);
pw.println(address);
pw.println(Class);
pw.println(dob);
pw.println(mobNo);
pw.println(age);
pw.println(nDate);
pw.println(nTime);
pw.println(nDateTime);
}
if (!found) {
System.out.println("\n---No Record Found!---");
}
file.close();
pw.close();
// Delete original file and rename temp file to original
if (inputFile.delete()) {
tempFile.renameTo(inputFile); //rename the temporary file
}
rm.showMenu();
}
//clear record
public void clear() throws IOException{
// Create a blank file
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("C:\\Users\\Khaye\\Documents\\sample\\studentRecords.txt")));
pw.close();
System.out.println("\nAll Records cleared successfully !");
for(int i=0;i<999999999;i++); // Wait for some time
rm.showMenu();
}//end of clear
}//end of class