手记

答答租车系统-感觉写的好乱啊,还有很多异常都没处理,等着我再写个第二版

package com.meituan.qa.zonghelianxi;

/**
 * Created by sunfang on 2017/10/6.
 */
public abstract class Car {
     int carType;
     float pricePerDay;
     String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCarType() {
        return carType;
    }

    public void setCarType(int carType) {
        this.carType = carType;
    }

    public float getPricePerDay() {
        return pricePerDay;
    }

    public void setPricePerDay(float pricePerDay) {
        this.pricePerDay = pricePerDay;
    }
    public abstract void showInfo();

}
package com.meituan.qa.zonghelianxi;

/**
 * Created by sunfang on 2017/10/6.
 */
public class Truck extends Car{
    private int cargoNum;

    public int getCargoNum(){
        return cargoNum;
    }
    public void setCargoNum(int cargoNum){
        this.cargoNum=cargoNum;
    }

    public Truck(int cargoNum,int carType,float pricePerDay,String name){
    this.cargoNum=cargoNum;
    this.carType=carType;
    this.pricePerDay=pricePerDay;
    this.name=name;
    }

    public Truck(){

    }

    @Override
    public  void showInfo(){
        System.out.println(this.getName()+"\t"+this.getPricePerDay()+"\t"+"载货:"+getCargoNum());
    }

}
package com.meituan.qa.zonghelianxi;

import com.meituan.qa.springtest.Cat;
import netscape.security.PrivilegeTable;

/**
 * Created by sunfang on 2017/10/6.
 */
public class PrivateCar extends Car {
    private int passengersNum;

    public int getPassengersNum() {
        return passengersNum;
    }

    public void setPassengersNum(int passengersNum) {
        this.passengersNum = passengersNum;
    }

    public PrivateCar(int passengersNum,int carType,float pricePerDay,String name){
        this.passengersNum=passengersNum;
        this.carType=carType;
        this.pricePerDay=pricePerDay;
        this.name=name;
    }
    public PrivateCar(){}

    @Override
    public  void showInfo(){
        System.out.println(this.getName()+"\t"+this.getPricePerDay()+"\t"+"载客:"+getPassengersNum());
    }

}
package com.meituan.qa.zonghelianxi;

/**
 * Created by sunfang on 2017/10/6.
 */
public class PickUp extends Car {
    private int cargoNum;
    private int passengersNum;

    public int getCargoNum() {
        return cargoNum;
    }

    public void setCargoNum(int cargoNum) {
        this.cargoNum = cargoNum;
    }

    public int getPassengersNum() {
        return passengersNum;
    }

    public void setPassengersNum(int passengersNum) {
        this.passengersNum = passengersNum;
    }

    public PickUp(int cargoNum,int passengersNum,int carType,float pricePerDay,String name){
        this.cargoNum=cargoNum;
        this.passengersNum=passengersNum;
        this.carType=carType;
        this.pricePerDay=pricePerDay;
        this.name=name;
    }
    public PickUp(){}

    @Override
    public  void showInfo(){
        System.out.println(this.getName()+"\t"+this.getPricePerDay()+"\t"+"载货:"+this.getCargoNum()+" 载客:"+this.getPassengersNum());
    }

}
package com.meituan.qa.zonghelianxi;

import java.util.Scanner;

/**
 * Created by sunfang on 2017/10/6.
 */
public class RentCarSystem {
    public static void main(String[] args) {
        System.out.println("欢迎使用答答租车系统:\n"+"您是否要租车:1是 0否");
        float rentMoney=0;

        Scanner scanner=new Scanner(System.in);
//        for (int i=0;i<10;i++)
        int input=scanner.nextInt();

        Car[] cars={new Truck(10,1,800,"货车-松花江"),new PrivateCar(4,2,500,"轿车-奥迪A4"),new PickUp(5,2,3,600,"皮卡雪")};

        if (input==1){
            System.out.println("您可租车的类型及价目表:");
            System.out.println("序号\t"+"汽车名称\t\t"+"租金\t\t"+"容量");
            for (int i=0;i<cars.length;i++){
                System.out.print((i+1)+".\t");
                cars[i].showInfo();
            }
        }
        else if (input==0){
        }
        else{
            System.out.println("您输入的选择有误!");
        }

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

        int rentCarNum=scanner.nextInt();
        Car[] selectCars=new Car[rentCarNum];

        for (int i=0;i<rentCarNum;i++){
          System.out.println("请输入第"+(i+1)+"辆车的序号:");
          int rentCarNo=scanner.nextInt();
          selectCars[i]=cars[rentCarNo-1];

          rentMoney=selectCars[i].pricePerDay+rentMoney;
        }

        System.out.println("请输入租车天数:");
        int rentDays=scanner.nextInt();

        //计算所选车及价格
        int passengersAllNum=0;
        int cargoAllNum=0;
        System.out.println("您的账单:");
        System.out.println("---可载人的车有:");
        for (Car car:selectCars) {
            if (car.carType==2){
                System.out.print(car.getName()+" ");
                passengersAllNum=((PrivateCar)car).getPassengersNum()+passengersAllNum;
            }
            else if (car.carType==3){
                System.out.print(car.getName()+" ");
                passengersAllNum=((PickUp)car).getPassengersNum()+passengersAllNum;
            }
        }
        System.out.println("共载人:"+passengersAllNum);

        System.out.println("---可载货的车有:");
        for (Car car:selectCars) {
            if (car.carType==1){
                System.out.print(car.getName()+" ");
                cargoAllNum=((Truck)car).getCargoNum()+cargoAllNum;
            }
           else if(car.carType==3){
                System.out.print(car.getName()+" ");
                cargoAllNum=((PickUp)car).getCargoNum()+cargoAllNum;
            }
        }
        System.out.println("共载货:"+cargoAllNum);

        rentMoney=rentMoney*rentDays;
        System.out.println("---租车总价格为"+rentMoney+"元");

    }
}
0人推荐
随时随地看视频
慕课网APP

热门评论

感谢,受益颇多!期待博主下一版

写的比较乱,但是是严格按照题目要求来的,运行结果也是一模一样

查看全部评论