JavaOOP 继承实现汽车租赁计算价格
2017-04-02 13:51
211 查看
package cn.hello.test5; /** * 客车类,继承汽车类。 * @author 北大青鸟 */ public final class Bus extends MotoVehicle { private int seatCount;// 座位数 public Bus() { } public Bus(String no, String brand, int seatCount) { super(no, brand); this.seatCount = seatCount; } public int getSeatCount() { return seatCount; } public void setSeatCount(int seatCount) { this.seatCount = seatCount; } /** * 计算客车租赁价 */ public int calRent(int days) { if (seatCount <= 16) { return days * 800; } else { return days * 1500; } } } package cn.hello.test5; /** * 轿车类,继承汽车类。 * @author 北大青鸟 */ public final class Car extends MotoVehicle { private String type;// 汽车型号 public Car() { } public Car(String no, String brand, String type) { super(no, brand); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } /** * 计算轿车租赁价 */ public int calRent(int days) { if ("1".equals(type)) {// 代表550i return days * 500; } else if ("2".equals(type)) {// 2代表商务舱GL8 return 600 * days; } else { return 300 * days; } } } package cn.hello.test5; /** * 汽车抽象类。 * @author 北大青鸟 */ public abstract class MotoVehicle { private String no;// 汽车牌号 private String brand;// 汽车品牌 /** * 无参构造方法。 */ public MotoVehicle() { } /** * 有参构造方法。 * @param no 汽车牌号 * @param brand 汽车品牌 */ public MotoVehicle(String no, String brand) { this.no = no; this.brand = brand; } public String getNo() { return no; } public String getBrand() { return brand; } /** * 抽象方法,计算汽车租赁价。 * */ public abstract int calRent(int days); } package cn.hello.test5; import java.util.Scanner; /** * 测试类。 * @author 北大青鸟 * */ public class TestRent { public static void main(String[] args) { String no,brand,mtype,type; int seatCount,days,rent; Car car; Bus bus; Scanner input = new Scanner(System.in); System.out.println("欢迎您来到汽车租赁公司!"); System.out.print("请输入要租赁的天数:"); days=input.nextInt(); System.out.print("请输入要租赁的汽车类型(1:轿车 2、客车):"); mtype = input.next(); if("1".equals(mtype)){ System.out.print("请输入要租赁的汽车品牌(1、宝马 2、别克):"); brand=input.next(); System.out.print("请输入轿车的型号 "); if("1".equals(brand)) System.out.print("1、550i:"); else System.out.print("2、商务舱GL8 3、林荫大道"); type=input.next(); no="京BK5543";//简单起见,直接指定汽车牌号 System.out.println("分配给您的汽车牌号是:"+no); car =new Car(no,brand,type); rent=car.calRent(days); } else{ System.out.print("请输入要租赁的客车品牌(1、金杯 2、金龙):"); brand=input.next(); System.out.print("请输入客车的座位数:"); seatCount=input.nextInt(); no="京AU8769";//简单起见,直接指定汽车牌号 System.out.println("分配给您的汽车牌号是:"+no); bus=new Bus(no,brand,seatCount); rent=bus.calRent(days); } System.out.println("\n顾客您好!您需要支付的租赁费用是"+rent+"。"); } }
相关文章推荐
- java 多线程并行计算之矩阵乘法继承Thread类实现(星星笔记)
- Java实现“汽车租赁项目”
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 在Java中实现浮点数的精确计算
- [原创]我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承
- Ajax - JavaScript之实现私有属性、像C++和Java一样支持基于类的继承方法之例子
- Java大整数实现计算catalan数
- 在Java中实现浮点数的精确计算
- 用RMI实现基于Java的分布式计算
- JAVA中如何用接口实现多继承和多态 (非常好)
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- Qomolangma实现篇(五):Qomo的OOP的语法和类继承体系
- 数据结构习作之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java) (技术含量少许)
- Qomolangma实现篇(五):Qomo的OOP的语法和类继承体系
- Qomolangma实现篇(五):Qomo的OOP的语法和类继承体系
- 我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java) (转载)
- Qomolangma实现篇(五):Qomo的OOP的语法和类继承体系
- 关于高精度正整数计算的JAVA实现
- 从设计模式学习oop思维,最后再具体通过java实现