资源描述
/**
* AWT Sample application
*
* @author
* @version 1.00 12/05/06
*/
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Unit
{
public static void main(String[] args)
{
Vector stuList=new Vector();
String stu="",line="",input="",name="";
boolean flag=true;
int value,age;
line="请选择你要的操作\n1.增加学生 2.修改学生信息\n3.删除学生 4.浏览所有学生信息\n5.统计学生人数 6.退出系统";
while(flag)
{
JOptionPane.showMessageDialog(null,line,"结果",JOptionPane.PLAIN_MESSAGE);
input=JOptionPane.showInputDialog("请输入数字,选择你要的操作");
value=Integer.parseInt(input);
if(value==1)
{
name=JOptionPane.showInputDialog("请输入要添加的学生的名字:");
age=Integer.parseInt(JOptionPane.showInputDialog("请输入要添加学生的年龄:"));
student Student=new student(name,age);
stuList.add(Student);
/* Enumeration items=stuList.elements();
Object o= items.nextElement();
Student sample=(Student)o;
JOptionPane.showMessageDialog(null,sample.getName(),"结果",JOptionPane.PLAIN_MESSAGE);
*/
}
else if(value==2)
{
name=JOptionPane.showInputDialog("请输入你要修改的学生的名字");
input=JOptionPane.showInputDialog("请输入你要修改后的学生的年龄");
age=Integer.parseInt(input);
Enumeration items=stuList.elements();
while(items.hasMoreElements())
{
// String namestr=S;
student Stu=(student)items.nextElement();
if(Stu.getName().equals(name))
{
Stu.Update(age);
break;
}
}
JOptionPane.showMessageDialog(null,"修改成功!","结果",JOptionPane.PLAIN_MESSAGE);
}
else if(value==3)
{
name=JOptionPane.showInputDialog("请输入你要删除的学生的名字");
Enumeration items=stuList.elements();
while(items.hasMoreElements())
{
// String namestr=S;
student Stu=(student)items.nextElement();
if(Stu.getName().equals(name))
{
stuList.remove(Stu);
break;
}
}
JOptionPane.showMessageDialog(null,"删除成功!","结果",JOptionPane.PLAIN_MESSAGE);
}
else if(value==4)
{
printVector(stuList);
}
else if(value==5)
{
JOptionPane.showMessageDialog(null,"学生人数是"+stuList.size(),"结果",JOptionPane.PLAIN_MESSAGE);
}
else if(value==6)
{
flag=false;
}
}
}
static public void printVector(Vector output)
{
String line="";
if(output.isEmpty())
JOptionPane.showMessageDialog(null,"The vector is empty","结果",JOptionPane.PLAIN_MESSAGE);
else
{
Enumeration items=output.elements();
while(items.hasMoreElements())
{
// String namestr=S;
student Stu=(student)items.nextElement();
line+=Stu.show();
}
JOptionPane.showMessageDialog(null,line,"学生信息如下",JOptionPane.PLAIN_MESSAGE);
}
}
}
public class student
{
String name;
int age;
static int count=0;
public student(String str,int m)
{
name=str;
age=m;
count++;
}
public void Update(int m)
{
age=m;
}
public String getName()
{
return name;
}
public String show()
{
return "名字:"+name+" 年龄:"+age+"\n";
}
}
展开阅读全文