为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

不错不错

0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

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

1 回复 有任何疑惑可以回复我~

太棒了!

0 回复 有任何疑惑可以回复我~

。。。

0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~
#1

biofool_0001 提问者

感谢指正,已改~
2015-03-27 回复 有任何疑惑可以回复我~
#2

慕函数8108156 回复 biofool_0001 提问者

楼主,这个你是怎么更改的呢
2017-04-17 回复 有任何疑惑可以回复我~
#3

慕函数8108156 回复 biofool_0001 提问者

不错~~
2017-04-17 回复 有任何疑惑可以回复我~
查看1条回复

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

0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~
首页上一页123下一页尾页

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信