第八章:面向对象编程后面的作业

先写后看,进步更快

一、定义一个Person类{name,age,job},初始化Person对象数组,有3个person对象,并按照age从大到小进行排序,使用冒泡

package com.smallchange;

public class SmallChangeSys {
    public static void main(String[] args) {
        ;
        Person[] people = {
                new Person("marry", "老师", 31),
                new Person("jack", "学生", 19),
                new Person("han", "程序员", 35)};
        Person person = null;
        for (int i = 0; i < people.length - 1; i++) {
            for (int j = 0; j < people.length - 1 - i; j++) {
                if (people[i].age < people[i + 1].age) {
                    person = people[i];
                    people[i] = people[i+1];
                    people[i+1] = person;
                }
            }
        }
        for (int i = 0; i < people.length; i++) {
            System.out.println(people[i]);
        }
    }
}

class Person {
    String name;
    String job;
    int age;

    public Person(String name, String job, int age) {
        this.name = name;
        this.job = job;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", age=" + age +
                '}';
    }
}

2.写出四种访问修饰符和各自的访问权限:

本类同包子类不同包
public
protected
默认
private

3.编写老师类:要求有属性:name,age,post,salary;编写业务方法,introduce(),实现输出一个教师的信息;编写教师类,教授类(Professor),副教授,讲师类。在三个子类都重写父类的introduce()方法。定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。

package com.smallchange;

public class SmallChangeSys {
    public static void main(String[] args) {
        System.out.println(new Professor("张三",20,"律师",12000).introduce());;
    }
}

class Teacher{
    String name;
    int age;
    String post;
    double salary;

    public Teacher(String name, int age, String post, double salary) {
        this.name = name;
        this.age = age;
        this.post = post;
        this.salary = salary;
    }

    public String introduce() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", post='" + post + '\'' +
                ", salary=" + salary +
                '}';
    }
}

class Professor extends Teacher{
    public Professor(String name, int age, String post, double salary) {
        super(name, age, post, salary);
    }

}

4.题目:

package com.smallchange;

public class SmallChangeSys {
    public static void main(String[] args) {
        Manager manager = new Manager("刘备", 100, 20, 1.2);
        manager.setBonus(3000);
        manager.printSal();

        Employee employee = new Employee("关羽",50,10,1);
        employee.printSal();
    }
}

class Employee{
    private String name;
    private double daySal;
    private int workDays;
    private double grade;

    public Employee() {
    }

    public Employee(String name, double daySal, int workDays, double grade) {
        this.name = name;
        this.daySal = daySal;
        this.workDays = workDays;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getDaySal() {
        return daySal;
    }

    public void setDaySal(double daySal) {
        this.daySal = daySal;
    }

    public int getWorkDays() {
        return workDays;
    }

    public void setWorkDays(int workDays) {
        this.workDays = workDays;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    public void printSal() {
        System.out.println(name+"工资="+daySal*workDays*grade);
    }
}

class Manager extends Employee{
    //特有属性
    private double bonus;

    public Manager(String name, double daySal, int workDays, double grade) {
        super(name, daySal, workDays, grade);
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public void printSal() {
        //因为经理的工资计算与Employee不同需要重写
        System.out.println("经理"+getName()+"工资= "+ (bonus+getDaySal()*getWorkDays()*getGrade()));
    }
}

5.题目:

package com.smallchange;

public class SmallChangeSys {
    public static void main(String[] args) {
        new Employee(3000,"工人").showYearSalary();
        new Employee(4000,"服务员").showYearSalary();
        new Employee(2000,"农名").showYearSalary();
        new Teacher(7000,"老师",50).showYearSalary();
        new Scientist(100000,"科学家",15000).showYearSalary();
    }
}

class Employee {
    public double salary;
    public String identify;

    public Employee(double salary, String identify) {
        this.salary = salary;
        this.identify = identify;
    }

    public void showYearSalary() {
        System.out.println(identify + "的年薪为" + salary * 12);
    }
}

class Teacher extends Employee {
    private double daySal;

    public Teacher(double salary, String identify, double daySal) {
        super(salary, identify);
        this.daySal = daySal;
    }

    @Override
    public void showYearSalary() {
        System.out.println(identify + "的年薪为" + ((salary + daySal * 30) * 12));
    }
}

class Scientist extends Employee {
    private double yearMoney;

    public Scientist(double salary, String identify, double yearMoney) {
        super(salary, identify);
        this.yearMoney = yearMoney;
    }

    @Override
    public void showYearSalary() {
        System.out.println(identify + "的年薪为" + (salary * 12 + yearMoney));
    }
}

房屋出租系统:

package com.dmjxcn.houserent;

import java.util.Scanner;

public class HouseTest {
    public static void main(String[] args) {
        new HouseView().mainMenu();
        System.out.println("==============你退出房屋出租系统===========");
    }
}

class House {
    private int id;
    private String name;
    private String phone;
    private String address;
    private int rent; //月租
    private String state;

    public House(int id, String name, String phone, String address, int rent, String state) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.rent = rent;
        this.state = state;
    }

    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 String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return id + "\t\t\t" +
                name + "\t\t" +
                phone + "\t\t\t" +
                address + "\t\t" +
                rent + "\t\t" +
                state;
    }
}

class HouseView {
    Scanner scanner = new Scanner(System.in);
    private boolean loop = true; //控制显示菜单
    private String key = ""; //接收用户的选择
    private HouseService houseService = new HouseService(10);//假定只能放10

