做好了,哈哈,分享给大家参考下,有些我没有去封装好!

来源:12-1 综合练习

慕设计7015894

2015-12-24 17:50

package com.dome;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/*

 * auther:yeshili

 * qq:583870616

 * 初学者

 * */

public class test {

static List<Car> clist = new ArrayList<Car>();

//匿名内部类

public class Car{

String code;//编号

String name;//名称

int money;//每日租金

int passenger;//载客认人数

int cargo;//载货吨位

public Car(String code,String name,int money,int passenger,int cargo){

this.code=code;

this.name=name;

this.money=money;

this.passenger=passenger;

this.cargo=cargo;

}

public void printInfo(){

System.out.println(this.code+"    "+this.name+"    "+this.money+"元     "+this.passenger);

}

public void printResInfo(){

System.out.println(this.code+"    "+this.name+"    "+this.money+"元/天     载人"+this.passenger+" 载货"+this.cargo);

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

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

@SuppressWarnings("resource")

Scanner in= new Scanner(System.in);

System.out.println("你是否要使用租车系统:1是  0否");

int is0=in.nextInt();

if(is0==1){

test t= new test();

Car c1 = t.new Car("1","宝马X1",1000,4,0);

   Car c2 = t.new Car("2","奔驰500",1500,4,0);

   Car c3 = t.new Car("3","东风",2000,2,10);

   Car c4 = t.new Car("4","时风",500,2,2);

   Car c5 = t.new Car("5","松花江",800,4,20);

   Car c6 = t.new Car("6","金龙",6000,20,0);

   clist.add(c1); clist.add(c2); clist.add(c3);

   clist.add(c4); clist.add(c5); clist.add(c6);

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

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

   for(Car temp:clist){

    if(temp.cargo==0){

    temp.printInfo();

    }else{

    temp.printResInfo();

    }

}

   System.out.println("请你输入你要租凭的车辆:");

   //累计各项数值

   int passNum = 0;//载客总人数

   int goNum = 0;//载货总吨位

   int carNum = 0;//租车总数量

   int rentNum = 0;//租赁总天数

   String arrRenName="";//计算可载人车辆

   String arrHuoName="";//计算可载货车辆

   double total = 0.0;//租金总额

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

    int is1= in.nextInt();

    if(is1>0 && is1<=6){

    Car a=clist.get((is1-1));

  arrRenName=arrRenName+"  "+a.name;

  passNum+=a.passenger;

  goNum+=a.cargo;

  total+=a.money;

  carNum++;

  if(a.cargo!=0) arrHuoName=arrHuoName+"  "+a.name;

  if(i<3){

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

  }else{

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

  int is2= in.nextInt();

  rentNum=is2;

  }

    }else{

    i--;

    System.out.println("输入错误,请从新输入辆车的序号");

    }

    }

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

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

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

    if(goNum!=0){

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

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

    }

    System.out.println("租车总价格:"+(total*rentNum)+"   租车总数量:"+carNum+"   租车总天数:"+rentNum);

}else{

System.out.println("参数错误");

}

}

}

写回答 关注

1回答

  • _泥人_
    2015-12-25 10:01:51

    简单的说几点:

        1、二季的最后一节里有提到过,最好能分成多个类来实现,Car是父类,子类分为:载人的车,载货的车,既能载人又能载货的车。

      2、提示用户输入是否要租车的时候,用户输入的不是0或1怎么处理,直接就死掉了。这个地方需要你再想想(还有客户输入要租赁的车的编号的时候也有这个问题,输入的编号没有对应的车)。

      3、这个可能是我个人的想法,也不知道对不对,共同学习。我觉得,这些车的对象不是说你不来租车就没有的,无论这个客户是不是来租车的,这些车都是存在的,所以车的这些对象应当在询问客户时候要租车前就应该准备好的。

    。。。。。随着我们的学习肯定还会发现更多的问题,慢慢完善

Java入门第二季 升级版

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

531114 学习 · 6326 问题

查看课程

相似问题