为了账号安全,请及时绑定邮箱和手机立即绑定

Java第二季答答租车系统代码

标签:
Java

有问题的同学,欢迎提问,我们一起进步,加油!
项目文件结构

io.leachar.rental 用来存放主函数
io.leachar.sys 用来存放答答租车系统主要部分
io.leachar.medel 用来存放车的类型。车、载人、载货、载人载货

io.leachar.rental.Rental(Rental.java)

package io.leachar.rental;

import java.util.Scanner;

import io.leachar.sys.Dada;

/**
 * 主函数
 * @author Leachar
 *
 */

public class Rental {

    public static void main(String[] args) {    
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车:1是      0否");
        Scanner input = new Scanner(System.in);
        int y = input.nextInt();
        if(y==1){
            Dada dada = new Dada();
            dada.userSys();
        }else if(y==0){
            System.out.println("欢迎下次使用!");
        }else{
            System.out.println("请输入1或者0");
        }
    }

}

io.leachar.sys.Dada(Dada.java)

package io.leachar.sys;

import io.leanchar.medel.Bus;
import io.leanchar.medel.Car;
import io.leanchar.medel.Pickup;
import io.leanchar.medel.Truck;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 答答租车系统主要部分
 * @author Leacahr
 *
 */

public class Dada {

    int sum = 0;        //总金额
    int peopleSum = 0;      //总载人数
    int goodsSum = 0;       //总载货数
    ArrayList<String> peopleName = new ArrayList<String>(); //可载人的汽车名称
    ArrayList<String> goodsName = new ArrayList<String>();  //可载人的汽车名称

    public void userSys(){

            System.out.println("您可租车的类型及其价目表");
            //创建车库数组
            Car[] car = {
                    new Bus(1,"奥迪A4",500,4),
                    new Bus(2, "马自达", 500, 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("序号\t汽车名称\t租金\t容量");
            for(int i=0;i<car.length;i++){
                System.out.println(car[i]);
            }
            System.out.println("请输入您要租车的数量");
            Scanner input = new Scanner(System.in);
            int n = input.nextInt();
            //判断输入的租车数量是否合理
            if(n>0){
                for(int j=1;j<=n;j++){
                    System.out.println("请输入第"+j+"辆车的序号");
                    int carnum = input.nextInt();
                    //判断输入的需要是否合理
                    if(carnum>6 || carnum <=0){
                        System.out.println("这次输入无效,请输入合理的序号!");
                        continue;
                    }else{
                        //得到数组的下标
                        carnum = carnum - 1;
                    }
                    //计算1天金额
                    sum += car[carnum].getRent();
                    //判断是载货还是载人
                    if(car[carnum].getClass()==Bus.class){
                        //计算总人数
                        peopleSum += ((Bus) car[carnum]).getPeople();
                        //将选中的载人汽车名称加入集合
                        peopleName.add(car[carnum].getName());
                    }else if(car[carnum].getClass()==Truck.class){
                        //计算总货物
                        goodsSum += ((Truck) car[carnum]).getGoods();
                        //将选中的载货汽车名称加入集合
                        goodsName.add(car[carnum].getName());
                    }else if(car[carnum].getClass()==Pickup.class){
                        //计算总人数和总货物,以及载人和载物汽车名称存入数组
                        peopleSum += ((Pickup) car[carnum]).getPeople();
                        goodsSum += ((Pickup) car[carnum]).getGoods();
                        goodsName.add(car[carnum].getName());
                        peopleName.add(car[carnum].getName());
                    }else{
                        System.out.println("没有该车型");
                    }
                }
                System.out.println("您要租车的天数");
                int day = input.nextInt();
                //天数总金额
                sum *= day;
                System.out.println("你的账单为");
                //使用遍历,输出载人汽车名称
                System.out.println("***可载人的汽车有");
                for(String newPeople:peopleName){
                    System.out.print(newPeople+"\t");
                }
                System.out.println("共载人"+peopleSum+"人");
                //使用遍历,输出载货汽车名称
                System.out.println("***可载货的汽车有");
                for(String newGoods:goodsName){
                    System.out.print(newGoods+"\t");
                }
                System.out.println("共载货"+goodsSum+"吨");
                //输出租车总金额
                System.out.println("****租车价格:"+sum+"元");
            }else if(n<=0){
                System.out.println("请输入一个合理的数字");
            }

    }

}

io.leanchar.medel.Car(Car.java)

package io.leanchar.medel;

/**
 * 所有车共有的
 * @author Leachar
 *
 */

public abstract class Car {

    private int num;        //序号 
    private String name;    //汽车名称
    private int 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;
    }

}

io.leanchar.medel.Truck(Truck.java)

package io.leanchar.medel;

/**
 * 只能载货
 * @author Leachar
 *
 */

public class Truck extends Car implements IGoods {

