受到其他网友的启发用了数组,直接粗暴,但是,惭愧没有用到继承、多态(看得懂,还不会用),等有时间再来构思。
先定义一个类
package com.imooc;
public class Rent {
int number;
String name;
int rent;
int room1;
int room2;
double room3;
//构造方法
public Rent(int number,String name,int rent,int room1) {
this.number = number;
this.name = name;
this.rent = rent;
this.room1 = room1;
System.out.println(number+"."+name+rent+"元/天"+"载人:"+room1+"人");
}//只载人
public Rent(int number,String name,int rent,int room1,int room2) {
this.number = number;
this.name = name;
this.rent = rent;
this.room1 = room1;
this.room2 = room2;
System.out.println(number+"."+name+rent+"元/天"+"载人:"+room1+"人"+"载货:"+ room2 +"吨");
}//载人载货
public Rent(int number,String name,int rent,double room3) {
this.number = number;
this.name = name;
this.rent = rent;
this.room3 = room3;
System.out.println(number+"."+name+rent+"元/天"+"载货:"+ room3 +"吨");
}//只载货
}
然后就是测试类
package com.imooc;
import java.util.Scanner;
import java.util.Arrays;//有用到相应方法先导入相应的包
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
int a =scan.nextInt();
if (a == 1) {//注意区分a=1 和a==1!
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
Rent obj1 = new Rent(1," 奥迪A4 ",500,4);
Rent obj2 = new Rent(2," 马自达6 ",400,4);
Rent obj3 = new Rent(3," 皮卡雪6 ",450,4,2);
Rent obj4 = new Rent(4," 金龙 ",800,20);
Rent obj5 = new Rent(5," 松花江 ",400,4.0);
Rent obj6 = new Rent(6," 依维河 ",1000,20.0);//用初始化构造方法的方式输出租车类型及价目表
String [] car12 = {"奥迪A4","马自达6"};
String [] car56 = {"松花江","依维河"};
String [] car34 = {"皮卡雪6","金龙"};//定义车名数组,车3较特殊,则切成12,34,56
int [] priceMan12 = {500,400};
int [] priceMan34 = {450,800};//前四辆载人,定义载人价格数组,切成12,34
int [] priceGood56 = {400,1000};
int [] priceGood3 = {450};//第3辆和后两辆载货,定义载货价格数组,切成3,56
int [] man12 = {4,4};
int [] man34 = {4,20};//定义载人数数组,切成12,34
int [] good56 = {4,20};
int [] good3 = {2};//定义载货数数组,切成3,56
System.out.println("请输入您要租汽车的数量:");
int b = scan.nextInt();
int i = 0;//控制输出到控制台的语句:请输入第"+i+"辆车的序号:
int j = 0;//控制账单车名显示数组的下标
int sumPriceMan = 0;//计算载人车总价钱
int sumPriceGood = 0;//计算载货车总价钱
int sumMan = 0;//计算载人总数
int sumGood = 0;//计算载货总数
String [] carManZ = new String[6];
String [] carGoodZ = new String[6];//定义两个账单车名显示数组
while (i<b) {//用循环来为账单车名显示数组赋值,计算总价,总载人数,总载货量
i++;
System.out.println("请输入第"+i+"辆车的序号:");
int p = scan.nextInt();
switch (p) {
case 1:
case 2:
//case 3:
//case 4:
carManZ[j] = car12[p-1];
int man1 = man12 [p-1];
int priceMan1 = priceMan12 [p-1];
sumMan += man1;
sumPriceMan += priceMan1;
break;
//case 3:
case 5:
case 6:
carGoodZ[j] = car56 [p-5];
int good1 = good56 [p-5];
int priceGood1 = priceGood56 [p-5];
sumGood += good1;
sumPriceGood += priceGood1;
break;
case 3:
carManZ[j] = car34 [0];
carGoodZ[j] = car34 [0];
man1 = man34 [0];
good1 = good3 [0];
priceGood1 = priceGood3 [0];
sumPriceGood += priceGood1;//姑且把车3的价格累加在计算载货车总价钱的变量上
sumMan += man1;
sumGood += good1;
break;
case 4 :
carManZ[j] = car34 [1];
man1 = man34 [1];
priceMan1 = priceMan34 [1];
sumMan += man1;
sumPriceMan += priceMan1;
break;
}
j++;
}
String cheMan = Arrays.toString(carManZ);
String cheGood = Arrays.toString(carGoodZ);//用Arrays.toString(数组名)来输出字符串数组
System.out.println("请输入租车天数:");
int tian = scan.nextInt();
int sumPrice =(sumPriceGood+sumPriceMan)*tian;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(cheMan+"共载人:"+sumMan+"人");
System.out.println("***载货的车有:");
System.out.println(cheGood+"共载货:"+sumGood+"吨");
System.out.println("***租车总价格:"+sumPrice+"元");
}
}
}
欢迎使用答答租车系统:
您是否要租车:1是 0否
1
您可租车的类型及其价目表:
序号 汽车名称 租金 容量
- 奥迪A4 500元/天载人:4人
- 马自达6 400元/天载人:4人
- 皮卡雪6 450元/天载人:4人载货:2吨
- 金龙 800元/天载人:20人
- 松花江 400元/天载货:4.0吨
- 依维河 1000元/天载货:20.0吨
请输入您要租汽车的数量:
3
请输入第1辆车的序号:
3
请输入第2辆车的序号:
2
请输入第3辆车的序号:
6
请输入租车天数:
2
您的账单:
可载人的车有:
[皮卡雪6, 马自达6, null, null, null, null]共载人:8人
载货的车有:
[皮卡雪6, null, 依维河, null, null, null]共载货:22吨
***租车总价格:3700元