汽车租赁(集合框架,封装继承多态等)

//父类Vehicle 

public abstract class Vehicle {
private String no;
private String brand;
private int money;


public Vehicle() {
}


public Vehicle(String no, String brand, int money) {
this.no = no;
this.brand = brand;
this.money = money;
}


public String getNo() {
return no;
}


public void setNo(String no) {
this.no = no;
}


public String getBrand() {
return brand;
}


public void setBrand(String brand) {
this.brand = brand;
}


public abstract double rent(int days);


public int getMoney() {
return money;
}


public void setMoney(int money) {
this.money = money;
}
}


//子类Car类

public class Car extends Vehicle {
private int type;


public Car() {
super();
}


public Car(String no, String brand, int type, int money) {
super(no, brand, money);
this.setType(type);
}


@Override
public double rent(int days) {
double price = 0;
double total = 0;
if ("宝马".equals(getBrand())) {
if (1 == getType()) {
price = days * getMoney();
} else if (2 == getType()) {
price = days * getMoney();
}
} else if ("别克".equals(getBrand())) {
if (1 == getType()) {
price = days * getMoney();
} else if (2 == getType()) {
price = days * getMoney();
}
}


if (days >= 150) {
total = price * 0.7;
} else if (days >= 30) {
total = price * 0.8;
} else if (days >= 7) {
total = price * 0.9;
} else {
total = price;
}


return total;
}


public int getType() {
return type;
}


public void setType(int type) {
this.type = type;
}


}

//子类Bus类

public class Bus extends Vehicle {
private int seats;


public Bus() {
super();
}


public Bus(String no, String brand, int seats, int money) {
super(no, brand, money);
this.setSeats(seats);
}


public int getSeats() {
return seats;
}


public void setSeats(int seats) {
this.seats = seats;
}


@Override
public double rent(int days) {
double price = 0;
double total = 0;
if ("金龙".equals(getBrand())) {
price = days * getMoney();
} else if ("金杯".equals(getBrand())) {
price = days * getMoney();
}


if (days >= 150) {
total = price * 0.6;
} else if (days >= 30) {
total = price * 0.7;
} else if (days >= 7) {
total = price * 0.8;
} else if (days >= 3) {
total = price * 0.9;
} else {
total = price;
}
return total;
}
}


//子类Truck类

public class Truck extends Vehicle {
private int tons;


public Truck() {
super();
}


public Truck(String no, String brand, int tons, int money) {
super(no, brand, money);
this.setTons(tons);
}


public int getTons() {
return tons;
}


public void setTons(int tons) {
this.tons = tons;
}


@Override
public double rent(int days) {
double price = 0;
price = days * tons * getMoney();
return price;
}
}


//业务类

import java.util.ArrayList;
import java.util.Scanner;