    private int goods;      //用来存放载货数量

    public Truck(int num,String name,int rent,int goods){
        super.setNum(num);
        super.setName(name);
        super.setRent(rent);
        this.goods = goods;
    }

    @Override
    public void setGoods(int goods) {
        // TODO Auto-generated method stub
        this.goods = goods;
    }

    @Override
    public int getGoods() {
        // TODO Auto-generated method stub
        return goods;
    }

    @Override
    public String toString() {
        return getNum()+"\t"+getName()+"\t"+getRent()+"元/天\t载货"+getGoods()+"吨";
    }

}

io.leanchar.medel.Pickup(Pickup)

package io.leanchar.medel;

/**
 * 可载人也可载货
 * @author Leachar
 *
 */

public class Pickup extends Car implements IGoods, IManned {

    private int people;
    private int goods;

    public Pickup(int num,String name,int rent,int people,int goods){
        super.setNum(num);
        super.setName(name);
        super.setRent(rent);
        this.people = people;
        this.goods = goods;
    }

    @Override
    public void setPeople(int people) {
        // TODO Auto-generated method stub
        this.people = people;
    }

    @Override
    public int getPeople() {
        // TODO Auto-generated method stub
        return people;
    }

    @Override
    public void setGoods(int goods) {
        // TODO Auto-generated method stub
        this.goods = goods;
    }

    @Override
    public int getGoods() {
        // TODO Auto-generated method stub
        return goods;
    }

    @Override
    public String toString() {
        return getNum()+"\t"+getName()+"\t"+getRent()+"元/天\t载人"+getPeople()+"人 载货"+getGoods()+"吨";
    }

}

io.leanchar.medel.Bus(Bus.java)

package io.leanchar.medel;

/**
 * 只能载人
 * @author Leachar
 *
 */

public class Bus extends Car implements IManned {
    private int people;         //使用people存放载人

    //给变量赋值
    public Bus(int num,String name,int rent,int people){
        super.setNum(num);
        super.setName(name);
        super.setRent(rent);
        this.people = people;
    }

    @Override
    public void setPeople(int people) {
        // TODO Auto-generated method stub
        this.people = people;
    }

    @Override
    public int getPeople() {
        // TODO Auto-generated method stub
        return people;
    }

    @Override
    public String toString() {
        return getNum()+"\t"+getName()+"\t"+getRent()+"元/天\t载人"+getPeople()+"人";
    }

}

io.leanchar.medel.IGoods(IGoods.java)

package io.leanchar.medel;

/**
 * 载货汽车共有的接口
 * @author Leachar
 *
 */

public interface IGoods {
    public void setGoods(int goods);
    public int getGoods();
}

io.leanchar.medel.IManned(IManned)

package io.leanchar.medel;

/**
 * 载人车型共有接口
 * @author Leachar
 *
 */

public interface IManned {
    public void setPeople(int people);      
    public int getPeople();             
}

运行结果示例图

点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消