Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help please....deleting in arrays

Status
Not open for further replies.

cindyg919

Programmer
Feb 10, 2001
17
US
hi i need help!!!!
i am writing a program that has a class Employee that is derived from a class Person.....i have another class EmployeeData and in this class:

1. has one array of max 20 elements.
Each element is an object of type Employee. This class
should have methods for adding an employee, deleting
an employee and modifying an employee.

so far i have:

public class EmployeeData
{
Employee[][][][] a = new Employee[20][20][20][20];

public void add()
{
System.out.println("Enter new employee socialnumber. [Example: 123456789]");
System.out.println("Enter new employee name:");
System.out.println("Enter new employee salary. Integer value only please. No decimals!!!");
System.out.println("Enter new employee email address:");

int i;
for(i = 0; i < a.length; i++)
{
a = SavitchIn.readLineEmployee();
}
}

public void delete()
{
System.out,println(&quot;Enter the employees social security number of that employee you want to delete&quot;);
String ans = SavitchIn.readLineNonwhiteChar();

int i;
for(i = 0; i < a.length; --i)
{
if(ssNum == ans)
{
System.out.println(&quot;Deleting: &quot; + ssNum + &quot; data&quot;);


i only have that much for deleting......i am stuck..please help how would i delete....the user would enter the ss number of the employee they want deleted and then it would search and find the array of that employee and delete it and move everything else up one index. how can i do that?????
please help me asap!!!!
thank-you so much.
 
Hi.

I do not understand why you use an four dimensions array in your EmployeeData.
If I understand well, EmployeeData is just an agregate of Employee instances.
It might be better if you do something like this :

import java.util.Hashtable;//assuming you have a JDK 1.2

public class EmployeeData
{
private Hashtable employees;

EmployeeData ()
{ employees = new Hashtable (20);/*Initial capacity*/ }

addEmployee (String socialNumber, Employee e)
{ employees.put (socialNumber, e); }

deleteEmployee (String socialNumber)
{ employees.remove (socialNumber); }

}

I hope this will help you.
--
Globos
 
hi thanx for helping but one problem in this program i am not using hashtables or importing anything.....you are right i dont need 4-dimensional arrays i changed it to:
Employee[] e = new Employee[20];

but i do not know how i can delete.....i know that i can replace each element with the next and then delete the empty space leftover....but how do i do this??????
thanx.....also i think i need to sort it first.
 
I would like to offer a solution or rather my opinion of what can be done, but first... why are you refusing to import? I have an idea but it requires the use of two imports. (java.lang.* and java.util.*)

Let me know if you are interested in what I mean.

Later.

I hope this helped! ;-)
- Casey Winans
 
ok sorry let me give you the whole discription of the program:
The program is about a university that maintains data about its employees in various department. A department can have at most 20 employees.
For each employee following data are to be stored:
1. Social Security number ( a unique string containing 9 digits )
2. Name ( may be duplicate )
3. Salary ( integer > 0 )
4. email address ( unique) somename@aol.com.....
(the email address can have numbers and letters (a-z, A-Z) and may hav two periods...... cg@ic.mon.edu)

I need to write the following classes:

1. DemoUinversity class with main method in which I will create only one object cs_employees of type EmployeeData. It displays a menu of choices asking user whether he/she wants to enter a new employee data , delete existing employee or modify employee data ( ssn can not be modified ), list all employees' data or find average salary of department. Social Security number should be used for
searching employee records whenever employee data is to be
changed/deleted. Program should continue asking the user what he/she wants to do until he/she says 'no'.

2. EmployeeData class which will have one array of max 20 elements. Each element will be an object of type Employee. This class should have methods for adding an employee, deleting an employee and modifying an employee. When new elements are being added/modified to the array, I need to make sure that ssn, email address are in correct format and salary is an integer more than 0. If not, appropriate exceptions should be thrown and caught and messages be displayed to re-enter the data. I have to make sure that the SSN and email are unique. Throw an appropriate exception to ensure this (e.g. NotUniqueException).

3. Employee class derived from Person class. With instance variables as specified above. It should have constructors, getter & setter methods, output methods. How many and which ones should be included must be determined by the problem requirement as described in 1 above.

I have done the Person class:

public class Person
{
private String name;

public Person()
{
name = &quot;No name yet.&quot;;
}

public Person(String initialName)
{
name = initialName;
}

public void setName(String newName)
{
name = newName;
}

public String getName()
{
return name;
}

public void writeOutput()
{
System.out.println(&quot;Name:&quot; + name);
}

public boolean sameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.name));
}
}

