学生日志管理系统(ArrayList集合的应用)

前几天学了集合框架,现在先大致对集合总结一下,再对ArrayList的一个实例进行具体讲解。

所有集合的上层接口为collection接口,它有三个子接口,分别为List,Set,Map。

其中,List接口的实现类均是线性结构,其中存储的元素是有序的,主要有三个常用的,分别是ArrayList,Vector,LinkedList,其中,前两个是线性表,便于查询,后一个是线性链表,便于对元素进行增删改操作

Set集合中不允许重复的元素存在,集合中元素的存放是无序的,常用的实现类有HashSet,TreeSet

Map集合中的元素是以键值对<key,value>的形式存在的,一个键对应一个值,集合中不允许重复的键,常用的实现类有HashMap和TreeMap,其中,前者不允许存在空值和空键

List集合中的ArrayList:相当于一个动态数组,数组的容量随着添加对象的增加而不断动态扩展,当容量不够时,容量扩展至原来的1.5倍,其实现是线程不同步的。

为了进一步对ArrayList进行深入的理解,下面介绍一个小项目,一个简单的学生日志管理系统,其主要实现为

1、学生信息管理:实现学生的登录,注册功能

2、学生日志管理:实现对学生日志增删改查功能


要对学生对象和学生日志进行管理,则首先要建立一个学生对象和日志对象(为图简便,将日志对象简化为了字符串,实际学生和日志应为一对多的依赖关系):

public class Student {
int sno;
String password;
ArrayList<String> dinary=new ArrayList<String>();

public Student(){};

public Student(int sno, String password) {
super();
this.sno = sno;
this.password = password;
}

public Student(int sno, String password, ArrayList<String> dinary) {
super();
this.sno = sno;
this.password = password;
this.dinary = dinary;
} 
}


接下来,要实现学生对象的登录注册功能,登录时,首先判断是否已注册,已注册的学生信息和将要注册的学生信息均放入动态数组ArrayList<Student>中临时保存,若还未注册,则要先注册,才能进行登录。

即登录时,首先判断学生输入的账号是否存在,若存在,则再验证其密码是否正确,若不存在,则给出提示其账号不存在,可先注册再登陆。

若登录成功,则用一个索引来记录当前登录的学生对象在存储其信息的容器ArrayList中的索引,以便于对当前登录的学生对象的信息进行管理。登录成功后,才可对当前学生对象的日志进行管理。


public class StudentManage {

int index=-1;
ArrayList<Student> db=new ArrayList<Student>();

private Scanner s(){
return new Scanner(System.in);
}

public void loagn(){
System.out.println("请输入账号:");
Scanner sc=s();
int ID=sc.nextInt();
for (int i = 0; i < db.size(); i++) {
if(ID==db.get(i).sno){
index=i;
break;
}
}
if(index==-1){
System.out.println("此账号未注册!");
System.out.println("重新输入账号请按 1,注册账户请按 2:");
if(sc.nextInt()==1)
loagn();
else if(sc.nextInt()==2){
addStudent();
loadMenue();
}
else
System.exit(0);
}else
checkCode();
}

private void  checkCode(){
System.out.println("请输入密码:");
Scanner sc=s();
String code=sc.nextLine();
if(code.equals(db.get(index).password)){
System.out.println("登录成功!");
DinaryManage();
}else{
System.out.println("密码错误!");
checkCode();
}
}


public void addStudent(){
System.out.println("请按格式输入注册信息:学号\t密码");
Scanner sc=s();
int no=sc.nextInt();
String code=sc.nextLine();
Student e=new Student(no,code);
db.add(e);
}

public void loadMenue2(){
System.out.println("添加日志请按 1,删除日志请按 2:");
Scanner sc=s();
int se=sc.nextInt();
if(se==1){
System.out.println("请输入要写的日志内容:");
String line=sc.nextLine();
String content=line;
if(line!=null){
line=sc.nextLine();
content=content+line;
}
db.get(index).dinary.add(content);
loadMenue2();
}else if(se==2){
System.out.println("请输入要删除的日志的序号:");
se=sc.nextInt();
db.get(index).dinary.remove(se);
}else
System.exit(0);
}

private void DinaryManage(){
loadMenue2();
}


public static void main(String[] args) {
StudentManage SM=new StudentManage();
SM.loadMenue();
}

private void loadMenue() {
System.out.println("登录请按 1,注册请按 2:");
Scanner sc=s();
int select=sc.nextInt();
if(select==1)
loagn();
else if(select==2){
addStudent();
loadMenue();
}
else
System.exit(0);
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值