研究了两天的代码,求大神指点

来源:12-2 项目问题解析 1

慕斯1379455

2017-07-06 23:17

//父类

package com.tow;


public abstract class Car {

int number;

String name;

float price;

public Car(int number,String name,float price){

this.number=number;

this.name=name;

this.price=price;

}

    @Override

public String toString() {

return number+"\t"+name+"\t"+price+"\n";

}


public int getCarcapacity() {

// TODO Auto-generated method stub

return 0;

}


public int getTrackcapacity() {

// TODO Auto-generated method stub

return 0;

}


    

}

//子类1

package com.tow;


public class Cars extends Car {

private int Carcapacity;

public Cars(int number, String name, float price,int Carcapacity) {

super(number, name, price);

this.setCarcapacity(Carcapacity);

// TODO Auto-generated constructor stub

}


public int getCarcapacity() {

return Carcapacity;

}


public void setCarcapacity(int carcapacity) {

Carcapacity = carcapacity;

}


@Override

public String toString() {

return number+"\t"+name+"\t"+price+"\t\t"+Carcapacity+"\n";

}


}

//子类2

package com.tow;


public class Track extends Car {

private int Trackcapacity;

public Track(int number, String name, float price,int Trackcapacity) {

super(number, name, price);

this.setTrackcapacity(Trackcapacity);

// TODO Auto-generated constructor stub

}



public int getTrackcapacity() {

return Trackcapacity;

}


public void setTrackcapacity(int trackcapacity) {

Trackcapacity = trackcapacity;

}


@Override

public String toString() {

return number+"\t"+name+"\t"+price+"\t\t\t"+Trackcapacity+"\n";

}


}

//子类3

package com.tow;


public class Pickup extends Car {

private int Carcapacity;

private int Trackcapacity;

public Pickup(int number, String name, float price,int Carcapacity,int Trackcapacity) {

super(number, name, price);

this.setCarcapacity(Carcapacity);

this.setTrackcapacity(Trackcapacity);

// TODO Auto-generated constructor stub

}

public int getCarcapacity() {

return Carcapacity;

}

public void setCarcapacity(int carcapacity) {

Carcapacity = carcapacity;

}

public int getTrackcapacity() {

return Trackcapacity;

}

public void setTrackcapacity(int trackcapacity) {

Trackcapacity = trackcapacity;

}

@Override

public String toString() {

return number+"\t"+name+"\t"+price+"\t\t"+Carcapacity+"\t"+Trackcapacity+"\n";

}


}

//测试类

package com.tow;

import java.util.*;

public class Initail {


public static void main(String[] args) {

// TODO Auto-generated method stub

    @SuppressWarnings("resource")

Scanner input=new Scanner (System.in);

    System.out.println("##欢迎使用DADA租车系统##");

    System.out.println("请问您是否租车:1.是,2.否");

    int choice=input.nextInt();

    switch(choice){

    case 1:

    System.out.println("下面是租车信息:");

    System.out.println("序号"+"\t"+"车型"+"\t"+"单价(天/元)"+"\t"+"载客以及载货量(t)");

    Car[] news=new Car[5];

    news[0]=new Cars(1,"奥迪A8",800,5);

    news[1]=new Cars(2,"金龙大巴",700,50);

    news[2]=new Track(3,"金杯",400,45);

    news[3]=new Track(4,"东风悦达",650,70);

    news[4]=new Pickup(5,"皮卡",500,4,60);

    System.out.println(Arrays.toString(news));

    System.out.print("请您输入租赁天数:");

     int day=input.nextInt();

     System.out.println("租用:"+day+"天");

     System.out.println("请您根据车型序号选择车型,输入“8”结束");

     int[] rent=new int[6];

    for(int i=1;i<6;i++){

   

        System.out.print("请输入您要选择的车型:");

          int score1=input.nextInt();

          if(score1==8){

        break;

          }else{

           System.out.print("请输入您要组的数量:");

           int score2=input.nextInt();

           float sum=news[score1-1].price*day;

           System.out.println("车型:"+news[score1-1].name+"\t"+score2+"辆"+"\t"+"合计"+sum+"元"); 

           float sum2=0;

           sum2=sum+sum2;

           System.out.println("总计:"+sum2);

           rent[score1]=score2;

    }

          

    }

   

    System.out.println("如下是您的租车信息,请您核对:");

    float sum=0;

    for(int i=0;i<6;i++){

    if(rent[i]!=0){

    System.out.println(news[i-1].name+":\t"+rent[i]+"辆"+"\t"+"单价(元/天):"+rent[i]*news[i-1].price+"\t\t"+"载客量:"+"\t"+rent[i]*news[i-1].getCarcapacity()+"人"+

        "\t"+"载重量:"+rent[i]*news[i-1].getTrackcapacity()+"吨"+"\t"+"合计:"+rent[i]*(rent[i]*news[i-1].price)*day+"元");

    sum+=rent[i]*rent[i]*news[i-1].price*day;

    }

   

    }

    System.out.println("租车总费用:"+sum+"元");

    break;

    case 2:

    System.out.println("欢迎使用,再见!");

    break;

    default:

    System.out.println("您的输入有误,请重新输入!");

    }

    

}


}


写回答 关注

1回答

  • 慕勒0975681
    2017-07-07 11:38:51

    可以去面试去了

Java入门第二季 升级版

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

530655 学习 · 6091 问题

查看课程

相似问题