    //显示主菜单
    public void mainMenu() {
        do {
            System.out.println("=================房屋出租菜单==================");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 源");
            System.out.println("\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退 出");
            System.out.print("请输入你的选择(1-6): ");
            key = scanner.next();
            switch (key) {
                case "1":
                    this.addHouse();
                    break;
                case "2":
                    this.findHouse();
                    break;
                case "3":
                    this.delHouse();
                    break;
                case "4":
                    this.changeHose();
                    break;
                case "5":
                    this.listHouses();
                    break;
                case "6":
                    this.loop = false;
                    System.out.println("退出");
                    break;
                default:
                    System.out.println("请输入正确的选择");
                    break;
            }
        } while (loop);
    }

    public void changeHose() {
        System.out.println("===========修改房屋信息=========");
        System.out.print("请输入修改房屋id: ");
        int id = scanner.nextInt();
        House house = houseService.find(id);
        if (house == null){
            System.out.println("房屋不存在");
            return;
        }
        System.out.print("姓名(" + house.getName() + ")");
        house.setName(scanner.next());
        System.out.print("电话(" + house.getPhone() + ")");
        house.setPhone(scanner.next());
        System.out.print("地址(" + house.getAddress() + ")");
        house.setAddress(scanner.next());
        System.out.print("租金(" + house.getRent() + ")");
        house.setRent(scanner.nextInt());
        System.out.print("状态(" + house.getState() + ")");
        house.setState(scanner.next());
        System.out.println("===修改成功===");
    }

    //显示房屋列表
    public void listHouses() {
        System.out.println("===========房屋列表==============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态");
        House[] list = houseService.list();
        for (int i = 0; i < list.length; i++) {
            if (list[i] == null) break;
            System.out.println(list[i]);
        }
    }

    public void delHouse() {
        System.out.println("============删除房屋信息========");
        System.out.print("输入id: ");
        int id = scanner.nextInt();
        if (houseService.del(id)) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    //添加House
    public void addHouse() {
        System.out.println("=========添加房屋=========");
        System.out.print("姓名: ");
        String name = scanner.next();
        System.out.print("电话: ");
        String phone = scanner.next();
        System.out.print("地址: ");
        String address = scanner.next();
        System.out.print("月租: ");
        int rent = scanner.nextInt();
        System.out.print("状态: ");
        String state = scanner.next();
        //创建一个新的house
        House house = new House(0, name, phone, address, rent, state);
        if (houseService.add(house)) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //查找房屋
    public void findHouse() {
        System.out.println("=============查找房屋的id=============");
        System.out.print("请输入房屋id: ");
        int id = scanner.nextInt();
        House house = houseService.find(id);
        System.out.println(house);
    }
}

class HouseService {
    private House[] houses;
    private int houseNums = 1;//记录当前还有多少个房屋信息
    private int counter = 1;

    public HouseService(int size) {
        houses = new House[size];
        houses[0] = new House(1, "jack", "112", "海淀区", 2000, "未出租");
    }

    public House[] list() {
        return houses;
    }

    public boolean add(House house) {
        //判断是否还可以添加
        if (houseNums >= houses.length) {
            System.out.println("数组已满不能在添加了");
            return false;
        }
        houses[houseNums++] = house;
        //id自增长
        house.setId(++counter);
        return true;
    }

    public boolean del(int id) {
        int index = -1;
        for (int i = 0; i < houseNums; i++) {
            if (id == houses[i].getId()) {
                index = i;
            }
        }
        if (index == -1) {
            return false;
        }
        for (int i = 0; i < houseNums - 1; i++) {
            houses[i] = houses[i + 1];
        }
        houses[--houseNums] = null;
        return true;
    }

    public House find(int id) {
        for (int i = 0; i < houseNums; i++) {
            if (id == houses[i].getId()) {
                return houses[i];
            }
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值