The employee ingest system for Java projects

Source: Internet
Author: User
Tags java se

In Java SE, the operation of IO streams and collections is important in the application. Next, I demonstrate the use of knowledge points such as IO streams and collections in practice in the form of a small project.

The project is named "Employee ingest system" and operates on the eclipse's console. The operating interface is as follows:

The file structure of the project is as follows:

Step 1:

The code for the Ingress class Systemmain is:

Package Empsystem;

Import Java.util.Scanner;

/**
* Main interface
* One scanner Entry object
* Employ class
* File path
* Check the weight searchid
* @author Li Zhangyong
*
*/

Public class Systemmain {
Private Scanner sc=new Scanner (system.in);

Public Systemmain () {
Showwelcome ();
}

public void Showwelcome () {
System.out.println ("----Employee ingest system");
System.out.println ("1. Adding staff functions");
System.out.println ("2. View employee Functions");
System.out.println ("3. Modify employee Functions");
System.out.println ("4. Remove employee Features");
System.out.println ("5. Exit system");
String choice=sc.nextline ();
Switch (choice) {
Case "1":
System.out.println ("You have selected to add user functionality");
Add
New Add ();
Break
Case "2":
System.out.println ("You have selected to view user features");
Search
New Showemp ();
Break
Case "3":
System.out.println ("You have selected to modify user features");
Modify
New Modify ();
Break
Case "4":
System.out.println ("You have selected to remove user features");
Delete user Delete
new delete ();
Break
Case "5":
System.out.println ("You have selected to exit the system");
Return
Default:
System.out.println ("No this feature");
Break
}
Showwelcome ();


}

public static void Main (string[] args) {
new Systemmain ();

}

}

Step 2:

Write file path filepath interface.

Package Empsystem;

Public interface FilePath {
public static final String path_name= "Emp.em";
}

Step 3:

Write employee class employ.

Package Empsystem;

Import java.io.Serializable;

/**
* Id,name
* @author Li Zhangyong
*
*/
Public class employ implements serializable{
private int id;
private String name;

Public employ () {

}

public employ (int ID, String name) {
Super ();
This.id = ID;
THIS.name = name;
}

public int getId () {
return ID;
}

public void setId (int id) {
This.id = ID;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

@Override
Public String toString () {
Return "employ [id=" + ID + ", name=" + name + "]\n";
}
}

Step 4:

Find the class Searchid of the employee based on the ID.

Package Empsystem;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import java.util.ArrayList;
Import java.util.List;

/**
* Find employees by ID
* @author Li Zhangyong
*
*/
public class Searchid {
Private Searchid () {}

public static employ Searchid (int id) {
File File=new file (filepath.path_name);
if (file.exists ()) {
try {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));
try {
Arraylist<employ> ems= (arraylist<employ>) ois.readobject ();
Ois.close ();
for (int i=0;i<ems.size (); i++) {
if (Id==ems.get (i). GetId ()) {
return Ems.get (i);
}
}

} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}else{
return null;
}

return null;
}
}

Step 5:

Next is to increase, check, change, delete the class, respectively, is the Add class, Showemp class, modify class, modify class.

(1)

Package Empsystem;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.util.ArrayList;
Import Java.util.Scanner;

