用接口方法简单编程,实现车辆租借功能

功能要求为

建立一个Car接口
package cn.qst.cheliang;

public interface Car {

public void chexing(int n ) ;                     

public void rizufei(int m) ;

public void zujiefei(int s,int t);

}
创建Jiaoche类继承Car接口
package cn.qst.cheliang;

public class Jiaoche implements Car{

@Override
public void chexing(int n ) {
      String cx;
      switch(n){
      case 1:
          cx = "宝马528i";
          System.out.println("您选择的车型是宝马528i。  ");
          break;
      case 2:
          cx = "奔驰E200";
          System.out.println("您选择的车型是奔驰E200。  ");
          break;
      case 3:
          cx = "宝马320";
          System.out.println("您选择的车型是宝马320 。  ");
          break;
      default:
          System.out.println("输入错误!!!");
          break;
      }


}

@Override
public void rizufei(int m) {
          int money;
          switch(m){
          case 1:
              money = 550;
              System.out.println("日租费是"+money+"元");
              break;
          case 2:
              money = 500;
              System.out.println("日租费是"+money+"元");
              break;
          case 3:
              money = 300;
              System.out.println("日租费是"+money+"元");
              break;
          default:
              System.out.println("输入错误!!!");
              break;
          } 
}
public void zujiefei(int s,int t){
     int m;
      switch(s){
      case 1:
          m = 550*t;
          System.out.println("您需要支付的租借费用总共为"+m+"元");
          break;
      case 2:
          m = 500*t;
          System.out.println("您需要支付的租借费用总共为"+m+"元");
          break;
      case 3:
          m = 300*t;
          System.out.println("您需要支付的租借费用总共为"+m+"元");
          break;
      default:
          System.out.println("输入错误!!!");
          break;
      } 
};         

}

建立一个Keche类继承Car接口
package cn.qst.cheliang;

public class Keche implements Car{

@Override
public void chexing( int n) {
      String cx;
      switch(n){
      case 4:
          cx = "<=16座";
          System.out.println("您选择的车型是  <=16座的客车。  ");
          break;
      case 5:
          cx = ">16座";
          System.out.println("您选择的车型是    >16座的客车。  ");
          break;
      default:
          System.out.println("输入错误!!!");
          break;
      }


}

@Override
public void rizufei(int m) {
    int money;
      switch(m){
      case 4:
          money = 800;
          System.out.println("日租费是"+money+"元");
          break;
      case 5:
          money = 1500;
          System.out.println("日租费是"+money+"元");
          break;

      default:
          System.out.println("输入错误!!!");
          break;
      }


}
public void zujiefei(int s,int t){
    int m;
      switch(s){
      case 4:
          m = 800*t;
          System.out.println("您需要支付的租借费用总共为"+m+"元");
          break;
      case 5:
          m = 1500*t;
          System.out.println("您需要支付的租借费用总共为"+m+"元");
          break;

      default:
          System.out.println("输入错误!!!");
          break;
      }

};

}

测试类Test获得结果
package cn.qst.cheliang;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {

    System.out.println("                                                                                  轿车                                                                                                              客车  ");
    System.out.println("    车型                            1、 宝马528i     2、奔驰E200        3、宝马320             4、<=16座                        5、 >=16座 ");
    System.out.println("    日租费(元/天)            550            500               300                   800              1500      ");              

    Car car = new Jiaoche();
    Car car1 = new Keche();

    Scanner sc = new Scanner(System.in);

    System.out.print("请输入你要租借车型的编号和租借天数(请在输入车型编号和租借天数时用空格隔开):");

    int i = sc.nextInt();
    int j = sc.nextInt();

    if(i>0&&i<4)
    {
         car.chexing(i);
         car.rizufei(i);
         car.zujiefei(i, j);    
    }
    if(i>3&&i<6)
    {
         car1.chexing(i);
         car1.rizufei(i);
         car1.zujiefei(i, j);
    }
    if(i>5||i<0)
    {
        System.out.println("输入错误!!!请重新输入");
    }

    }
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值