贴出自己的代码,请各位指教

package com.zuoye;

import java.util.Scanner;

public class Initial {
	
	private static final String NULL=null;
	private static Scanner input;    //用来输入数据

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(;;){
			//无条件执行语句,直到执行循环内部的跳出语句
			System.out.println("欢迎使用租车系统!");
			System.out.println("您是否需要租车1:是   2:否");
			input=new Scanner(System.in);
			int i=input.nextInt();		//完成数据的输入
			if(i==1){
				//显示可以租车的信息
				System.out.println("您可以租车的相关信息:");
				Car[] car={new PersonCar(1,"奥迪A4",500,4),new PersonCar(2,"马自达6",400,4), new PersonCar(3," 金龙 ",800,20),
						new Pickup(4,"皮卡雪6",450,4,2),new Truck(5,"松花江",400,4),
						new Truck(6,"依维柯",1000,20)};    //初始化对象,建立Car类对象的数组car
				//遍历car类对象输出
				System.out.println("序号"+"  名称"+"  价格"+"  容量1");
				for(Car rendcar:car){
					if(rendcar instanceof PersonCar){
						//判断是否有客车类对象的元素,输出相关的信息
						System.out.println(rendcar.getCarNum()+" "+rendcar.getCarName()+" "+rendcar.getCarPrice()+"/天"+" "+
						((PersonCar)rendcar).getCarCapacity()+"人");    //注意这里必须进行强制类型转换,转换为客车类,否则无法调用输出容量的信息
					}
					
					if(rendcar instanceof Pickup){
						//判断是否有皮卡类对象的元素,输出相关的信息
						System.out.println(rendcar.getCarNum()+" "+rendcar.getCarName()+" "+rendcar.getCarPrice()+"/天"+" "+
						((Pickup)rendcar).getCarCapacity()+"人"+" "+((Pickup)rendcar).getCarryingCapacity()+"吨");
					}
					
					if(rendcar instanceof Truck){
						//判断是否有货车类对象的元素,输出相关的信息
						System.out.println(rendcar.getCarNum()+" "+rendcar.getCarName()+" "+rendcar.getCarPrice()+"/天"+" "+
						((Truck)rendcar).getCarryingCapactity());
					}
					//System.out.println(rendcar);   //直接输出,输出的是对象的哈希码
				}
				
				System.out.println("请输入您的租车数量!");
				
				int rendNum=input.nextInt();   //获取租车数量
				String[] name=new String[rendNum];    //建立数组,保存租车的名称
				String[] name2=new String[rendNum];
				int sumCapacity=0;		//计算总的载人数
				int sumCCapacity=0;     //计算中的载货量
				int sumPrice=0;         //计算总的价格
				int a=0;
				int b=0;
				for(int i1=0;i1<rendNum;i1++){
					//获取顾客租车的相关信息
					System.out.println("请输入租借的第"+(i1+1)+"辆车的编号");
					int num=input.nextInt();
					System.out.println("请输入租借天数");
					int day=input.nextInt();
					switch(num){
					case 1:
						sumPrice=sumPrice+car[0].getCarPrice()*day;    //计算租车费用
						sumCapacity=sumCapacity+((PersonCar)car[0]).getCarCapacity();   //计算租车的载人量
					case 2:
						sumPrice=sumPrice+car[1].getCarPrice()*day;
						sumCapacity=sumCapacity+((PersonCar)car[1]).getCarCapacity();
					case 3:
						sumPrice=sumPrice+car[2].getCarPrice()*day;
						sumCapacity=sumCapacity+((PersonCar)car[2]).getCarCapacity();
					case 4:
						sumPrice=sumPrice+car[3].getCarPrice()*day;
						if(car[3] instanceof Pickup){
						sumCapacity=sumCapacity+((Pickup)car[3]).getCarCapacity();
						sumCCapacity=sumCCapacity+((Pickup)car[3]).getCarryingCapacity();      //计算租车的载货量
						}else{
							System.out.println("数据出差错!!!");
						}
					case 5:
						sumPrice=sumPrice+car[4].getCarPrice()*day;
						sumCCapacity=sumCapacity+((Truck)car[4]).getCarryingCapactity();
					case 6:
						sumPrice=sumPrice+car[5].getCarPrice()*day;
						sumCCapacity=sumCapacity+((Truck)car[5]).getCarryingCapactity();
					break;
					}
					
					//将选好的车分类
					if(car[num-1] instanceof PersonCar){
						//判断是否为客车
						name[a]=car[num-1].getCarName();
						a++;
					}else if(car[num-1] instanceof Pickup){
						//判断是否为皮卡
						name[a]=car[num-1].getCarName();
						name2[b]=car[num-1].getCarName();
						a++;
						b++;
					}else if(car[num-1] instanceof Truck){
						//判断是否为货车
						name2[b]=car[num-1].getCarName();
						b++;
					}
				}
				//输出客户选择的车的相关信息;
				System.out.println("您一共要租"+rendNum+"辆车!");
				if(a!=0){
					//通过a的大小来判断客户是否租借载人的车
					System.out.println("载人的车有");
					for(String n1:name){
					System.out.println(n1);
					}
					System.out.println("一共可以乘坐"+sumCapacity+"位乘客");
				}else{
					System.out.println("您没有租借载人的车");
				}
				if(b==0){
					System.out.println("您没有租借载货的车");
				}else{
					System.out.println("载货的车有");
					for(String n2:name2){
					System.out.println(n2);
					}
					System.out.println("一共可以运输"+sumCCapacity+"吨的货物");
				}
				System.out.println("您一共需要支付"+sumPrice+"元");
			}else if(i==2){
				System.out.println("欢迎下次光临!");
				break;
			}else{
				System.out.println("您的输入有误,系统自动退出!");
				break;
			}
		}

	}

}
package com.zuoye;

