提交作业,大家可以参考下,我觉得还能优化,望各位多提意见

来源:12-1 综合练习

慕丝4409378

2019-07-06 12:34

package DDproject;


public abstract class car {     //汽车父类

public int id;

public String name;

    public int price;

    public int people;

    public int carry;

    public abstract void run();  

}

public class HuoCar extends car {   //货车子类

public HuoCar(int id,String name,int price,int carry) {

this.id=id;

this.name=name;

this.price=price;

this.carry=carry;

}      

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载货:"+carry+"吨");

}


}

public class buscar extends car {         //载人子类

public buscar(int id,String name,int price,int people) {

this.id=id;

this.name=name;

this.price=price;

this.people=people;

}

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+people+"人");

}

}

public class PKcar extends car {    //皮卡子类

public PKcar(int id,String name,int price,int people,int carry) {

this.id=id;

this.name=name;

this.price=price;

this.people=people;

this.carry=carry;

}

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+people+"人"+"\t"+"载货:"+carry+"吨");

}

}

package DDproject;


import java.util.Scanner;


public class Demotest {      //测试类

public void ZuChe() {      //是否进人租车系统方法

Scanner input = new Scanner(System.in);

try {

int select = input.nextInt();

if (select == 1) {

means();

} else if (select == 0) {

System.out.println("感谢您使用答答租车系统~再见!");

} else {

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

System.out.println("您是否需要租车:1、是" + "\t" + "0、否");

ZuChe();

}

} catch (Exception e) {

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

System.out.println("您是否需要租车:1、是" + "\t" + "0、否");

ZuChe();

}

}


public void means() {     //租车系统选车主方法

car che[] = { new buscar(1, "奥迪4A", 500, 4), new buscar(2, "马自达6", 400, 4), new PKcar(3, "皮卡雪6", 450, 4, 2),

new buscar(4, "金龙", 800, 20), new HuoCar(5, "松花江", 400, 4), new HuoCar(6, "依维柯", 1000, 20) };

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

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

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

che[i].run();

}

double money = 0, ton = 0;

int sum = 0;

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

Scanner input = new Scanner(System.in);

int amount = input.nextInt();

int ID[] = new int[amount];

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

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

int id = input.nextInt();

money = money + che[id - 1].price;

sum = sum + che[id - 1].people;

ton = ton + che[id - 1].carry;

ID[i] = id;

}

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

int day = input.nextInt();

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

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

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

if (che[ID[i] - 1].people != 0) {

System.out.print(che[ID[i] - 1].name + "\t");

}

}

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

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

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

if (che[ID[i] - 1].carry != 0) {

System.out.print(che[ID[i] - 1].name + "\t");

}

}

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

System.out.println("***租车总价格:" + money * day + "元");

}


public static void main(String[] args) {

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

System.out.println("您是否需要租车:1、是" + "\t" + "0、否");

Demotest t = new Demotest();

t.ZuChe();

}

}


写回答 关注

4回答

  • 慕用3459506
    2019-10-15 10:45:45

    我还是有个问题啊 ,就是载人的车和载重的车就没有区分呀 

    载人的车打印的也是

    System.out.print(che[ID[i] - 1].name + "\t");


    载重的车打印也是

    System.out.print(che[ID[i] - 1].name + "\t");



  • 是个杀手莫得感情
    2019-07-20 16:05:52

    try-catch那段,else{} 可以省掉,或者将try-catch换成while测无限循环语句,并且用while的话能用continue而不用嵌套,try-catch用在这感觉不合适;

    总体感觉挺简洁到位的,赞?。

    慕丝4409...

    用try-catch语句比较安全

    2019-07-20 16:18:40

    共 2 条回复 >

  • qq_慕沐4213791
    2019-07-20 10:58:10

    请问那个means();是什么意思?

    qq_慕沐4...

    不好意思 没看到 现在看到了

    2019-07-20 10:58:57

    共 1 条回复 >

  • qq_慕圣2069850
    2019-07-10 20:35:55

    你在父类里调用输出函数就可以。不需要每个子类都写

Java入门第二季 升级版

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

530099 学习 · 6086 问题

查看课程

相似问题