java达达租车接口_Java入门第二季:达达租车系统的分析及代码实现

开始自己写的代码结构比较复杂,用到了父类子类继承及接口实现,后来看了最后一节项目分析,发现其实自己把问题考虑复杂了,于是从视频给出的参考代码出发按照其分析思路重新优化了代码,分享如下:

共有五个.java源文件,一共设计了五个类,分别是:Car(车类),PassengerCar(客车类),Trunk(货车类),PickUp(皮卡类),Test(测试类)。

其中Car是父类,PassengerCar,Trunk,PickUp三个子类分别继承Car的属性和方法,但各自拥有自己特有的新增属性和方法,Test类是主方法入口,用于控制界面显示和业务流程。

1.为什么要用继承?

租车系统只涉及三种车型:客车,货车和皮卡,而三种车都拥有名字(型号)和租金价格两个共同属性,因此可以抽象出一个共同父类来统一管理,方便我们后续操作。

2.小客车和大客车,轻卡车和重卡车为什么不再进一步分别设为继承于客车和货车类的更小子类?

小客车与大客车,轻卡车与重卡车的区别只在于属性值的设定不同,并没有需要新增的特有属性和方法,通过构造方法初始化时,只需给相应属性赋予不同值即可。PS:只有需要新增一些原有类所不具备的属性和方法时,我们才需要考虑是否要新增一个子类。

3.为什么不用接口?

原因有二:一是对于初学者来说,继承关系更好把握,更好理解和运用;二是问题涉及的关系并不复杂,单继承即可描述清楚,无需用接口自找麻烦。当然,有些涉及多继承关系的情况就需要考虑接口了。

4.目前代码存在的问题:

业务流程中有三个地方需要用户输入信息,对于用户非法输入的控制有待加强完善。

5个.java文件我都放在了名为dada的package包下,慕友们用eclipse新建工程后新建个dada包然后对应新建5个类,将五个.java文件直接拷贝后即可编译运行了,O(∩_∩)O~

Car.java

package dada;

public class Car {

public String name;

public double rent;

public void setName(String name){

this.name=name;

}

public String getName() {

return this.name;

}

public void setRent(double rent){

this.rent=rent;

}

public double getRent() {

return this.rent;

}

}

PassengerCar.java

package dada;

public class PassengerCar extends Car{

private int peopleCapacity; //客车类新增属性:载客量

public PassengerCar(String name,double rent,int peopleCapacity){

this.name=name;

this.rent=rent;

this.peopleCapacity=peopleCapacity;

}

public void setPeopleCapacity(int peopleCapacity){

this.peopleCapacity=peopleCapacity;

}

public int getPeopleCapacity() {

return this.peopleCapacity;

}

}

Trunk.java

package dada;

public class Trunk extends Car{

private double cargoCapacity; //货车类新增属性:载货量

public Trunk(String name,double rent,double cargoCapacity){

this.name=name;

this.rent=rent;

this.cargoCapacity=cargoCapacity;

}

public void setCargoCapacity(double cargoCapacity){

this.cargoCapacity=cargoCapacity;

}

public double getCargoCapacity() {

return this.cargoCapacity;

}

}

PickUp.java

package dada;

public class PickUp extends Car{

private double cargoCapacity; //皮卡类新增属性:载货量

private int peopleCapacity; //皮卡类新增属性:载客量

public PickUp(String name,double rent,int peopleCapacity,double cargoCapacity){

this.name=name;

this.rent=rent;

this.peopleCapacity=peopleCapacity;

this.cargoCapacity=cargoCapacity;

}

public void setPeopleCapacity(int peopleCapacity){

this.peopleCapacity=peopleCapacity;

}

public int getPeopleCapacity() {

return this.peopleCapacity;

}

public void setCargoCapacity(double cargoCapacity){

this.cargoCapacity=cargoCapacity;

}

public double getCargoCapacity() {

return this.cargoCapacity;

}

}

Test.java

package dada;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

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

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

Scanner scan = new Scanner(System.in);//定义键盘读取对象,从键盘读取用户输入

