java用this- gt_java入门第二季<---->答答租车系统v1.0(希望多多交流,给出修改意见)...

一、创建汽车父类Car

package FirstProject;

public class Car {

public String name;

public float price;

public int person;

public float goods;

/*public Car(String name,float price){

this.name = name;

this.price = price;

}*/

public int getPerson(){

return this.person;

}

public void setPerson(int person){

this.person = person;

}

public float getGoods(){

return this.goods;

}

public void setGoods(float goods){

this.goods = goods;

}

public String getName(){

return this.name;

}

public void setName(String name){

this.name = name;

}

public float getPrice(){

return this.price;

}

public void setPrice(float price){

this.price = price;

}

}

二、创建客车子类Coach

package FirstProject;

public class Coach extends Car {

public Coach(String name,float price,int person){

//super(name,price);

this.person = person;

this.name = name;

this.price = price;

}

public String toString(){

return this.name+"\t"+this.price+"元/天"+"\t"+"载人:"+

this.person+"人";

}

}

三、创建货车子类Trcuk

package FirstProject;

public class Trcuk extends Car {

public Trcuk(String name,float price,float goods){

//super(name,price);

this.goods = goods;

this.name = name;

this.price = price;

}

public String toString(){

return this.name+"\t"+this.price+"元/天"+"\t"+"载货:"+

this.goods+"吨";

}

}

四、创建皮卡子类Pickup

package FirstProject;

public class Pickup extends Car{

public Pickup(String name,float price,int person,float goods){

//super(name,price);

this.person = person;

this.goods = goods;

this.name = name;

this.price = price;

}

public String toString(){

return this.name+"\t"+this.price+"元/天"+"\t"+

"载客:"+this.person+"人"+'\t'+"载货:"+this.goods+"吨";

}

}

五、创建封装操作类Rent

package FirstProject;

import java.util.InputMismatchException;

import java.util.Scanner;

public class Rent {

public void run(){

while(true){

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

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

try{

Scanner a = new Scanner(System.in);

int s = a.nextInt();

//定义汽车数组

Car[] cars ={new Coach("奥迪A4",500,4),

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

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

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

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

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

if(s==1){

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

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

for(int i=0;i

System.out.println((i+1)+"\t"+cars[i]);

}

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

int b = a.nextInt();

Car[] carn = new Car[b];//定义新的数组存放车辆

float price=0;

for(int i=0;i

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

int x = a.nextInt();

carn[i] = cars[x-1];//将cars数组中车辆信息赋值carn数组。

price += carn[i].getPrice();//遍历循环要租的车辆,然后每辆车获取的价格累加,获取一天租车所需的总金额。

}

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

int y = a.nextInt();

float sum = y*price;//总价格

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

int person=0;//定义载人量

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

for(int i=0;i

if(carn[i] instanceof Coach||carn[i] instanceof Pickup){

person = person + carn[i].getPerson();

System.out.println(carn[i].getName()+"\t"+"x"+carn[i].getPerson());

}

}

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

float goods=0;//定义载货量

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

for(int i=0;i

if(carn[i] instanceof Trcuk||carn[i] instanceof Pickup){

goods += goods + carn[i].getGoods();

System.out.print(carn[i].getName()+"\t"+"x"+carn[i].getGoods());

}

}

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

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

}

else if(s==0){

System.out.println("感谢使用,欢迎下次使用");

}

else{

System.out.println("对不起,您输入的格式有误");

//return;

}

}catch(InputMismatchException e){

System.out.println("您的输入有误");

}catch(NegativeArraySizeException e){

System.out.println("您的输入有误");

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("数组角标越界,请重新输入");

}

}

}

}

六、创建主函数所在类RentDemo,在主函数中创建Rent对象,调用租车方法。

/*最终展示效果:欢迎使用嗒嗒租车系统:

您是否要租车:1是 0否您可租车的类型及其价目表:

序号 汽车名称 租金 容量1. 奥迪A4 500元/天 载人:4人

2. 马自达6 400元/天 载人:4人3. 皮卡雪6 450元/天 载人:4人 载货:2吨

4. 金龙 800元/天 载人:20人5. 松花江 400元/天 载货:4吨

6. 依维柯 1000元/天 载货:20吨请输入您要租汽车的数量:

请输入第i辆车的序号:请输入租车天数:

您的账单:可载人的车有:

奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人可载货的车有:

皮卡雪6 共载货:2.0吨租车总价格:6450.0元

*/

/**异常处理

对ArrayIndexOutBoundException异常处理。

对InputMismatchException异常处理。

对NegativeArraySizeException异常处理。

*/

package FirstProject;

public class RentDemo {

public static void main(String[] args) {

Rent t = new Rent();

t.run();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值