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

答答租车系统,望各位提提建议(ps:这是我根据几位大神发的代码编的)

标签:
Java

昨天刚看完《Java入门第二季》的视频,根据各位大神的代码加以改编,佩服你们能自己编出来,没有你们的模板,感觉两眼一抹黑,完全没思路;
1.读取用户的输入,并对错误的输入有相应的提示和重新输入;

  1. 价格表通过对象实现赋值和输出;
    3.考虑到用户可能一种车租很多辆,每一辆车租用的天数不一样。系统采用先让用户选择一种要租的车,再选择该种车需租几辆和几天(这里面用数组存储信息);
    4.利用for循环实现计算总额和输出订单详情并打印订单。

车的种类,价格构成车的抽象类

package com.DADA;
//车抽象类
public abstract class Vehicle {
    private String car;
    private int price;
    public  String getcar(){
        return car;
    }
    public void setcar(String car){
        this.car=car;
    }
    public  int getprice(){
        return price;
    }
    public void setprice(int price){
        this.price=price;
    }
}

客车和货车因为载客量和载货量不同,所以将其分别定义为2种接口,而皮卡车接口可以通过继承这2种接口实现:


package com.DADA;
//客车接口
public interface Izaike {
    public void setzaikenum(int zaikenum);
    public int getzaikenum();
}
package com.DADA;
//货车接口
public interface Izaizong {
    public void setzaizongnum(int zaizongnum);
    public int getzaizongnum();
}
package com.DADA;
//皮卡接口
public interface Ipk extends Izaike,Izaizong{

}

定义三种车的类,分别继承抽象类Vehicle并各自实现接口Izaike,Izaizong,Ipk;

package com.DADA;
//客车类
public class keche extends Vehicle implements Izaike {
    private int zaikenum;
    public keche(String car,int price,int zaikenum){
        this.setcar(car);
        this.setprice(price);
        this.setzaikenum(zaikenum);
    }

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

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

}
package com.DADA;
//货车类
public class huoche extends Vehicle implements Izaizong {
    private int zaizongnum;
    public huoche(String car,int price,int zaizongnum){
        this.setcar(car);
        this.setprice(price);
        this.setzaizongnum(zaizongnum);
    }

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

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

}
package com.DADA;
//皮卡类
public class PiKa extends Vehicle implements Ipk {
    private int zaikenum;
    private int zaizongnum;
    public PiKa(String car,int price,int zaikenum,int zaizongnum){
        this.setcar(car);
        this.setprice(price);
        this.setzaikenum(zaikenum);
        this.setzaizongnum(zaizongnum);
    }

    @Override
    public void setzaikenum(int zaikenum) {
        // TODO Auto-generated method stub
        this.zaikenum=zaikenum;
    }
    @Override
    public int getzaikenum() {
        // TODO Auto-generated method stub
        return zaikenum;
    }
    @Override
    public void setzaizongnum(int zaizongnum) {
        // TODO Auto-generated method stub
        this.zaizongnum=zaizongnum;
    }
    @Override
    public int getzaizongnum() {
        // TODO Auto-generated method stub
        return zaizongnum;
    }

}

系统函数:

package com.DADA;
import java.util.*;
/*
 *答答租车系统 
 */
