java控制臺實現(xiàn)學生信息管理系統(tǒng)
簡介
最近學了java基礎后對以前不會寫的作業(yè)深有感觸,想起以前各種在網(wǎng)上找資料找別人的代碼參考,所以今天特地寫了了簡單的基于控制臺的學生信息管理系統(tǒng)供還在學基礎的同學參考,寫的有問題的地方也希望能指出來,好了,下面開始進入正題。
環(huán)境
我使用的是idea編譯器和1.8的jdk版本,有使用1.7以下jdk版本的同學運行會報錯,解決方法是把ArrayLis<Student> arrayList=new Array<>();改成 ArrayLis<Student> arrayList=new Array<Student>(),下面開始講解學生信息管理系統(tǒng)的實現(xiàn)過程。
系統(tǒng)分析
學生信息管理系統(tǒng)主要是實現(xiàn)讓老師登入賬號(賬號和密碼我設置的都是“123”,你們也可以改成其他的字符串)后可以對學生進行增刪改查的操作和查看所有學生的信息,所以我各自定義了一個方法來實現(xiàn)它的功能。
代碼實現(xiàn)
首先定義一個學生類Student.java,關于toString是重寫了父類也就是Object的方法,是用于方便查看結果的,學生的屬性可以按需求添加多個,這里我為了方便只寫了四個。
public class Student {
? ? //學號
? ? private int ?sno;
? ? //姓名
? ? private String ?name;
? ? //性別
? ? private String xes;
? ? //年齡
? ? private int age;
//創(chuàng)建get/set方法
? ? public int getSno() {
? ? ? ? return sno;
? ? }
?
? ? public void setSno(int sno) {
? ? ? ? this.sno = sno;
? ? }
?
? ? public String getName() {
? ? ? ? return name;
? ? }
?
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
?
? ? public String getXes() {
? ? ? ? return xes;
? ? }
?
? ? public void setXes(String xes) {
? ? ? ? this.xes = xes;
? ? }
?
? ? public int getAge() {
? ? ? ? return age;
? ? }
?
? ? public void setAge(int age) {
? ? ? ? this.age = age;
? ? }
//重寫toString方法方便打印
? ? @Override
? ? public String toString() {
? ? ? ? return "Student{" +
? ? ? ? ? ? ? ? "sno=" + sno +
? ? ? ? ? ? ? ? ", name='" + name + '\'' +
? ? ? ? ? ? ? ? ", xes='" + xes + '\'' +
? ? ? ? ? ? ? ? ", age=" + age +
? ? ? ? ? ? ? ? '}';
? ? }
}然后定義一個結構類Structure.java,把需要實現(xiàn)的功能方法寫出來。
import java.util.ArrayList;
import java.util.Scanner;
?
/**
?* 老師進行登錄操作后(老師的賬號密碼固定為123)
?* 才能對學生進行增刪改查
?*/
public class Structure {
? ? //用來給學生學號賦值自增
? ? static int i=1;
? ? //創(chuàng)建ArrayList集合對象用來存放學生對象
? ? ArrayList<Student> arrayList=new ArrayList<>();
? ? Scanner scanner=new Scanner(System.in);
? ? public void enter(){
? ? ? ? System.out.print("請輸入用戶名:");
? ? ? ? if (scanner.next().equals("123")){
? ? ? ? ? ? System.out.print("請輸入密碼:");
? ? ? ? ? ? if (scanner.next().equals("123")){
? ? ? ? ? ? ? ? System.out.println("登錄成功!");
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? System.out.println("密碼錯誤!請重新登入");
? ? ? ? ? ? ? ? enter();
? ? ? ? ? ? }
? ? ? ? }else {
? ? ? ? ? ? System.out.println("用戶名錯誤!請重新登入");
? ? ? ? ? ? enter();
? ? ? ? }
? ? }
? ? //增加學生信息
? ? public void add(){
? ? ? ? //創(chuàng)建學生對象
? ? ? ? Student student=new Student();
? ? ? ? //System.out.print("學生ID自增");
? ? ? ? student.setSno(i);
? ? ? ? System.out.print("學生姓名:");
? ? ? ? student.setName(scanner.next());
? ? ? ? System.out.print("性別:");
? ? ? ? student.setXes(scanner.next());
? ? ? ? System.out.print("年齡:");
? ? ? ? student.setAge(scanner.nextInt());
? ? ? ? //把學生對象存入集合中
? ? ? ? arrayList.add(student);
? ? ? ? i++;
? ? }
? ? //查看所有學生信息
? ? public void print(){
? ? ? ? if (arrayList.size()==0){
? ? ? ? ? ? System.out.println("沒有學生!??!");
? ? ? ? }else {
? ? ? ? ? ? //使用增強for循環(huán)遍歷集合
? ? ? ? ? ? for (Student student:arrayList){
? ? ? ? ? ? ? ? System.out.println(student.toString());
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //通過學號查找指定的學生信息
? ? public void inquiry(){
? ? ? ? if (arrayList.size()==0){
? ? ? ? ? ? System.out.println("沒有學生?。?!");
? ? ? ? }else {
? ? ? ? ? ? System.out.print("請輸入想要查找的學生的學號:");
? ? ? ? ? ? for (Student student:arrayList){
? ? ? ? ? ? ? ? if (student.getSno()==scanner.nextInt()){
? ? ? ? ? ? ? ? ? ? System.out.println(student.toString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //刪除指定學生的信息
? ? public void delete(){
? ? ? ? if (arrayList.size()==0){
? ? ? ? ? ? System.out.println("沒有學生?。?!");
? ? ? ? }else {
? ? ? ? ? ? System.out.print("請輸入想要刪除的學生的學號:");
? ? ? ? ? ? for (int i=0;i<=arrayList.size();i++){
? ? ? ? ? ? ? ? if (arrayList.get(i).getSno()==scanner.nextInt()){
? ? ? ? ? ? ? ? ? ? arrayList.remove(i);
? ? ? ? ? ? ? ? ? ? System.out.println("刪除成功!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //修改指定學生的信息
? ? public void revisesp(){
? ? ? ? if (arrayList.size()==0){
? ? ? ? ? ? System.out.println("沒有學生?。。?);
? ? ? ? }else {
? ? ? ? ? ? System.out.print("請輸入想要修改的學生的學號:");
? ? ? ? ? ? for (Student student:arrayList){
? ? ? ? ? ? ? ? if (student.getSno()==scanner.nextInt()){
? ? ? ? ? ? ? ? ? ? revisesp01(student);
? ? ? ? ? ? ? ? ? ? System.out.println("修改成功!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public void revisesp01(Student student) {
? ? ? ? System.out.println("請根據(jù)序號選擇指令");
? ? ? ? System.out.println("1.修改姓名");
? ? ? ? System.out.println("2.修改性別");
? ? ? ? System.out.println("2.修改年齡");
? ? ? ? switch (scanner.nextInt()){
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? student.setName(scanner.next());
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? student.setXes(scanner.next());
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? student.setAge(scanner.nextInt());
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}最后創(chuàng)建一個測試類Test.java用來測試功能的實現(xiàn)
import java.util.Scanner;
?
public class Test {
? ? Structure structure=new Structure();
? ? static Scanner scanner=new Scanner(System.in);
? ? public static void main(String[] args) {
? ? ? ? Test test=new Test();
? ? ? ? test.a();
? ? }
? ? public void a(){
? ? ? ? System.out.println("老師登錄(賬號密碼固定123/123)");
? ? ? ? structure.enter();
? ? ? ? while (true){
? ? ? ? ? ? print();
? ? ? ? ? ? switch (scanner.nextInt()){
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? structure.add();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? structure.print();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? structure.inquiry();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? structure.delete();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? structure.revisesp();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? System.out.println("請輸入正確的指令!");
? ? ? ? ? ? }
?
? ? ? ? }
? ? }
?
? ? public void print(){
? ? ? ? System.out.println("根據(jù)指令選擇操作:");
? ? ? ? System.out.println("1. 添加學生信息");
? ? ? ? System.out.println("2. 學生信息列表");
? ? ? ? System.out.println("3. 查詢指定學號的學生信息");
? ? ? ? System.out.println("4. 根據(jù)學號刪除指定學生信息");
? ? ? ? System.out.println("5. 請輸入想要修改的學生的學號");
? ? ? ? System.out.println("0.退出");
? ? }
}最后我們就順利的完成了一個簡單的學生管理系統(tǒng),能獨立完成這個對我們鞏固基礎知識是能起到一定的幫助的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解java WebSocket的實現(xiàn)以及Spring WebSocket
這篇文章主要介紹了詳解java WebSocket的實現(xiàn)以及Spring WebSocket ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01

