java简单租车系统 慕课手记_Java入门第二季——答答租车系统

我写的这个系统注释很详细,应该很适合小白参考,经运行没有任何问题,并且运用了面向对象的三大特性封装、继承和多态(在代码里也都有注释),欢迎参考,也希望大家能提出宝贵的意见,一起学习,共同进步!

AAffA0nNPuCLAAAAAElFTkSuQmCC

创建父类Car,子类PassengerCar,Pickup,Truck,详见代码。

父类Car:

package com.imooc;

public class Car {

//描述汽车可能有特征

public String name;//车的名称

public double cargoCapacity;//车的载货量

public int busLoad;//车的载客量

public int dailyRent;//车的日租金

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getCargoCapacity() {

return cargoCapacity;

}

public void setCargoCapacity(double cargoCapacity) {

this.cargoCapacity = cargoCapacity;

}

public int getBusLoad() {

return busLoad;

}

public void setBusLoad(int busLoad) {

this.busLoad = busLoad;

}

public int getDailyRent() {

return dailyRent;

}

public void setDailyRent(int dailyRent) {

this.dailyRent = dailyRent;

}

}

子类:小汽车PassengerCar

package com.imooc;

public class PassengerCar extends Car {//使用面向对象的继承特性

//使用面向对象的封装特性

private String name;

private int busLoad;

private int dailyRent;

//客车的构造方法

public PassengerCar(String name,int busLoad,int dailyRent){

this.name=name;

this.busLoad=busLoad;

this.dailyRent=dailyRent;

}

//自动生成getter和setter函数

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getBusLoad() {

return busLoad;

}

public void setBusLoad(int busLoad) {

this.busLoad = busLoad;

}

public int getDailyRent() {

return dailyRent;

}

public void setDailyRent(int dailyRent) {

this.dailyRent = dailyRent;

}

}

子类:皮卡Pickup

package com.imooc;

