第三次项目《汽车租赁》

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

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

在.NET项目中嵌入Python代码的实践指南

《在.NET项目中嵌入Python代码的实践指南》在现代开发中,.NET与Python的协作需求日益增长,从机器学习模型集成到科学计算,从脚本自动化到数据分析,然而,传统的解决方案(如HTTPAPI或... 目录一、CSnakes vs python.NET:为何选择 CSnakes?二、环境准备:从 Py

基于 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