public class VehicleBiz {
static String no = "苏A12345", brand;
static int type, seats, tons, days, num, money;
static double allMoney;
static Vehicle ve[] = new Vehicle[100];
static ArrayList<Vehicle> list = new ArrayList<Vehicle>();


public void showChoice() {
Scanner input = new Scanner(System.in);
System.out.println("请输入你想租的车子数量:");
num = input.nextInt();
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "辆车子的天数:");
days = input.nextInt();
System.out.println("请输入要租赁的车子类型(1.轿车    2.客车   3.卡车):");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("请输入轿车的品牌(1.宝马    2.别克):");
int CarChoice = input.nextInt();
if (CarChoice == 1) {
System.out.println("请输入宝马的型号(1.550   2.X6):");
int CarType = input.nextInt();
brand = "宝马";
type = (CarType == 1) ? 1 : 2;
money = (CarType == 1) ? 100 : 200;
} else if (CarChoice == 2) {
System.out.println("请输入别克的型号(1.君威  2.君越):");
int CarType = input.nextInt();
brand = "别克";
type = (CarType == 1) ? 1 : 2;
money = (CarType == 1) ? 200 : 300;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Car(no, brand, type, money);
list.add(i, ve[i]);
break;
case 2:
System.out.println("请选择客车的品牌(1.金龙   2.金杯):");
int BusChoice = input.nextInt();
System.out.println("请输入你得座位数:");
int BusSeats = input.nextInt();
if (BusChoice == 1) {
brand = "金龙";
seats = BusSeats;
money = (BusSeats < 16) ? 1000 : 1500;
} else if (BusChoice == 2) {
brand = "金杯";
seats = BusSeats;
money = (BusSeats < 16) ? 1500 : 2000;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Bus(no, brand, seats, money);
list.add(i, ve[i]);
break;
case 3:
System.out.println("请选择卡车的品牌(1.一汽重卡    2.重庆红岩):");
int trcukChoice = input.nextInt();
System.out.println("请选择你要的吨位:");
int truckTons = input.nextInt();
if (trcukChoice == 1) {
brand = "一汽重卡";
tons = truckTons;
money = 50;
} else if (trcukChoice == 2) {
brand = "重庆红岩";
tons = truckTons;
money = 50;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Truck(no, brand, tons, money);
list.add(i, ve[i]);
break;
}
allMoney = allMoney + ve[i].rent(days);
}
System.out.println("您一共租了" + num + "车子");
System.out.println("租金是" + allMoney + "元");
for (int j = 0; j < num; j++) {
if (ve[j] != null) {
System.out.println("您车子的型号是" + ve[j].getBrand() + ",您租赁了"
+ days + "天" + ",日租金是" + ve[j].getMoney() + "元");
}
}
}
}


//程序入口类(Test)类

public class Test {
public static void main(String[] args) {
VehicleBiz vb=new VehicleBiz();
vb.showChoice();

}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个简单的汽车租赁管理系统(C++控制台程序): 利用C++实现对汽车和客户信息的增、删、改等操作,并保存。 部分代码: // CarRent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "CarData.h" #include "Menu.h" #include"CustomerData.h" int main() { Menu::welcome(); Menu::login(); //登录界面 char choice; int carid, customerid; //汽车编号,客户编号 CustomerData CustomerDB; //客户库 Customer *r; //客户 CarData CarDB; //汽车库 Car *b; //汽车 Menu::showMenu(); //显示菜单 cout <> choice; while (choice != '0') { switch (choice) { case '1': //输入客户编号和要出租的汽车 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); if (NULL == r) { cout << "不存在该客户!"; cout << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!"; cout <borrowCar() == 0) { cout << "该汽车已租出!"; cout <borrowCar(b->getNo()); cout << "你在" <getBorTime()) << "成功出租一辆" <getName() << endl << "每日租金为(不足一天按一天计算):" <getPay(); break; case '2': //归还操作 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); //按编号查找 if (r == NULL) { cout << "不存在该客户!" << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!" <getOnshelf() == 1) { cout << "该汽车已经归还!" << endl; break; } cout << "您成功归还一辆"
封装、继承和多态是面向对象编程中的三个重要概念。 封装是指将数据和对数据的操作封装在一个类中,通过访问修饰符来控制对数据的访问权限。这样可以隐藏内部实现细节,提高代码的安全性和可维护性。\[2\] 继承是指一个类可以继承另一个类的属性和方法。通过继承,子类可以重用父类的代码,并且可以在子类中添加新的属性和方法。这样可以提高代码的复用性和可扩展性。\[2\] 多态是指同一个方法在不同的对象上可以有不同的行为。通过多态,可以实现方法的重写和重载,使得程序可以根据对象的实际类型来调用相应的方法。这样可以提高代码的灵活性和可扩展性。\[1\] 总结起来,封装、继承和多态是面向对象编程的三个基本特性,它们可以使代码更加模块化、可维护和可扩展。 #### 引用[.reference_title] - *1* *2* [封装、继承、多态](https://blog.csdn.net/yahid/article/details/125665027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C++ 类的封装继承多态](https://blog.csdn.net/secondtonone1/article/details/124485035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值