[java]
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
/*
* 作者:鐘志鋼
* 功能:集合框架之ArrayList
* 時間:2013-1-26
* 1,List結構:ArrayList,LinkedList,Vector,Stack;
* 2, Map結構:HashMap, Hashtable;
* 3, set結構:HastSet,TreeSet;
* 4, Queue結構:Queue接口
*/
public class 集合框架 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//ArrayList
ArrayList<Clerk> al = new ArrayList<Clerk>();
Clerk c1 = new Clerk("宋江", 50, 1000);
al.add(c1);//添加到尾部
Clerk c3 = new Clerk("吳用", 45, 2000);
al.add(c3);//添加到尾部
al.add(c3);//可以加相同的對象
System.out.println("al.lenght=" + al.size());
Clerk c2 = new Clerk("武松", 30, 10004.4f);
al.add(0, c2);//加到指定位置,原來的會往看推
al.remove(3);//刪除相應的對象
//System.out.println(al.get(1).getName());
for(Clerk c : al){
//System.out.println(c.getName());
}
//用一個類來專門管理員工的信息
EmpManage emp = new EmpManage();
//讀取輸入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("請按提示操作");
System.out.println("1:添加一個員工");
System.out.println("2:查找一個員工");
System.out.println("3:修改一個員工工資");
System.out.println("4:刪除一個員工");
System.out.println("6: 查找所有員工");
System.out.println("5:退出程序");
String op = br.readLine();
if(op.equals("1")){
System.out.println("請輸入名字:");
String name = br.readLine();
System.out.println("請輸入年齡:");
int age = Integer.valueOf(br.readLine());
System.out.println("請輸入薪水:");
float sal = Float.parseFloat(br.readLine());
Clerk c = new Clerk(name, age, sal);
emp.addEmp(c);
//System.out.println("添加成功");
}else if(op.equals("2")){
System.out.println("請輸入員工姓名");
String qname = br.readLine();
emp.info(qname);
}else if(op.equals("3")){
System.out.println("請輸入要修改員工的姓名:");
String uname = br.readLine();
System.out.println("請輸入要修改的工資:");
float newsal = Float.parseFloat(br.readLine());
emp.updateSal(uname, newsal);
//System.out.println("修改成功");
}else if(op.equals("4")){
System.out.println("請輸入要刪除的員工名字:");
String dname = br.readLine();
emp.delEmp(dname);
//System.out.println("刪除成功");
}else if(op.equals("5")){
System.exit(0);
}else if(op.equals("6")){
emp.queal();
}
}
//LinkedList.可序列的鏈表
//LinkedList ll = new LinkedList();
}
}
class EmpManage{//員工管理類
private ArrayList<Clerk> al = null;
public EmpManage(){
al = new ArrayList<Clerk>();
}
public void queal() {
for(Clerk c : al){
System.out.println("員工名:" + c.getName()+",薪水是:"
+ c.getSal()+ ",年齡是" + c.getAge());
}
}
//加入員工
public void addEmp (Clerk c ){
al.add(c);
}
//刪除員工
public void delEmp (String name){
boolean b = false;
for(Clerk c : al){
if(c.getName().equals(name)){//String 類型數據用==比較的是地址
al.remove(c);
b = true;
break;
}
}
if(!b){
System.out.println("查無此人");
}else{
System.out.println("刪除成功");
}
}
//顯示員工的相關信息
public void info(String name){
boolean b = false;
for(int i = 0; i < al.size(); i ++){
Clerk c = al.get(i);
if(c.getName().equals(name)){
b = true;
System.out.println("你查找的員工:" + c.getName()+",薪水是:"
+ c.getSal()+ ",年齡是" + c.getAge());
break;
}
}
if(!b){
System.out.println("查無此人");
}else{
System.out.println("查詢成功");
}
}
//修改員工工資
public void updateSal(String name , float sal){
boolean b = false;
for(Clerk c : al){
if(c.getName().equals(name)){
c.setSal(sal);
b = true;
}
}
if(!b){
System.out.println("查無此人");
}else{
System.out.println("修改成功");
}
}
}
class Clerk {//員工類
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
private int age;
private float sal;
public Clerk (String name, int age, float sal){
this.age = age;
this.name = name;
this.sal = sal;
}
}