题目
运行结果
**************
欢迎使用本租车系统
**************
请问是否需要租车?1是0否
1
**************
您可以租车的类型以及价目表:
**************
序号 汽车名称 租金 容量
1. 奥迪A4 500元/天 载人:4人
2. 马自达5 400元/天 载人:4人
3. 皮卡雪6 450元/天 载人:4人载货:2.0吨
4. 金龙 800元/天 载人:20人
5. 松花江 400元/天 载货:4.0吨
6. 依维柯 1000元/天 载货:20.0吨
**************
请输入您要租车的数量:
**************
5
**************
您需要租5台车
**************
请依次输入要租用车的编号:
**************
请输入第1辆车的序号:
**************
1
已选中1编号的车型
**************
请输入第2辆车的序号:
**************
3
已选中3编号的车型
**************
请输入第3辆车的序号:
**************
4
已选中4编号的车型
**************
请输入第4辆车的序号:
**************
5
已选中5编号的车型
**************
请输入第5辆车的序号:
**************
6
已选中6编号的车型
**************
请输入您要租车的天数:
**************
3
好的,您准备租用3天
**************
您的账单如下:
**************
------------------------
可以载人的车有:
奥迪A4
皮卡雪6
金龙
共载人:28人
------------------------
可以载货的车有:
皮卡雪6
松花江
依维柯
共载货物:26.0吨
------------------------
您需要支付:9450元
学习重点
- 如何建立对象数组,再批量建立对象
- 方法的重载(不是重建!!!)
批量建立对象,看这两个代码段(注意这里的参数是不一样的,有int有双精度浮点):
//批量创建6个对象
Car[] cars = new Car[6];
cars[0] = new Car(1,"奥迪A4",4,500);
cars[1] = new Car(2,"马自达5",4,400);
cars[2] = new Car(3,"皮卡雪6",4,2.0,450);
cars[3] = new Car(4,"金龙",20,800);
cars[4] = new Car(5,"松花江",4.0,400);
cars[5] = new Car(6,"依维柯",20.0,1000);
再看Car类,这里既是方法的重载,同名方法会根据参数的不同,选择合适的方法执行
public Car(){
}
public Car(int id, String name, int zairen, int zujin) {
super();
this.id = id;
this.name = name;
this.zairen = zairen;
this.zujin = zujin;
}
public Car(int id, String name,double zeihuo, int zujin) {
super();
this.id = id;
this.name = name;
this.zeihuo = zeihuo;
this.zujin = zujin;
}
public Car(int id, String name, int zairen, double zeihuo, int zujin) {
super();
this.id = id;
this.name = name;
this.zairen = zairen;
this.zeihuo = zeihuo;
this.zujin = zujin;
}
源代码
这里只用到两个类,一个Car类一个执行类.
本来想用继承多态的思想来做,觉得代码量很麻烦,
就用最简单的方式来写了.
抱歉的是用到命名都没有按照规范来命名,所以很乱七八糟.
car类
package com.dada;
public class Car {
public int id;
public String name;
public int zairen;
public double zeihuo;
public int zujin;
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 getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public double getZeihuo() {
return zeihuo;
}
public void setZeihuo(double zeihuo) {
this.zeihuo = zeihuo;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
public Car(){
}
public Car(int id, String name, int zairen, int zujin) {
super();
this.id = id;
this.name = name;
this.zairen = zairen;
this.zujin = zujin;
}
public Car(int id, String name,double zeihuo, int zujin) {
super();
this.id = id;
this.name = name;
this.zeihuo = zeihuo;
this.zujin = zujin;
}
public Car(int id, String name, int zairen, double zeihuo, int zujin) {
super();
this.id = id;
this.name = name;
this.zairen = zairen;
this.zeihuo = zeihuo;
this.zujin = zujin;
}
}
zuche类
package com.dada;
import java.util.Scanner;
public class zuchexitong {
public static String star = "**************\n";
public static void main(String[] args) {
System.out.println(star + "欢迎使用本租车系统\n"+ star + "请问是否需要租车?1是0否");
Scanner input =new Scanner(System.in);
if(input.nextInt() == 1){
System.out.println(star + "您可以租车的类型以及价目表:\n" + star + "序号 汽车名称 租金 容量");
//重点 批量创建对象
Car[] cars = new Car[6];
cars[0] = new Car(1,"奥迪A4",4,500);
cars[1] = new Car(2,"马自达5",4,400);
cars[2] = new Car(3,"皮卡雪6",4,2.0,450);
cars[3] = new Car(4,"金龙",20,800);
cars[4] = new Car(5,"松花江",4.0,400);
cars[5] = new Car(6,"依维柯",20.0,1000);
for(int i=0 ; i<cars.length ; i++){
Car car = cars[i];
if(car.getZeihuo() != 0 && car.getZairen() != 0){
System.out.println( car.id +". "
+car.name +" "
+car.zujin +"元/天 "
+ "载人:" + car.zairen + "人"
+ "载货:" + car.zeihuo + "吨");
}else if(car.getZeihuo() == 0 && car.getZairen() != 0){
System.out.println( car.id +". "
+car.name +" "
+car.zujin +"元/天 "
+ "载人:" + car.zairen + "人");
}else{
System.out.println( car.id +". "
+car.name +" "
+car.zujin +"元/天 "
+ "载货:" + car.zeihuo + "吨");
}
}
System.out.println(star + "请输入您要租车的数量:\n" + star);
int rents = input.nextInt();
System.out.println(star + "您需要租"+rents+"台车\n" + star + "请依次输入要租用车的编号:");
Car[] zuche = new Car[rents];
for(int j=0; j<rents;j++){
System.out.print(star + "请输入第"+(j+1)+"辆车的序号:\n"+ star);
int xuhao = input.nextInt();
System.out.println("已选中"+xuhao+"编号的车型");
for(int k=0; k<cars.length;k++){
if(xuhao == cars[k].id){
zuche[j] = cars[k];
break;
}
}
}
System.out.println(star + "请输入您要租车的天数:\n"+ star);
int days = input.nextInt();
System.out.println("好的,您准备租用"+days+"天\n"+ star +"您的账单如下:\n" + star);
int totlemoney = 0;
System.out.println("------------------------");
System.out.println("可以载人的车有:");
int totlepeople = 0;
for(int j=0; j<zuche.length;j++){
if(zuche[j].zairen !=0){
System.out.println(zuche[j].name+" ");
totlepeople = totlepeople + zuche[j].zairen;
if(!(zuche[j].zairen !=0 && zuche[j].zeihuo != 0)){
totlemoney = totlemoney + zuche[j].zujin;
}
}
}
System.out.println("共载人:" + totlepeople + "人");
System.out.println("------------------------");
System.out.println("可以载货的车有:");
double totlezaizhong = 0;
for(int k=0; k<zuche.length;k++){
if(zuche[k].zeihuo !=0){
System.out.println(zuche[k].name+" ");
totlezaizhong = totlezaizhong + zuche[k].zeihuo;
totlemoney = totlemoney + zuche[k].zujin;
}
}
System.out.println("共载货物:" + totlezaizhong + "吨");
System.out.println("------------------------");
System.out.println("您需要支付:" + totlemoney*days + "元");
}else{
System.out.println("再见!期待您再次使用本系统~");
}
}
}