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

Java入门第二季,第6章,练习,答答租车系统

标签:
Java
package car;

public class Car {
	private int id;
	private String name;
	private int price;
	
	public Car(int id, String name, int price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 car;

public class GoodCar extends Car {
	private int goodcapacity;
	
	public GoodCar(int id, String name, int price, int goodcapacity) {
		super(id, name, price);
		this.goodcapacity = goodcapacity;
	}
	
	public int getGoodcapacity() {
		return goodcapacity;
	}

	public void setGoodcapacity(int goodcapacity) {
		this.goodcapacity = goodcapacity;
	}
	
}
------------------------------------------------------------------
package car;

public class GPCar extends Car {
	private int goodcapacity;
	private int peoplecapacity;
	
	public GPCar(int id, String name, int price, int goodcapacity, int peoplecapacity) {
		super(id, name, price);
		this.goodcapacity = goodcapacity;
		this.peoplecapacity = peoplecapacity;
	}
	public int getGoodcapacity() {
		return goodcapacity;
	}
	public void setGoodcapacity(int goodcapacity) {
		this.goodcapacity = goodcapacity;
	}
	public int getPeoplecapacity() {
		return peoplecapacity;
	}
	public void setPeoplecapacity(int peoplecapacity) {
		this.peoplecapacity = peoplecapacity;
	}
	
}
-----------------------------------------------------------------------------
package car;

public class PeopleCar extends Car {
	private int peoplecapacity;
	
	public PeopleCar(int id, String name, int price, int peoplecapacity) {
		super(id, name, price);
		this.peoplecapacity = peoplecapacity;
	}

	public int getPeoplecapacity() {
		return peoplecapacity;
	}

	public void setPeoplecapacity(int peoplecapacity) {
		this.peoplecapacity = peoplecapacity;
	}
	
}
----------------------------------------------------------------------
package main;

import java.util.Scanner;

import car.*;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PeopleCar pc1 = new PeopleCar(1,"奥迪A4",500,4);
		PeopleCar pc2 = new PeopleCar(2,"马自达6",400,4);
		GPCar gpc = new GPCar(3,"皮卡雪6",450,2,4);
		PeopleCar pc3 = new PeopleCar(4,"金龙",800,20);
		GoodCar gc1 = new GoodCar(5,"松花江",400,4);
		GoodCar gc2 = new GoodCar(6,"依维柯",1000,20);
		Scanner sc = new Scanner(System.in);
		boolean keep = true;
		System.out.println(">>>>>>>>>>>>>>>>>>>>打打租车系统<<<<<<<<<<<<<<<<<<<<<<");
		while(keep){
			System.out.print("是否租用汽车1/0:");
			if("0".equals(sc.next())){
				System.out.println(">>>>>>>>>>>>>>>>>>>系统已退出<<<<<<<<<<<<<<<<<<<");
				break;
			}
 
			System.out.println("您可租车的类型及价目表:");
			System.out.println("1. 奥迪A4 500元/天 载人:4人\n"
					          +"2. 马自达6 400元/天 载人:4人\n"
					          +"3. 皮卡雪6 450元/天 载人:4人载货:2吨\n"
					          +"4. 金龙 800元/天 载人:20人\n"
					          +"5. 松花江 400元/天 载货:4吨\n"
					          +"6. 依维柯 1000元/天 载货:20吨\n");
			
			int peoplesum=0;
			int goodsum=0;
			int pricesum=0;
			String carname="";
			
			System.out.print("输入你租用车的数量:");
			int carnum = sc.nextInt();
			for(int i=0;i<carnum;i++){
				System.out.print("输入你租用车的id:");
				int id = sc.nextInt();
				if(id==1){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=pc1.getName()+"  ";
					peoplesum += pc1.getPeoplecapacity();
					pricesum += pc1.getPrice()*day;
				}else if(id==2){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=pc2.getName()+"  ";
					peoplesum += pc2.getPeoplecapacity();
					pricesum += pc2.getPrice()*day;
				}else if(id==3){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=gpc.getName()+"  ";
					peoplesum += gpc.getPeoplecapacity();
					goodsum += gpc.getGoodcapacity();
					pricesum += gpc.getPrice()*day;
				}else if(id==4){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=pc3.getName()+"  ";
					peoplesum += pc3.getPeoplecapacity();
					pricesum += pc3.getPrice()*day;
				}else if(id==5){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=gc1.getName()+"  ";
					goodsum += gc1.getGoodcapacity();
					pricesum += gc1.getPrice()*day;
				}else if(id==6){
					System.out.print("输入租用天数:");
					int day = sc.nextInt();
					carname +=gc2.getName()+"  ";
					goodsum += gc2.getGoodcapacity();
					pricesum += gc2.getPrice()*day;
				}else{
					System.out.println("您输入的id有误!");
				}
			}
			System.out.println("选择的车有:"+carname);
			System.out.println("租金:"+pricesum);
			System.out.println("载人量:"+peoplesum+"人");
			System.out.println("载货量:"+goodsum+"吨");
		}
	}	
}

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消