汽车租赁系统
一、创建抽象父类
public abstract class Car {
private String number;//车牌号
private String brand;//品牌
private int price;//日租金
public Car() {
}
//有参构造
public Car(String number, String brand, int price) {
this.brand = brand;
this.number = number;
this.price = price;
}
//抽象方法计算总租金
public abstract double sumprice(int days);
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
二、轿车和汽车类继承抽象类
轿车类
public class PeopleCar extends Car {
protected int site;//独有site属性 座位数
double num;
public PeopleCar(){
}
//有参构造
public PeopleCar( String number,String brand, int price,int site){
super(number,brand,price);
this.site=site;
print();
}
//实现抽象方法
public double sumprice(int days){
double num=this.getPrice()*days;
if (days>=150){
num= num*0.7;
}else if (days>=30){
num= num*0.8;
}if (days>=7){
num= num*0.9;
}
return num;
}
public int getSite() {
return site;
}
public void setSite(int site) {
this.site = site;
}
//打印客车的类型
public void print(){
System.out.println("品牌:"+this.getBrand()+",车牌号:"+this.getNumber()+",日租金:"+this.getPrice()+",座位数:"+this.site);
}
}
//汽车类
public class SaloonCar extends Car{
protected String type;//独有属性type 类型
double num;
public SaloonCar(){
}
public SaloonCar( String number,String brand, int price,String type ){
super(number,brand,price);
this.type=type;
print();
}
//实现抽象方法
public double sumprice(int days){
double num=this.getPrice()*days;
if (days>=150){
num= num*0.6;
}else if (days>=30){
num= num*0.7;
}if (days>=7){
num= num*0.8;
}if (days>=3){
num= num*0.9;
}
return num;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//打印客车的类型
public void print(){
System.out.println("品牌:"+this.getBrand()+",车牌号:"+this.getNumber()+",日租金:"+this.getPrice());
}
}
三、创建车行类管理汽车信息
public class Carshoop {
private Car car[]=new Car[8];//创建数组
//初始化车辆信息
public void info(){
car[0] = new SaloonCar("京NY28588", "宝马", 800,"X6");
car[1] = new SaloonCar("京CNY3284", "宝马", 600,"550i");
car[2] = new SaloonCar("京NT37465", "别克", 300,"林荫大道");
car[3] = new SaloonCar("京NT96968", "别克", 600,"GL8");
car[4] = new PeopleCar("京6566754", "金杯", 800,16);
car[5] = new PeopleCar("京8696997", "金龙", 800,16);
car[6] = new PeopleCar("京9696996", "金杯", 1500,34);
car[7] = new PeopleCar("京8696998", "金龙", 1500,34);
}
//调用方法,根据传参,来判断顾客选择的是哪辆车;
public Car zhu(String brand,String type,int site){
for (Car cars:car){//for 增强循环 ,循环数组
//判断是那种类型的车
if (cars instanceof PeopleCar){//判断父类创建的实例化是哪个类型
PeopleCar peopleCar=(PeopleCar)cars;//类型转换
if (peopleCar.getBrand().equals(brand)//判断值是否相等
&&peopleCar.getSite()==site){
return peopleCar;//返回这个车,放在数组里
}
}
if (cars instanceof SaloonCar){
SaloonCar saloonCar=(SaloonCar)cars;
if (saloonCar.getBrand().equals(brand)
&&saloonCar.getType().equals(type)){
return saloonCar;
}
}
}
return null;
}
}
4、创建测试类选择车辆信息
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//变量初始化
String brand = "";
String type = "";
int site = 0;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到汽车租赁系统!!!");
Carshoop carshoop=new Carshoop();//实例化对象
carshoop.info();//打印车辆信息
System.out.println("1、轿车 \t2、客车");
System.out.print("请选择你要租赁的汽车类型:");
int motoType = sc.nextInt();
if (motoType==1){
System.out.print("请选择你要租赁的汽车品牌:1、别克 2、宝马:");
int choose1 = sc.nextInt();
if (choose1==1){
brand = "别克";
System.out.print("请选择你要租赁的汽车类型:1、林荫大道 2、GL8 :");
type=sc.nextInt()==1?"林荫大道":"GL8";//三元运算符
}
if (choose1==2){
brand = "宝马";
System.out.print("请选择你要租赁的汽车类型:1、X6 2、550i :");
type=sc.nextInt()==1?"X6":"550i";//三元运算符==1选择x6,否则选择550i
}
}
if (motoType==2){
System.out.print("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
brand = sc.nextInt()==1?"金龙":"金杯";
System.out.print("请选择你要租赁的汽车座位数:1、16座 2、34座");
site = sc.nextInt()==1?16:34;
}
//把获得的参数传入方法,用car接收返回值
Car car=carshoop.zhu(brand,type,site);
System.out.print("请输入租赁天数:");
int day= sc.nextInt();
//money 接收返回值
double money=car.sumprice(day);
System.out.println("给您分配的汽车为:"+car.getNumber());
System.out.println(money);
}
}
System.out.print("请输入租赁天数:");
int day= sc.nextInt();
//money 接收返回值
double money=car.sumprice(day);
System.out.println("给您分配的汽车为:"+car.getNumber());
System.out.println(money);
}
}