手记

Java实战练习——租车系统

package car(表示车辆的包)

package car;

public class All {
    private int num;
    private String brand;
    private int price;
    private int per_carry;
    private int cargo_carry;
    public void display(){
        System.out.println("  ");
    }
    public int getNum() {
        return num;
    }
    public String getBrand() {
        return brand;
    }
    public int getPrice() {
        return price;
    }
    public int getPer_carry() {
        return per_carry;
    }
    public int getCargo_carry() {
        return cargo_carry;
    }
}

All.java(父类)

package car;

public class Cargo extends All{
    private int num;
    private String brand;
    private int price;
    private int cargo_carry;
    public void display(){
        System.out.println(num+"."+"   "+brand+" "+"\t"+price+"元/天     "+"\t"+"载货:"+cargo_carry+"吨");
    }
    public Cargo(int num,String brand,int price,int cargo_carry){
        this.num=num;
        this.brand=brand;
        this.price=price;
        this.cargo_carry=cargo_carry;
    }
    public int getNum() {
        return num;
    }
    public String getBrand() {
        return brand;
    }
    public int getPrice() {
        return price;
    }
    public int getCargo_carry() {
        return cargo_carry;
    }
}

Cargo.java(表示载货车的子类)

package car;

public class Pers extends All {
    private int num;
    private String brand;
    private int price;
    private int per_carry;
    public void display(){
        System.out.println(num+"."+"   "+brand+" "+"\t"+price+"元/天     "+"\t"+"载人:"+per_carry+"人");
    }
    public Pers(int num,String brand,int price,int per_carry){
        this.num=num;
        this.brand=brand;
        this.price=price;
        this.per_carry=per_carry;
    }
    public int getNum() {
        return num;
    }
    public String getBrand() {
        return brand;
    }
    public int getPrice() {
        return price;
    }
    public int getPer_carry() {
        return per_carry;
    }
}

Pers.java(表示载人车的子类)

package car;

public class Pika extends All {
    private int num;
    private String brand;
    private int price;
    private int per_carry;
    private int cargo_carry;
    public void display(){
        System.out.println(num+"."+"   "+brand+" "+"\t"+price+"元/天     "+"\t"+"载人:"+per_carry+"人  "+"载货:"+cargo_carry+"吨");
    }
    public Pika(int num,String brand,int price,int per_carry,int cargo_carry){
        this.num=num;
        this.brand=brand;
        this.price=price;
        this.per_carry=per_carry;
        this.cargo_carry=cargo_carry;
    }
    public int getNum() {
        return num;
    }
    public String getBrand() {
        return brand;
    }
    public int getPrice() {
        return price;
    }
    public int getPer_carry() {
        return per_carry;
    }
    public int getCargo_carry() {
        return cargo_carry;
    }

}

Pika.java(表示皮卡车的子类)


package main(主界面的包)

package main;
import car.*;

public class Show {
    private int num2;
    private int[] array=new int[num2];
    private int day;
    private int sumprice=0;
    private int sumpers=0;
    private int sumcargo=0;
    All[] cars={new car.Pers(1, "奥迪A4", 500, 4),
            new car.Pers(2, "马自达6", 400, 4),
            new car.Pika(3, "皮卡雪6", 450, 4, 2),
            new car.Pers(4, "金龙", 800, 20),
            new car.Cargo(5, "松花江", 400, 4),
            new car.Cargo(6, "依维柯", 1000, 20)
            };
    public void setArray(int array[]){
        this.array=array;
    }
    public void setNum2(int num2){
        this.num2=num2;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public void  choose(){
        System.out.println("您可租车的类型和价目表");
        System.out.println("序号"+"      汽车名称         "+"            租金"+"\t"+"                            容量");
        for(All y:cars){
            y.display();
        }
    }
    public void calculate(int num2,int day,int array[]){
        this.num2=num2;
        this.day=day;
        this.array=array;
        All[] ch=new All[num2];
        for(int i=0;i<num2;i++){
            ch[i]=cars[array[i]-1];
        }
        for(All x:ch){
            //System.out.println(x.getPrice());
            sumprice+=x.getPrice();
            if(x instanceof car.Pers||x instanceof car.Pika){
                sumpers+=x.getPer_carry();
            }
            if(x instanceof car.Cargo||x instanceof car.Pika){
                sumcargo+=x.getCargo_carry();
            }
        }
        sumprice*=day;
        System.out.println("您的账单:");
        System.out.println("总金额:     "+sumprice+"元");
        if(sumpers!=0){
            System.out.println("总载人数:       "+sumpers+"人");
        }
        if(sumcargo!=0){
            System.out.println("总载重:         "+sumcargo+"吨");
        }
    }
}   

Show.java(进行计算和初始化的类)

package main;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("是否要使用本租车系统?    1、是    2、否");
        Scanner num1=new Scanner(System.in);
        int numg= num1.nextInt();
        if(numg==1){
            System.out.println("欢迎使用本租车系统");
        }else if(numg==2){
            System.out.println("退出本系统");
            System.exit(0);
        }else{
            System.out.println("输入错误");
            System.exit(0);
        }
        Show gen=new Show();
        gen.choose();
        System.out.println("请输入您要租车的数量:");
        Scanner num=new Scanner(System.in);
        int num2=num.nextInt();
        int x=0;
        int[] chNum= new int[num2];
        while (x<num2){
            int y=x+1;
            System.out.println("请输入第"+y+"辆的序号:");
            Scanner ch=new Scanner(System.in);
            chNum[x]=ch.nextInt();
            x++;
        }
        System.out.println("请输入您要租车的天数:");
        @SuppressWarnings("resource")
        Scanner day=new Scanner(System.in);
        gen.calculate(num2,day.nextInt(),chNum);
    }
}

Main.java(主界面类)

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