赞
踩
借鉴黑马程序员java经典入门
1.创建学生类:
- package project1StudentManager;
-
- public class student {
- private String id;
- private String name;
- private String age;
- private String address;
- //ALT+INS
- public student() {
- }
-
- public student(String id, String name, String age, String address) {
- this.id = id;
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAge() {
- return age;
- }
-
- public void setAge(String age) {
- this.age = age;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
- }

2.创建学生测试类:
- package project1StudentManager;
-
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class studentmanager {
- public static void main(String[] args) {
-
- ArrayList<student> array = new ArrayList<student>();
-
- while (true) {
- System.out.println("--------欢迎使用学生管理系统--------");
- System.out.println("1.添加学生信息");
- System.out.println("2.删除学生信息");
- System.out.println("3.修改学生信息");
- System.out.println("4.查看某个学生信息");
- System.out.println("5.查看所有学生信息");
- System.out.println("6.退出");
-
- System.out.println("请输入你要选择的功能编号:");
- Scanner sc = new Scanner(System.in);
- int i = sc.nextInt();
- if (i == 1){
- addStudent(array);
- }else if (i == 2){
- deleStudent(array);
- }else if (i == 3){
- modifyStudent(array);
- }else if (i == 4){
- seeStudent(array);
- }else if (i == 5){
- findallStudent(array);
- }
- else if (i == 6){
- System.out.println("谢谢使用");
- break;
- }else {
- System.out.println("你输入的编号有误,请重新输入");
- }
-
- }
- }
-
- //添加学生信息
- public static void addStudent(ArrayList<student> array){
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入学生学号:");
- String id = sc.nextLine();
- int index = -1;
- for (int i = 0;i < array.size();i++){
- student s = array.get(i);
- if(s.getId().equals(id));
- index = 1;
- System.out.println("学号重复");
- break;
- }
- if (index ==-1){
- System.out.println("请输入学生姓名:");
- String name = sc.nextLine();
- System.out.println("请输入学生年龄:");
- String age = sc.nextLine();
- System.out.println("请输入学生地址:");
- String address = sc.nextLine();
- student s = new student();
- s.setId(id);
- s.setName(name);
- s.setAge(age);
- s.setAddress(address);
- array.add(s);
- System.out.println("添加学生信息成功");
- }
- }
- //通过学生学号删除学生信息
- public static void deleStudent(ArrayList<student> array){
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入学生学号:");
- String id = sc.nextLine();
- int index = -1;
- for (int i = 0;i < array.size();i++){
- student s = array.get(i); //遍历列表,得到所有学生信息
- if(s.getId().equals(id)){
- index = 1;
- array.remove(i);
- System.out.println("删除学生信息成功");
- }
- }
- if (index == -1){
- System.out.println("你输入的学号有误");
- }
-
-
- }
- //通过学生学号修改学生信息-------
- public static void modifyStudent(ArrayList<student> array){
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你要修改的学生学号:");
- String id = sc.nextLine();
-
-
- int index = -1;
- for (int i = 0;i < array.size();i++){
- student s = array.get(i);
- if(s.getId().equals(id)){
- index = 1;
- System.out.println("请输入新的学生姓名:");
- String name = sc.nextLine();
- System.out.println("请输入新的学生年龄:");
- String age = sc.nextLine();
- System.out.println("请输入新的学生地址:");
- String address = sc.nextLine();
- //创建学生对象
- student s1 = new student();
- s1.setId(id);
- s1.setName(name);
- s1.setAge(age);
- s1.setAddress(address);
- array.set(i,s1); //修改array数组第i个位置的数据
- System.out.println("修改学生信息成功");
- break;
- }
- }
- if(index == -1) {
- System.out.println("你输入的学号有误");
- }
-
- }
- //通过学号查看学生信息
- public static void seeStudent(ArrayList<student> array){
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你要查看的学生学号");
- String id = sc.nextLine();
- int index = -1; //判断输入的学号是否有误
- for (int i = 0;i < array.size();i++){
- student s = array.get(i);
- if(s.getId().equals(id)){
- index = 1;
- System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t地址");
- System.out.println(s.getId()+"\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
- break;
- }
- }
- if (index ==-1){
- System.out.println("你输入的学号有误");
- }
-
- }
- //查看所有学生信息
- public static void findallStudent(ArrayList<student> array){
- if(array.size() == 0){
- System.out.println("无学生信息,请先添加再查看");
- }else {
- System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t地址");
- for (int i = 0;i < array.size();i++){
- student s = array.get(i);
- System.out.println(s.getId()+"\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
- }
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。