答答租车系统

小白初学者,没有思路,参考幕友的代码写的,勿喷,接受各位大佬提的意见,谢谢!
父类Car

package com.imooc.www;
	public class Car {
	int id;	//序列号
	String name; //汽车名称
	double price; //价格
	int manned; //载人
	double cargo; //载货
//定义有参数的构造方法
public Car(int id, String name, double price, int manned, double cargo){
	this.id=id;
	this.name=name;
	this.price=price;
	this.manned=manned;
	this.cargo=cargo;
}
//重写toString方法,输出对象的时候输出对象的属性值
public String toString(){
	return id+"\t"+name+"\t"+price+"元\t"+manned+"人\t"+cargo+"吨";
}

}

子类汽车类AutoMobile

package com.imooc.www;

public class AutoMobile extends Car{

//定义子类的有参的构造方法,并显示的调用父类的参数值,如不调则编译会出错
public AutoMobile(int id, String name, double price, int manned) {
	super(id, name, price, manned, 0);
}

//重写toString方法,输出子类对象时输出子类对象的属性值
public String toString(){
	
	return id+"\t"+name+"\t"+price+"元/天\t"+"载人:"+manned+"人";
}

}

子类货车类Trucks

package com.imooc.www;

public class Trucks extends Car {
//带参数的子类的构造方法
public Trucks(int id, String name, double price, double cargo){
	super(id, name, price, 0, cargo);
}

//重写toString方法
public String toString(){
	return id+"\t"+name+"\t"+price+"元/天\t"+"载货:"+cargo+"吨";
	}

}

子类皮卡类Pickup

package com.imooc.www;

public class Pickup extends Car {
	public Pickup(int id, String name, double price, int manned, double cargo){
		super(id,name,price,manned,cargo);
	}
	public String toString(){
		return id+"\t"+name+"\t"+price+"元/天\t"+"载人:"+manned+"人 "+" 载货:"+cargo+"吨";
	}
}

主函数

package com.imooc.www;

import java.util.Scanner;

public class TestMain {
	public static void main(String[] args) {
		Car [] cars={
				new AutoMobile(1,"奥迪A4",500,4),
				new AutoMobile(2,"马自达6",400,4),
				new Pickup(3,"皮卡雪6",450,4,2),
				new AutoMobile(4,"金龙",800,20),
				new Trucks(5,"松花江",400,4),
				new Trucks(6,"依维柯",1000,20)
		};
		System.out.println("欢迎使用答答租车系统");
		System.out.println("您是否要租车:1是 0否");
		Scanner input=new Scanner(System.in);
		int a=input.nextInt();
		if(a==0){
			System.out.println("欢迎再次使用,再见");
		}
		if(a==1){
			System.out.println("您可租车的类型及其价目表");
			System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"\t"+"容量");
			for(int i=0; i<cars.length; i++){
				System.out.println(cars[i]);
			}
			System.out.println("请输入您要租汽车的数量:");
			int nums=input.nextInt();  //nums为租车数量

			int sumPrice=0;  //定义变量,用来保存车的价格之和
			int sumManned=0;  //定义变量,用来保存车的载人数之和
			int sumCargo=0;  //定义变量,用来保存载货吨数之和
			int [] names=new int[nums];  //定义数组,用来保存输入车辆的序号信息
			for(int j=1; j<=nums; j++){	
				System.out.println("请输入第"+j+"辆车的序号");
				 int c=input.nextInt();  //记录车的序号信息,并保存在变量c里面	
				 sumPrice+=cars [c-1].price; //求所输入车辆的价格之和 ;减一的目的是当输入车辆数1时,车辆序号为1的车在数组中的位置是0,因为数组的下标是从0开始
				 sumManned+=cars [c-1].manned; //求所输入车辆的载人数之和
				 sumCargo+=cars [c-1].cargo; //求所输入车辆的载货吨数之和
				 names[j-1]=c; //把c的值保存在names变量中
				}	
			System.out.println("请输入租车的天数:");
			int day=input.nextInt();
			System.out.println("您的账单:");
			System.out.println("***可载人的车有:");
			//使用for循环打印可载人的车,h小于租车的数量nums
			for(int h=0; h<nums; h++){   
				//添加判断条件,通过数组cars调用数组中的manned值,并筛选出数组中的manned的值不为0的车,也就是可载人的车,载货的车为0
				if(cars[names[h]-1].manned != 0){
					System.out.print(cars[names[h]-1].name+"\t");
				}
			}
			System.out.println("共载人"+sumManned+"人");
			
			System.out.println("***可载货的车有:");
			for(int h=0; h<nums; h++){
				if(cars[names[h]-1].cargo != 0){
					System.out.print(cars[names[h]-1].name+"\t");
				}
			}
			System.out.println("共载货"+sumCargo+"吨");
			
			System.out.println("***租车总价格:"+sumPrice*day+"元");
		}		
	}
}

运行结果

欢迎使用答答租车系统
您是否要租车:1是 0否
1
您可租车的类型及其价目表
序号	汽车名称	租金		容量
1	奥迪A4	500.0元/天	载人:4人
2	马自达6	400.0元/天	载人:4人
3	皮卡雪6	450.0元/天	载人:4人  载货:2.0吨
4	金龙		800.0元/天	载人:20人
5	松花江	400.0元/天	载货:4.0吨
6	依维柯	1000.0元/天	载货:20.0吨
请输入您要租汽车的数量:
4
请输入第1辆车的序号
1
请输入第2辆车的序号
2
请输入第3辆车的序号
3
请输入第4辆车的序号
4
请输入租车的天数:
3
您的账单:
***可载人的车有:
奥迪A4	马自达6	皮卡雪6	金龙	共载人32人
***可载货的车有:
皮卡雪6	共载货2吨
***租车总价格:6450元
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值