public class Car {

	//创建汽车父类
	private int carNum;
	private String carName;
	private int carPrice;
	
	public Car(int newCarNum,String newCarName,int newCarPrice){
		this.setCarNum(newCarNum);
		this.setCarName(newCarName);
		this.setCarPrice(newCarPrice);
	}
	
	public int getCarNum() {
		return carNum;
	}
	public void setCarNum(int carNum) {
		this.carNum = carNum;
	}
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public int getCarPrice() {
		return carPrice;
	}
	public void setCarPrice(int carPrice) {
		this.carPrice = carPrice;
	}
	
	
}
package com.zuoye;

public class PersonCar extends Car {

	//客车类
	private int carCapacity;    //载人量
	
	public PersonCar(int newCarNum,String newCarName,int newCarPrice,int newCarCapacoty){
		super(newCarNum,newCarName,newCarPrice); 	//显式调用父类的构造方法
		this.setCarCapacity(newCarCapacoty);      //初始化载货量
	}

	public int getCarCapacity() {
		return carCapacity;
	}

	public void setCarCapacity(int carCapacity) {
		this.carCapacity = carCapacity;
	}
	
}

package com.zuoye;


public class Pickup extends Car {

//皮卡类

private int carCapacity;      //载人量

private int carryingCapacity;    //载货量

public Pickup(int newCarNum,String newCarName,int newCarPrice,int newCarCapacity,int newCarryingCapacity){

super(newCarNum, newCarName, newCarPrice);

this.setCarCapacity(newCarCapacity);     //初始化载人量

this.setCarryingCapacity(newCarryingCapacity);      //初始化载货量

}


public int getCarCapacity() {

return carCapacity;

}


public void setCarCapacity(int carCapacity) {

this.carCapacity = carCapacity;

}


public int getCarryingCapacity() {

return carryingCapacity;

}


public void setCarryingCapacity(int carryingCapacity) {

this.carryingCapacity = carryingCapacity;

}


}


package com.zuoye;


public class Truck extends Car {

//货车类

private int carryingCapactity;

public Truck(int newCarNum,String newCarName,int newCarPrice,int newCarryingCapacity){

super(newCarNum, newCarName, newCarPrice);

this.setCarryingCapactity(newCarryingCapacity);

}


public int getCarryingCapactity() {

return carryingCapactity;

}


public void setCarryingCapactity(int carryingCapactity) {

this.carryingCapactity = carryingCapactity;

}

}


庞止大坑
浏览 1211回答 1
1回答

ziom

是有bug吗?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java