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

参考其他同学以后写的...(api我还不太理解, 所以就没使用)

//汽车父类
public abstract class Car {
	//	基本变量
	int number; // 汽车序号
	String name; //	汽车名称
	double cargoCap;//	载货量
	int peopleCap; //	载人量
	int rent; //	租金	
	
	//基本方法
	public String getName() {
		//获得名称
		return name;
	}
	public int getRent() {
		//获得租金
		return rent;
	}
	public double getCargo(){
		//获得载货量
		return cargoCap;
	}
	public int getPeople() {
		//获得载人量
		return peopleCap;
	}
	public int getNumber() {
		//获得车辆序号
		return number;
	}
	
	public abstract String getType(); //获得车辆类型的抽象方法
	
	public String carInfo(){
		// 获得可阅读的车辆信息
		String result = this.getNumber() + ". 汽车名称: " + this.getName() + ", 汽车类型: " + this.getType() +
				", 租金: " + this.getRent() +"元/天, "; 
		if (this.getType() == "轿车") {
			result += "载人数: " + this.getPeople() + "人";
		} else if (this.getType() == "货车") {
			result += "载货量: " + this.getCargo() + "顿";
		} else {
			result += "载人数: " + this.getPeople() + "人, 载货量: " + this.getCargo() + "顿";
		}
		return result;
	}
	
	public String toString() {
		//以车辆名称覆盖默认print方式
		return this.name;
	}
	
}


//货车子类
public class Truck extends Car {
	public Truck(int number, String name, int rent, double cargoCap) {
		this.number = number;
		this.name = name;
		this.rent = rent;
		this.cargoCap = cargoCap;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "货车";
	}

	
//轿车子类
public class Normal extends Car {
	public Normal(int number, String name, int rent, int peopleCap) {
		this.number = number;
		this.name = name;
		this.rent = rent;
		this.peopleCap = peopleCap;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "轿车";
	}

}


//皮卡子类
public class PickUp extends Car {
	public PickUp(int number, String name, int rent, int peopleCap, double cargoCap){
		this.number = number;
		this.name = name;
		this.rent = rent;
		this.peopleCap = peopleCap;
		this.cargoCap = cargoCap;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "皮卡";
	}

}


import java.util.Scanner;

// main方法
public class init {

	public static void main(String[] args) {
		// 所有可租车辆
		Car[] allRent = {new Normal(1, "奥迪A4", 500, 4),
						new Normal(2, "马自达6", 400, 4),
						new PickUp(3, "皮卡雪6", 450, 4, 2.0),
						new Normal(4, "金龙", 800, 20),
						new Truck(5, "松花江", 400, 4.0),
						new Truck(6, "依维河", 1000, 20.0)};
		
		System.out.println("欢迎光临didi打车");
		Scanner input = new Scanner(System.in);
		int choice;
		do {
			System.out.println("是否继续? 0:退出, 1:继续");
			choice = input.nextInt();
		}
        while (choice != 0 && choice != 1);        	
        if (choice == 0) {
        	System.out.println("很遗憾, 期待您的下次光临");
        	return;
       	}
       	System.out.println("我们提供如下车辆:");
		for (int i = 0; i < allRent.length; i++) {
			System.out.println(allRent[i].carInfo());
		}
		System.out.println("请问您一共需要几辆汽车? (请输入数字)");
		int numberCar = input.nextInt();
		Car[] picking = new Car[numberCar];
		for (int i = 0; i < numberCar; i++) {
			do { 
				System.out.println("请输入您的第 " + (i + 1) + " 辆车的序号? "
						+ "(请输入1-" + allRent.length + ")");
				choice = input.nextInt();
			}
			while (choice < 1 || choice > 6);
			picking[i] = allRent[choice - 1];
		}
		System.out.println("您需要使用的天数: ");
		int days = input.nextInt();
		int totalRent = 0, totalCargo = 0, totalPeople = 0;
		System.out.println("========您的租赁信息如下: ========");
		System.out.println("您的租赁天数为: " + days + "天");
		System.out.print("租赁的车辆: ");
		for (int i = 0; i < numberCar; i++) {
			System.out.print(picking[i] + " ");
			totalRent += days * picking[i].getRent();
			totalCargo += picking[i].getCargo();
			totalPeople += picking[i].getPeople();
		}
		System.out.println("");
		System.out.println("总计租金为: " + totalRent + "元");
		System.out.println("总计载人数为: " + totalPeople + "人");
		System.out.println("总计载货量为: " + totalCargo + "顿");
		System.out.println("谢谢使用租车系统, 欢迎下次光临!");
		return;
	}

}


正在回答

1 回答

这样写是没有问题的,把货车,客车的属性都归为车的类型。如何需要用接口细分的话,可以将货车的属性与客车的属性分为两个interface,然后分别去继承这些接口。这样使整体的代码看起来耦合比较松。

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

举报

0/150
提交
取消

参考其他同学以后写的...(api我还不太理解, 所以就没使用)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信