/**
* One input object scanner
* File
* Collection Object ArrayList
* @author Li Zhangyong
*
*/
public class Add {
Private Scanner sc=new Scanner (system.in);
Private file File=new file (filepath.path_name);
Private arraylist<employ> EMS;


Public Add () {
if (file.exists ()) {
try {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));
try {
Ems= (arraylist<employ>) ois.readobject ();
Ois.close ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}else{
Ems=new arraylist<employ> ();
}
if (ems!=null) {
Add ();
}else{
SYSTEM.OUT.PRINTLN ("System internal problem, unable to operate");
Return
}

}

public boolean checknum (String idstr) {
Detecting input formats
if (Idstr==null | | idstr.equals ("")) {
SYSTEM.OUT.PRINTLN ("Illegal input, repeat");
return false;
}
Char[] Cs=idstr.tochararray ();
for (int i=0;i<cs.length;i++) {
if (cs[i]< ' 0 ' | | cs[i]> ' 9 ') {
SYSTEM.OUT.PRINTLN ("Input illegal, repeat");
return false;
}
}
return true;
}
Private String idstr;
public int Getrightnum () {
Idstr=sc.nextline ();
if (!checknum (IDSTR)) {
Getrightnum ();
}
int Id=integer.parseint (IDSTR);
return ID;
}

public void Askgoon () {
System.out.println ("Would you like to continue typing?") y/n ");
String Choice=sc.nextline ();

if ("Y". Equalsignorecase (choice)) {
Add ();
}else if ("N". Equalsignorecase (choice)) {
Save to File
SaveToFile ();
Return
}else{
System.out.println ("No this command, please re-select!") ");
Askgoon ();
}
}

public void SaveToFile () {
try {
ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (file));
Oos.writeobject (EMS);
Oos.close ();
Test Print View
System.out.println ("add success");
SYSTEM.OUT.PRINTLN (EMS);
Return
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}


public void Add () {
System.out.println ("Please enter User id:");
Returns an integer
int Id=getrightnum ();

for (int i=0;i<ems.size (); i++) {
if (Id==ems.get (i). GetId ()) {
SYSTEM.OUT.PRINTLN ("ID already exists, please re-enter");
Add ();
}
}
System.out.println ("Please enter employee Name:");
String Name=sc.nextline ();
Employ em=new employ (id,name);
Ems.add (EM);

Ask if you want to continue typing
Askgoon ();

}
}

(2)

Package Empsystem;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import java.util.ArrayList;
Import Java.util.Scanner;

/**
* One input object scanner
* File
* Collection Object ArrayList
* @author Li Zhangyong
*
*/
public class Showemp {
Private Scanner sc=new Scanner (system.in);
Private file File=new file (filepath.path_name);
Private arraylist<employ> EMS;

Public Showemp () {
if (file.exists ()) {
try {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));
try {
Ems= (arraylist<employ>) ois.readobject ();
Ois.close ();
if (ems!=null) {
Show ();
}else{
SYSTEM.OUT.PRINTLN ("System internal problem, unable to operate");
Return
}

} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}else{
SYSTEM.OUT.PRINTLN ("Data file does not exist, cannot be viewed");
Return
}
}

public boolean checknum (String idstr) {
Detecting input formats
if (Idstr==null | | idstr.equals ("")) {
SYSTEM.OUT.PRINTLN ("Illegal input, repeat");
return false;
}
Char[] Cs=idstr.tochararray ();
for (int i=0;i<cs.length;i++) {
if (cs[i]< ' 0 ' | | cs[i]> ' 9 ') {
SYSTEM.OUT.PRINTLN ("Input illegal, repeat");
return false;
}
}
return true;
}
Private String idstr;
public int Getrightnum () {
Idstr=sc.nextline ();
if (!checknum (IDSTR)) {
Getrightnum ();
}
int Id=integer.parseint (IDSTR);
return ID;
}

public void Show () {
System.out.println ("View all employee input Y, view individual employee input n");
String Choice=sc.nextline ();
if ("Y". Equalsignorecase (choice)) {
SYSTEM.OUT.PRINTLN (EMS);
Return
}else if ("N". Equalsignorecase (choice)) {
System.out.println ("Please enter the ID of the member to be queried:");
int Id=getrightnum ();
if (Searchid.searchid (ID)!=null) {
SYSTEM.OUT.PRINTLN ("The employee information you are looking for is: \ n" +searchid.searchid (ID));
Return
}else{
System.out.println ("No such user");
Return
}
}else{
System.out.println ("No this command, please re-select!") ");
Show ();
}

}

}

(3)

Package Empsystem;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.util.ArrayList;
Import Java.util.Scanner;

