为了账号安全,请及时绑定邮箱和手机立即绑定

做了个差不多。。。 账单那简化了一点 不 其实都简化了点。。。

入口:

package com.mgh;
import java.util.Scanner;
public class Initial {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Vehicle vcl[]=new Vehicle[6];
        vcl[0]=new Car("奥迪A4",1,500,4);
        vcl[1]=new Car("马自达6",2,400,4);
        vcl[2]=new Pickup("皮卡学6",3,450,4,2);
        vcl[3]=new Car("金龙",4,800,20);
        vcl[4]=new Truck("松花江",5,400,4);
        vcl[5]=new Truck("依维柯",6,1000,20);
        
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车: 1.是 0.否");
        
        Scanner scan=new Scanner(System.in);
        String isRent=scan.next();
        if(isRent.equals("1")){
        	System.out.println("您可租车的类型及其价目表:");
        	for(int i=0;i<5;i++){
        		vcl[i].infoShow();
        	}
            System.out.println("请输入您要租汽车的数量:");
            int rentNum=scan.nextInt();
            StringBuffer selectVclList=new StringBuffer();
            int costPerDay=0;
            int selectVclID[]=new int[rentNum];
            for(int i=0;i<rentNum;i++){
            	System.out.println("请输入第"+(i+1)+"辆车的序号:");
                selectVclID[i]=scan.nextInt()-1;
                selectVclList.append(vcl[selectVclID[i]].name+" ");
                costPerDay+=vcl[selectVclID[i]].rentPrice;
            }
            System.out.println("请输入租车天数:");
            int rentDays=scan.nextInt();
            int allCost=costPerDay*rentDays;
            String bill=new String();
            System.out.println("您的账单:");
            bill=selectVclList.toString()+"  "+ rentDays +"天   总价"+allCost+"元";
            
            System.out.println(bill);
            scan.close();
        }
        else{
        	System.out.println("感谢使用答答租车系统,欢迎再来!");
        }
	}

}

父类:

package com.mgh;

public abstract class Vehicle {
	String name=new String();
	int rentPrice;
	int vehicleNum;
	public abstract void infoShow();
}

Car子类,客车也在其中了 属性一样没必要另开一类:

package com.mgh;

public class Car extends Vehicle {
    int menLoad;
    public Car(String vName,int vNum,int rPrice,int vmLoad){
    	this.name=vName;
    	this.vehicleNum=vNum;
    	this.rentPrice=rPrice;
    	this.menLoad=vmLoad;
    }
    @Override
    public void infoShow(){
		// TODO Auto-generated method stub
        System.out.println(this.vehicleNum+". "+this.name+"   "+this.rentPrice+"元/天    载客:"+this.menLoad+"人");
	}

}

Truck子类:

package com.mgh;

public class Truck extends Vehicle {
    private int cargoLoad;
	public Truck(String vName,int vNum,int rPrice,int vcLoad){
    	this.name=vName;
    	this.vehicleNum=vNum;
    	this.rentPrice=rPrice;
    	this.cargoLoad=vcLoad;
	}
    @Override
	public void infoShow() {
		// TODO Auto-generated method stub
        System.out.println(this.vehicleNum+". "+this.name+"   "+this.rentPrice+"元/天    载重:"+this.cargoLoad+"吨");
	}

}

Pickup子类 皮卡:

package com.mgh;

public class Pickup extends Vehicle {
    private int menLoad;
    private int cargoLoad;
	public Pickup(String vName,int vNum,int rPrice,int vmLoad,int vcLoad){
    	this.name=vName;
    	this.vehicleNum=vNum;
    	this.rentPrice=rPrice;
    	this.cargoLoad=vcLoad;
    	this.menLoad=vmLoad;
	}
    @Override
	public void infoShow() {
		// TODO Auto-generated method stub
        System.out.println(this.vehicleNum+". "+this.name+"   "+this.rentPrice+"元/天    载客:"+this.menLoad+"人,载重:"+this.cargoLoad+"吨");
	}

}


正在回答

9 回答

有没有javae版本的租车系统

0 回复 有任何疑惑可以回复我~

但是账单里不是应该有乘客数和载货量的计数吗

0 回复 有任何疑惑可以回复我~

写的真好,比我简略了好多好多。。

0 回复 有任何疑惑可以回复我~

selectVclID[i]=scan.next-1为什么啊,不太懂

0 回复 有任何疑惑可以回复我~
#1

hmily8003

因为用户输入的scan.next()是车的序号!从1开始计数的,如果要用来做为数组的序号就必须按着数组的序号从0开始。倒如第一辆车用户输的是1 但是你程序要却只能写成vcl[0]才能表达第一辆车。
2015-06-13 回复 有任何疑惑可以回复我~

selectVclList.append(vcl[selectVclID[i]].name+" ");

costPerDay+=vcl[selectVclID[i]].rentPrice;

这两句话是高级语言吗?没怎么见过,求楼主科普一下。

0 回复 有任何疑惑可以回复我~
#1

Jukie

第二句明白,第一句不明白。 .append是什么用法呢?
2015-02-08 回复 有任何疑惑可以回复我~
#2

单车上的阿凡提 提问者 回复 Jukie

append是StringBuffer类里面用于拼接字符串的方法
2015-03-14 回复 有任何疑惑可以回复我~

54699d1800011c2d05000071.jpg

为什么不能实例化呢?

0 回复 有任何疑惑可以回复我~
#1

单车上的阿凡提 提问者

Car类 写了没有
2014-11-17 回复 有任何疑惑可以回复我~

必须赞,惭愧啊

0 回复 有任何疑惑可以回复我~

挺好的

0 回复 有任何疑惑可以回复我~

还有封装和各种容错都没做 

0 回复 有任何疑惑可以回复我~
#1

a01chen

很厉害
2014-11-17 回复 有任何疑惑可以回复我~
#2

hmily8003

已经很历害了,如果把第三季的try语句加上就相当完美了,我正在纠结这个,
2015-06-13 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530714    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程
意见反馈 帮助中心 APP下载
官方微信