import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Disp { private List emplist = new ArrayList(); // these are methods used for the structure of the arraylist private void addemp(Emp employee) { emplist.add(employee); // adding the list with employees } private void removeemp(int age) { for (Emp emp : emplist) { if (emp.getAge() == age) { emplist.remove(emp); } } } private void search_emp_byname(String name) { for (Emp emp : emplist) { if (emp.getName() == name) { System.out.println("Info for: " + emp.getName()); System.out.println("Age: " + emp.getAge()); System.out.println("Lastname: " + emp.getLastname()); } } } private void show_emp() { for (Emp emp : emplist) { System.out.println(emp.getName() + " " + emp.getLastname() + " " + emp.getAge()); } } private void clearemp() { emplist.clear(); } public static void main(String[] args) { Disp disp = new Disp(); // create objects for class Disp Scanner sc = new Scanner(System.in); boolean flag = true; while (flag) { System.out.println("Enter 1 if you want to add information , enter 2 if you want to delete information, enter 3 if you want to show the information" + " " + " enter 4 if you want to exit or enter 5 if you want to clear all data: "); int nr = sc.nextInt(); switch (nr) { case 1: System.out.println("How many employees do you want to show: "); int e = sc.nextInt(); for (int i = 0; i < e; i++) { System.out.println("Enter name: "); sc.nextLine(); String name = sc.nextLine(); System.out.println("Enter lastname: "); String lastname = sc.nextLine(); System.out.println("Enter age: "); int age = sc.nextInt(); Emp emp = new Emp(lastname, name, age); disp.addemp(emp); } break; case 2: System.out.println("Which Employee do you want to delete by age: "); int delete = sc.nextInt(); disp.removeemp(delete); // using the object from class Disp System.out.println("Deleted"); break; case 3: System.out.println("The Employees are: "); disp.show_emp(); break; case 4: flag=false; System.out.println("You exit"); break; case 5: disp.clearemp(); System.out.println("The register is empty"); } } } } //after you have created the employee class do this class. // you can input the values
This helped me a lot! Thanks for the tutorial
Hi ,How to add multiple class objects(like employee, customer) to single list/arraylist
This is very helpful. please comeup with similar projects using linkedlist and iterator.
Hello if we want add emplyee by name what we do
how to return the object when its id is matvhed
thank you so much for this
how to update on arraylist?
Which app you are writing
It's an online tool, lucid chart
Can we take details from user
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Disp {
private List emplist = new ArrayList();
// these are methods used for the structure of the arraylist
private void addemp(Emp employee) {
emplist.add(employee); // adding the list with employees
}
private void removeemp(int age) {
for (Emp emp : emplist) {
if (emp.getAge() == age) {
emplist.remove(emp);
}
}
}
private void search_emp_byname(String name) {
for (Emp emp : emplist) {
if (emp.getName() == name) {
System.out.println("Info for: " + emp.getName());
System.out.println("Age: " + emp.getAge());
System.out.println("Lastname: " + emp.getLastname());
}
}
}
private void show_emp() {
for (Emp emp : emplist) {
System.out.println(emp.getName() + " " + emp.getLastname() + " " + emp.getAge());
}
}
private void clearemp() {
emplist.clear();
}
public static void main(String[] args) {
Disp disp = new Disp(); // create objects for class Disp
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("Enter 1 if you want to add information , enter 2 if you want to delete information, enter 3 if you want to show the information" + "
" + " enter 4 if you want to exit or enter 5 if you want to clear all data: ");
int nr = sc.nextInt();
switch (nr) {
case 1:
System.out.println("How many employees do you want to show: ");
int e = sc.nextInt();
for (int i = 0; i < e; i++) {
System.out.println("Enter name: ");
sc.nextLine();
String name = sc.nextLine();
System.out.println("Enter lastname: ");
String lastname = sc.nextLine();
System.out.println("Enter age: ");
int age = sc.nextInt();
Emp emp = new Emp(lastname, name, age);
disp.addemp(emp);
}
break;
case 2:
System.out.println("Which Employee do you want to delete by age: ");
int delete = sc.nextInt();
disp.removeemp(delete); // using the object from class Disp
System.out.println("Deleted");
break;
case 3:
System.out.println("The Employees are: ");
disp.show_emp();
break;
case 4:
flag=false;
System.out.println("You exit");
break;
case 5:
disp.clearemp();
System.out.println("The register is empty");
}
}
}
}
//after you have created the employee class do this class.
// you can input the values
Yes , I have made new video, in that we had taken input from user. You can refer that
@@prasadkadu9737 can you please provide the new video link?
@@raihanchowdhury295 ua-cam.com/video/ycrxp5c3OUY/v-deo.html
There was issue with mic so volume is low
Solution: for (Iterator iterator = employeeList.iterator(); iterator.hasNext();){
Employee employee = iterator.next();
if (employee.getId()==id){
iterator.remove();
}
}
Here why using private
Private is always preferred
Thanks
Thank you..
sort kese krenge bhai by name
May allah guide you txs