4. Ask the user for a choice through a menu, to add or append, delete or modify
ID: 3905152 • Letter: 4
Question
4. Ask the user for a choice through a menu, to add or append, delete or modify a record in the file. Do the following according to the user's choice: a. Add: i. accept the Employee ld (unique) and the age from the user. ii. Write the data on to a new file and use the same file once created b. Delete: i. Accept the unique ID from the user that matches the existing one and remove the record from the file. c. Modify: i. Accept the unique ID from the user (pre-existing) and let the user modify the age. ii. Make the change in the file too Sort the data in the file before closing the file.Explanation / Answer
import java.io.*;
import java.util.*;
public class DemoRecord{
public static void sort(String[] id, int[] age, int count){
for (int i = 0; i<count; i++){
for (int j = i+1; j<count; j++){
if (id[i] > id[j]){
String t = id[i];
id[i] = id[j];
id[j] = t;
int temp = age[i];
age[i] = age[j];
age[j] = temp;
}
}
}
}
public static void main(String[] args){
try {
String[] id = new String[100];
int[] age = new int[100];
Scanner sc = new Scanner(System.in);
int count = 0;
while(true){
System.out.println("1.Add Employee ");
System.out.println("2.Delete Employee ");
System.out.println("3.Modify Employee ");
System.out.println("Enter your choice:");
int n = Integer.parseInt(sc.nextLine());
if (n == 1){
System.out.println("Enter id:");
id[count] = sc.nextLine();
System.out.println("Enter age:");
age[count] = sc.nextLine();
count++;
sort(id,age,count);
FileOutputStream fout = new FileOutStream("file1.txt");
PrintWriter pw = new PrintWriter(fout);
for (int i = 0; i<count; i++){
pw.println(id[i] + " " + age[i]);
}
pw.close();
fout.close();
System.out.println("Added ");
}
if (n == 2){
System.out.println("Enter id:");
String id1 = sc.nextLine();
int found = 0;
for (int i = 0; i<count; i++){
if (id[i] == id1){
for (int j = i+1; j<count; j--){
id[j-1] = id[j];
age[j-1] = age[j];
}
found = 1;
count--;
sort(id,age,count);
FileOutputStream fout = new FileOutStream("file1.txt");
PrintWriter pw = new PrintWriter(fout);
for (int i = 0; i<count; i++){
pw.println(id[i] + " " + age[i]);
}
pw.close();
fout.close();
}
}
if (found == 0){
System.out.println("Not found");
}
}
if (n == 3){
System.out.println("Enter id:");
String id1 = sc.nextLine();
int found = 0;
for (int i = 0; i<count; i++){
if (id[i] == id1){
System.out.println("Enter id:");
id[i] = sc.nextLine();
System.out.println("Enter age:");
age[i] = sc.nextLine();
found = 1;
sort(id,age,count);
FileOutputStream fout = new FileOutStream("file1.txt");
PrintWriter pw = new PrintWriter(fout);
for (int i = 0; i<count; i++){
pw.println(id[i] + " " + age[i]);
}
pw.close();
fout.close();
}
}
if (found == 0){
System.out.println("Not found");
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.