本周学习了集合,静态,继承,多态,抽象类抽象方法以及接口;
并及完成了简单的学生管理系统的实现
点击查看代码
//创建学生类
public class Student {
private String name;
private int age;
private String id;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.name = name;
this.age = age;
this.id = id;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
点击查看代码
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
loop: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("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
switch (input) {
case "1"-> addStudent(students);
case "2"-> deleteStudent(students);
case "3"-> updateStudent(students);
case "4"-> queryStudent(students);
case "5"-> {
System.out.println("退出");
break loop;
}
default -> System.out.println("没有这个选项");
}
}
}
//添加学生信息
public static void addStudent(ArrayList<Student> students) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入学生Id");
id = sc.next();
boolean flag = contain(students, id);
if (flag) {
System.out.println("Id存在重新录入");
}
else {
student.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
student.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
student.setAge(age);
System.out.println("请输入学生家庭住址");
String address = sc.next();
student.setAddress(address);
students.add(student);
System.out.println("学生信息添加成功");
}
//删除学生信息
public static void deleteStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
String id = null;
System.out.println("请输入删除的Id");
id = sc.next();
int index = index(students, id);
if (index >= 0) {
students.remove(index);
System.out.println("Id删除成功");
}
else {
System.out.println("Id不存在,删除失败");
}
}
//修改学生信息
public static void updateStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
String id = null;
System.out.println("请输入要修改的Id");
id = sc.next();
int index = index(students, id);
if (index >= 0) {
Student student = students.get(index);
System.out.println("请输入要修改的学生姓名");
String name = sc.next();
System.out.println("请输入要修改的学生年龄");
int age = sc.nextInt();
System.out.println("请输入要修改的学生家庭住址");
String address = sc.next();
student.setName(name);
student.setAge(age);
student.setAddress(address);
System.out.println("学生信息修改成功");
}
else {
System.out.println("要修改的Id不存在");
}
}
//查询学生信息
public static void queryStudent(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("无学生数据,请添加后查看");
return;
}
System.out.println("id\t\t\t姓名\t\t年龄\t\t家庭住址");
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge()+ "\t" + student.getAddress());
}
}
//判断Id是否存在
public static boolean contain (ArrayList<Student> students, String id){
return index(students, id) >= 0;
}
//获得Id索引
public static int index(ArrayList<Student> students, String id) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
String studentId = student.getId();
if(Objects.equals(studentId, id)) {
return i;
}
}
return -1;
}
}
点击查看代码
public abstract class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
点击查看代码
public abstract class Sporter extends Person {
public Sporter() {
}
public Sporter(String name, int age) {
super(name, age);
}
public abstract void study();
}
点击查看代码
public interface English {
public abstract void speakEnglish();
}
点击查看代码
public class pingpongSporter extends Sporter implements English {
public pingpongSporter() {}
public pingpongSporter(String name, int age) {
super(name, age);
}
@Override
public void study(){
System.out.println("乒乓球运动员在练习乒乓球");
}
@Override
public void speakEnglish(){
System.out.println("乒乓球运动员在说英语");
}
}
点击查看代码
public class Test {
public static void main(String[] args) {
pingpongSporter pps = new pingpongSporter("刘诗雯",23);
System.out.println(pps.getName()+","+pps.getAge());
pps.study();
pps.speakEnglish();
}
}
下周学习内部类以及进行简单的项目实现,对近期所学进行复习掌握。
标签:String,System,name,第五,println,public,out From: https://www.cnblogs.com/sbc11/p/18340865