this is the Person class.
Please try to help me as much as possible .... as i am an ameuture at Java....;-)
 
Ok... this is my take on your problem.

Employee class
[tt]
public class Employee {
private String name;
private String soc_no;
private String email;
private Integer salary;

public Employee() {
name = null;
soc_no = null;
email = null;
salary = null;
}

public Employee(String name, String soc_no, String email, int salary) {
this.name = name;
this.soc_no = soc_no;
this.email = email;
this.salary = new Integer(salary);
}

public String getName() {
return name;
}

public String getEmailAddress() {
return email;
}

public String getSocSecNo() {
return soc_no;
}

public int getSalary() {
return salary.intValue();
}

public boolean isSameSocNo(String soc_no) {
return (this.soc_no.equalsIgnoreCase(soc_no));
}

public boolean isSameEmail(String email) {
return (this.email.equalsIgnoreCase(email));
}

public String toString() {
String temp = &quot;Name: &quot; + name + &quot;\nSoc No: &quot; + soc_no + &quot;\nEmail: &quot; +
email + &quot;\nSalary: &quot; + salary.intValue() + &quot;\n&quot;;
return temp;
}

//...
//add your get/set and other comparative methods
}
[/tt]




EmployeeData class
[tt]

import java.lang.*;
import java.util.*;

public class EmployeeData extends Vector {
private int MAX_CAPACITY;

public static void main(String [] args) {
EmployeeData ed = new EmployeeData();

Employee emp = new Employee(&quot;Casey&quot;, &quot;123456789&quot;, &quot;you@me.com&quot;, 60000);
ed.addEmployee(emp);
emp = null;

//emp = new Employee(&quot;Casey&quot;, &quot;123456789&quot;, &quot;you@me.com&quot;, 60000); - doesn't get added
emp = new Employee(&quot;Casey&quot;, &quot;124456789&quot;, &quot;you@me2.com&quot;, 60000);
ed.addEmployee(emp);

System.out.println(ed);
}

public EmployeeData() {
super();// initialize Vector
MAX_CAPACITY = 20;
}

public EmployeeData(int initCap) {
super(initCap);// initialize Vector
MAX_CAPACITY = initCap;
}

public void setMaxCapacity(int cap) {
MAX_CAPACITY = cap;
}

public boolean addEmployee(Employee emp) {
// Enforcing size restrictions
if(size() >= MAX_CAPACITY)
return false;

String soc_no = emp.getSocSecNo();
String email = emp.getEmailAddress();

for(int i = 0; i < size(); i++) { // inherited method [size] from Vector
// Checking for duplicates
Employee e = (Employee)elementAt(i); // inherited method [elementAt] from Vector

if(e.isSameSocNo(soc_no) || e.isSameEmail(email))
return false;
}

// No duplicates were found... adding employee to list
addElement(emp); // inherited method [addElement] from Vector
return true;
}

public void removeEmployee(int index) {
removeElementAt(index); // inherited method [removeElementAt] from Vector
}

public String toString() {
StringBuffer temp = new StringBuffer();

for(int i = 0; i < size(); i++)
temp.append((Employee)elementAt(i) + &quot;\n&quot;);

return temp.toString();
}

}
[/tt]


Just yell if you have trouble following this!!
I hope this helped! ;-)
- Casey Winans
 
k thank-you but the only problem is that the Employee data has to be in arrays...ex:
Employee e = new Employee[20];
and each element has to be type Employee
and then the methods : add, delte,modify.....
does this make sense??
thanks again :)
 
Hi.

You want an array to store your Employees, but this might not be a good way. You can use the classes given by cwinans, and when you really want an array of Employees, you can do :

...
EmployeeData data = new EmployeeData ();
...//Enter data
//You want an array of your Employees?
//Do :
Employee[] dataArray = (Employee[]) data.toArray();
...

