[Java]基础入门-Day 4
[Java]基础入门-Day4
学习使用工具
黑马2023新版Java视频教程 https://www.bilibili.com/video/BV1Fv4y1q7ZH?p=8&vd_source=03da0cdb826d78c565cd22a83928f4c2
Java程序员进阶之路 https://tobebetterjavaer.com/overview/java-can-do-what.html
零、前言
内容:实现一个简单的学生信息管理系统,具有增加学生、查询学生、删除学生、修改学生信息、输出当前学生信息等功能。
目的:熟悉Java基本操作与语法。
一、Student类
public class Student {
private String id;
private String name;
private int age;
private String date;
Student(String id, String name, int age, String date){
this.id = id;
this.name = name;
this.age = age;
this.date = date;
}
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 getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
二、StudentTable类
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
public class StudentTable {
private LinkedList<Student> list;
StudentTable(){
list = new LinkedList<Student>();
}
public boolean addStudent(String ID, String name, int age, String date){
for (Student stu: list) {
if (stu.getId().equals(ID)) {
return false; // 代表ID重复,添加失败
}
}
Student stu = new Student(ID, name, age, date);
list.add(stu);
return true; // 添加成功
}
public LinkedList<Student> getList() {
return list;
}
public boolean removeStudent(String ID){
Iterator<Student> stu = list.iterator();
while (stu.hasNext()) {
Student temp = stu.next();
if (ID.equals(temp.getId())) {
stu.remove();
return true;
}
}
return false;
}
public Student findStudent(String ID){
Iterator<Student> stu = list.iterator();
while (stu.hasNext()) {
Student temp = stu.next();
if (ID.equals(temp.getId())) {
return temp;
}
}
return null;
}
public boolean changeStudentName(String ID, String name){
Iterator<Student> stu = list.iterator();
while (stu.hasNext()) {
Student temp = stu.next();
if (ID.equals(temp.getId())) {
temp.setName(name);
return true;
}
}
return false;
}
public boolean changeStudentAge(String ID, int age){
Iterator<Student> stu = list.iterator();
while (stu.hasNext()) {
Student temp = stu.next();
if (ID.equals(temp.getId())) {
temp.setAge(age);
return true;
}
}
return false;
}
public boolean changeStudentDate(String ID, String date){
Iterator<Student> stu = list.iterator();
while (stu.hasNext()) {
Student temp = stu.next();
if (ID.equals(temp.getId())) {
temp.setDate(date);
return true;
}
}
return false;
}
}
三、Main类
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static StudentTable studentTable;
public static void addStudent(){
Scanner sc = new Scanner(System.in);
System.out.println("--------添加学生--------");
System.out.println("请输入学生学号:");
String ID = sc.nextLine();
System.out.println("请输入学生名称:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
System.out.println("请输入学生出生日期:");
String date = sc.next();
boolean flag = studentTable.addStudent(ID, name, age, date);
while(!flag){
System.out.println("学号已经存在,请重新输入学号:");
ID = sc.nextLine();
flag = studentTable.addStudent(ID, name, age, date);
}
System.out.println("学号为:"+ID+"的学生信息添加成功!");
}
public static void findStudent(){
Scanner sc = new Scanner(System.in);
System.out.println("--------查找学生--------");
System.out.println("请输入学生学号:");
String ID = sc.nextLine();
Student stu = studentTable.findStudent(ID);
if(stu == null){
System.out.println("未查找到对应学生。");
}else{
System.out.println("该学生的信息如下:");
System.out.println("学号:"+stu.getId()+",姓名:"+stu.getName()+",年龄:"+stu.getAge()+",生日:"+stu.getDate());
}
}
public static void removeStudent(){
Scanner sc = new Scanner(System.in);
System.out.println("--------删除学生--------");
System.out.println("请输入学生学号:");
String ID = sc.nextLine();
if(studentTable.removeStudent(ID)){
System.out.println("删除成功!");
}else{
System.out.println("未查找到对应学生,删除失败!");
}
}
public static int ChangeStudent(){
Scanner sc = new Scanner(System.in);
System.out.println("--------修改学生信息--------");
System.out.println("请输入学生学号:");
String ID = sc.nextLine();
System.out.println("请选择修改部分:");
System.out.println("1.姓名:");
System.out.println("2.年龄:");
System.out.println("3.生日:");
while(true){
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("请输入姓名:");
String name = sc.nextLine();
if(studentTable.changeStudentName(ID,name)){
System.out.println("修改成功!");
}else{
System.out.println("未查找到对应学生,修改失败!");
}
return 0;
case 2:
System.out.println("请输入年龄:");
int age = sc.nextInt();
if(studentTable.changeStudentAge(ID,age)){
System.out.println("修改成功!");
}else{
System.out.println("未查找到对应学生,修改失败!");
}
return 0;
case 3:
System.out.println("请输入出生日期:");
String date = sc.nextLine();
if(studentTable.changeStudentDate(ID,date)){
System.out.println("修改成功!");
}else{
System.out.println("未查找到对应学生,修改失败!");
}
return 0;
default:
System.out.println("输入错误,请重新输入。");
break;
}
}
}
public static void PrintStudent(){
System.out.println("--------当前学生信息--------");
System.out.printf("%5s%5s%10s","姓名","年龄","出生日期");
System.out.println();
LinkedList<Student> stu = studentTable.getList();
for(Student temp:stu){
System.out.printf("%5s%6d%16s",temp.getName(),temp.getAge(),temp.getDate());
System.out.println();
}
}
public static void main(String[] args) {
studentTable = new StudentTable();
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用学生管理系统!请选择您的操作:");
while(true){
System.out.println("1.添加学生");
System.out.println("2.查询学生");
System.out.println("3.删除学生");
System.out.println("4.修改学生信息");
System.out.println("5.输出当前所有学生信息");
System.out.println("6.退出程序");
int choice = sc.nextInt();
switch(choice) {
case 1:
addStudent();
break;
case 2:
findStudent();
break;
case 3:
removeStudent();
break;
case 4:
ChangeStudent();
break;
case 5:
PrintStudent();
break;
case 6:
System.out.println("谢谢您的使用,再见!");
System.exit(0);
default:
System.out.println("输入错误,请重新输入。");
break;
}
}
}
}