我的嗒嗒租车系统----- 花了一下午做出来,还有很多不足,请大家指正~

来源:12-1 综合练习

biofool_0001

2015-03-11 16:14

package com.imooc;

public abstract class Car { //所有车的父类
	public  String name;		//车名
	public int price;		//价格
	
	public abstract void showInfo();	//抽象方法,子类继承时重写,显示车名、价格、载客或载货量

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

}
package com.imooc;

public class Auto extends Car {	//汽车类
	public String name;
	public int price;
	public int capPerson;	//载客量
	
	public Auto(String name,int price,int capPerson){
		this.name=name;
		this.price=price;
		this.capPerson=capPerson;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getCapPerson() {
		return capPerson;
	}
	public void setCapPerson(int capPerson) {
		this.capPerson = capPerson;
	}
	@Override
	public void showInfo() {
		// TODO 自动生成的方法存根
		System.out.println(getName() + '\t' +getPrice() +"元/天" + '\t' + "载人:"+getCapPerson() +"人");
	}

}
package com.imooc;

public class Pickup extends Car {	//皮卡类
	public String name;
	public int price;
	public int capPerson;	//载客量
	public int capThings;	//载货量
	
	//构造方法
	public Pickup(String name,int price,int capPerson,int capThings){
		this.name = name;
		this.price = price;
		this.capPerson = capPerson;
		this.capThings = capThings;
	}
	
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getCapPerson() {
		return capPerson;
	}

	public void setCapPerson(int capPerson) {
		this.capPerson = capPerson;
	}

	public int getCapThings() {
		return capThings;
	}

	public void setCapThings(int capThings) {
		this.capThings = capThings;
	}



	@Override
	public void showInfo() {
		// TODO 自动生成的方法存根
		System.out.println(getName() + '\t' +getPrice() +"元/天" + '\t' + "载人:"+getCapPerson() +"人"+ "  载货:"+getCapThings() +"吨");
	}

}
package com.imooc;

public class Truck extends Car {	//货车类
	public String name;
	public int price;
	public int capThings;
	
	//构造方法
	public Truck(String name,int price,int capThings){
		this.name = name;
		this.price = price;
		this.capThings = capThings;	//载货量
	}
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getPrice() {
		return price;
	}


	public void setPrice(int price) {
		this.price = price;
	}


	public int getCapThings() {
		return capThings;
	}


	public void setCapThings(int capThings) {
		this.capThings = capThings;
	}

	
	@Override
	public void showInfo() {
		// TODO 自动生成的方法存根
		System.out.println(getName() + '\t' +getPrice() +"元/天" + '\t' + "载货:"+getCapThings() +"吨");
	}

}
package com.imooc;

import java.util.*;

public class DadaRent {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

//创建车辆信息
		Car[] allRent = {new Auto("奥迪A4",500,4),new Auto("马自达6",400,4),new Pickup("皮卡雪6",450,4,2),new Auto("金龙  ",800,20),new Truck("松花江",400,4),new Truck("依维河",1000,20)};
		System.out.println("欢迎使用嗒嗒租车系统:");
		System.out.println("您是否想要租车:1是  0否");

	
//显示租车信息
		Scanner input = new Scanner(System.in);
		int choice = input.nextInt();
		while(choice!=0||choice !=1)
		{	//如果输入不为0或1,则重新输入
			if(choice ==0){
				System.out.println("感谢您使用嗒嗒租车系统,下次再见!");
				break;
			}else if(choice ==1){
				System.out.println("您可租车的类型及其价目表:");
				System.out.println("序号" + '\t' + "汽车名称" + '\t' + "租金" + '\t' +"容量");
				for(int i=0;i<allRent.length;i++){
					System.out.print((i+1) + ".\t");
					allRent[i].showInfo();
				}
				System.out.println("请输入想要租车的数量:");
				break;
			}else {
					System.out.println("请输入正确的数字:1是  0否");
					choice = input.nextInt();
				}
			}
		
		int carNum = input.nextInt();	//租车数量
		Car[] choiceCar = new Car[carNum];		//将客户选择的车辆对象放入choiceCar数组
		for(int i=0;i<carNum;i++){
			System.out.println("请输入第" + (i+1) +"辆车的序号:");
			int num =input.nextInt();//每辆车的序号
			choiceCar[i]=allRent[num-1];
		}
		
		System.out.println("请输入想要租车的天数:");
		int rentDay = input.nextInt();  //租车天数
		
		
//计算并显示账单
		System.out.println("********************您的账单信息如下:********************");
		int dayPrice=0;	//每天租车总价
		
		
		System.out.println(">>>>>>>您要租的车是:   ");
			for(int i=0;i<choiceCar.length;i++){
				dayPrice=choiceCar[i].getPrice()+dayPrice;

				choiceCar[i].showInfo();
			}
			//System.out.println("每天总价:"+dayPrice);
		System.out.println(">>>>>>>您总共要租借:  " + rentDay  + "  天");
		
//计算总载客载货量
		int totalCapPerson = 0;		//总载客量
		int totalCapThings = 0;		//总载货量
		for(int i = 0; i < choiceCar.length; i++){
			//判断所选车是Auto、Truck还是Pickup
			if(choiceCar[i] instanceof Auto){	//汽车载客量
				totalCapPerson += ((Auto)choiceCar[i]).getCapPerson(); 
			}
			
			if(choiceCar[i] instanceof Truck){	//货车载货量
				totalCapThings += ((Truck)choiceCar[i]).getCapThings();
			}
			
			if(choiceCar[i] instanceof Pickup){		//皮卡载客和载货量
				totalCapPerson +=((Pickup)choiceCar[i]).getCapPerson();
				totalCapThings +=((Pickup)choiceCar[i]).getCapThings();
			}
		}
		
