Java面向对象实现 * 系统
作者:勤奋的小镇青年、 时间:2023-05-20 07:03:06
本文实例为大家分享了Java实现 * 系统的具体代码,供大家参考,具体内容如下
父类Vehicle
public abstract class Vehicle {
private String num;
private String brand;
private double rent;
//重写equals方法
public abstract boolean equals(Vehicle v);
//计算费用
public abstract double cost(int days,double rent);
//3个参数的有参构造
public Vehicle(String num, String brand, double rent) {
this.num = num;
this.brand = brand;
this.rent = rent;
}
//无参构造
public Vehicle() {
}
//get,set方法
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getRent() {
return rent;
}
public void setRent(double rent) {
this.rent = rent;
}
}
子类Cars
public class Cars extends Vehicle{
private String type;
//重写父类equals方法
@Override
public boolean equals(Vehicle v) {
if(v instanceof Cars){
Cars c=(Cars) v;
return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType());
}
return false;
}
//重写父类费用方法
@Override
public double cost(int days,double sent) {
if (days>150){
return days*sent*0.7;
}else if (days>30){
return days*sent*0.8;
}else if (days>7){
return days*sent*0.9;
}else {
return days*sent;
}
}
//无参构造
public Cars() {
}
//有参构造
public Cars(String num, String brand, double rent, String type) {
super(num, brand, rent);
this.type = type;
}
//2个有参构造的方法
public Cars(String brand,String type){
super.setBrand(brand);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
子类Bus
public class Bus extends Vehicle{
private int seat;
//重写父类的equals方法
@Override
public boolean equals(Vehicle v) {
if(v instanceof Bus){
Bus b=(Bus) v;
return this.getBrand().equals(b.getBrand())&&this.getSeat()==b.getSeat();
}
return false;
}
//重写父类费用方法
@Override
public double cost(int days,double rent) {
if (days>=150){
return days*rent*0.6;
}else if (days>=30){
return days*rent*0.7;
}else if (days>=7){
return days*rent*0.8;
}else if (days>=3){
return days*rent*0.9;
}else {
return days*rent;
}
}
//2个参数的有参构造
public Bus(String brand,int seat){
super.setBrand(brand);
this.seat=seat;
}
//子类有参构造
public Bus(String num, String brand, double rent, int seat) {
super(num, brand, rent);
this.seat = seat;
}
//子类无参构造
public Bus(){}
//子类get set 方法
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
}
汽车业务类VehicleServicer
public class VehicleServicer {
public static List initVehicle(){
Vehicle v1=new Bus("京6566754","金杯",800,16);
Vehicle v2=new Bus("京9696996","金杯",1500,34);
Vehicle v3=new Bus("京8696997","金龙",800,16);
Vehicle v4=new Bus("京8696998","金龙",1500,34);
Vehicle c1 =new Cars("京NT37465","别克",300,"林荫大道");
Vehicle c2 =new Cars("京9696996","别克",600,"GLB");
Vehicle c3 =new Cars("京8696997","宝马",800,"X6");
Vehicle c4 =new Cars("京8696998","宝马",600,"550i");
//先装入数组中
Vehicle[] ve = {v1,v2,v3,v4,c1,c2,c3,c4};
//将数组转换成集合
List<Vehicle> vehicles = Arrays.asList(ve);
return vehicles;
}
}
测试类Test
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("**********欢迎光临秋明山守望者 * 公司**********");
System.out.println("1.轿车 2.客车");
System.out.print("请选择你要租赁的汽车类型:");
int type = sc.nextInt();
//桥车
Vehicle ve;
String brand;
if(type==1){
System.out.print("请选择你要租赁的汽车品牌(1.别克 2.宝马):");
int pinpai = sc.nextInt();
String model=pinpai==1?"别克":"宝马";
if(pinpai==1){
System.out.print("请输入你要租赁的汽车类型(1.X6 2.550i):");
int leixin = sc.nextInt();
brand=leixin==1?"林荫大道":"GL8";
}else {
System.out.print("请输入你要租赁的汽车类型(1.X6 2.550i):");
int leixin = sc.nextInt();
brand=leixin==1?"X6":"550i";
}
ve = new Cars(model, brand);
}else {//客车
int seat;
System.out.print("请选择你要租赁的汽车品牌(1.金龙 2.金杯):");
int pinpai = sc.nextInt();
String s=pinpai==1?"金龙":"金杯";
System.out.print("请选择你要租赁的汽车座位数(1.16座 2.34座):");
int z = sc.nextInt();
seat =z==1?16:34;
ve = new Bus(s, seat);
}
//根据选好的车型,输出车牌和总价
List<Vehicle> list = VehicleServicer.initVehicle();
for (Vehicle v:list) {
if(ve.equals(v)){
System.out.print("请输入你要租赁的天数:");
int days = sc.nextInt();
System.out.println("分配给您的汽车牌号是:"+v.getNum());
System.out.println("您需要支付的租赁费用是:"+v.cost(days,v.getRent()));
}
}
}
}
来源:https://blog.csdn.net/weixin_53106424/article/details/111911118
标签:java,租赁系统
0
投稿
猜你喜欢
java实现打砖块游戏算法
2023-01-28 20:51:06
C#纯技术之Class写入Json
2023-01-15 17:13:49
总结的5个C#字符串操作方法分享
2022-08-10 04:02:01
Java简单实现SpringMVC+MyBatis分页插件
2023-09-09 23:08:45
Spring Boot中的那些条件判断的实现方法
2023-04-26 15:02:07
基于Android中的 AutoCompleteTextView实现自动填充
2023-04-02 07:11:36
Spring JPA find单表查询方法示例详解
2022-11-03 19:43:38
Java面试题-实现复杂链表的复制代码分享
2023-11-23 20:05:39
Java封装的实现访问限定符、包
2023-03-20 07:08:48
C# 程序集和反射详解
2022-12-29 20:24:18
深入XPath的详解以及Java示例代码分析
2021-11-01 13:42:33
详解feign调用session丢失解决方案
2021-08-29 20:38:20
MyBatis动态Sql之if标签的用法详解
2023-04-20 14:02:26
C#队列Queue用法实例分析
2023-02-27 22:35:14
JavaSE-面向对象(方法重写)
2023-01-27 10:51:09
Android操作SQLite基本用法
2022-04-28 20:42:02
Java并发编程同步器CountDownLatch
2022-10-17 18:59:34
Android特效之水波纹的实现
2022-04-30 22:55:09
Android去除AlertDialog的按钮栏的分隔线
2021-06-06 18:53:54
Android自定义View绘图实现拖影动画
2023-04-16 00:06:38