Java学习第二季最后的练习 答答租车系统

307 查看

刚学完第二季,准备测试下自己学习的如何,花了不少时间写下了这个练习.总的来说结构还是很简单.我抽象定义了一个Car类带有私有属性车的类型和价格,和两个接口,一个是载客接口,一个是载货接口.然后客车,货车,皮卡继承Car类,并按要求引用特定接口.
结构上很容易理清楚.我觉得尤其耗时间的还是主程序,没什么算法,但是代码要敲不少.
下面和大家分享一下我的代码.
package com.mengtan;
//Car类
public abstract class Car {
    private String type;//车的类型
    private int price;//租金
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }   
}
package com.mengtan;
//载客接口
public interface IPassenger {
    public void setPassengerNum(int passengerNum);
    public int getPassengerNum();
}
package com.mengtan;
//载货接口
public interface ICargo {
    public void setCargoNum(int cargoNum);
    public int getCargoNum();
}
package com.mengtan;
//客车类,只能载客不能载货
public class BusCar extends Car implements IPassenger {
    private int passengerNum;
    public BusCar(String type,int price,int passengerNum){
        this.setType(type);
        this.setPrice(price);
        this.setPassengerNum(passengerNum);
    }
    @Override
    public void setPassengerNum(int passengerNum) {
        // TODO 自动生成的方法存根
        this.passengerNum=passengerNum;
    }

    @Override
    public int getPassengerNum() {
        // TODO 自动生成的方法存根
        return passengerNum;
    }

}
package com.mengtan;

public class GoodsCar extends Car implements ICargo {
    private int cargoNum;
    public GoodsCar(String type,int price,int cargoNum){
        this.setType(type);
        this.setPrice(price);
        this.setCargoNum(cargoNum);
    }
    @Override
    public void setCargoNum(int cargoNum) {
        // TODO 自动生成的方法存根
        this.cargoNum=cargoNum;
    }

    @Override
    public int getCargoNum() {
        // TODO 自动生成的方法存根
        return cargoNum;
    }

}
package com.mengtan;

public class PickupCar extends Car implements ICargo, IPassenger {
    private int passengerNum;
    private int cargoNum;
    public PickupCar(String type,int price,int passengerNum,int cargoNum){
        this.setType(type);
        this.setPrice(price);
        this.setPassengerNum(passengerNum);
        this.setCargoNum(cargoNum);
    }
    @Override
    public void setPassengerNum(int passengerNum) {
        // TODO 自动生成的方法存根
        this.passengerNum=passengerNum;
    }

    @Override
    public int getPassengerNum() {
        // TODO 自动生成的方法存根
        return passengerNum;
    }

    @Override
    public void setCargoNum(int cargoNum) {
        // TODO 自动生成的方法存根
        this.cargoNum=cargoNum;
    }

    @Override
    public int getCargoNum() {
        // TODO 自动生成的方法存根
        return cargoNum;
    }

}
package com.mengtan;

import java.util.Scanner;

