手记

答答租车系统,输入序列不会对齐,有点丑(纠结)

import java.util.Scanner;
父类
class Carc{
    private int num;       //序号
    private String carName;//车名
    private int money;     //租金
    private int manned;    //载人
    private int load;     //载货
    public int getManned() {
        return manned;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }
    public int getLoad() {
        return load;
    }
    public void setLoad(int load) {
        this.load = load;
    }
    public Carc(){}

    public Carc(int num,String carName,int money,int manned,int load){
      this.num=num;
      this.carName=carName;
      this.money=money;
      this.manned=manned;
      this.load=load;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
}
子类
//载人类

class CarA extends Carc{

    public CarA(int num, String carName, int money,int manned) {
    super.setNum(num);
    super.setManned(manned);
    super.setMoney(money);
    super.setCarName(carName);

    }
    public String toString() {
        return super.getNum()+".   "+ super.getCarName()+"   "+ super.getMoney()+"元/天  "+"载人:"+ super.getManned()+"人";
    }

}
//载货类
class CarB extends Carc {

public CarB(int num,String carName,int money,int load){
    //这里也可以用this,调用继承的setNum方法
    super.setNum(num);
    super.setLoad(load);
    super.setMoney(money);
    super.setCarName(carName);
}
public String toString() {
    return  super.getNum()+".   "+ super.getCarName()+"  "+ super.getMoney()+"元/天  "+"载货:"+ super.getLoad()+"吨";
}

}
//载货和载人类
class Card extends Carc{

public Card(int num,String carName,int money,int manned,int load){
    super.setNum(num);
    super.setManned(manned);
    super.setLoad(load);
    super.setMoney(money);
    super.setCarName(carName);
}
public String toString() {
    return  super.getNum()+".   "+ super.getCarName()+"  "+ super.getMoney()+"元/天  "+"载货:"+ super.getLoad()+"吨";
}

}
测试类
public class Demo3 {

    public static void main(String[] args) {

        // TODO Auto-generated method stub
    Scanner sc=new Scanner(System.in);
    int sum=0;
    String Smanned = "" ;
    String Sload="";

        System.out.println("欢迎使用答答租车系统"+"\r\n"+"您是否要租车1是0否");
        int a=sc.nextInt();
        if(a==0){

        return;
        }
        if(a==1){
            //输出结果没有对齐
            System.out.println("序号   租车名称     租金             容量");
            //使用对象数组
    Carc[] c=new Carc[]{new CarA(1,"奥迪a4",500,4),new CarA(2,"马自达6",400,4),new CarA(3,"金龙",800,20),new CarB(4,"松花江",400,4),new CarB(5,"依维柯",1000,20),new Card(6,"皮卡6",450,4,2)};
        for(int i=0;i<c.length;i++){
            System.out.println(c[i]);
        }
            System.out.println("请输入要租车的数目:");
            int b=sc.nextInt();

            //这个数组用以保存输入的序号
            int Array[]=new int[b];
        for(int i=0;i<b;i++){
            System.out.println("请输入要租车的序号:");
            //这里可以完善一下
            int  d=sc.nextInt();
            //防止数组越界
            d-=1;
            Array[i]=d;   //存入数组
        }
        System.out.println("请输入要租车的期限");
        int e=sc.nextInt();
        System.out.println("您的账单:");

        for(int i=0;i<Array.length;i++){

         sum =sum+c[Array[i]].getMoney()*e;
            if(!(c[Array[i]].getManned()==0)){
                Smanned=Smanned+String.valueOf(c[Array[i]].getCarName()+"     ");
            }
            if(!(c[Array[i]].getLoad()==0)){
                Sload=Sload+String.valueOf(c[Array[i]].getCarName()+"     ");
            }
            }
        System.out.println("***可载人的车有:"+"\r\n"+Smanned);
        System.out.println("***可载货的车有:"+"\r\n"+Sload);
        System.out.println("租车总价钱:"+sum);

        }

    }

}

7人推荐
随时随地看视频
慕课网APP

热门评论

测试类那里,为了防止数组越界,d-=1是什么意思

查看全部评论