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

imooc中java基础第二季的项目实站实现代码

标签:
Java
package cn.form1;
import java.util.Scanner;
//import java.util.Arrays;

public class Runmain {

    private static Scanner scanner;

    public static void main(String[] args) {
        int isrental = 0;
        int carcount = 0;
        int cardays = 0;
        int car_total = 0;
        String strs = "";
        scanner = new Scanner(System.in);

        System.out.println("欢迎使用租车系统:");
        System.out.println("您是否要租车?1是、0否");
        isrental = scanner.nextInt();
        if(isrental == 0){
            System.out.println("欢迎下次光临!!!");
        }else if(isrental == 1){

            Car[] arrcar = new Car[3];
            arrcar[0] = new Aodi(1,"奥迪",600,5);
            arrcar[1] = new Pika(2,"皮卡",300,3,2);
            arrcar[2] = new Sohujang(3,"花江",200,5);

            System.out.println("可租车的类型与价格表:");
            System.out.println("序号   名称       租金                             容量");
            for(int i = 0; i < arrcar.length; i++){
                Car car = arrcar[i];
                if(car.goods != 0) strs = "载货:" + car.goods + "吨";
                if(car.persons != 0) strs = "载人:" + car.persons + "个";
                if(car.persons != 0 && car.goods != 0) strs = "载人:" + car.persons + "个, 载货:" + car.goods + "吨";
                System.out.println(car.id + "    " + car.name + "      " + car.price + "元/每天                 " + strs);
            }

            System.out.println("输入您要租车数量:");
            carcount = scanner.nextInt();
            int[] input = new int[carcount];//保存客户输入的序号
            Car[] newcar = new Car[carcount];//存放客户选择的车

            if(carcount > arrcar.length){
                System.out.println("超出可租车数量");
            }else{
                for(int i = 0; i < carcount; i++){
                    System.out.println("请输入第"+ (i + 1)+"辆的序号");
                    input[i] = scanner.nextInt();
                }
                System.out.println("请输租车天数:");
                cardays = scanner.nextInt();
                System.out.println("您的账单:");

                for(int i = 0; i < arrcar.length; i++){
                    Car car = arrcar[i];
                    for(int l = 0; l < input.length; l++){
                        if(car.id == input[l]){
                            newcar[l] = car;//得到客户选择的车
                        }
                    }
                }

                String str_goods = "";
                int good_count = 0;
                String str_persons = "";
                int person_count = 0;
                for(int i = 0; i < newcar.length; i++){
                    Car car = newcar[i];
                    if(car.goods != 0){//载货
                        str_goods += car.name+"  ";
                        good_count += car.goods;
                    }
                    if(car.persons != 0){//载人
                        str_persons += car.name+"   ";
                        person_count += car.persons;
                    }
                    car_total += car.price * cardays;
                }

                System.out.println("****可载人的车如下:");
                System.out.println(str_persons + "共载人:"+person_count+"人");
                System.out.println("****可载货的车如下:");
                System.out.println(str_goods+"共载货:"+good_count+"吨");
                System.out.println("****总价格:" + car_total +"元");
            }

        }else{
            System.out.println("输入错误");
        }

    }

}
点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消