手记

Java入门第二季6-1项目问题 —嗒嗒租车系统(参考各位大神的思路写出来的)

载客接口A-载客量
package car.rental;

public interface IPassengerCapacity {
public int PassengerCapacity(int i);
}
载货接口B-载货量
package car.rental;

public interface ICargoCapacity {
public int CargoCapacity(int i);
}
载客和货接口C extend 载客接口A,载货接口B(载客量、载货量)
package car.rental;

public interface IBoth extends IPassengerCapacity, ICargoCapacity {
public int PassengerCapacity(int i);
public int CargoCapacity(int i);
}
一个抽象父类Car
package car.rental;

public abstract class Car {
String type=null;
int pC=0;
int cC=0;
int price=0;
public abstract String VehicleType(int i);
public abstract int Price(int i);
}
实现-客车类 extend 父类Car implement 载客接口A
package car.rental;

public class Bus extends Car implements IPassengerCapacity {

@Override
public int PassengerCapacity(int i) {
    // TODO Auto-generated method stub
    int audiA4pC=4;
    int mazda6pC=4;
    int kingLongpC=20;
    switch(i){
    case 1:pC=audiA4pC;break;
    case 2:pC=mazda6pC;break;
    case 4:pC=kingLongpC;break;
    }
    return pC;
}

@Override
public  String VehicleType(int i) {
    // TODO Auto-generated method stub
    String audiA4="奥迪A4";
    String mazda6="马自达6";
    String kingLong="金龙";
    switch(i){
    case 1:type=audiA4;break;
    case 2:type=mazda6;break;
    case 4:type=kingLong;break;
    }
    return type;
}

@Override
public int Price(int i3) {
    // TODO Auto-generated method stub
    int audiA4Price=500;
    int mazda6Price=400;
    int kingLongPrice=800;
    switch(i3){
    case 1:price=audiA4Price;break;
    case 2:price=mazda6Price;break;
    case 4:price=kingLongPrice;break;
    }
    return price;
}

}
实现-皮卡类 extend 父类Car implement 载客和货接口C
package car.rental;

public class Pickup extends Car implements IPassengerCapacity, ICargoCapacity {

@Override
public int CargoCapacity(int i) {
    // TODO Auto-generated method stub
    int pickupSnow6=2;
    if(i==3){
        cC=pickupSnow6;
    }
    return cC;
}

@Override
public int PassengerCapacity(int i) {
    // TODO Auto-generated method stub
    int pickupSnow6pC=4;
    if(i==3){
        pC=pickupSnow6pC;
    }
    return pC;
}

@Override
public String VehicleType(int i) {
    // TODO Auto-generated method stub
    String pickupSnow6="皮卡雪6";
    if(i==3){
        type=pickupSnow6;
    }
    return type;
}

@Override
public int Price(int i) {
    // TODO Auto-generated method stub
    int pickupSnow6Price=450;
    if(i==3){
        price=pickupSnow6Price;
    }
    return price;
}

}
实现-卡车类 extend 父类Car implement 载货接口B
package car.rental;

