磕磕绊绊的,一知半解的写了出来,感觉比你们写的要繁琐很多啊

来源:12-1 综合练习

慕数据3565510

2017-05-19 07:59

父类

package com.imooc.www;


public class Che {

private int zaike;

private int zaihuo;

private int getZaike() {

return zaike;

}

public void setZaike(int zaike) {

this.zaike = zaike;

}

public int getZaihuo() {

return zaihuo;

}

public void setZaihuo(int zaihuo) {

this.zaihuo = zaihuo;

}

private int xuhao;

private int jiage;

private String name;

public int getXuhao() {

return xuhao;

}

public void setXuhao(int xuhao) {

this.xuhao = xuhao;

}

public int getJiage() {

return jiage;

}

public void setJiage(int jiage) {

this.jiage = jiage;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

子类1

package com.imooc.www;


public class Keche extends Che {



@Override

public String toString() {

return  getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaike()+"人";

}


public Keche(int xuhao,String name,int jiage,int zaike){

this.setZaike(zaike);

this.setXuhao(xuhao);

this.setJiage(jiage);

this.setName(name);

}


}

子类2

package com.imooc.www;


public class Huoche extends Che {

@Override

public String toString() {

return  getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaihuo()+"吨";

}


public Huoche(int xuhao,String name,int jiage,int zaihuo){

this.setJiage(jiage);

this.setName(name);

this.setXuhao(xuhao);

this.setZaihuo(zaihuo);

}


}

子类3

package com.imooc.www;


public class Pika extends Che {

@Override

public String toString() {

return  getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaike()+"人"+getZaihuo()+"吨";

}

public Pika(int xuhao,String name,int jiage,int zaike,int zaihuo){

this.setJiage(jiage);

this.setName(name);

this.setXuhao(xuhao);

this.setZaihuo(zaihuo);

this.setZaike(zaike);

}


}

测试类

package com.imooc.www;

import java.util.Scanner;

public class Initail {

public static void main(String[] args) {

// TODO Auto-generated method stub

 Che [ ] whatChe={new Keche(1,"奥迪A4",500,4),

                               new Keche(2,"马自达6",400,4),

                               new Pika(3,"皮卡雪6",450,4,2),

                               new Keche(4," 金龙 ",800,20),

                               new Huoche(5,"松花江",400,4),

                               new Huoche(6,"依维柯",1000,20)};

        System.out.println("欢迎您使用答答租车系统!");

         System.out.println("您是否要租车?1  是   2  否");

         Scanner shuru1=new Scanner(System.in);

         int a=shuru1.nextInt();

             if(a==1){

                        System.out.println("您可租车的类型及其价目表:");

                        System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");

 

            for(int i=0;i<=whatChe.length-1;i++){

            System.out.println(whatChe[i]);   //重写了toString方法,输出了值而不是那个码

                   }

              System.out.println("请输入想要租车的数量:");

               Scanner shuru2=new Scanner(System.in);

               int b=shuru2.nextInt();

               Che thische[]=new Che[b];         

               int tianjia = 0;//每天的价格

               for(int j=1;j<=b;j++){

               System.out.println("请输入第"+j+"辆车的序号");

               Scanner shuru3=new Scanner(System.in);

               int c=shuru3.nextInt();

 

               tianjia=tianjia+ whatChe[c-1].getJiage();

               thische[j-1]=whatChe[c-1];

    

 }

    

               System.out.println("请输入租车天数");

               Scanner shuru4=new Scanner(System.in);

               int d=shuru4.nextInt();

               System.out.println("您的账单:");

               System.out.println("可载人的车有:");

               int renshu = 0;                       //不赋值为什么不行?

               for(int y=0;y<thische.length;y++){

               if(thische[y].getZaike()!=0){

               System.out.print(thische[y].getName()+"\t");

               renshu=thische[y].getZaike()+renshu;

               }

               }

               System.out.println("共载人:"+renshu+"人");

               System.out.println("可载货的车有:");

               int huoshu = 0;

               for(int k=0;k<thische.length;k++){

               if(thische[k].getZaihuo()!=0){

               System.out.print(thische[k].getName()+"\t");

               huoshu=thische[k].getZaihuo()+huoshu;

 }

 }

               System.out.println("共载货"+huoshu+"吨");

               System.out.println("租车总价格:"+d*tianjia);

               }else if(a==2){

               System.out.println("欢迎您下次使用");

               }else{

               System.out.println("您的输入有误");

 }


}


}


写回答 关注

1回答

  • alanYANG
    2017-05-26 10:25:40

    哈哈哈

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530084 学习 · 6086 问题

查看课程

相似问题