JavaOOP 继承实现汽车租赁计算价格

来源:互联网 发布:如何安装php环境 编辑:程序博客网 时间:2024/04/20 10:32
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)) {// 代表550ireturn days * 500;} else if ("2".equals(type)) {// 2代表商务舱GL8return 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:");elseSystem.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+"。");}}

0 0
原创粉丝点击