怎么让数组变成动态的,插入删除数据动态增减大小。

//Car
package com.Car;

public  class Car {
	String name;
	double rent;
	public void Car(String name,double rent){};
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getRent() {
		return rent;
	}
	public void setRent(double rent) {
		this.rent = rent;
	}
	
}
//PassengerCar
package com.Car;

public class PassengerCar extends Car {
	private double peopleC;
	public PassengerCar(String name,double rent,double peopleC){
		this.name = name;
		this.rent = rent;
		this.peopleC = peopleC;
	}
	public double getPeopleC() {
		return peopleC;
	}
	public void setPeopleC(double peopleC) {
		this.peopleC = peopleC;
	}
	
}

//Pickup 
package com.Car;

public  class Pickup extends Car{
	private double peopleC;
	private double cargoC;
	public Pickup(String name,double rent,double cargoC,double peopleC) {
		this.name = name;
		this.rent = rent;
		this.cargoC = cargoC;
		this.peopleC = peopleC;
	}
	public double getCargoC() {
		return cargoC;
	}
	public void setCargoC(double cargoC) {
		this.cargoC = cargoC;
	}
	public double getPeopleC() {
		return peopleC;
	}
	public void setPeopleC(double peopleC) {
		this.peopleC = peopleC;
	}
	
}

//Truck 
package com.Car;

public class Truck extends Car {
	private double cargoC;
	public Truck(String name,double rent,double cargoC) {
		this.name = name;
		this.rent = rent;
		this.cargoC = cargoC;
	}
	public double getCargoC() {
		return cargoC;
	}
	public void setCargoC(double cargoC) {
		this.cargoC = cargoC;
	}
	
}

