public static void findInfo(String filePath) throws IOException {
//把之前存入到数据的文件,读取到集合中来。
ArrayList<Student> list = new ArrayList<Student>()
readData(list,filePath)
//遍历集合
for(int i=0i<list.size()i++) {
Student stu = list.get(i)
System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getAddress())
}
}
private static void readData(ArrayList<Student> list ,String filePath) throws NumberFormatException, IOException{
FileReader fr = new FileReader(filePath)
BufferedReader br = new BufferedReader(fr)
//读物文件里面的信息
String line = null
while((line=br.readLine())!=null) {
String[] str = line.split(",")
//获取的数据封装成对象
//stu.getId()+","+stu.getName()+","+stu.getAge()
Student stu = new Student()
stu.setId(str[0])
stu.setName(str[1])
stu.setAge(Integer.valueOf(str[2]))
//将对象放到集合中区
list.add(stu)
}
}
//输入学生的信息
public static void addInfo(String filePath) throws IOException{
ArrayList<Student>list = new ArrayList<Student>()
Scanner sc = new Scanner(System.in)
//将输入的信息存放到集合里面去
for(int i=1i<=3i++) {
System.out.println("请输入第"+i+"个学生的id")
String id = sc.next()
System.out.println("请输入第"+i+"个学生的name")
String name = sc.next()
System.out.println("请输入第"+i+"个学生的age")
int age = sc.nextInt()
Student stu = new Student()
stu.setId(id)
stu.setAge(age)
stu.setName(name)
list.add(stu)
}
//将集合里面的信息写到文件里面去
writeDate(list,filePath)
}
Student 类public class Student
{
private int id
private int age
private int score
private String name
public Student()
{
}
public Student(int id, int age, int score, String name)
{
this.id = id
this.age = age
this.score = score
this.name = name
}
public int getId()
{
return id
}
public void setId(int id)
{
this.id = id
}
public int getAge()
{
return age
}
public void setAge(int age)
{
this.age = age
}
public int getScore()
{
return score
}
public void setScore(int score)
{
this.score = score
}
public String getName()
{
return name
}
public void setName(String name)
{
this.name = name
}
@Override
public String toString()
{
return "学号:" + id + " 姓名:" + name + " 年龄:" + age + " 成绩:" + score
}
}
Manager类
import java.util.ArrayList
import java.util.Iterator
import java.util.List
public class Manager
{
private List<Student>list
public Manager(List<Student>list)
{
this.list = list
}
public List<Student>getList()
{
return list
}
public void setList(List<Student>list)
{
this.list = list
}
//添加学生
public void add(Student s)
{
list.add(s)
}
//根据学生学号返回学生年龄
public int search(int id)
{
for(Iterator<Student>iter = list.iterator()iter.hasNext())
{
Student s = iter.next()
if(s.getId() == id)
{
return s.getAge()
}
}
return -1
}
//删除学生
public void remove(int id)
{
for(Iterator<Student>iter = list.iterator()iter.hasNext())
{
Student s = iter.next()
if(s.getId() == id)
{
list.remove(s)
}
}
}
//计算总成绩
public int allScore()
{
int score = 0
int temp = 0
for(Iterator<Student>iter = list.iterator()iter.hasNext())
{
Student s = iter.next()
temp = s.getScore()
score += temp
}
return score
}
//修改成绩
public void update(int id)
{
for(Iterator<Student>iter = list.iterator()iter.hasNext())
{
Student s = iter.next()
if(s.getId() == id)
{
s.setScore(s.getScore() + 10)
}
}
}
}
测试类 Client
import java.util.ArrayList
import java.util.List
public class Client
{
public static void main(String[] args)
{
List<Student>list = new ArrayList<Student>()
Manager manager = new Manager(list)//创建一个管理者
Student s1 = new Student()//无参构造方法创建的学生实例
//通过方法设置s1的属性
s1.setId(201105)
s1.setAge(20)
s1.setScore(100)
s1.setName("zhangsan")
Student s2 = new Student(201101,21,98,"lisi")//通过带参数的构造方法创建实例
Student s3 = new Student(201108,25,95,"zhaoliu")
Student s4 = new Student(201110,23,80,"xiaoming")
Student s5 = new Student(201106,28,78,"hello")
//放到集合当中
manager.getList().add(s1)//添加学生
manager.getList().add(s2)
manager.getList().add(s3)
manager.getList().add(s4)
manager.getList().add(s5)
System.out.println(list)
System.out.println(manager.allScore())
System.out.println(manager.search(201110))//根据学生学号查询学生年龄
manager.remove(201110)//删除学生
manager.update(201101)//修改成绩
}
}
可以完成你上述的基本要求,如果改动可以自行修改 很简单。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)