--
Globos
 
Every is doing such a great job in helping you Cindy, so I won't comment on this, but just so everyone knows:

FYI: java.lang.* package does not have to be imported into any java program. This package is implicitly put imported into all java programs because it is the package for the basis of the language itself.

Brian
 
Hi Cindy,

Take a look at this...I guess this should be what you wanted. Please keep in mind that this is NOT a complete program. It is still short of plenty of error checkings etc.

import java.io.*;
class Person
{
private String name;
public Person()
{
name = &quot;&quot;;
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println(&quot;Name:&quot; + name);
}
public boolean sameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.name));
}
}

class Employee extends Person
{
private String ssno;
private static int runno=1000;
//String name;
private int salary;
private String email;
public Employee(String name,int salary,String email)
{
ssno = &quot;&quot;+(runno+1);
runno++;
super.setName(name);
this.salary = salary;
this.email = email;
}
public String getSSNo()
{
return ssno;
}
public String getName()
{
return super.getName();
}
public int getSalary()
{
return salary;
}
public String getEmail()
{
return email;
}
}

class EmployeeData
{
private Employee[] employeeList;
public EmployeeData()
{
employeeList = new Employee[20];
}
public void addEmployee(String name,int salary,String email)
{
for (int i=0;i<employeeList.length;i++)
{
if (employeeList == null)
{
employeeList = new Employee(name,salary,email);
break;
}
}
}
public void deleteEmployee(String ssno)
{
for (int i=0;i<employeeList.length;i++)
{
if (employeeList != null)
{
Employee e = employeeList;
if (e.getSSNo().equals(ssno))
{
for (int j=i;j<employeeList.length-1;j++)
{
employeeList[j] = employeeList[j+1];
}
}
}
}
}
public void displayAllEmployee()
{
for (int i=0;i<employeeList.length;i++)
{
if (employeeList != null)
{
Employee e = employeeList;
System.out.println(&quot;SSNO : &quot;+e.getSSNo());
System.out.println(&quot;Name : &quot;+e.getName());
System.out.println(&quot;Salary : &quot;+e.getSalary());
System.out.println(&quot;Email : &quot;+e.getEmail()+&quot;\n&quot;);
}
}
}
}

public class mainApp
{
public static void main(String[] args)
{
try
{
String choice = &quot;&quot;;
EmployeeData ed = new EmployeeData();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println(&quot;\nMain Menu&quot;);
System.out.println(&quot;---------&quot;);
System.out.println(&quot;1)Add Employee&quot;);
System.out.println(&quot;2)Delete Employee&quot;);
System.out.println(&quot;3)Display All Employee&quot;);
System.out.println(&quot;4)Quit&quot;);
System.out.print(&quot;Enter a choice : &quot;);
choice = (br.readLine()).trim();
if (choice.equals(&quot;1&quot;))
{
String name=&quot;&quot;;
int salary=0;
String email=&quot;&quot;;

System.out.print(&quot;Enter Name of New Employee : &quot;);
name = br.readLine();
//assume input is integer
try
{
System.out.print(&quot;Enter Salary of New Employee : &quot;);
salary = (new Integer(br.readLine())).intValue();
}
catch (Exception e)
{
}
System.out.print(&quot;Enter Email of New Employee : &quot;);
email = br.readLine();

ed.addEmployee(name,salary,email);
}
else if (choice.equals(&quot;2&quot;))
{
try
{
System.out.print(&quot;Enter SSNo of Employee to be deleted : &quot;);
String ssno = (br.readLine()).trim();
ed.deleteEmployee(ssno);
}
catch (Exception e)
{
}
}
else if (choice.equals(&quot;3&quot;))
{
System.out.println(&quot;Displaying all Employees....\n&quot;);
ed.displayAllEmployee();
}
else
{
if (!choice.equals(&quot;4&quot;))
{
System.out.println(&quot;Invalid Choice!!!&quot;);
System.out.print(&quot;Please enter again : &quot;);
}
}
}while (!choice.equals(&quot;4&quot;));
}
catch (Exception e)
{
}
}
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
hi everybody....thanx for the help....i got the program the way i needed it to be....thanks for the help... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top