JAVA数组求和利用函数封装_【Java】理解封装、继承、多态和对象数组的综合实例(简易的租车系统,附代码)...

f1a1ac7a94d27174dd4cdee57ee4e15e.png

一、Drive.java(汽车类 - 父类)

package Z_CarManger;

import java.io.ObjectInputStream.GetField;

public abstract class Drive {

private String brand; // 品牌 brand

private String id; // 车牌号

private int rent;// 日租金 rent

// =================================== 【设置构造方法】

public Drive() {}

public Drive(String brand,String id,int rent)

{

this.brand=brand;

this.id=id;

this.rent=rent;

}

// =================================== 【设置属性】

public void setBrand(String brand) {

this.brand=brand; // 品牌 brand

}

public String getBrand() {

return this.brand; // 品牌 brand

}

public void setId(String id)

{

this.id=id; // 车牌号

}

public String getId() {

return this.id; // 车牌号

}

public void setRent(int rent) {

this.rent=rent;// 日租金 rent

}

public int getRent() {

return rent;// 日租金 rent

}

// 计算租金,传入天数,计算租金。 (抽象方法,要声明抽像类)

public abstract float Money(int days);

}

二、Car.java(轿车类 - 子类)

package Z_CarManger;

//===== 【轿车类】

public class Car extends Drive{

private String type;

// =================================== 【设置构造方法】

public Car() {}

public Car(String brand,String id,int rent, String type)

{

super(brand,id,rent); // 引用父类的构造方法

this.type=type;

}

// =================================== 【设置属性】

public void setType(String type) {

this.type=type;

}

public String getType()

{

return this.type;

}

// 计算租金,传入天数,计算租金。 (抽象方法,要声明抽像类)

public float Money(int days) {

float price=this.getRent();

if(days>7)

{

price=price*0.9f;

}

if(days>30)

{

price=price*0.8f;

}

if(days>150)

{

price=price*0.7f;

}

return price;

}

}

三、Bus.java(客车类 - 子类)

package Z_CarManger;

import org.omg.CORBA.PRIVATE_MEMBER;

// ===== 【客车类】

public class Bus extends Drive{

private int seat;

// =================================== 【设置构造方法】

public Bus() {};

public Bus(String brand,String id,int rent,int seat) {

super(brand,id,rent); // 引用父类的构造方法

this.seat=seat;

}

// -------- 【设置属性 - 座位】

public void setSeat(int seat)

{

this.seat=seat;

}

public int getSeat()

{

return this.seat;

}

// 计算租金,传入天数,计算租金。 (抽象方法,要声明抽像类)

public float Money(int days) {

float price=this.getRent();

if(days>3)

{

price=price*0.9f;

}

if(days>7)

{

price=price*0.8f;

}

if(days>30)

{

price=price*0.7f;

}

if(days>150)

{

price=price*0.6f;

}

return price;

}

}

四、Data1.java(数据类 - 存放需要比对和处理的对象数组)

package Z_CarManger;

public class Data1 {

Drive[] m_car=new Drive[8]; // 通过父类创建对象数组

public int init(String brand,String id,int days,String type,int seat)

{

//System.out.println("品牌:"+brand+" 牌照号:"+id+" 天数:" +days+" 车型:"+type +" 座位:"+seat);

// 【轿车】:品牌、车辆号、日租金、型号

m_car[0]=new Car("宝马","京N28588",800,"X6"); // 对应Car类的构造函数

m_car[1]=new Car("宝马","京CNY3284",600,"550i"); // 对应Car类的构造函数

m_car[2]=new Car("别克","京NT37465",300,"林荫大道"); // 对应Car类的构造函数

m_car[3]=new Car("别克","京NT96968",600,"GL8"); // 对应Car类的构造函数

// 【客车】:品牌、车辆号、日租金、型号

m_car[4]=new Bus("金杯","京6566754",800,16); // 对应Bus类的构造函数

m_car[5]=new Bus("金龙","京8696997",800,16); // 对应Bus类的构造函数

m_car[6]=new Bus("金杯","京9696996",1500,34); // 对应Bus类的构造函数

m_car[7]=new Bus("金龙","京8696998",1500,34); // 对应Bus类的构造函数

//String brand="宝马";

//String id="京N28588";

//int days=3;

//String type="X6";

int money=0;

// ====================================== 【正确方法,遍历对象数组】

Drive m_meney=null; // 创建一个对象变量

for(Drive moto:m_car) // 用父类Drive建立一个新的数组对象moto,进行遍历和比较

{

if(moto instanceof Car) // 遍历Car子类的对象

{

moto=(Car)moto;

if(brand.equals(moto.getBrand()) && id.equals(moto.getId()) && type.equals(((Car)moto).getType()))

{

money=days*(moto.getRent());

System.out.println("应该交费:"+money);

}

}

if(moto instanceof Bus) // 遍历Bus子类的对象

{

moto=(Bus)moto;

if(brand.equals(moto.getBrand()) && id.equals(moto.getId()) && seat==(((Bus)moto).getSeat()))

{

money=days*(moto.getRent());

System.out.println("应该交费:"+money);

}

}

}

return money;

}

}

五、App.java(主程序 main)

package Z_CarManger;

import java.util.Scanner;

import org.omg.CORBA.OMGVMCID;

import Book.database;

// ========= 【主程序】

public class App {

public static void main(String[] args)

{

int m_car=1; // 轿车或客车

String m_brand=""; // 品牌

String m_id=""; // 牌照号

int m_seat=0; // 座位数

int m_rent=0; // 日租金

String m_type=""; // 车型

int m_days=0; // 天数

System.out.println("--------------- 腾飞汽车租赁管理系统 ---------------");

System.out.println(" 1、轿车 2、客车");

System.out.println(" 请选择汽车类型:");

Scanner input=new Scanner(System.in);

int m_input = input.nextInt();

m_car=m_input;

switch(m_car) {

case 1: // 轿车

System.out.print("请继续选择品牌:1.宝马 2、别克");

m_input = input.nextInt();

if(m_input==1)

{

m_brand="宝马";

System.out.print("请继续选择车型:1.X6 2、550i");

m_type=(input.nextInt()==1)?"X6":"550i";

}else

{

m_brand="别克";

System.out.print("请继续选择车型:1.林荫大道 2、GL8");

m_type=(input.nextInt()==1)?"林荫大道":"GL8";

}

break;

case 2: // 客车

System.out.print("请继续选择品牌:1.金杯 2、金龙");

m_brand=(input.nextInt()==1)?"金杯":"金龙";

System.out.print("请继续选择座位:1.16座 2、34座");

m_seat=Integer.valueOf((input.nextInt()==1)?"16":"34");

break;

}

System.out.print("输入租赁天数:");

m_days=input.nextInt();

System.out.print("输入牌照号:");

m_id=input.next();

Data1 uuu = new Data1();

uuu.init(m_brand,m_id,m_days,m_type,m_seat);

}

}

标签:days,Java,String,int,brand,多态,id,租车,public

来源: https://blog.csdn.net/dxnn520/article/details/102757646

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值