Java对象数组学生管理系统

  • Post author:
  • Post category:java




Java对象数组练习


Student类



package Student.com;

/**
 * 学生类
 * @author Strive
 */
public class Student {
	private int id;
	private String name;
	private double score;
	
	public Student() {}
	public Student(int id, String name, double score) {
		this.id = id;
		this.name = name;
		this.score = score;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
}

StudentManager






package Student.com;

public class StudentManager {
	private int index = 1;
	private int count = 0;
	private Student[] student;
	//ID
	public int getStudentId() {
		return index++;
	}
	public StudentManager() {
		student = new Student[100];
		String[] name = {"张三","李四","王五","张明","比尔盖茨","礼记","二狗"};
		double[] score = {70,68,58,98,86,74,94};
		for (int i = 0; i < student.length; i++) {
			if (student[i] == null) {
				if (i == 7) {
					break;
				}
				student[i] = new Student(getStudentId(),name[i],score[i]);
				count++;
			}
		}
	}
	/**
	 * 查询所有学生
	 * @return 学生数组
	 */
	public Student[] list() {
		Student[] stu = new Student[count];
		for (int i = 0,len = student.length; i < len; i++) {
			if (student[i] != null) {
				stu[i] = student[i];
			}
		}
		return stu;
	}
	/**
	 * 添加学生
	 * @param stu 学生对象
	 * @return 是否成功
	 */
	public boolean addStudent(Student stu) {
		boolean flag = false;
		for (int i = 0,len = student.length; i < len; i++) {
			if (student[i] == null) {
				stu.setId(getStudentId());
				stu.setName(stu.getName());
				stu.setScore(stu.getScore());
				student[i] = stu;
				count++;
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	public boolean removeStudent(int id) {
		int index = 0;
		boolean flag = false;
		for (int i = 0,len = student.length -  1; i < len; i++) {
			if (student[i] == null) {
				break;
			}
			if (student[i].getId() == id) {
				count--;
				index = i;
				flag = true;
				break;
			} 
		}
		for (int i = index; i < student.length - 1; i++) {
			student[i] = student[i + 1];
		}
		student[student.length - 1] = null;
		return flag;
		
	}

	public boolean updateStudent(Student s) {
		boolean flag = false;
		for (int i = 0,len = student.length; i < len; i++) {
			if (student[i] == null) {
				break;
			}
			if (student[i].getId() == s.getId()) {
				student[i].setName(s.getName());
				student[i].setScore(s.getScore());
				flag = true;
				break;
			}
		}
		return flag;
	}
}

Test


package Student.com;

import java.util.Scanner;

/**
 * 测试类
 * @author Strive
 */
public class Test {
	private Scanner input = new Scanner(System.in);
	private StudentManager mana = new StudentManager();
	public static void main(String[] args) {
		new Test().Menu();
	}
	public void Menu() {
		do {
			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("请输入操作符:");
			String key = input.next();
			switch (key) {
				case "1":
					all();
					break;
				case "2":
					delete();
					break;
				case "3":
					update();
					break;
				case "4":
					search();
					break;
				case "5":
					add();
					break;
				case "6":
					sort();
					break;
				default:
					System.out.println("输出错误!");
					continue;
			}
			break;
		} while(true);
	}
	public void all() {
		Student[] list = mana.list();
		System.out.println("id\t姓名\t成绩");
		if (list.length != 0) {
			for (Student li : list) {
				System.out.println(li.getId() + "\t" + li.getName() + "\t" + li.getScore());
			}
		} else {
			System.out.println("没有更多数据!");
		}
		next();
	}
	public void add() {
		do {
			Student stu = new Student();
			System.out.println("请输入姓名:");
			String name = input.next();
			if (existsStudentName(name)) {
				System.out.println("学生姓名已存在!");
				continue;
			}
			double score = 0;
			System.out.println("请输入成绩:");
			try {
				score =input.nextDouble();
			}catch(Exception e) {
				System.out.println("只能输入数字!");
				continue;
			}
			stu.setName(name);
			stu.setScore(score);
			System.out.println(mana.addStudent(stu) == true ? "添加学生\""+name+"\"成功!" : "添加学生\""+name+"\"失败!");
			next();
			break;
		} while(true);
	}
	public boolean existsStudentName(String name) {
		Student[] list = mana.list();
		for (int i = 0,len = list.length; i < len; i++) {
			if (list[i].getName().equals(name)) {
				return true;
			}
		}
		return false;
	}
	public void update() {
		System.out.println("请输入学生id:");
		int id = input.nextInt();
		System.out.println("请输入学生姓名:");
		String name = input.next();
		System.out.println("请输入学生成绩:");
		double score = input.nextDouble();
		Student stu = new Student(id,name,score);		
		System.out.println(mana.updateStudent(stu) == true ? "更新成功!" : "更新失败!");
		next();
	}
	public void search() {
		System.out.println("请输入学生姓名:");
		String name = input.next();
		Student[] list = mana.list();
		for (int i = 0; i < list.length; i++) {
			if (list[i].getName().equals(name)) {
				System.out.println(list[i].getId() + "\t" + list[i].getName() + "\t" + list[i].getScore());
			}
		}
		next();
	}
	public void delete() {
		System.out.println("请输入学生id:");
		int id = input.nextInt();
		boolean flag = mana.removeStudent(id);
		System.out.println(flag == true ? "删除成功!" : "删除失败!");
		next();
	}
	public void sort() {
		Student[] list = mana.list();
		Student student;
		for (int i = 0; i < list.length; i++) {
			for (int j = 0; j < i; j++) {
				if (list[i].getScore() > list[j].getScore()) {
					student = list[i];
					list[i] = list[j];
					list[j] = student;
				}
			}
		}
		for (Student li : list) {
			System.out.println(li.getId() + "\t" + li.getName() + "\t" + li.getScore());
		}
		next();
	}
	public void next() {
		do {
			System.out.println("按e返回:");
			String name = input.next();
			if (name.equals("e")) {
				Menu();
			} else {
				continue;
			}
		} while(true);
	}
}











版权声明:本文为qq_34095828原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。