//测试类
public class Test {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        String name1="";
        String name2="";
        String name3="";
        String name4="";
        String name5="";
        String name6="";
        int sumPassenger=0;
        double sumCargo=0;
        double sumPrice=0;
        System.out.println("欢迎来到答答租车系统");
        BusCar bCar1=new BusCar("小型客车",500,45);
        BusCar bCar2=new BusCar("大型客车",1000,100);
        GoodsCar gCar1=new GoodsCar("小型货车",400,2);
        GoodsCar gCar2=new GoodsCar("大型货车",800,5);
        PickupCar pCar1=new PickupCar("小型皮卡",600,2,2);
        PickupCar pCar2=new PickupCar("大型皮卡",1200,5,5);
        Scanner input=new Scanner(System.in);
        System.out.println("若您是租车用户请输入1,否则输入0!");
        int choice;
        choice=input.nextInt();
        for(;;){

            if(choice==0)break;//退出租车服务
            else if(choice==1){//进行租车服务
                //打印所有车辆信息
                System.out.println("您可租车类型和价目表:");
                System.out.println("序号\t汽车类型\t租金\t容量");
                System.out.println(1+"\t"+bCar1.getType()+"\t"+bCar1.getPrice()+"元/天"+"\t"+bCar1.getPassengerNum()+"人");
                System.out.println(2+"\t"+bCar2.getType()+"\t"+bCar2.getPrice()+"元/天"+"\t"+bCar2.getPassengerNum()+"人");
                System.out.println(3+"\t"+gCar1.getType()+"\t"+gCar1.getPrice()+"元/天"+"\t"+gCar1.getCargoNum()+"吨");
                System.out.println(4+"\t"+gCar2.getType()+"\t"+gCar2.getPrice()+"元/天"+"\t"+gCar2.getCargoNum()+"吨");
                System.out.println(5+"\t"+pCar1.getType()+"\t"+pCar1.getPrice()+"元/天"+"\t"+pCar1.getPassengerNum()+"人"+"\t"+pCar1.getCargoNum()+"吨");
                System.out.println(6+"\t"+pCar2.getType()+"\t"+pCar2.getPrice()+"元/天"+"\t"+pCar2.getPassengerNum()+"人"+"\t"+pCar2.getCargoNum()+"吨");
                System.out.println("接下来您将进行租车服务,如果租车完成请输入数字0结束!");
                for(int i=1;;i++){
                    System.out.println("请输入第"+i+"辆车的序号:");
                    int b=input.nextInt();
                    if(b==0)break;
                    switch(b){
                    case 1: name1=bCar1.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            int Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            int Tnum=input.nextInt();
                            sumPassenger+=bCar1.getPassengerNum()*Lnum;
                            sumPrice+=bCar1.getPrice()*Lnum*Tnum;
                            break;
                    case 2: name2=bCar2.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            Tnum=input.nextInt();
                            sumPassenger+=bCar2.getPassengerNum()*Lnum;
                            sumPrice+=bCar2.getPrice()*Lnum*Tnum;
                            break;
                    case 3: name3=gCar1.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            Tnum=input.nextInt();
                            sumCargo+=gCar1.getCargoNum()*Lnum;
                            sumPrice+=gCar1.getPrice()*Lnum*Tnum;
                            break;
                    case 4: name4=gCar2.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            Tnum=input.nextInt();
                            sumCargo+=gCar2.getCargoNum()*Lnum;
                            sumPrice+=gCar2.getPrice()*Lnum*Tnum;
                            break;
                    case 5: name5=pCar1.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            Tnum=input.nextInt();
                            sumPassenger+=pCar1.getPassengerNum()*Lnum;
                            sumCargo+=pCar1.getCargoNum()*Lnum;
                            sumPrice+=pCar1.getPrice()*Lnum*Tnum;
                            break;
                    case 6: name5=pCar2.getType();
                            System.out.println("请输入该车型您要租多少辆:");
                            Lnum=input.nextInt();
                            System.out.println("请输入该车型您要租多少天:");
                            Tnum=input.nextInt();
                            sumPassenger+=pCar2.getPassengerNum()*Lnum;
                            sumCargo+=pCar2.getCargoNum()*Lnum;
                            sumPrice+=pCar2.getPrice()*Lnum*Tnum;
                            break;
                    }
                }
            }else{
                System.out.println("输入错误,请重新输入!");
                continue;
            }
            System.out.println("如果租车完毕,请输入0完成租车!");
            choice=input.nextInt();
        }
        System.out.println("租车成功,您的账单如下:");
        System.out.println("您租的车型有:"+name1+"\t"+name2+"\t"+name3+"\t"+name4+"\t"+name5+"\t"+name6+"\t"+"共载人:"+sumPassenger+"人"+"\t"+"共载货:"+sumCargo+"吨");
        System.out.println("租车总金额:"+sumPrice);
        System.out.println("欢饮使用答答租车系统!!再见");
    }
}

代码还有瑕疵,可以尽管提出来,大家一起互相学习.