0% found this document useful (0 votes)
17 views

Assignment 0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment 0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 0

Student Name: Maitreyee Shendye


USN Number: UCE2023460
Division: Comp A
Batch: A3
Problem Statement: Consider the Employees data like (EmpNo, EmpName,
Post, Salary, Department) and implement the following operations on the
database using file handling in any language like JAVA, Python etc.
Code:
package ass;
import java.util.*;
import java.io.*;
class InvalidNameException extends Exception{
InvalidNameException(){
super("Name cannot contain numbers");
}
}
class EmployeeData
{
boolean flag=false;
int id;
long salary;
String name,post,department,data;
Scanner sc = new Scanner(System.in);
void accept()
{
System.out.print("Enter employee id: ");
id = sc.nextInt();
do {
try {
System.out.print("Enter employee name: ");
name = sc.next();
if(name.contains("1")||name.contains("2")||name.contains("3")||
name.contains("4")||name.contains("5")||name.contains("6")||
name.contains("7")||name.contains("8")||name.contains("9")||
name.contains("0")) {
flag=true;
throw new InvalidNameException();
}
}
catch(InvalidNameException e)
{
System.out.println(e+e.getMessage());
}
}while(flag==true);
System.out.print("Enter employee department: ");
department = sc.next();
System.out.print("Enter employee post: ");
post = sc.next();
System.out.print("Enter employee salary: ");
salary = sc.nextLong();
data ="Id: "+ id+ ",Name: "+ name+",Department: "+department+ ",Post:
"+post+",Salary: "+salary+"rupees"+"\n";
}
}
public class Main {
public static void main(String[] args) {
EmployeeData employee [] = new EmployeeData[100];
Scanner in = new Scanner(System.in);
int n,option=0;
ArrayList<String> Data = new ArrayList<>();
System.out.print("Enter number of employee: ");
n = in.nextInt();
try {
FileWriter writer = new FileWriter("C:\\Users\\DELL\\OneDrive\\Desktop\\
DBMSL assignments\\Assignment_0\\EmployeeData.txt");
for(int i=0;i<n;i++) {
employee[i] = new EmployeeData();
employee[i].accept();
writer.write(employee[i].data);
System.out.println("Data is written to the file.");
Data.add(employee[i].data);
}
writer.close();
}
catch (Exception e) {
e.getStackTrace();
}
do{
System.out.println("Enter\n1: Add new records\n2: Search by Id\n3: Search
by name\n4: Exit");
option = in.nextInt();
switch(option)
{
case 1:
{
employee[n] = new EmployeeData();
employee[n].accept();
Data.add(employee[n].data);
n++;
try {
FileWriter writer = new FileWriter("C:\\Users\\DELL\\OneDrive\\Desktop\\
DBMSL assignments\\Assignment_0\\EmployeeData.txt");
for(int i=0;i<n;i++) {
writer.write(Data.get(i));
}
System.out.println("Data is written to the file.");
writer.close();
}
catch (Exception e) {
e.getStackTrace();
}
break;
}
case 2:
{
System.out.println("Enter id to be serched");
CharSequence id=in.next();
id="Id: "+id;
File EmpData=new File("C:\\Users\\DELL\\OneDrive\\Desktop\\DBMSL
assignments\\Assignment_0\\EmployeeData.txt");
try {
int i=0;
boolean flag=false;
Scanner sc = new Scanner(EmpData).useDelimiter(" ");
while(sc.hasNext()) {
i++;
final String linefromfile=sc.nextLine();
if(linefromfile.contains(id)) {
flag=true;
System.out.println("Employee found!");
System.out.println(Data.get(i-1));
}
}
if(flag==false) {
System.out.println("Employee not found!");
}
}
catch(Exception e) {
e.printStackTrace();
}
break;
}
case 3:
{
int i=0;
System.out.println("Enter name to be searched: ");
CharSequence name = in.next();
File EmployeeData=new File("C:\\Users\\DELL\\OneDrive\\Desktop\\DBMSL
assignments\\Assignment_0\\EmployeeData.txt");
try {
boolean flag=false;
Scanner sc2=new Scanner(EmployeeData).useDelimiter(" ");
while(sc2.hasNext()){
i++;
final String linefromfile=sc2.nextLine();
if(linefromfile.contains(name)) {
flag=true;
System.out.println("Employee found!");
System.out.println(Data.get(i-1));
}
}
if(flag==false) {
System.out.println("Employee not found!");
}
}
catch(Exception e) {
e.printStackTrace();
}
break;
}
case 4:{
System.out.println("Thanks for using our services");
break;
}
default:
{
System.out.println("Error!! Match not found.\nPlease check your input");
}
}
}while(option!=4);
}
}

Output:
Enter number of employee: 10
Enter employee id: 1
Enter employee name: Kai
Enter employee department: HR
Enter employee post: Manager
Enter employee salary: 110000
Data is written to the file.
Enter employee id: 2
Enter employee name: Aria
Enter employee department: Sales
Enter employee post: CustomerServices
Enter employee salary: 700000
Data is written to the file.
Enter employee id: 3
Enter employee name: Elara
Enter employee department: Legal
Enter employee post: Manager
Enter employee salary: 800000
Data is written to the file.
Enter employee id: 4
Enter employee name: Nova
Enter employee department: Administration
Enter employee post: Assistant
Enter employee salary: 500000
Data is written to the file.
Enter employee id: 5
Enter employee name: Sitara
Enter employee department: Executive
Enter employee post: COO
Enter employee salary: 8000000
Data is written to the file.
Enter employee id: 6
Enter employee name: Anika
Enter employee department: Marketing
Enter employee post: ContentCreator
Enter employee salary: 600000
Data is written to the file.
Enter employee id: 7
Enter employee name: Clio
Enter employee department: R&D
Enter employee post: TechnicalWriter
Enter employee salary: 700000
Data is written to the file.
Enter employee id: 8
Enter employee name: Finn
Enter employee department: Accounting
Enter employee post: Accountant
Enter employee salary: 600000
Data is written to the file.
Enter employee id: 9
Enter employee name: Orian
Enter employee department: IT
Enter employee post: Developer
Enter employee salary: 5000000
Data is written to the file.
Enter employee id: 10
Enter employee name: Seema
Enter employee department: R&D
Enter employee post: DevelopmentEngineer
Enter employee salary: 5600000
Data is written to the file.
File:

Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
1
Enter employee id: 11
Enter employee name: Sora
Enter employee department: Sales
Enter employee post: Manager
Enter employee salary: 800000
Data is written to the file.
File:
Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
2
Enter id to be serched
7
Employee found!
Id: 7,Name: Clio,Department: R&D,Post: TechnicalWriter,Salary: 700000rupees

Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
2
Enter id to be serched
67
Employee not found!

Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
3
Enter name to be searched:
Clio
Employee found!
Id: 7,Name: Clio,Department: R&D,Post: TechnicalWriter,Salary: 700000rupees

Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
3
Enter name to be searched:
Minnie
Employee not found!

Enter
1: Add new records
2: Search by Id
3: Search by name
4: Exit
4
Thanks for using our services

You might also like