继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java第二季租车系统作业

慕粉3347856
关注TA
已关注
手记 2
粉丝 0
获赞 2

用到的知识点:
1.封装
2.继承
3.多态
4.对象数组

package com.imooc;

public abstract class Car {
	private int orderNum;//序号
	private String name;//车的名称
	double  money;//租金
	private int mannedCapacity;//载人量
	private int cargoVolume;//载客量
	
	public int getOrderNum() {
		return orderNum;
	}
	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public int getMannedCapacity() {
		return mannedCapacity;
	}
	public void setMannedCapacity(int mannedCapacity) {
		this.mannedCapacity = mannedCapacity;
	}
	public int getCargoVolume() {
		return cargoVolume;
	}
	public void setCargoVolume(int cargoVolume) {
		this.cargoVolume = cargoVolume;
	}



}

package com.imooc;

//载人
public class Manned extends Car {


	public Manned(int orderNum, String name, double money, int mannedCapacity) {
		super.setOrderNum(orderNum);
		super.setName(name);
		super.setMoney(money);
		super.setMannedCapacity(mannedCapacity);
	}

	@Override
	public String toString() {
		return super.getOrderNum() + ".\t" + super.getName() + "\t" + super.getMoney() + "元/天\t载人:" + super.getMannedCapacity() + "人";
	}

}

package com.imooc;

//皮卡
public class Pickup extends Car {

	public Pickup(int orderNum, String name, double money, int mannedCapacity, int cargoVolume) {
		super.setOrderNum(orderNum);
		super.setName(name);
		super.setMoney(money);
		super.setMannedCapacity(mannedCapacity);
		super.setCargoVolume(cargoVolume);
	
	}

	@Override
	public String toString() {
		return  super.getOrderNum() + ".\t" + super.getName() + "\t" + super.getMoney() + "元/天\t载人:"
				+ super.getMannedCapacity() + "人 载货:" + super.getCargoVolume() + "吨";
	}


}

package com.imooc;

//载货
public class Truck extends Car {
	


	public Truck(int orderNum, String name, double money, int cargoVolume) {
			super.setOrderNum(orderNum);
			super.setName(name);
			super.setMoney(money);
			super.setCargoVolume(cargoVolume);
	}

	@Override
	public String toString() {
		return  super.getOrderNum() + ".\t" + super.getName() + "\t" + super.getMoney() + "元/天\t载货:" + super.getCargoVolume() + "吨";
	}

}

package com.imooc;

import java.util.Scanner;

public class Test3 {

	public static void main(String[] args) {
		System.out.println("欢迎使用哒哒租车系统:");
		System.out.println("您是否要租车:1是 0否");
		Scanner sc = new Scanner(System.in);
		if ("0".equals(sc.nextLine())) {
			System.exit(0);// 关闭当前进程。
		}
		Car[] c = { new Manned(1, "奥迪A4", 500, 4), new Manned(2, "马自达6", 400, 4), new Pickup(3, "皮卡雪6", 450, 4, 2),
				new Manned(4, "金龙", 800, 20), new Truck(5, "松花江", 400, 4), new Truck(6, "依维柯", 1000, 20) };
		System.out.println("序号	汽车名称	租金	容量");
		for (int i = 0; i < c.length; i++) {
			System.out.println(c[i]);
		}
		System.out.println("请出入您要租汽车的数量:");
		int num = sc.nextInt();
		//将输入的序号放到数组中
		int[] arr = new int[num];
		for (int i = 0; i < arr.length; i++) {
			System.out.println("请输入第" + (i + 1) + "辆车的序号:");
			arr[i] = sc.nextInt();
			
		}
	    System.out.println("请输入租车天数:");
	    int dayNum = sc.nextInt();//租车天数
	    int mannedSum =0;//载人总数初始值
	    int cargoSum=0;//载货总数初始值
	    double priceSum=0;//租车总价格
	    System.out.println("您的账单:");
	    System.out.println("***可载人的车有:");
	    //打印载人的车;用来求载人量和总价格
	    for (int i = 0; i < arr.length; i++) {
	    	//boolean result = obj instanceof Class
	    	//其中 obj 为一个对象,Class 表示一个类或者一个接口,当 obj 为 Class 的对象,或者是其直接或间接子类,或者是其接口的实现类,结果result 都返回 true,否则返回false。
	    	//判断对象数组中的对象 为载人的类或皮卡的类     
	    	if(c[(arr[i]-1)] instanceof Manned ||c[(arr[i]-1)] instanceof Pickup){
	    	System.out.print(c[(arr[i]-1)].getName()+" ");
	    	mannedSum+=c[(arr[i]-1)].getMannedCapacity();
		}  
	    	priceSum+=c[(arr[i]-1)].getMoney();
	}
	    System.out.println("共载人:"+mannedSum+"人");
	    System.out.println("***可载货的车有:");
	    //打印载货的车;用来总求载货量
	    for (int i = 0; i < arr.length; i++) {
	    	//同理上述
	    	if(c[(arr[i]-1)] instanceof Truck ||c[(arr[i]-1)] instanceof Pickup){
	    	System.out.print(c[(arr[i]-1)].getName()+" ");
	    	cargoSum+=c[(arr[i]-1)].getCargoVolume();
		}

	}
	    System.out.println("共载货:"+cargoSum+"吨");
	    System.out.println("***租车总价格:"+(priceSum*dayNum)+"元");
}
}

结果:

欢迎使用哒哒租车系统:
您是否要租车:1是 0否
1
序号	汽车名称	租金	容量
1.	奥迪A4	500.0元/天	载人:4人
2.	马自达6	400.0元/天	载人:4人
3.	皮卡雪6	450.0元/天	载人:4人 载货:2吨
4.	金龙	800.0元/天	载人:20人
5.	松花江	400.0元/天	载货:4吨
6.	依维柯	1000.0元/天	载货:20吨
请出入您要租汽车的数量:
4
请输入第1辆车的序号:
1
请输入第2辆车的序号:
2
请输入第3辆车的序号:
3
请输入第4辆车的序号:
4
请输入租车天数:
3
您的账单:
***可载人的车有:
奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人
***可载货的车有:
皮卡雪6 共载货:2吨
***租车总价格:6450.0元
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP