package com.bk.may.test01;
import java.util.Scanner;
public class Test {
//静态初始化对象 ps:传上来的时候直接粘贴忘了分行,,,点进去一看全都贴在一起了
static Car c0 = new Car();
static Scanner sc = new Scanner(System.in);
//初始化所有汽车的属性
public static void setAttribute(){
c0.setCarAttribute(1,500,4,"奥迪A4 ",0);
c0.setCarAttribute(2,400,4,"马自达6 ",0);
c0.setCarAttribute(3,450,4,"皮卡雪6 ",0);
c0.setCarAttribute(4,800,20,"金龙 ",2);
c0.setCarAttribute(5,400,0,"松花江 ",4);
c0.setCarAttribute(6,1000,0,"依维柯 ",20);
}
//进入系统时询问使用者是否要租车
public static void enterSystem() {
System.out.println("欢迎使用哒哒租车系统:\n请问您是否要租车?(1.是 2.否)");
int num = sc.nextInt();
if(num == 1)
mainSystem();
else if(num == 2)
System.out.println("感谢您的来访!");
else {
System.out.println("输入信息非法,请重试!");
enterSystem();
}
}
//main方法执行设置属性方法和进入系统方法
public static void main(String[] args) {
setAttribute();
enterSystem();
}
//主菜单方法
public static void mainSystem() {
int car_num = 0;
//展示租车信息
System.out.println("以下是您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
for(int i=1;i<7;i++)
c0.showInformation(i);
//询问租车数量
do {
System.out.println("请输入您要租汽车的数量:");
car_num = sc.nextInt();
if(car_num<1) {
System.out.println("输入数量超出范围,请重新输入!");
}
}while(car_num<1);
//根据租车数量设置数组的长度
//c0.setC_r_n(car_num); 暂时没有用到
for(int i=1; i<=car_num; i++) {
System.out.println("请输入第"+i+"辆车的序号:");
int s_n = sc.nextInt();
if(s_n>0 && s_n<7)
c0.scanInformation(i,s_n);
else {
System.out.println("输入序号超出范围,请重新输入!");
i--;
}
}
System.out.println("请输入您要租汽车的天数:");
int car_days = sc.nextInt();
while(car_days<=0){
System.out.println("输入天数不在法定范围内,请重新输入!");
System.out.println("请输入您要租汽车的天数:");
car_days = sc.nextInt();
}
//传递租车天数后打印账单
c0.setA_r(car_days);
c0.settleAccounts();
}
}
public class Car {
//分别定义:序号、租金/天,载人量
private int seq_name[] = new int[10];
private int rent[] = new int[10];
private int per_cap[] = new int[10];
private int car_rent_num[] = new int[100];
private int all_rent_daily = 0;
private int all_rent = 0;
private int all_per_cap = 0;
//定义载货量
private double fre_vol[] = new double[10];
private double all_fre_vol = 0;
//定义汽车名称
private String car_name[] = new String[10];
private String all_per_cap_name = "";
private String all_fre_vol_name = "";
//当载人数>0时返回字符串
private String printPer_cap(int s_n){
if(per_cap[s_n-1]>0){
return "载人数:"+per_cap[s_n-1]+"人 ";
}
else{
return " ";
}
}
//当载货量>0时返回字符串
private String printFre_vol(int s_n){
if(fre_vol[s_n-1]>0){
return "载货量:"+fre_vol[s_n-1]+"吨 ";
}
else{
return "";
}
}
//返回含有汽车相关信息的字符串
private String printFirst(int s_n){
return seq_name[s_n-1]+". "+car_name[s_n-1]+" "+rent[s_n-1]+"元/天 ";
}
//输出汽车的所有相关信息
public void showInformation(int s_n){
System.out.println(printFirst(s_n)+printPer_cap(s_n)+printFre_vol(s_n));
}
//统计所租汽车的信息
public void scanInformation(int i,int s_n){
car_rent_num[i-1] = s_n;
all_rent_daily += rent[s_n-1];
if(per_cap[s_n-1]>0) {
all_per_cap += per_cap[s_n-1];
all_per_cap_name += car_name[s_n-1]+" ";
}
if(fre_vol[s_n-1]>0) {
all_fre_vol += fre_vol[s_n-1];
all_fre_vol_name += car_name[s_n-1]+" ";
}
}
//暂时没有用到
int getC_r_n(int s_n){
return car_rent_num[s_n];
}
String getC_n(){
return " "+car_name;
}
//定义一个set方法给数组赋值
void setCarAttribute(int s_n, int r, int p_c, String c_n, double f_v){
seq_name[s_n-1] = s_n;
rent[s_n-1] = r;
per_cap[s_n-1] = p_c;
car_name[s_n-1] = c_n;
fre_vol[s_n-1] = f_v;
}
//设置所租车数组的长度并储存所租的每一辆车的序号
/* 暂时没有用到,可以通过检索每种车序号出现的次数打印出完整的可用车辆
* eg: "奥迪A4*5 松花江*3"
* 但是已经懒得做了23333
void setC_r_n(int c_n){
int car_rent_num[] = new int[c_n];
}
*/
//根据租车天数和每日租金计算得出总租金
void setA_r(int c_ds){
all_rent = c_ds*all_rent_daily;
}
//输出账单
public void settleAccounts() {
System.out.println("您的账单:");
if(all_per_cap>0)
System.out.println("***可载人的车有:\n"+all_per_cap_name+" 共载人:"+all_per_cap+"人");
if(all_fre_vol>0)
System.out.println("***可载货的车有:\n"+all_fre_vol_name+" 共载货:"+all_fre_vol+"吨");
System.out.println("***租车总价格:"+all_rent+"元");
}
}