public class Truck extends Car implements ICargoCapacity{
@Override
public int CargoCapacity(int i) {
// TODO Auto-generated method stub
int songHuaJiangcC=4;
int iveco=20;
if(i==5)cC=songHuaJiangcC;
if(i==6)cC=iveco;
return cC;
}

@Override
public String VehicleType(int i) {
    // TODO Auto-generated method stub
    String songHuaJiang="松花江";
    String iveco="依维柯";
    if(i==5)type=songHuaJiang;
    if(i==6)type=iveco;
    return type;
    }

@Override
public int Price(int i) {
    // TODO Auto-generated method stub
    int songHuaJiangPrice=400;
    int iveco=1000;
    if(i==5)price=songHuaJiangPrice;
    if(i==6)price=iveco;
    return price;
}

}
租车系统:客户选车、分类汇总车辆后,输出租车明细与总价
package car.rental;
import java.util.Scanner;
public class RentSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用嗒嗒租车系统:");
System.out.println("您是否要租车(请输入序号):1.是 2.否");
String[] car={"序号 租车名称 租金 容量",
"1. 奥迪A4 500/天 载人:4人",
"2. 马自达6 400/天 载人:4人",
"3. 皮卡雪6 450/天 载人4人载货2吨",
"4. 金龙 800/天 载人:20人",
"5. 松花江 400/天 载货:4吨",
"6. 依维柯 1000/天 载货:20吨"};
String type="";

    int sumP=0;
    double sumC=0;
    double sumPrice=0;
    String[] typeP={"","","","","","","","","","","",""};
    String[] typeC={"","","","","","","","","","","",""};
    Scanner input=new Scanner(System.in);
    Bus busRent=new Bus();
    Pickup pickupRent=new Pickup();
    Truck truckRent=new Truck();
    int i1=input.nextInt();
    try{for(int i=1;;i++){
            if(i1!=1&&i1!=2){
            System.out.println("您的输入有误,请重新输入:");
            System.out.println("您是否要租车(请输入序号):1.是 2.否");
            i1=input.nextInt();
            }else
                break;
            }
        if(i1==1){
            System.out.println("您可租车的类型及其价目表:");
            for(String cars:car){
                System.out.println(cars);
        }
            System.out.println("请输入您要租汽车的数量:");
            int i2=input.nextInt();
            for(int j=1;j<=i2;j++){
                System.out.println("请输入您要租的第"+j+"辆车的序号:");
                int i3=input.nextInt();
                int pC=0;
                int cC=0;
                int price=0;
                switch(i3){
                case 1 :
                case 2 :
                case 4 :{
                 type=busRent.VehicleType(i3);
                 price=busRent.Price(i3);
                 pC=busRent.PassengerCapacity(i3);
                 }break;
                case 3 :{
                 type=pickupRent.VehicleType(i3);
                 price=pickupRent.Price(i3);
                 pC=pickupRent.PassengerCapacity(i3);
                 cC=pickupRent.CargoCapacity(i3);
                 }break;
                case 5:
                case 6:{
                 type=truckRent.VehicleType(i3);
                 price=truckRent.Price(i3);
                 cC=truckRent.CargoCapacity(i3);
                 }break;
                }
                switch(i3){
                case 1:typeP[0]=type;break;
                case 2:typeP[1]=type;break;
                case 3:typeP[2]=type;typeC[0]=type;break;
                case 4:typeP[3]=type;break;
                case 5:typeC[1]=type;break;
                case 6:typeC[2]=type;break;
                }
                sumP=pC+sumP;
                sumC=cC+sumC;
                sumPrice=price+sumPrice;
            }
            System.out.println("请输入租车天数:");
            int rentDays=input.nextInt();
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            for(String types:typeP){
                System.out.print(types+"  ");
            }
            System.out.println("    共载人:"+sumP+"人");
            System.out.println("***可载货的车有:");
            for(String types:typeC){
                System.out.print(types+"  ");
            }
            System.out.println("    共载货:"+sumC+"吨");
            System.out.println("租车总价格:"+rentDays*sumPrice+"元");
            System.out.print("感谢使用!");
            }else{System.out.println("感谢使用!欢迎再来使用!");               
        }
            }catch(Exception e){
        e.printStackTrace();
        System.out.println("程序运行异常!请检查输入是否有误!");
    }
}

}

运行结果:
欢迎使用嗒嗒租车系统:
您是否要租车(请输入序号):1.是 2.否
1
您可租车的类型及其价目表:
序号 租车名称 租金 容量

  1. 奥迪A4 500/天 载人:4人
  2. 马自达6 400/天 载人:4人
  3. 皮卡雪6 450/天 载人4人载货2吨
  4. 金龙 800/天 载人:20人
  5. 松花江 400/天 载货:4吨
  6. 依维柯 1000/天 载货:20吨
    请输入您要租汽车的数量:
    4
    请输入您要租的第1辆车的序号:
    1
    请输入您要租的第2辆车的序号:
    2
    请输入您要租的第3辆车的序号:
    3
    请输入您要租的第4辆车的序号:
    4
    请输入租车天数:
    3
    您的账单:
    可载人的车有:
    奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人
    可载货的车有:
    皮卡雪6 共载货:2.0吨
    租车总价格:6450.0元
    感谢使用!
11人推荐
随时随地看视频
慕课网APP