public class DADASystem {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] sumzaike=new int[5];
        double[] sumzaizong=new double[5];
        double[] sumprice=new double[5];
        String[] name=new String[5];
        int[] n=new int[5];
        int[] j=new int[5];
        //对象赋值
        keche kcar1=new keche("小型客车",100,8);
        keche kcar2=new keche("大型客车",300,20);
        huoche hcar1=new huoche("小型货车",200,5);
        huoche hcar2=new huoche("大型货车",500,20);
        PiKa car=new PiKa("皮卡",300,4,2);
        for(;;){
            System.out.println("欢迎使用答答租车系统");
            System.out.print("你是否要租车:1是  0否:");
            Scanner in = new Scanner(System.in);
            int choice = in.nextInt();
            if(choice==0){
                System.out.println("欢迎下次使用!"); 
                break;
            }else if(choice==1){
                System.out.println("你可租车的类型及其价格表:");
                System.out.println("序号\t汽车种类\t价格(元/天)\t功能");
                System.out.println("--------------------------------------");
                System.out.println(1+"\t"+kcar1.getcar()+"\t"+"  "+kcar1.getprice()+"\t\t"+"载人:"+kcar1.getzaikenum()+"人");
                System.out.println(2+"\t"+kcar2.getcar()+"\t"+"  "+kcar2.getprice()+"\t\t"+"载人:"+kcar2.getzaikenum()+"人");
                System.out.println(3+"\t"+hcar1.getcar()+"\t"+"  "+hcar1.getprice()+"\t\t"+"载重:"+hcar1.getzaizongnum()+"T");
                System.out.println(4+"\t"+hcar2.getcar()+"\t"+"  "+hcar2.getprice()+"\t\t"+"载重:"+hcar2.getzaizongnum()+"T");
                System.out.println(5+"\t"+car.getcar()+"\t"+"  "+car.getprice()+"\t\t"+"载人:"+car.getzaikenum()+"人"+""+"载重:"+car.getzaizongnum()+"T");
                System.out.println("--------------------------------------");
                System.out.println("接下来您将进行租车服务,如果租车完成请输入数字0结束!");
                int i;
                for(i=0;;i++){
                    System.out.print("请输入一种所需车的序号:");
                    int m = in.nextInt();
                    if(m==0){
                        System.out.println();
                        System.out.println("谢谢使用。");
                        in.close();
                        break;
                    }else if(m<0m>5){
                        System.out.println("指令有误!请输入正确的指令。"); 
                        i--;
                        continue; 
                    }
                    switch(m){
                    case 1: name[i]=kcar1.getcar();
                            System.out.print("请输入该s车型您要租多少辆:");
                            n[i] = in.nextInt();
                            System.out.print("请输入该车型您要租多少天:");
                            j[i] = in.nextInt();
                            sumzaike[i]+=kcar1.getzaikenum()*n[i];
                            sumprice[i]+=kcar1.getprice()*n[i]*j[i];
                            break;
                    case 2: name[i]=kcar2.getcar();
                            System.out.print("请输入该车型您要租多少辆:");
                             n[i] = in.nextInt();
                            System.out.print("请输入该车型您要租多少天:");
                             j[i] = in.nextInt();
                             sumzaike[i]+=kcar1.getzaikenum()*n[i];
                             sumprice[i]+=kcar1.getprice()*n[i]*j[i];
                             break;
                    case 3: name[i]=hcar1.getcar();
                            System.out.print("请输入该车型您要租多少辆:");
                             n[i] = in.nextInt();
                            System.out.print("请输入该车型您要租多少天:");
                             j[i] = in.nextInt();
                             sumzaizong[i]+=hcar1.getzaizongnum();
                             sumprice[i]+=hcar1.getprice()*n[i]*j[i];
                             break;
                    case 4: name[i]=hcar1.getcar();
                            System.out.print("请输入该车型您要租多少辆:");
                             n[i] = in.nextInt();
                            System.out.print("请输入该车型您要租多少天:");
                             j[i] = in.nextInt();
                             sumzaizong[i]+=hcar1.getzaizongnum();
                             sumprice[i]+=hcar1.getprice()*n[i]*j[i];
                             break;
                    case 5: name[i]=car.getcar();
                            System.out.print("请输入该车型您要租多少辆:");
                             n[i] = in.nextInt();
                            System.out.print("请输入该车型您要租多少天:");
                             j[i] = in.nextInt();
                             sumzaike[i]+=car.getzaikenum()*n[i];
                             sumzaizong[i]+=car.getzaizongnum()*n[i];
                             sumprice[i]+=car.getprice()*n[i]*j[i];
                             break;
                    }
                }
                System.out.println();
                System.out.println("----------------------------------------------");
                System.out.println("你的订单:");
                System.out.println("----------------------------------------------");
                System.out.println("汽车名称\t数量(辆)\t天数(天)\t可载人数/重量\t总租金");
                for(int l=0;l<i;l++){
                        System.out.println(name[l] + "\t"+" " + n[l] +"\t"+" "+ j[l] + "\t"+" " + sumzaike[l]+"/" +sumzaizong[l] + "\t\t" + sumprice[l]);
                }
                System.out.println("----------------------------------------------");
                double sum=0;
                for(double sumprices:sumprice){
                    sum+=sumprices;
                }
                System.out.println("\t\t\t\t总金额(¥):" + sum);
                System.out.println("----------------------------------------------");
                System.out.println();
                System.out.println("感谢您使用答答租车系统!");
                in.close();
                break;
            }else{ 
                System.out.println("指令有误!请输入正确的指令。"); 
                continue; 
            }
        }

    }
}

效果图:
图片描述

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消