String input=scan.next();

if(input.equals("1")){

menu();

System.out.println("请输入您要租汽车的数量:");

int nums=scan.nextInt();

//定义车辆对象数组,存储所有租用车辆

Car[] cars=new Car[nums];

for(int n=1;n

{

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

int type=scan.nextInt();

switch(type){

case 1:{

cars[n-1]=new PassengerCar("奥迪A6",500,4);

break;

}

case 2:{

cars[n-1]=new PassengerCar("马自达6",400,4);

break;

}

case 3:{

cars[n-1]=new PickUp("皮卡雪6",450,4,2);

break;

}

case 4:{

cars[n-1]=new PassengerCar("金龙",800,20);

break;

}

case 5:{

cars[n-1]=new Trunk("松花江",400,4);

break;

}

case 6:{

cars[n-1]=new Trunk("依维柯",1000,20);

break;

}

default:{

System.out.println("非法输入,请重输...");

}

}

}

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

int days=scan.nextInt();

//调用账单方法,计算并显示账单信息

Bill(cars,days);

System.out.println("谢谢惠顾,系统结束!");

scan.close(); //退出系统前一定要将Scanner对象关闭,否则会造成内存泄漏

return;

}

if(input.equals("0")){

scan.close(); //退出系统前一定要将Scanner对象关闭,否则会造成内存泄漏

return;

}

System.out.println("非法输入,异常退出!");

scan.close(); //退出系统前一定要将Scanner对象关闭,否则会造成内存泄漏

return;

}//main方法结束

//***菜单方法:显示可租车辆信息

public static void menu(){

Car[] carsForRent={new PassengerCar("奥迪A6",500,4),

new PassengerCar("马自达6",400,4),

new PickUp("皮卡雪6",450,4,2),

new PassengerCar("金龙",800,20),

new Trunk("松花江",400,4),

new Trunk("依维柯",1000,20)};

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

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

int i=1;

for(Car currentCar:carsForRent){

if(currentCar instanceof PassengerCar){

System.out.println(i+"\t"+currentCar.getName()

+"\t"+currentCar.getRent()+"元/天\t载人:"

+((PassengerCar) currentCar).getPeopleCapacity()+"人");

}

if(currentCar instanceof Trunk){

System.out.println(i+"\t"+currentCar.getName()

+"\t"+currentCar.getRent()+"元/天\t载货:"

+((Trunk) currentCar).getCargoCapacity()+"吨");

}

if(currentCar instanceof PickUp){

System.out.println(i+"\t"+currentCar.getName()

+"\t"+currentCar.getRent()+"元/天\t载人:"

+((PickUp) currentCar).getPeopleCapacity()

+"人\t载货:"+((PickUp) currentCar).getCargoCapacity()+"吨");

}

i++;

}

}//menu方法结束

//***账单方法:计算并显示账单信息

public static void Bill(Car[] cars,int days){

double sum=0.0; //租金总价格

int persons=0; //载客总人数

double goods=0.0; //载货总吨数

for(Car currentCar:cars){

sum+=currentCar.rent;

if(currentCar instanceof PassengerCar){

persons+=((PassengerCar) currentCar).getPeopleCapacity();

}

if(currentCar instanceof Trunk){

goods+=((Trunk) currentCar).getCargoCapacity();

}

if(currentCar instanceof PickUp){

persons+=((PickUp) currentCar).getPeopleCapacity();

goods+=((PickUp) currentCar).getCargoCapacity();

}

}

sum=days*sum;

System.out.println("您的账单:");

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

for(Car currentCar:cars){

if(currentCar instanceof PassengerCar

||currentCar instanceof PickUp){

System.out.print(currentCar.getName()+" ");

}

}

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

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

for(Car currentCar:cars){

if(currentCar instanceof Trunk

||currentCar instanceof PickUp){

System.out.print(currentCar.getName()+" ");

}

}

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

System.out.println("***租车总价格:"+sum);

System.out.println("");

}//Bill方法结束

}//类结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值