第三次项目《汽车租赁》

2024-02-26 10:38
文章标签 项目 汽车 租赁 第三次

本文主要是介绍第三次项目《汽车租赁》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*** 父类车(抽象类)*/
public abstract class Car {/**品牌*/private String brand;/**牌照*/private String licenseTag;/**日租金*/private int dayRant;public Car() {}public Car(String brand, String licenseTag, int dayRant) {this.brand = brand;this.licenseTag = licenseTag;this.dayRant = dayRant;}/**汽车信息*/public void Information() {System.out.println("品牌是:"+brand);}/**租金*/public abstract float money(int day);/**设置品牌*/public void setBrand(String brand) {this.brand = brand;}/**得到品牌*/public String getBrand() {return brand;}/**得到牌照*/public String getLicenseTag() {return licenseTag;}/**设置牌照*/public void setLicenseTag(String licenseTag) {this.licenseTag = licenseTag;}/**得到日租金*/public int getDayRant() {return dayRant;}/**设置日租金*/public void setDayRant(int dayRant) {this.dayRant = dayRant;}
}
/**客车(具体产品类)*/
public class PassengerCar extends Car{/**座位*/private String seat;public PassengerCar() {}// 构造方法public PassengerCar(String brand, String licenseTag, int dayRant, String seat) {super(brand, licenseTag, dayRant);this.seat = seat;}/**轿车租赁信息*/@Overridepublic void Information() {super.Information();System.out.println("座位数:"+seat);System.out.println("牌照是:"+getLicenseTag());System.out.println("日租金:"+getDayRant());}public String getSeat() {return seat;}public void setSeat(String seat) {this.seat = seat;}/**客车租金计算*/@Overridepublic float money(int days) {float price = this.getDayRant() * days;// 天数if (days >= 150) {// 打折price *= 0.6; }else if(days >= 30) {price *= 0.7;}else if(days >= 7) {price *= 0.8;}else if(days >= 3) {price *= 0.9;}return price;}
}
/**轿车(子类) */
public class SaloonCar extends Car {/**型号*/private String type;public SaloonCar() {}// 构造方法public SaloonCar(String brand, String licenseTag, int dayRant, String type) {super(brand, licenseTag, dayRant);this.type = type;}/**轿车租赁信息 */@Overridepublic void Information() {super.Information();System.out.println("型号是:"+type);System.out.println("牌照是:"+getLicenseTag());System.out.println("日租金:"+getDayRant());}public String getType() {return type;}public void setType(String type) {this.type = type;}/**客车租金计算*/@Overridepublic float money(int days) {float price = this.getDayRant() * days;if (days > 150) {price *= 0.7; }else if(days > 30) {price *= 0.8;}else if(days > 7) {price *= 0.9;}return price;}
}
/** 用户租车 (客户类)*/
public class Client {public static void main(String[] args) {// 创建工厂类对象Factory factory = new Factory();Scanner input = new Scanner(System.in);do {System.out.println("=======欢迎来到太阳载具公司=======");System.out.println("请选择你的车型:1、汽车  2、其他");int choise = input.nextInt();// 品牌String brand = "";// 型号String type = "";// 座位数String seat = "";switch (choise) {case 1:System.out.println("请选择汽车品牌: 1、保时捷  2、布加迪");int ScBrand = input.nextInt();brand = ScBrand == 1 ? "保时捷" : "布加迪";if (ScBrand == 1) {System.out.println("请选择型号:1、911 2、新能源 3、卡宴");int ScType = input.nextInt();if (ScType == 1) {type = "911";} else if(ScType == 2) {type = "新能源";} else if(ScType == 3) {type = "卡宴";}} else {System.out.println("请选择型号:1、威龙 2、威航");type = input.nextInt() == 1 ? "威龙" : "威航";}break;case 2:System.out.println("请选择其他载具: 1、UFO  2、和谐号");int select = input.nextInt();brand = select == 1 ? "UFO" : "和谐号";System.out.println("请选择座位类型: 1、100座 2、300座");int pcSeat = input.nextInt();if (pcSeat == 1) {seat = "100座";}else {seat = "300座";}break;default:System.out.println("输入错误!");break;}// 存车到父类引用Car car = factory.rantCar(brand, type, seat);// 输出车辆信息System.out.println("恭喜您!系统选车成功!车辆信息如下:");car.Information();// 判断子类对象(动态绑定)if(car instanceof SaloonCar) {// 调用子类特有方法要强转SaloonCar sc = (SaloonCar)car;System.out.println("您租的是"+sc.getBrand()+sc.getType()+",现有租赁活动: 租车大于7天打9折,租车大于30天打8折,租车大于150天打7折.");// 判断子类对象(动态绑定)}else if(car instanceof PassengerCar){// 调用子类特有方法要强转PassengerCar pc = (PassengerCar)car;System.out.println("您租的是"+pc.getBrand()+pc.getSeat()+",现有租赁活动: 租车3天打9折,租车7天打8折,租车30天打7折,租车150天6折.");}System.out.print("请输入您租赁的天数:");// 租赁天数int rentDay = input.nextInt();// 调用子类租金计算double rant = car.money(rentDay);System.out.println("租金为:"+rant);}while(isContinue());System.out.println("后期会有更多黑科技,欢迎下次光临呦~!");}/**是否继续方法*/public static boolean isContinue() {Scanner input = new Scanner(System.in);System.out.println("是否继续(y/n)?");String select = input.next();if(select.equals("y")) {return true;}else {return false;}}
}
/**载具工厂(工厂类)*/
public class Factory {public static Car[] car = new Car[8];/**初始化载具信息*/static{car[0] = new SaloonCar("保时捷", "京11111", 1800, "911");car[1] = new SaloonCar("保时捷", "京22222", 1500, "新能源");car[2] = new SaloonCar("保时捷", "京33333", 1200, "卡宴");car[3] = new SaloonCar("布加迪", "京44444", 2000, "威航");car[4] = new PassengerCar("UFO", "宇55555", 2200, "100座");car[5] = new PassengerCar("和谐号", "GYYYY", 3200, "300座");car[6] = new PassengerCar("UFO", "$$$$$$", 10000, "300座");car[7] = new PassengerCar("和谐号", "FY0000", 4200, "100座");}/***  判断租哪辆载具* @param brand 品牌* @param type 型号* @param seat 座位* @return 返回Car*/public Car rantCar(String brand, String type, String seat) {for (Car car1 : this.car) {// 租轿车if(car1 instanceof SaloonCar) {// 调用子类特有方法 SaloonCar sc = (SaloonCar)car1;if (sc.getBrand().equals(brand) && sc.getType().equals(type)) {return sc;}// 租其他载具}else if(car1 instanceof PassengerCar) {// 调用子类特有方法PassengerCar pc = (PassengerCar)car1;if (pc.getBrand().equals(brand) && pc.getSeat().equals(seat)) {return pc;}}}return null;}
}

这篇关于第三次项目《汽车租赁》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/748580

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav