package step1;
import java.util.Scanner;
class Person {
String name;
String sex;
int age;
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
}
class Student extends Person {
String no;
String enter;
String major;
public Student(String name, String sex, int age, String no, String enter, String major) {
super(name, sex, age);
this.no = no;
this.enter = enter;
this.major = major;
}
@Override
public String toString() {
return name + "," + sex + "," + age + "," + no + "," + enter + "," + major;
}
}
class Teacher extends Person {
String pro;
String department;
public Teacher(String name, String sex, int age, String pro, String department) {
super(name, sex, age);
this.pro = pro;
this.department = department;
}
@Override
public String toString() {
return name + "," + sex + "," + age + "," + pro + "," + department;
}
}
public class Lab3_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student student = new Student(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.next());
Teacher teacher = new Teacher(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next());
System.out.println("学生基本信息为:" + student);
System.out.println("教师的信息为:" + teacher);
sc.close();
}
}
package step2;
abstract class Employee {
public abstract double earnings();
}
class YearWorker extends Employee {
@Override
public double earnings() {
return 100000;
}
}
class MonthWorker extends Employee {
@Override
public double earnings() {
return 10000*12;
}
}
class WeekWorker extends Employee {
@Override
public double earnings() {
return 5000*4*12;
}
}
class Company {
Employee[] employees;
double salaries = 0;
Company(Employee[] employees) {
this.employees = employees;
}
public double salariesPay() {
salaries = 0;
for (Employee employee : employees) {
salaries += employee.earnings();
}
return salaries;
}
}
public class HardWork {
public static void main(String[] args) {
Employee[] employees = new Employee[20];
for (int i = 0; i < employees.length; i++) {
if (i % 3 == 0)
employees[i] = new WeekWorker();
else if (i % 3 == 1)
employees[i] = new MonthWorker();
else if (i % 3 == 2)
employees[i] = new YearWorker();
}
Company company = new Company(employees);
System.out.println("公司年工资总额:" + company.salariesPay());
}
}
package step3;
interface Displayable {
void display();
}
class Inform implements Displayable {
/*
* private String content;
*
* public Inform(String content) { this.content = content; }
*/
@Override
public void display() {
System.out.println("通知内容" /* + content */);
}
}
class Car implements Displayable {
/*
* private double fuel;
*
* public Car(double fuel) { this.fuel = fuel; }
*/
@Override
public void display() {
System.out.println("汽车油量" /* + fuel */);
}
}
class Adervise implements Displayable {
/*
* private String message;
*
* public Advertise(String message) { this.message = message; }
*/
@Override
public void display() {
System.out.println("广告消息" /* + message */);
}
}
public class Lab3_3 {
public static void main(String[] args) {
Displayable[] arr = { new Inform(), new Car(), new Adervise() };
for (Displayable d : arr) {
d.display();
}
}
}
package step4;
public class Student{
private String sno;
private String sname;
private String sdept;
public Student() {
}
public Student(String sno, String sname, String sdept) {
this.sno = sno;
this.sname = sname;
this.sdept = sdept;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
@Override
public String toString() {
return "学号: " + sno + "\t姓名: " + sname + "\t系部: " + sdept ;
}
}
package step4;
public interface IStudentDAO {
// 插入学生信息记录
public void insertStudent(Student stu);
// 删除指定学号学生信息记录
public void deleteStudent(String sno);
// 更新指定学生信息记录
public void updateStudent(Student stu);
// 按学号查询指定学生信息记录
public Student findStudentBySno(String sno);
// 显示指定学号学生信息记录
public void displayStudentBySno(String sno);
// 显示所有学生信息记录
public void displayAllStudent();
}
package step4;
import java.util.ArrayList;
import java.util.List;
public class StudentDAOImpl implements IStudentDAO {
static List students = new ArrayList();
@Override
public void insertStudent(Student stu) {
students.add(stu);
}
@Override
public void deleteStudent(String sno) {
for (Student stu : students) {
if(sno.equals(stu.getSno())) {
students.remove(stu);
break;
}
}
}
@Override
public void updateStudent(Student stu) {
for (Student s : students) {
if(stu.getSno().equals(s.getSno())) {
s.setSname(stu.getSname());
s.setSdept(stu.getSdept());
}
}
}
@Override
public Student findStudentBySno(String sno) {
for (Student stu : students) {
if(sno.equals(stu.getSno())) {
return stu;
}
}
return null;
}
@Override
public void displayStudentBySno(String sno) {
Student stu = findStudentBySno(sno);
if (stu != null) {
System.out.println("学号: " + stu.getSno() + "\t姓名: "
+ stu.getSname() + "\t系部: " + stu.getSdept());
} else {
System.out.println("没有找到学号为 " + sno + " 的学生。");
}
}
@Override
public void displayAllStudent() {
if (students.size() > 0) {
for (Student stu : students) {
System.out.println("学号: " + stu.getSno() + "\t姓名: "
+ stu.getSname() + "\t系部: " + stu.getSdept());
}
}else {
System.out.println("数据库中无学生记录!");
}
}
}
package step4;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentDAOImpl studentDAOImpl = new StudentDAOImpl();
//1. 插入学生信息
Student stu = new Student();
stu.setSno(scanner.next());
stu.setSname(scanner.next());
stu.setSdept(scanner.next());
studentDAOImpl.insertStudent(stu);
//2. 显示插入学生信息
System.out.println("1. 插入学生信息如下:");
studentDAOImpl.displayAllStudent();
//3. 更新学生信息
stu.setSname("李四");
stu.setSdept("计算机系");
studentDAOImpl.updateStudent(stu);
System.out.println("2. 更新后学生信息如下:");
System.out.println(studentDAOImpl.findStudentBySno(stu.getSno()));
//4. 删除指定学生信息
System.out.println("3. 删除当前学号学生信息:" + stu.getSno());
studentDAOImpl.deleteStudent(stu.getSno());
System.out.println("学生信息已删除!");
//2. 显示插入学生信息
System.out.println("5. 显示所有学生信息:");
studentDAOImpl.displayAllStudent();
scanner.close();
}
}