第十次作业

# 题目一

编写一个应用程序,模拟中介和购房者完成房屋购买过程。

共有一个接口和三个类:

  • Business—— 业务接口
  • Buyer —— 购房者类
  • Intermediary—— 中介类
  • Test —— 主类

# 源代码

  ## Business类

public interface IBusiness {
    double ratio = 0.022;
    double tax = 0.03;
    
    void buying(double price);
}

  ## Buyer类

public class Buyer implements IBusiness{
    private String name = null;
    
    public Buyer(){}
    public Buyer(String name){
        this.name = name;
    }
    
    @Override
    public void buying(double price) {
        System.out.println(this.name + "购买一套标价为" + price + "元的住宅");
    }
    
    private String getName() {
        return name;
    }
}

  ## Intermediary类

public class Intermediary implements IBusiness{
    Buyer buyer = null;
    
    public Intermediary(){}
    public Intermediary(Buyer buyer) {
        this.buyer = buyer;
    }
    
    @Override
    public void buying(double price) {
        buyer.buying(price);
        charing(price);
    }
    
    public void charing(double price) {
        System.out.println("房屋中介收取的费用为:" + (price * IBusiness.ratio) + "元");
        System.out.println("需要交纳的契税为:" + (price * IBusiness.tax) + "元");
    }
}

  ## Test

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        IBusiness iBusiness = new Intermediary(new Buyer("Lisa"));
        System.out.println("请输入所购买房屋的标价:");
        double price = input.nextDouble();
        iBusiness.buying(price);
    }
}

   ## 运行结果

 

# 题目二

  输入5个数,代表学生成绩,计算其平均成绩。当输入值为负数或大于100时,通过自定义异常处理进行提示。

# 源代码

  ##  NumEerException

/**
 * 小于0的数的异常
 * @author 喵
 * @date 2019年11月13日下午8:33:30
 */
public class NumEerException extends Exception{
    
    @Override
    public String toString() {
        return "输入的数字小于0或者大于100!";
    }
    
    @Override
    public String getMessage() {
        return super.getMessage();
    }
    
}

  ## Test

/**
 * 测试异常错误
 * @author 喵
 * @date 2019年11月13日下午8:42:23
 */
public class TestException {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double score = 0.0;
        double total = 0.0;
        System.out.println("请输入成绩:");
        try {
            for (int i = 0; i < 5; i++) {
                score = input.nextDouble();
                if (score < 0 || score > 100) {
                    throw new NumEerException();
                }
                total += score;
            }
            System.out.println("学生的平均学习成绩:" + total / 5);
        } catch (NumEerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

   ## 运行结果

 

 

 

 

posted @ 2019-11-13 21:51  摸凹猫  阅读(162)  评论(0)    收藏  举报