package com.Car;
import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		Car[] cars = { new PassengerCar("金龙大巴", 1000, 40),
				new PassengerCar("奥迪A6", 500, 4), new Pickup("小皮卡", 300, 5, 4),
				new Truck("大卡车", 800, 25) };
		Scanner Type = new Scanner(System.in);
		System.out.println("欢迎使用答答租车系统");
		System.out.println("您是否要租车:1是 0否");
		String input = Type.next();
		if (input.equals("1")) {
			System.out.println("您可租车的类型及价目表:");
			System.out.println("序号\t汽车名称\t租金\t\t容量");
			int i = 1;// 车辆序号
			for (Car Allcars : cars) {
				if (Allcars instanceof PassengerCar) {
					System.out.println(i + "\t" + Allcars.getName() + "\t"
							+ Allcars.getRent() + "/天\t载人:"
							+ ((PassengerCar) Allcars).getPeopleC() + "/人");
					i += 1;
				}
				if (Allcars instanceof Pickup) {
					System.out.println(i + "\t" + Allcars.getName() + "\t"
							+ Allcars.getRent() + "/天\t载人:"
							+ ((Pickup) Allcars).getPeopleC() + "/人 载货:"
							+ ((Pickup) Allcars).getCargoC() + "吨");
					i += 1;
				}
				if (Allcars instanceof Truck) {
					System.out.println(i + "\t" + Allcars.getName() + "\t"
							+ Allcars.getRent() + "/天\t载货:"
							+ ((Truck) Allcars).getCargoC() + "吨");
					i += 1;
				}
			}

			System.out.println("请输入您要租车的数量:");
			int num = Type.nextInt();
			double rentsum = 0;// 租车总金额
			double Peosum = 0;// 载人总数
			double CargoCsum = 0;// 载货总数
			String[] PassengerName = new String[4];// 存放可载人车辆的name的数组
			String[] CargoCars = new String[4];// 存放可载货车辆的name的数组
			int p = 0;
			int c = 0;
			for (int n = 1; n <= num; n++) {
				System.out.println("输入第" + n + "辆车的序号:");
				int x = Type.nextInt();
				rentsum += cars[x - 1].rent;// 计算租车金额
				switch (x - 1) {
				case 0:
					if (cars[x - 1] instanceof PassengerCar) {
						PassengerName[p] = cars[x - 1].name;
						Peosum += ((PassengerCar) cars[x - 1]).getPeopleC();
						p += 1;
					}
					if (cars[x - 1] instanceof Pickup) {
						PassengerName[p] = cars[x - 1].name;
						CargoCars[c] = cars[x - 1].name;
						Peosum += ((Pickup) cars[x - 1]).getPeopleC();
						CargoCsum += ((Pickup) cars[x - 1]).getCargoC();
						p += 1;
						c += 1;
					}
					if (cars[x - 1] instanceof Truck) {
						CargoCars[c] = cars[x - 1].name;
						CargoCsum += ((Truck) cars[x - 1]).getCargoC();
						c += 1;
					}
					break;
				case 1:
					if (cars[x - 1] instanceof PassengerCar) {
						PassengerName[p] = cars[x - 1].name;
						Peosum += ((PassengerCar) cars[x - 1]).getPeopleC();
						p += 1;
					}
					if (cars[x - 1] instanceof Pickup) {
						PassengerName[p] = cars[x - 1].name;
						CargoCars[c] = cars[x - 1].name;
						Peosum += ((Pickup) cars[x - 1]).getPeopleC();
						CargoCsum += ((Pickup) cars[x - 1]).getCargoC();
						p += 1;
						c += 1;
					}
					if (cars[x - 1] instanceof Truck) {
						CargoCars[c] = cars[x - 1].name;
						CargoCsum += ((Truck) cars[x - 1]).getCargoC();
						c += 1;
					}
					break;
				case 2:
					if (cars[x - 1] instanceof PassengerCar) {
						PassengerName[p] = cars[x - 1].name;
						Peosum += ((PassengerCar) cars[x - 1]).getPeopleC();
						p += 1;
					}
					if (cars[x - 1] instanceof Pickup) {
						PassengerName[p] = cars[x - 1].name;
						CargoCars[c] = cars[x - 1].name;
						Peosum += ((Pickup) cars[x - 1]).getPeopleC();
						CargoCsum += ((Pickup) cars[x - 1]).getCargoC();
						p += 1;
						c += 1;
					}
					if (cars[x - 1] instanceof Truck) {
						CargoCars[c] = cars[x - 1].name;
						CargoCsum += ((Truck) cars[x - 1]).getCargoC();
						c += 1;
					}
					break;
				case 3:
					if (cars[x - 1] instanceof PassengerCar) {
						PassengerName[p] = cars[x - 1].name;
						Peosum += ((PassengerCar) cars[x - 1]).getPeopleC();
						p += 1;
					}
					if (cars[x - 1] instanceof Pickup) {
						PassengerName[p] = cars[x - 1].name;
						CargoCars[c] = cars[x - 1].name;
						Peosum += ((Pickup) cars[x - 1]).getPeopleC();
						CargoCsum += ((Pickup) cars[x - 1]).getCargoC();
						p += 1;
						c += 1;
					}
					if (cars[x - 1] instanceof Truck) {
						CargoCars[c] = cars[x - 1].name;
						CargoCsum += ((Truck) cars[x - 1]).getCargoC();
						c += 1;
					}
					break;
				default:
					break;
				}
			}
			System.out.println("请输入租车天数:");
			int y = Type.nextInt();

			System.out.println("您的账单:");
			System.out.println("***可载人的车有:");
			for (String pn : PassengerName) {
				System.out.print(pn + "\t");
			}
			System.out.println("载人总数:" + Peosum + "人");

			System.out.println("***可载货的车有:");
			for (String cc : CargoCars) {
				System.out.print(cc + "\t");
			}
			System.out.println("载货总数:" + CargoCsum + "吨");

			rentsum = rentsum * y;
			System.out.println("***租车总金额:" + rentsum);
		} else if (input.equals("2")) {
			System.out.println("欢迎您访问答答租车系统,再见。");
		} else {
			System.out.println("输入错误!");
			main(null);// 再次调用主函数
		}
	}
}


lsnFor
浏览 1866回答 3
3回答

yanrun

先回答你的问题动态数组可以使用ArrayList。再说你的代码中有几个错误Car类的构造方法是不需要返回值的,构造方法后面的;也是没用的,还有就是Car类需要一个无参的构造方法,否则是无法通过编译的,你的输入提示是输入1和0,然而if判断的时候变成了1和2。最后提几个建议,变量名首字母最好是小写,Scanner类的对象type在最后最好调用close方法关闭资源,获得输入数据和计算价格的代码可以变成两个方法,这样可以提高程序的灵活性。希望对你有帮助。

cafebabe0x

相当凌乱,有待重构
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java