问答详情
源自:12-2 项目问题解析 1

这是我的代码,仅供参考,欢迎大家指正,谢谢!!

//测试类
package com.imooc;

import java.util.Scanner;

public class Initial {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Car[] carsForRent={new PassengerCar("奥迪A4",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("您是否需要租车:1是  0否");
		Scanner input=new Scanner(System.in);
		int u=input.nextInt();
		if(u==1){
			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.getCost()+"/每天 \t\t"+currentCar.getCapacity()+"人");
				}
				else if(currentCar instanceof PickUp){
					System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getCost()+"/每天 \t\t"+currentCar.getCapacity()+"人/"+currentCar.getCapacity1()+"吨");
				}
				else{
					System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getCost()+"/每天\t\t"+currentCar.getCapacity1()+"吨");
				}
				i++;
			}
			System.out.println("请输入您要租汽车的数量:");
			int n=input.nextInt();
			int fair=0;
			int contain=0;
			int contain1=0;
			int[] a = new int[6];
			for(int j=1;j<=n;j++){
				System.out.println("请输入第"+j+"辆汽车的序号:");
				i=input.nextInt();
				a[j-1]=i;
				fair+=carsForRent[i-1].cost;
				contain+=carsForRent[i-1].capacity;
				contain1+=carsForRent[i-1].capacity1;
			}
			System.out.println("请输入租车天数:");
			int day=input.nextInt();
			fair*=day;
			System.out.println("您的账单:");
			System.out.println("***可载人的车有:");
			int h=0;
			for(int k=0;k<n;k++){
				if(a[k]!=5||a[k]!=6){
					System.out.print(carsForRent[(a[k]-1)].name+"\t");
					h++;
				}
			}
			if(h==0){
				System.out.println("***没有可以载人的车!");
			}
			else
				System.out.println("共载人:"+contain+"人");
			h=0;
			System.out.println("***可以运货的车有:");
			for(int k=0;k<n;k++){
				if(a[k]==5||a[k]==6||a[k]==3){
					System.out.print(carsForRent[(a[k]-1)].name+"\t");
					h++;
				}
			}
			if(h==0){
				System.out.println("***没有可以运货的车!");
			}
			else
				System.out.println("共载货:"+contain1+"吨");
			System.out.println("***租车总价格:"+fair+"元");
		}
		input.close();
		System.out.println("感谢您的使用!!!");
	}
}

//父类

package com.imooc;


public class Car {

String name;

int cost;

int capacity;

int capacity1;

public String getName(){

return name;

}

public int getCost(){

return cost;

}

public int getCapacity(){

return capacity;

}

public int getCapacity1(){

return capacity1;

}

}

//三个子类(1)

package com.imooc;


public class PassengerCar extends Car{

public PassengerCar(String name,int cost,int capacity){

this.name=name;

this.cost=cost;

this.capacity=capacity;

}

public String getName(){

return name;

}

public int getCost(){

return cost;

}

public int getCapacity(){

return capacity;

}

}

//三个子类(2)

package com.imooc;


public class PickUp extends Car {

public PickUp(String name,int cost,int capacity,int capacity1){

this.name=name;

this.cost=cost;

this.capacity=capacity;

this.capacity1=capacity1;

}

public String getName(){

return name;

}

public int getCost(){

return cost;

}

public int getCapacity(){

return capacity;

}

public int getCapacity1(){

return capacity1;

}

}

//三个子类(3)

package com.imooc;


public class Trunk extends Car {

public Trunk(String name,int cost,int capacity1){

this.name=name;

this.cost=cost;

this.capacity1=capacity1;

}

public String getName(){

return name;

}

public int getCost(){

return cost;

}

public int getCapacity1(){

return capacity1;

}

}


提问者:hahadog 2016-02-17 17:05

个回答

  • 正在入门的程序猿
    2017-01-17 20:09:59

    37 行有错误  imput

  • bcdea
    2016-02-18 16:20:45

    57行,应换成if(a[k]!=5&&a[k]!=6){  吧 。数组a长度为6,相当于限制了借车的辆数,只能借6辆。建议在输入车辆序号时加一个是否小于6的判断条件。  谢谢您提供的代码,自己估计很难做出来。。

  • 张三十
    2016-02-17 21:02:37

    好厉害,话说用继承的话是不是可以只用写子类的构造方法,其余的返回价格 型号可以不用写(从父类继承)