自己做了一天,有点慢
package easyCar;
import java.util.Scanner;
/*
* 功能输出
*
*
*/
public class EasycarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] car=new Car[10];
car[0]=new PassengerCar("奥迪A4",500,4);
car[1]=new PassengerCar("马自达6",400,4);
car[2]=new PickupCar("皮卡雪6",450,2,4);
car[3]=new PassengerCar("金龙 ",800,20);
car[4]=new Truck("松花江",400,4);
car[5]=new Truck("依维柯",1000,20);
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否租车 1 是 0 否");
Scanner scan=new Scanner(System.in);
int isSelect=scan.nextInt();
if(isSelect==1){
System.out.println("您可租车的类型及其价格表:");
System.out.println("序号\t汽车名称\t金额\t\t容量");
//调用车型列表
for(int i=0;i<6;i++){
System.out.print(" "+(i+1)+"\t");
car[i].print();
}
}else{
System.out.println("欢迎您再次光临!");
System.exit(0);
}
//执行客户要求,输入车型和天数
System.out.println("请输入您要租车的数量:");
int num=scan.nextInt(); //记录要车数量
//System.out.println(num);
int[] nums=new int[6]; //各车型租用辆数
double cost=0;
for(int i=1;i<=num;){
System.out.println("请输入您要的第"+i+"辆车的序号:");
int o=scan.nextInt();
for(;;){
if(o<1||o>6){
System.out.println("输入有误,重新输入:");
o=scan.nextInt();
}else{
break;
}
}
nums[i-1]=o-1; //获取车型
i++;
}
System.out.println("请输入租车天数:");
int days=scan.nextInt();
//打印账单
System.out.println("您的账单:");
System.out.println("可载人的车有:");
int people=0;
double sumWeight=0;
for(int i=0;i<num;i++){
if(car[nums[i]].getNum()!=0){
System.out.print(car[nums[i]].getCarName()+" ");
people+=car[nums[i]].getNum();
}
}
System.out.print("共载人:"+people+"人");
System.out.println();
System.out.println("可载货的车有:");
for(int i=0;i<num;i++){
if(car[nums[i]].getWeight()!=0){
System.out.print(car[nums[i]].getCarName()+" ");
sumWeight+=car[nums[i]].getWeight();
}
}
System.out.println("共载货:"+sumWeight+"吨");
System.out.println();
for(int i=0;i<num;i++){
cost+=car[nums[i]].getRent(); //每天费用
}
System.out.println("共计费用:"+cost*3+"元");
}
}
各个车自类:
package easyCar;
/**
*
* @author user
*
*/
public abstract class Car {
private String carName; //车名
private double rent; //每天租金
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public double getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public Car(String carName, int rent) {
super();
this.carName = carName;
this.rent = rent;
}
public void setNum(int num) {
}
public double getWeight() {
return 0;
}
public int getNum() {
return 0;
}
public void print(){
}
}
++++++++++++++++++++++++++++++++++++
package easyCar;
/*
* 载人的车
*
*/
public class PassengerCar extends Car {
private int num;//载人数量
private char[] getCarName;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public PassengerCar(String carName, int rent,int num) {
super(carName, rent);
// TODO Auto-generated constructor stub
this.num=num;
}
public void print(){
System.out.println(this.getCarName()+" "+this.getRent()+
"元/天"+" \t载人"+this.getNum());
}
}
****************************************************************************
package easyCar;
public class PickupCar extends Car {
private double weight; //载重(吨)
private int num;//载人数量
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public PickupCar(String carName, int rent,double weight,int num) {
super(carName, rent);
// TODO Auto-generated constructor stub
this.weight=weight;
this.num=num;
}
public void print(){
System.out.println(this.getCarName()+" "+this.getRent()+
"元/天"+"\t载重"+this.getWeight()+" 载人"+this.getNum());
}
}
************************************************************************************
package easyCar;
public class Truck extends Car {
private double weight; //载重(吨)
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Truck(String carName, int rent,double weight) {
super(carName, rent);
this.weight=weight;
// TODO Auto-generated constructor stub
}
public void print(){
System.out.println(this.getCarName()+"\t"+this.getRent()+
"元/天"+" \t载重"+this.getWeight());
}
}
**********************************************************
欢迎使用答答租车系统
您是否租车 1 是 0 否
1
您可租车的类型及其价格表:
序号 汽车名称 金额 容量
1 奥迪A4 500.0元/天 载人4
2 马自达6 400.0元/天 载人4
3 皮卡雪6 450.0元/天 载重2.0 载人4
4 金龙 800.0元/天 载人20
5 松花江 400.0元/天 载重4.0
6 依维柯 1000.0元/天 载重20.0
请输入您要租车的数量:
4
请输入您要的第1辆车的序号:
1
请输入您要的第2辆车的序号:
2
请输入您要的第3辆车的序号:
3
请输入您要的第4辆车的序号:
4
请输入租车天数:
3
您的账单:
可载人的车有:
奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人
可载货的车有:
皮卡雪6 共载货:2.0吨
共计费用:6450.0元