手记

java达达租车系统,初学者,希望大家能给些批评建议,共同学习

package com.edward;

public class Vehicle {//父类
int id,people;
String name;
float price,cargo;
}
public class PeopleCar extends Vehicle {//子类,载人车
PeopleCar(int id,String name,float price,int people){
this.id=id;
this.name=name;
this.price=price;
this.people=people;
}
}
public class CargoCar extends Vehicle {//子类,载货车
CargoCar(int id,String name,float price,float cargo) {
// TODO 自动生成的构造函数存根
this.id=id;
this.name=name;
this.price=price;
this.cargo=cargo;
}
}
public class BothCar extends Vehicle {//子类,两用车
BothCar(int id,String name,float price,int people,float cargo){
this.id=id;
this.name=name;
this.price=price;
this.people=people;
this.cargo=cargo;
}
}
package com.edward;
import java.util.Scanner;
public class DadaRentNew {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    int flag;//用于判断是否进入租车系统
    int carNum;//用于保存用户租车的数量
    int rentDay;//保存租车天数
    Vehicle cars[]={new PeopleCar(1, "奥迪A4", 500f, 4),new PeopleCar(2, "马自达6", 400f, 4),new BothCar(3, "皮卡雪6", 450f, 4, 2f),new PeopleCar(4, "金龙", 800f, 20),new CargoCar(5, "松花江", 400f, 4f),new CargoCar(6, "依维柯", 1000f, 20f)};//创建各类车型的对象
    System.out.print("欢迎使用达达租车系统:\n您是否要租车? 1(是)0(否):");
    //根据用户输入确定是否进入租车系统
    Scanner input=new Scanner(System.in);
    flag=input.nextInt();
    while(flag==1){//判断flag确定是否进入租车系统
        getMassage(cars);
        System.out.print("请输入您要租的车的数量:");
        carNum=input.nextInt();
        //用数组choose【】保存用户的租车选择
        int choose[]=new int[carNum];
        for(int i=0;i<carNum;i++){
            System.out.print("请输入第"+(i+1)+"辆车的序号:");
            choose[i]= input.nextInt();
        }
        System.out.print("请输入租车天数:");
        rentDay=input.nextInt();
        printBill(cars,choose,rentDay);//调用函数printBill打印账单
        System.out.print("欢迎使用达达租车系统:\n您是否要租车? 1(是)0(否):");
        flag=input.nextInt();
    }
    System.out.println("谢谢使用,再见");
    input.close();
}
public static void getMassage(Vehicle cars[]){
    //打印可租汽车的信息
    System.out.println("您可租的车的类型及其价目表:\n序号\t汽车名称\t租金\t容量");
    for(int i=0;i<6;i++){
        //判断是载人车,载货车还是两用车,并输出(instanceof用于判断对象类型)
        if(cars[i] instanceof PeopleCar){
            System.out.println(cars[i].id+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t"+"载人:"+cars[i].people+"人");
        }else if(cars[i] instanceof CargoCar){
            System.out.println(cars[i].id+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t"+"载货:"+cars[i].cargo+"吨");
        }else{
            System.out.println(cars[i].id+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t"+"载人:"+cars[i].people+"人\t"+"载货:"+cars[i].cargo+"吨");
        }
    }
}
public static void printBill(Vehicle cars[],int choose[],int rentDay){
    //打印账单
    int sumPeople=0;//总载客数
    float sumPrice=0f,sumCargo=0f;//总价格总载货数
    System.out.println("您的账单:\n******可载人的车有:");
    for(int i=0;i<choose.length;i++){
        if(cars[choose[i]-1] instanceof PeopleCarcars[choose[i]-1] instanceof BothCar){//判断车型是否可载人
            System.out.print(cars[choose[i]-1].name+"\t");
            sumPeople+=cars[choose[i]-1].people;//总载人数
        }
        sumPrice+=cars[choose[i]-1].price;//遍历数组的同时累加租车价格
    }
    sumPrice*=rentDay;//算总价格
    System.out.println("共载人:"+sumPeople+"人");
    System.out.println("******可载货的车有:");
    for(int i=0;i<choose.length;i++){//判断是否可以载货
        if(cars[choose[i]-1] instanceof CargoCarcars[choose[i]-1] instanceof BothCar){
            System.out.print(cars[choose[i]-1].name+"\t");
            sumCargo+=cars[choose[i]-1].cargo;//总载货数
        }
    }
    System.out.println("共载货:"+sumCargo+"吨");
    System.out.println("******租车总价格为:"+sumPrice+"元");

}

}
我是初学者,自己原先编了一个带接口的程序,后来看了大家的代码,觉得接口好像用起来更麻烦了,又自己改进了一下,传上来请大家帮忙看看有什么问题以及可以改进的地方,欢迎批评建议,谢谢(特别感谢id:charsandrew很多地方看了你的代码后启发的,谢谢)

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