public class Pickup extends Car {

private String name;

private double cargoCapacity;

private int busLoad;

private int dailyRent;

public Pickup(String name,double cargoCapacity,int busLoad,int dailyRent){

this.name=name;

this.cargoCapacity=cargoCapacity;

this.busLoad=busLoad;

this.dailyRent=dailyRent;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getCargoCapacity() {

return cargoCapacity;

}

public void setCargoCapacity(double cargoCapacity) {

this.cargoCapacity = cargoCapacity;

}

public int getBusLoad() {

return busLoad;

}

public void setBusLoad(int busLoad) {

this.busLoad = busLoad;

}

public int getDailyRent() {

return dailyRent;

}

public void setDailyRent(int dailyRent) {

this.dailyRent = dailyRent;

}

}

子类:卡车Truck

package com.imooc;

public class Truck extends Car {

private String name;

private double cargoCapacity;

private int dailyRent;

public Truck(String name,double cargoCapacity,int dailyRent){

this.name=name;

this.cargoCapacity=cargoCapacity;

this.dailyRent=dailyRent;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getCargoCapacity() {

return cargoCapacity;

}

public void setCargoCapacity(double cargoCapacity) {

this.cargoCapacity = cargoCapacity;

}

public int getDailyRent() {

return dailyRent;

}

public void setDailyRent(int dailyRent) {

this.dailyRent = dailyRent;

}

}

主程序:Test类

package com.imooc;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("欢迎使用答答租车系统!");

System.out.println("您是否需要租车:1是 0否");

Scanner input1=new Scanner(System.in);

int is=input1.nextInt();

//创建一个Car类数组来保存各种车,利用引用多态来创建不同的子类对象

Car[] cars={new PassengerCar("奥迪A4",5,500),

new Truck("小货车",5,150),

new PassengerCar("奔驰E400",5,700),

new Pickup("皮卡",1.5,4,200),

new Truck("大型货车",10,600),

new PassengerCar("大客车",25,650)

};

//输入1,需要租车

if(is==1){

int totalMoney=0;

double totalcargoCapacity=0;

int totalbusLoad=0;

int rentCarDays=0;

System.out.println("您可租车的类型极其价目表:");

System.out.println("序号\t"+"汽车名称\t"+"日租金\t"+"容量\t");

int num=1;//定义初始序号

for(Car currentCar:cars){//对所有车进行一次循环遍历,即foreach(),来创建一个汽车一览表

if(currentCar instanceof PassengerCar){//判断正在遍历的car是否是Passenger类的一个实例

System.out.println(

"No."+num+'\t'+currentCar.getName()+'\t'//封装后只能通过get()方法来获取值

+currentCar.getDailyRent()+"元/天\t"

+"载人:"+currentCar.getBusLoad()+"人");

num++;

}

if(currentCar instanceof Truck){//判断正在遍历的car是否是Truck类的一个实例

System.out.println(

"No."+num+'\t'+currentCar.getName()+'\t'

+currentCar.getDailyRent()+"元/天\t"

+"载货:"+currentCar.getCargoCapacity()+"吨");

num++;

}

if(currentCar instanceof Pickup){//判断正在遍历的car是否是Pickup类的一个实例

System.out.println(

"No."+num+'\t'+currentCar.getName()+'\t'

+currentCar.getDailyRent()+"元/天\t"

+"载人:"+currentCar.getBusLoad()+"人"

+",载货:"+currentCar.getCargoCapacity()+"吨");

num++;

}

}

int totalDailyMoney = 0;//每日租金的总和

System.out.println("请输入您的租车数量(最大租车数为6):");

Scanner input2=new Scanner(System.in);

int carRentAmount=input2.nextInt();//租出去的车数

if(carRentAmount>0&&carRentAmount<=6){

int[] rentCarsArray=new int[carRentAmount];//创建一个数组,用来保存租出去的车辆,长度为租出去的车数

for(int i=1;i<=carRentAmount;i++){

System.out.println("请输入第"+i+"辆车的序号:");

Scanner input3=new Scanner(System.in);

int carNum=input3.nextInt();//输入车的序号

rentCarsArray[i-1]=carNum-1;//保存每一辆租出去的车在cars数组中的序号

totalDailyMoney+=cars[carNum-1].getDailyRent();

totalcargoCapacity+=cars[carNum-1].getCargoCapacity();

totalbusLoad+=cars[carNum-1].getBusLoad();

}

System.out.println("请输入需要租车的天数:");

Scanner input4=new Scanner(System.in);

rentCarDays=input4.nextInt();

totalMoney=totalDailyMoney*rentCarDays;

System.out.println("总载货量:"+totalcargoCapacity

+"\t总载客量:"+totalbusLoad

+"\t总价格:"+totalMoney);

System.out.println("您的账单:\n***可载人的车有:");

//载人账单

for(int i=0;i

if((cars[rentCarsArray[i]] instanceof PassengerCar)||(cars[rentCarsArray[i]]) instanceof Pickup){

System.out.println(cars[rentCarsArray[i]].getName());

}

}

System.out.println("——————共载人:"+totalbusLoad+"人");

//载货账单

System.out.println("***可载货的车有:");

for(int i=0;i

if((cars[rentCarsArray[i]] instanceof Truck)||(cars[rentCarsArray[i]]) instanceof Pickup){

System.out.println(cars[rentCarsArray[i]].getName());

}

}

System.out.println("——————共载货:"+totalcargoCapacity+"吨");

}else{

System.out.println("请修改租车数量!");

}

}else{

System.out.println("感谢您访问答答租车系统");

}

}

}

运行结果:

欢迎使用答答租车系统!

您是否需要租车:1是 0否

1

您可租车的类型极其价目表:

序号 汽车名称 日租金 容量

No.1 奥迪A4 500元/天 载人:5人

No.2 小货车 150元/天 载货:5.0吨

No.3 奔驰E400 700元/天 载人:5人

No.4 皮卡 200元/天 载人:4人,载货:1.5吨

No.5 大型货车 600元/天 载货:10.0吨

No.6 大客车 650元/天 载人:25人

请输入您的租车数量(最大租车数为6):

5

请输入第1辆车的序号:

6

请输入第2辆车的序号:

4

请输入第3辆车的序号:

2

请输入第4辆车的序号:

1

请输入第5辆车的序号:

3

请输入需要租车的天数:

3

总载货量:6.5 总载客量:39 总价格:6600

您的账单:

***可载人的车有:

大客车

皮卡

奥迪A4

奔驰E400

——————共载人:39人

***可载货的车有:

皮卡

小货车

——————共载货:6.5吨

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值