继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

我自己写的答答租车系统

ID真不好取
关注TA
已关注
手记 1
粉丝 0
获赞 2

父类(Car)

package com.DDCar;

public class Car {
public int num;
public String name;
public int rent;
public int manned;
public double cargo;

public Car(int num,String name,int rent){
    this.num=num;this.name=name;this.rent=rent;
}
public int getNum() {
    return num;
}
public void setNum(int num) {
    this.num = num;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getRent() {
    return rent;
}
public void setRent(int rent) {
    this.rent = rent;
}
public int getManned() {
    return manned;
}
public void setManned(int manned) {
    this.manned = manned;
}
public double getCargo() {
    return cargo;
}
public void setCargo(double cargo) {
    this.cargo = cargo;
}

}

各种子类
package com.DDCar; //小型汽车 public class LitCar extends Car{ public LitCar(int num,String name,int rent,int manned){ super(num,name,rent); this.manned=manned; } }

package com.DDCar;
//大型客车
public class Bus extends Car{

    public Bus(int num, String name, int rent,int manned) {
        super(num, name, rent);
        this.manned=manned;
        // TODO Auto-generated constructor stub
    }

}
package com.DDCar;
//皮卡
public class Pickup extends Car{

    public Pickup(int num, String name, int rent,int manned,double cargo) {
        super(num, name, rent);
        this.manned=manned;
        this.cargo=cargo;
        // TODO Auto-generated constructor stub
    }

}
package com.DDCar;
//载货汽车
public class Truck extends Car{

    public Truck(int num, String name, int rent,double cargo) {
        super(num, name, rent);
        this.cargo=cargo;
        // TODO Auto-generated constructor stub
    }

}

以下是主函数

package com.DDCar;

import java.util.Scanner;

public class Main {
    public static String[] name = new String[6];
    //public static int[] count = new int[6];
    //public static int[] money = new int[6];
    public static void main(String[] args) {
        Car[] car={new LitCar(1,"奥迪A4",500,4),new LitCar(2,"马自达6",400,4),
                   new Pickup(3,"皮卡雪6",450,4,2),new Bus(4,"金龙",800,20),
                   new Truck(5,"松花江",400,4),new Truck(6,"依维柯",1000,20)};
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("请问是否租车?      (1租车  0退出)");
        Scanner input=new Scanner(System.in);
        int ch1 =input.nextInt();
        if(ch1==1){
            System.out.println("你可租车的类型及价格表:");
            System.out.println("序号"+"\t"+"汽车名称"+"\t\t"+"租金"+"\t\t"+"容量 ");
            System.out.println(car[0].num+"\t"+car[0].name+"\t\t"+car[0].rent+"元/天\t\t"+"载人:"+car[0].manned+"人");
            System.out.println(car[1].num+"\t"+car[1].name+"\t\t"+car[1].rent+"元/天\t\t"+"载人:"+car[1].manned+"人");
            System.out.println(car[2].num+"\t"+car[2].name+"\t\t"+car[2].rent+"元/天\t\t"+"载人:"+car[2].manned+"人"+" 载货:"+car[2].cargo+"吨");
            System.out.println(car[3].num+"\t"+car[3].name+"\t\t"+car[3].rent+"元/天\t\t"+"载人:"+car[3].manned+"人");
            System.out.println(car[4].num+"\t"+car[4].name+"\t\t"+car[4].rent+"元/天\t\t"+"载货:"+car[4].cargo+"吨");
            System.out.println(car[5].num+"\t"+car[5].name+"\t\t"+car[5].rent+"元/天 \t"+"载货:"+car[5].cargo+"吨");
            System.out.println("请输入您要租车的数量:");
            int ch2=input.nextInt();//ch2表示租车的数量
            if(ch2<=0  || ch2>6){
                System.out.println("你的输入有误,再见!");
                System.exit(0);
            }
            int manned=0;double money=0,b=0;
            for(int i=0;i<ch2;i++){
                System.out.println("请输入第"+(i+1)+"辆车的序号:");
                int ch3=input.nextInt();//ch3表示租的车的序号
                if(ch3>0&&ch3<=6){
                        name[i]=car[ch3-1].name;
                        manned+=car[ch3-1].manned;//计算所租的车总共载人数
                        //count[i]=car[ch3-1].num;
                        money+=car[ch3-1].rent;//计算总租金
                        b+=car[ch3-1].cargo;//计算总载货量
                        //System.out.println(name[i]);//测试
                }
                else{
                    System.out.println("你的输入有误");break;
                }

            }

            System.out.println("请输入租车的天数:");
            int ch4=input.nextInt();
            System.out.println("您的账单如下:"+"\n"+"您总共租赁了"+ch2+"辆车"+"\t"+"租赁了"+ch4+"天");
            System.out.println("***可载人的车有:");
            for(int n=0;n<ch2;n++){
                if(name[n].equals("奥迪A4") || name[n].equals("马自达6") || name[n].equals("皮卡雪6")
                        || name[n].equals("金龙"))//*****此处待改进*****
                {                   
                    System.out.print(name[n]+"\t");
                }
            }
            System.out.println("共载人:"+manned+"人");
            System.out.println("***可载货的车有:");
            for(int n=0;n<ch2;n++){//*****下列待改进*****
                if(name[n].equals("皮卡雪6") || name[n].equals("松花江") || name[n].equals("依维柯"))
                System.out.print(name[n]+"\t");

            }
            System.out.println("共载货:"+b+"吨");
            System.out.println("***租车总价格:"+(money*ch4)+"元");
        }
        else if(ch1==0)
        {
            System.out.println("谢谢您的光临");
            System.exit(0);
        }
        else{
            System.out.println("您的输入有误,再见!!!!");
            System.exit(0);
        }
    }

}

任然存在不足可以改进的地方,欢迎各位同学、大神指点一二, 拜谢

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP