参考了很多的大神的代码,有些地方看不懂,我用一个上午的时间用自己的能够理解的代码写的,通俗易懂些,但是有很多不足,希望大神指点,提多多的意见!

来源:12-1 综合练习

Merderer零度

2015-10-17 13:11

public  class Car {
	private int carNumber;
	private String carName;
	private int carRentMoney;
	
	public int getCarNumber() {
		return carNumber;
	}
	public void setCarNumber(int carNumber) {
		this.carNumber = carNumber;
	}
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public int getCarRentMoney() {
		return carRentMoney;
	}
	public void setCarRentMoney(int carRentMoney) {
		this.carRentMoney = carRentMoney;
	}
}

public class passengerCar extends Car {
	private int CarCapacity;
	public passengerCar(int carNumber,String carName,int carRentMoney,int carCapacity){
		this.setCarNumber(carNumber);
		this.setCarName(carName);
		this.setCarRentMoney(carRentMoney);
		CarCapacity = carCapacity;
	}

	public int getCarCapacity() {
		return CarCapacity;
	}

	public void setCarCapacity(int carCapacity) {
		CarCapacity = carCapacity;
	}
}

public class pickUp extends Car {
	private int CarCapacity;
	private int CarCarry;
	
	public pickUp(int carNumber,String carName,int carRentMoney,int CarCapacity,int CarCarry){
		this.setCarNumber(carNumber);
		this.setCarName(carName);
		this.setCarRentMoney(carRentMoney);
		this.CarCapacity = CarCapacity;
		this.CarCarry = CarCarry;
		
	}
	public int getCarCapacity() {
		return CarCapacity;
	}
	public void setCarCapacity(int carCapacity) {
		CarCapacity = carCapacity;
	}
	public int getCarCarry() {
		return CarCarry;
	}
	public void setCarCarry(int carCarry) {
		CarCarry = carCarry;
	}

}

public class trunkCar extends Car {
	private int CarCarry;
	
	public trunkCar(int carNumber,String carName,int carRentMoney,int CarCarry){
		this.setCarNumber(carNumber);
		this.setCarName(carName);
		this.setCarRentMoney(carRentMoney);
		this.CarCarry = CarCarry;
		
	}
	public int getCarCarry() {
		return CarCarry;
	}

	public void setCarCarry(int carCarry) {
		CarCarry = carCarry;
	}
}

import java.util.Scanner;
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("欢迎使用答答租车系统:");
		System.out.println("你是否要租车?  1是    2否");
		Scanner s = new Scanner(System.in);
		int  k = s.nextInt();
		if(k == 1){
			System.out.println("您可租车的类型及其价格表:");
			Car RentCar[] = {new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"马自达6",400,4),new pickUp(3,"皮卡雪6",450,4,2),new passengerCar(4,"金龙",800,20),new trunkCar(5,"松花江",400,4),new trunkCar(6,"依维柯",1000,20)};
			System.out.println("序号"+"\t"+"汽车名称"+"\t"+"\t"+"租金"+"\t"+"容量");
			for(int i = 0; i < 6; i++){
				////若RentCar[i]它是客车类型的实例,则打印出客车应该有的属性值,这里注意强转,否则不会出现
				if(RentCar[i] instanceof passengerCar){
				System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((passengerCar)RentCar[i]).getCarCapacity()+"人");//将Car类型强制转换成passengerCar类,得到passegerCard的属性
				}
				if(RentCar[i] instanceof pickUp){
					System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((pickUp)RentCar[i]).getCarCapacity()+"人  载货:"+((pickUp)RentCar[i]).getCarCarry()+"吨");
				}
				if(RentCar[i] instanceof trunkCar){
					System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((trunkCar)RentCar[i]).getCarCarry()+"吨");
				}
			}
			System.out.println("请输入您要租汽车的数量:");
			//已经有Scanner对象,直接调用
			int totalNum = s.nextInt();
			int sum = 0;
			for(int j = 1; j <= totalNum; j++){
				System.out.println("请输入第"+j+"辆车的序号");
				int q = s.nextInt();
				switch(q){
				case 1:
					 RentCar[0].getCarRentMoney();
				case 2:
					RentCar[1].getCarRentMoney();
				case 3:
					RentCar[2].getCarRentMoney();
				case 4:
					RentCar[3].getCarRentMoney();
				case 5:
					RentCar[4].getCarRentMoney();
				case 6:
					RentCar[5].getCarRentMoney();
				}
				sum = sum + RentCar[q-1].getCarRentMoney();
			}
				System.out.println("请输入租车的天数:");
				int day = s.nextInt();
				System.out.println("您的账单:");
				System.out.println(sum * day);
				
			
			
		}
		

	}

}


写回答 关注

1回答

  • Java程序猿进化中___
    2015-12-23 01:58:39

    怎么截图的啊?写得不错!

Java入门第二季 升级版

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

530655 学习 · 6091 问题

查看课程

相似问题