用面向对象知识建一个学生管理系统

//学生类

package cn.itsource_01review;

public class Studaent {
	private Integer id;
	private String name;
	private int age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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 Studaent(Integer id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Studaent [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
		
}


//学生的增删改查
package cn.itsource_01review;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StudentDoa {
	 private static List<Studaent> stu = new ArrayList();
	 
	 //查询所有
	 public List<Studaent>query(){
		return stu;
		 
	 }
	 //id 查询一个
	 public Studaent queryOne(Integer id){
		 //获得一个迭代器
		 Iterator<Studaent> iterator = stu.iterator();
		 while (iterator.hasNext()) {
			Studaent next = iterator.next();
			if (next.getId().equals(id)) {
				return next;
			}
		}
		return null;
		 
	 }
	 //添加
	 public void add(Studaent s){
		 if (stu.size()<=0) {
			stu.add(s);
		} else {
			for (int i = 0; i < stu.size(); i++) {
				Studaent studaent = stu.get(i);
				if (studaent.getId().equals(s.getId())) {
					return;
				}
			}
			stu.add(s);
		}
	 }
	 //删除
	 public boolean dele(int id){
		 //调用查询方法
		 Studaent student = queryOne(id);
		return stu.remove(student);
		 
	 }
	 //修改
	 public void update(Studaent s){
		 Studaent Student = queryOne(s.getId());
		 int indexOf = stu.indexOf(Student);
		 if (indexOf>=0) {
			stu.set(indexOf, s);
		}
	 }
}

//测试类
package cn.itsource_01review;

import java.util.List;

public class Test {

	public static void main(String[] args) {
		StudentDoa ddd = new StudentDoa();
		ddd.add(new Studaent(1, "小A", 12));
		ddd.add(new Studaent(2, "小B", 12));
		ddd.add(new Studaent(3, "小C", 12));
		ddd.add(new Studaent(5, "小D", 12));
		ddd.add(new Studaent(6, "小F", 12));
		ddd.add(new Studaent(6, "小E", 12));
		ddd.add(new Studaent(7, "小G", 12));
		ddd.add(new Studaent(9, "小K", 12));
		
		ddd.update(new Studaent(1, "小B", 12));
		
		System.out.println(ddd.dele(2));
		
		List<Studaent> query = ddd.query();
		System.out.println(query);
	}
}

 

转载于:https://my.oschina.net/u/4083635/blog/3027082

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值