		//输出总载货量和总载客量
		System.out.println(">>>>>>>您所要租借的总载客量为: " + totalCapPerson + "人\t" + "总载货量为:" + totalCapThings + "吨");
		int totalPrice = dayPrice*rentDay;	//总价
		System.out.println(">>>>>>>您总共需要支付:  " + totalPrice  + "  元");
		System.out.println("感谢您使用嗒嗒租车系统,下次再见!");
		input.close();
	}
	

	
}


写回答 关注

28回答

  • 天蓝色的彼岸_123
    2015-04-28 13:41:37
    已采纳

    写的不错哦~~学习了……将客户选择的车辆对象放入choiceCar数组这点很棒,我就觉得自己学的太死板了

    提点小建议:

    1、楼主的instanceof用法不是特别推荐啊,没有好好利用面向对象中的多态性,可以在父类里面直接定义属性——载客量和载货量

    2、还有关于异常的处理,如果能加上就更完美了~~~


    慕雪0737... 回复慕粉3904...

    可以看看我写的

    2017-10-11 11:03:26

    共 3 条回复 >

  • 慕移动6171685
    2022-07-19 05:36:11

    明白了,是我自己搞错了,原来loop=$a这个是写test.php变量名而不是写变量的值,只要把loop=$a改为loop=$article就可以了

  • 枸杞我最爱
    2018-06-18 11:31:26

    choiceCar[i]=allRent[num-1];   请问这一句可以解释一下吗


  • 慕九州7146727
    2017-11-20 11:40:50

    厉害,我最近也在看慕客网,菜鸟阶段,大三了,希望好好学习一下,请多帮助。

  • 慕九州2344582
    2017-09-23 13:17:10

    好多病句!整个抄下来了!不过还是谢谢了!我一点不会写!最重要的你没抓dayprice!晕

  • 精慕门7114099
    2017-08-06 22:48:09

    为什么我自己写的是这样的?

    http://img.mukewang.com/59872c0e0001052e07450352.jpg

  • 精慕门7114099
    2017-08-06 22:43:58
    这个程序有不足的地方吗? 子类中可以不写父类里的属性了

  • 扶不起的蜗牛
    2016-07-15 15:25:42

    要输出序号的时候,输出的序号数大于车的数量这个怎么没有加个判断?

  • Maplelove
    2016-07-05 03:50:23

    感谢楼主的代码,给了很大的启发。不然都觉得无从下手

  • 北道
    2016-06-26 22:57:02

    谢谢   非常好

  • 菜菜AN
    2016-04-17 15:48:21

    我想了一下午都没想好怎么写。。。

  • 涞涞涞
    2016-03-28 11:41:07

    同问,Car[] allRent是什么?是不是用父类名定义子类组成的数组?

  • 不再怀念
    2016-01-16 19:03:07

    牛B 牛B 

  • QQ玩转大峰车
    2016-01-15 21:32:46

    厉害,学的蛮好的

  • 慕粉7971722
    2015-12-19 15:59:04

    楼主,你父类为什么要用get与set的方法呢?get与set不是针对封闭类的吗?


  • stonerock
    2015-12-04 09:47:14

    Car[] allRent =

    这样的写法是数组还是什么?如果是数组的话前面应该是类型才对。

    如果是方法的话,那么【】代表数组的意思吗?

    意轩邈

    对象数组

    2016-08-22 21:50:49

    共 1 条回复 >

  • 小V爱大黎
    2015-11-22 20:03:21

    写得不错,值得我去借鉴


  • 慕粉8648889
    2015-11-10 11:07:44

    这个while循环写的好

  • MR一辰
    2015-11-05 13:28:51

    你的main方法里面的while循环条件可以直接为true

  • MR一辰
    2015-11-05 12:58:17

    不错不错

  • 小华的好朋友
    2015-10-28 21:45:56

    楼主写的很好  给了我很大的帮助 说声谢谢

  • sgcwddhr
    2015-09-24 17:10:30

    为什么父类的属性name 和price 在子类中重新定义?

  • 我是菜鸟一级
    2015-08-04 15:57:43

    父类中有get和set,还有变量申明的,子类可以不用重复写,个人感觉是这样吧,新人,说错了别打脸

  • 王宁已经被注册
    2015-07-31 15:20:23

    太棒了!

  • 珍贞范儿
    2015-06-14 10:59:55

    。。。

  • Frank_l
    2015-03-27 16:33:23

    这个程序执行后,在输入0后,应该退出,但是还是提示用户选择租车数量

    慕函数810... 回复biofoo...

    不错~~

    2017-04-17 20:23:48

    共 4 条回复 >

  • 夏进牛奶
    2015-03-18 16:44:28

    mark   写的很棒 学习了!!!!

  • 一不易
    2015-03-11 23:30:24

    额,看着起码是比我刚写的好些。

Java入门第二季 升级版

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

530084 学习 · 6086 问题

查看课程

相似问题