/**
* One input object scanner
* File
* Collection Object ArrayList
* @author Li Zhangyong
*
*/
public class Modify {
Private Scanner sc=new Scanner (system.in);
Private file File=new file (filepath.path_name);
Private arraylist<employ> EMS;

Public Modify () {
if (file.exists ()) {
try {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));
try {
Ems= (arraylist<employ>) ois.readobject ();
Ois.close ();
if (ems!=null) {
Modify ();
}else{
SYSTEM.OUT.PRINTLN ("System internal problem, unable to operate");
Return
}

} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}else{
SYSTEM.OUT.PRINTLN ("Data file does not exist, cannot be viewed");
Return
}
}

public boolean checknum (String idstr) {
Detecting input formats
if (Idstr==null | | idstr.equals ("")) {
SYSTEM.OUT.PRINTLN ("Illegal input, repeat");
return false;
}
Char[] Cs=idstr.tochararray ();
for (int i=0;i<cs.length;i++) {
if (cs[i]< ' 0 ' | | cs[i]> ' 9 ') {
SYSTEM.OUT.PRINTLN ("Input illegal, repeat");
return false;
}
}
return true;
}
Private String idstr;
public int Getrightnum () {
Idstr=sc.nextline ();
if (!checknum (IDSTR)) {
Getrightnum ();
}
int Id=integer.parseint (IDSTR);
return ID;
}

public void SaveToFile () {
try {
ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (file));
Oos.writeobject (EMS);
Oos.close ();
Test Print View
SYSTEM.OUT.PRINTLN ("modified successfully");
SYSTEM.OUT.PRINTLN (EMS);
Return

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public void Modify () {
System.out.println ("Please enter the user ID to be modified:");
int Id=getrightnum ();
if (Searchid.searchid (ID)!=null) {
System.out.println ("The name of the user before modification is:" +searchid.searchid (ID). GetName ());
System.out.println ("Please enter the modified name:");
String Name=sc.nextline ();
for (int i=0;i<ems.size (); i++) {
if (Id==ems.get (i). GetId ()) {
Ems.get (i). SetName (name);
SaveToFile ();
}
}
}else{
System.out.println ("No such user");
Return
}
}

}

(4)

Package Empsystem;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import Java.util.Scanner;

/**
* One input object scanner
* File
* Collection Object ArrayList
* @author Li Zhangyong
*
*/
public class Delete {
Private Scanner sc=new Scanner (system.in);
Private file File=new file (filepath.path_name);
Private arraylist<employ> EMS;

Public Delete () {
if (file.exists ()) {
try {
ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));
try {
Ems= (arraylist<employ>) ois.readobject ();
Ois.close ();
if (ems!=null) {
Delete ();
}else{
SYSTEM.OUT.PRINTLN ("System internal problem, unable to operate");
Return
}

} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}else{
SYSTEM.OUT.PRINTLN ("Data file does not exist, cannot be viewed");
Return
}
}

public boolean checknum (String idstr) {
Detecting input formats
if (Idstr==null | | idstr.equals ("")) {
SYSTEM.OUT.PRINTLN ("Illegal input, repeat");
return false;
}
Char[] Cs=idstr.tochararray ();
for (int i=0;i<cs.length;i++) {
if (cs[i]< ' 0 ' | | cs[i]> ' 9 ') {
SYSTEM.OUT.PRINTLN ("Input illegal, repeat");
return false;
}
}
return true;
}
Private String idstr;
public int Getrightnum () {
Idstr=sc.nextline ();
if (!checknum (IDSTR)) {
Getrightnum ();
}
int Id=integer.parseint (IDSTR);
return ID;
}

public void SaveToFile () {
try {
ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (file));
Oos.writeobject (EMS);
Oos.close ();
System.out.println ("delete succeeded");
Test Print View
SYSTEM.OUT.PRINTLN (EMS);
Return

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public void Delete () {
System.out.println ("Please enter the employee ID to be deleted:");
int Id=getrightnum ();

if (Searchid.searchid (ID)!=null) {
System.out.println ("The name of the user before deletion is:" +searchid.searchid (ID). GetName ());
Iterator<employ> It=ems.iterator ();
while (It.hasnext ()) {
Employ Em=it.next ();
if (Id==em.getid ()) {
It.remove ();
SaveToFile ();
}
}
}else{
System.out.println ("No such user");
Return
}
}
}

The employee ingest system for Java projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.