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

哒哒租车系统,自己学了一段时间写的。

标签:
Java

这是父类和子类:
package com.imooc;

/**

  • Created by Administrator on 2017/1/18.
    */
    public abstract class Che
    {
    public String name;
    public int charge;

    public Che() {}

    public Che(String name, int charge)
    {
    this.name = name;
    this.charge = charge;
    }

    public String getName()
    {
    return name;
    }

    public void setName(String name)
    {
    this.name = name;
    }

    public int getCharge()
    {
    return charge;
    }

    public void setCharge(int charge)
    {
    this.charge = charge;
    }
    public abstract int getRcou();//自己做时对数组Che []ches调用自类方法解决办法
    public abstract int getTon();//自己做时对数组Che []ches调用自类方法解决办法
    }

class ZRen extends Che
{
private int Rcou;

public ZRen(String name, int charge, int rcou)
{
    super(name, charge);
    Rcou = rcou;
}

public int getRcou()
{
    return Rcou;
}

public void setRcou(int rcou)
{
    Rcou = rcou;
}
//需要完善另一个抽象方法此时让返回值为0
public int getTon()
{
    return 0;
}

}

class Zwu extends Che
{
private int ton;

public Zwu(String name, int charge, int ton)
{
    super(name, charge);
    this.ton = ton;
}

public int getTon()
{
    return ton;
}

public void setTon(int ton)
{
    this.ton = ton;
}
//需要完善另一个抽象方法此时让返回值为0
public int getRcou()
{
    return 0;
}

}

class Pk extends Che
{
private int Rcou;
private int ton;

public Pk(String name, int charge, int rcou, int ton)
{
    super(name, charge);
    Rcou = rcou;
    this.ton = ton;
}

public int getRcou()
{
    return Rcou;
}

public void setRcou(int rcou)
{
    Rcou = rcou;
}

public int getTon()
{
    return ton;
}

public void setTon(int ton)
{
    this.ton = ton;
}

}
下面是main方法:
package com.imooc;

import java.util.Scanner;

/**

  • Created by Administrator on 2017/1/18.
    */
    public class Test
    {
    public static Scanner input= new Scanner(System.in);
    public static int cost=0;
    public static int ren=0;
    public static int huo=0;

    public static void main(String[] args)
    {

    while (true)
    {
        System.out.println("欢迎使用哒哒租车系统:");
        System.out.println("您是否要租车:1、是          0、否");
        int p1=input.nextInt();
        if(p1==1)
        {
            show();
            break;
        }
        else if(p1==0)
        {
            System.out.println("欢迎您的光临,很高兴为您服务,再见!");
            break;
        }
        else
        {
            System.out.println("请输入信息不符合选项要求!");
            break;
        }
    }
    clear();

    }

public static Che []ches={new ZRen("奥迪A4",500,4),
new ZRen("马自达6",400,4),
new Pk("皮卡雪6",450,4,2),
new ZRen("金龙",800,20),
new Zwu("松花江",400,4),
new Zwu("依维柯",1000,20)};

public static void show()
{
    System.out.println("序号   汽车名称    租金     容量");
    for(int i=0;i<ches.length;i++)
    {
        switch (i)
        {
            case 0:
            case 1:
            case 3:
                System.out.println((i+1)+"  "+ches[i].getName()+"   "+
                        ches[i].getCharge()+"元/天  "+"载人:"+ches[i].getRcou()+"人");
                break;
            case 2:
                System.out.println((i+1)+"  "+ches[i].getName()+"   "+
                        ches[i].getCharge()+"元/天  "+"载人:"+ches[i].getRcou()+"人"+"  载货:"+ches[i].getTon()+"吨");
                break;
            case 4:
            case 5:
                System.out.println((i+1)+"  "+ches[i].getName()+"   "+
                        ches[i].getCharge()+"元/天  "+"载货:"+ches[i].getTon()+"吨");
                break;

        }

    }
    System.out.println("请输入您想要汽车的数量:");
}
public static void clear()
{
    int p2=input.nextInt();
    int []chex=new int[p2];
    String []RC=new String[p2];
    String []HC=new String[p2];
    int count1=0;
    int count2=0;

    for(int i=0;i<p2;i++)
    {
        System.out.println("请输入第"+(i+1)+"辆车的序号:");
        chex[i]=input.nextInt();
        cost=cost+ches[chex[i]-1].getCharge();
        switch (chex[i])
        {
            case 1:
            case 2:
            case 4:
                ren =ren+ches[chex[i]-1].getRcou();
                RC[count1]=ches[chex[i]-1].getName();
                count1++;
                break;
            case 3:
                ren =ren+ches[chex[i]-1].getRcou();
                huo=huo+ches[chex[i]-1].getTon();
                RC[count1]=ches[chex[i]-1].getName();
                count1++;
                HC[count2]=ches[chex[i]-1].getName();
                count2++;
                break;
            case 6:
            case 5:
                huo=huo+ches[chex[i]-1].getTon();
                HC[count2]=ches[chex[i]-1].getName();
                count2++;
                break;
        }

    }
    System.out.println("请输入租车天数:");
    int p3=input.nextInt();
    cost=cost*p3;

    System.out.println("您的账单:");
    System.out.println("***可载人的车有:");

    for(int k=0;k<count1;k++)
    {
        for(int l=k+1;l<count1;l++)
        {
            if(RC[k]==RC[l])
            {
                RC[l]=null;
            }else {continue;}
        }
        if(RC[k]!=null)
        {
        System.out.println(RC[k]);
        }
    }
    System.out.println("共载客:"+ren+"人");
    System.out.println("***可载货的车有:");

    for(int n=0;n<count2;n++)
    {
        for(int m=n+1;m<count2;m++)
        {
            if(HC[n]==HC[m])
            {
                HC[m]=null;
            }else {continue;}
        }
        if(HC[n]!=null)
        {
            System.out.println(HC[n]);
        }
    }
    System.out.println("共载货:"+huo+"吨");
    System.out.println("***租车的总金额:"+cost+"元");
}

}

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消