问答详情
源自:12-1 综合练习

还有什么需要改进的地方吗(租车公司)

package com.car;

//主要计算

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class Calculation {

public static void rest() {

System.out.println("欢迎进入答答租车责任有限公司!");

System.out.println("您是否需要租车? 1:yes,0:no");

Scanner scan = new Scanner(System.in);

int t = scan.nextInt();

if (t == 1) {

Calculation.cal();

} else if (t == 0) {

System.out.println("欢迎您的下次光临!");

} else {

System.out.println("您输入的信息有误!");

Calculation.rest();

}


}


public static void cal() {

Car[] cars = { new PassengerCar(1, "奥迪A4", 500, 4),

new PassengerCar(2, "马自达6", 400, 4),

new PassengerCar(3, " 金龙 ", 800, 20),

new PickUp(4, "皮卡雪6", 450, 4, 2),

new CarryCar(5, "松花江", 400, 2, 4),

new CarryCar(6, "依维柯", 1000, 2, 20) };

Scanner scan = new Scanner(System.in);


System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "载人"

+ "\t" + "容量");

for (Car car : cars) {

// System.out.println(car.toString());

if (car instanceof PassengerCar) {

// 若它是客车类型的实例,则打印出客车应该有的属性值,这里注意强转,否则不会出现

// passengerCar类的CarCapacity属性

System.out.println(car.getCarNumber() + "\t" + car.getCarName()

+ "\t" + car.getCarRent() + "/天" + "\t"

+ ((PassengerCar) car).getCarPassenger() + "人");

}

if (car instanceof PickUp) {

System.out.println(car.getCarNumber() + "\t" + car.getCarName()

+ "\t" + car.getCarRent() + "/天" + "\t"

+ ((PickUp) car).getCarPassenger() + "人" + "\t"

+ ((PickUp) car).getCarCarrying() + "吨");

}

if (car instanceof CarryCar) {

System.out.println(car.getCarNumber() + "\t" + car.getCarName()

+ "\t" + car.getCarRent() + "/天" + "\t"

+ ((CarryCar) car).getCarPassenger() + "人" + "\t"

+ ((CarryCar) car).getCarCarrying() + "吨");

}


}

System.out.println("请输入您要租用的汽车数量: ");

int talNum = scan.nextInt();// 租用数量

int talRent = 0;// 租用金额

int peoples = 0;// 载人数量

int carry = 0;// 载货数量

List<Car> carLists = new ArrayList<Car>();

List<Car> pc = new ArrayList<Car>();

List<Car> cc = new ArrayList<Car>();

for (int i = 0; i < talNum; i++) {

System.out.println("请输入您需要租用的第" + (i + 1) + "辆车的序号:");

int num = scan.nextInt();

if (num <= cars.length) {

System.out.println("1");

carLists.add(cars[num - 1]);

System.out.println("成功添加:" + carLists.get(i).carName);// 获取车的名称

talRent += carLists.get(i).carRent;

if (carLists.get(i) instanceof PassengerCar) {

pc.add(cars[num - 1]);

peoples += ((PassengerCar) carLists.get(i)).carPassenger;

}

if (carLists.get(i) instanceof CarryCar) {

cc.add(cars[num - 1]);

carry += ((CarryCar) carLists.get(i)).carCarrying;

}

if (carLists.get(i) instanceof PickUp) {

pc.add(cars[num - 1]);

cc.add(cars[num - 1]);

peoples += ((PickUp) carLists.get(i)).carPassenger;

carry += ((PickUp) carLists.get(i)).carCarrying;

}

// System.out.println(carLists.toString());

// System.out.println(carLists.get(0).carRent);


} else {

System.out.println("输入的数据有误!");

i--;

continue;

}

}

System.out.println("请输入您租用的天数:");

int day = scan.nextInt();

if (pc.size() > 0) {

System.out.print("***可载人的车有: ");

for (Car car : pc) {

System.out.print(car.carName + " ");

}

System.out.println(" 可载人总数: " + peoples);

System.out.println();

}

if (cc.size() > 0) {

System.out.print("***可载货的车有: ");

for (Car car : cc) {

System.out.print(car.carName + " ");

}

System.out.println(" 可载货总数: " + carry);

System.out.println();

}

System.out.println("租车所用总金额: " + talRent * day);


}

}


//car类

package com.car;


public class Car {

public int carNumber;

public String carName;

public int carRent;


public Car(int i, String string, int j) {

// TODO Auto-generated constructor stub

this.carNumber = i;

this.carName = string;

this.carRent = j;

}


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 getCarRent() {

return carRent;

}


public void setCarRent(int carRent) {

this.carRent = carRent;

}


@Override

public String toString() {

return "[carName=" + carName + ", carNumber=" + carNumber

+ ", carRent=" + carRent + "]";

}


}

//货车类

package com.car;


public class CarryCar extends Car {

public int carPassenger;

public int carCarrying;


public CarryCar(int num, String name, int monery, int passenger,

int carrying) {

super(num, name, monery);

// TODO Auto-generated constructor stub

this.carNumber = num;

this.carName = name;

this.carRent = monery;

this.carPassenger = passenger;

this.carCarrying = carrying;

}


public int getCarPassenger() {

return carPassenger;

}


public void setCarPassenger(int carPassenger) {

this.carPassenger = carPassenger;

}


public int getCarCarrying() {

return carCarrying;

}


public void setCarCarrying(int carCarrying) {

this.carCarrying = carCarrying;

}


@Override

public String toString() {

return carNumber + " " + carName + " " + carRent + " " + carPassenger

+ " " + carCarrying;

}


}

//载人客车

package com.car;


public class PassengerCar extends Car {

public int carPassenger;


public PassengerCar(int num, String name, int monery, int passenger) {

super(num, name, monery);

// TODO Auto-generated constructor stub

this.carNumber = num;

this.carName = name;

this.carRent = monery;

this.carPassenger = passenger;

}


public int getCarPassenger() {

return carPassenger;

}


public void setCarPassenger(int carPassenger) {

this.carPassenger = carPassenger;

}


@Override

public String toString() {

return carNumber + " " + carName + " " + carRent + " " + carPassenger;

}


}

//皮卡

package com.car;


public class PickUp extends Car {

public int carPassenger;

public int carCarrying;


public PickUp(int num,String name, int monery,int passenger,int carrying) {

super (num,name,monery);

// TODO Auto-generated constructor stub

this.carNumber = num;

this.carName = name;

this.carRent = monery;

this.carPassenger = passenger;

this.carCarrying = carrying;

}


public int getCarPassenger() {

return carPassenger;

}


public void setCarPassenger(int carPassenger) {

this.carPassenger = carPassenger;

}


public int getCarCarrying() {

return carCarrying;

}


public void setCarCarrying(int carCarrying) {

this.carCarrying = carCarrying;

}


@Override

public String toString() {

return carNumber + " " + carName + " " + carRent + " " + carPassenger

+ " " + carCarrying;

}

}

//main

package com.car;


import java.util.Scanner;



public class Test {


public static void main(String[] args) {

System.out.println("欢迎进入答答租车责任有限公司!");

System.out.println("您是否需要租车? 1:yes,0:no");

Scanner scan = new Scanner(System.in);

int t = scan.nextInt();

if (t == 1) {

Calculation.cal();

}else if (t == 0) {

System.out.println("欢迎您的下次光临!");

} else {

System.out.println("您输入的信息有误!");

Calculation.rest();

}

}

}


提问者:独在阳光下 2016-03-05 14:26

个回答

  • 一条小咸鱼
    2016-03-05 15:00:08

    考虑一下你使用的数据类型。价格和载重用double不是更好