没有用到继承、封装、接口、抽象类等
有些繁琐,如有问题希望大家指出
// main 类
package com.sysDada;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Welc welc = new Welc();
welc.print();// 打印欢迎界面
if (welc.check != 0) {//判断是否跳出程序
return;
}
Select s = new Select();// 调用Select
s.sel();
System.out.println("谢谢使用,欢迎继续使用!");
System.out.print("是否继续使用?继续使用请输入 1 输入其他任意值退出:");
double i = scan.nextDouble();
while(i==1){
welc.print();// 打印欢迎界面
if (welc.check != 0) {
return;
}
// 调用Select
s.sel();
System.out.println("谢谢使用,欢迎继续使用!");
System.out.print("是否继续使用?继续使用请输入 1 输入其他任意值退出:");
i = scan.nextDouble();
if(i!=1){
System.out.println("再见!");
return;
}
}
System.out.println("再见!");
}
}
//Welc 类
package com.sysDada;
import java.io.PrintStream;
import java.util.Scanner;
public class Welc {
Aodi aodi = new Aodi();
Pika pika = new Pika();
Mazida mazida = new Mazida();
Jinlong jinlong = new Jinlong();
Song song = new Song();
Yi yi = new Yi();
int check = 0;//定义一个判断输入是否错误的数
public void print() {
PrintStream out = System.out;
System.out.println("欢迎使用答答租车系统!\n您是否要租车?1 是 0 否");
Scanner scan = new Scanner(System.in);
double i = scan.nextDouble();
//判断输入的数字 选择打印类型
if (i == 0) {
out.println("系统已退出!");// 不租车则退出系统
check++;//判断是否跳出程序的条件成立
return;
} else if (i == 1) {
out.println("您可租用的汽车类型及其价目表:\n序号\t汽车名称\t\t租金\t\t容量");
} else {
out.println("输入有误,请重试!");
check++;//判断是否跳出程序的条件成立
return;
}
out.println(aodi);
out.println(mazida);
out.println(pika);
out.println(jinlong);
out.println(song);
out.println(yi);
}
}
//select类
package com.sysDada;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import jdk.nashorn.internal.ir.BreakableNode;
public class Select {
int cargo=0;//声明载货车辆的个数
int mancar=0;//声明载人车辆的个数
int sumMoney = 0;// 声明总金额
int sumVolume1 = 0;// 声明总载人量
int sumVolume2 = 0;// 声明总载货量
Aodi aodi = new Aodi();
Pika pika = new Pika();
Mazida mazida = new Mazida();
Jinlong jinlong = new Jinlong();
Song song = new Song();
Yi yi = new Yi();
Scanner scan = new Scanner(System.in);
public void sel() {
PrintStream out = System.out;
out.println("请输入需要租车的数量:");
int i = scan.nextInt();//i作为需要租车的数量
if(i<=0){
out.println("输入有误!");
return;
}
int[] n =new int[i];//定义一个数组保存输入的车型序号
for (int j = 1; j <= i; j++) {
String[] Car=new String[i];
out.print("请选择第" + j + "辆车:");
n[j-1] = scan.nextInt(); //读取输入的车辆序号
// out.println();
switch (n[j-1]) { //判断选择的车型
case 1:
sumMoney = sumMoney + aodi.money;
sumVolume1 = sumVolume1 + aodi.volume;
mancar++;
break;
case 2:
sumMoney = sumMoney + mazida.money;
sumVolume1 = sumVolume1 + mazida.volume;
mancar++;
break;
case 3:
sumMoney = sumMoney + pika.money;
sumVolume1 = sumVolume1 + pika.volume1;
sumVolume2 = sumVolume2 + pika.volume2;
mancar++;
cargo++;
break;
case 4:
sumMoney = sumMoney + jinlong.money;
sumVolume1 = sumVolume1 + jinlong.volume;
mancar++;
break;
case 5:
sumMoney = sumMoney + song.money;
sumVolume2 = sumVolume2 + song.volume2;
cargo++;
break;
case 6:
sumMoney = sumMoney + yi.money;
sumVolume2 = sumVolume2 + yi.volume2;
cargo++;
break;
default:
out.println("输入有误,请重新输入");
j--;
break;
}
/*if (n[j-1]> 6 || n[j-1]< 1) {
out.println("输入有误,请重新输入");
j--;
continue;*/
}
out.println("请输入需要租车的天数:");//输入租车天数
int day=scan.nextInt();
if(i<=0){
out.println("输入有误!");
return;
}
sumMoney=sumMoney*day; //总金额
String[] manCar = new String[i];//定义载人汽车的数组
String[] cagoCar= new String[i];//定义载货汽车的数组
//给载人汽车数组赋值
for(int k=0;k<i;k++){
switch (n[k]) {
case 1:
manCar[k]=aodi.name;
break;
case 2:
manCar[k]=mazida.name;
break;
case 3:
manCar[k]=pika.name;
break;
case 4:
manCar[k]=jinlong.name;
break;
default:
continue;
}
}
//给载货汽车数组赋值
for(int k=0;k<i;k++){
switch (n[k]) {
case 3:
cagoCar[k]=pika.name;
break;
case 5:
cagoCar[k]=song.name;
break;
case 6:
cagoCar[k]=yi.name;
break;
default:
continue;
}
}
//打印载人汽车
if(mancar!=0){
out.print("您的账单:\n载人汽车:");
for(String car:manCar){
if(car!=null)//避免输出空值
out.print(car+" ");
}
}else{
out.println("无载人型汽车");
}
out.println();
//打印载货汽车
if(cargo!=0){
out.print("载货汽车:");
for(String car:cagoCar){
if(car!=null)
out.print(car+" ");
}
}else{
out.println("无载货型汽车");
}
out.println();
//out.println(Arrays.toString(n));
out.println("共"+sumMoney+"元钱\t可载"+sumVolume1+"人\t可载货"+sumVolume2+"吨");
}
}
//奥迪
package com.sysDada;
public class Aodi {
public int number=1;
public String name="奥迪A4";
public int money=500;
public int volume=4;
@Override
public String toString() {
return number +".\t " + name + "\t\t" + money + "元/天\t\t载人:" +volume+ "人";
}
}
//马自达
package com.sysDada;
public class Mazida {
public int number=2;
public String name="马自达6";
public int money=400;
public int volume=4;
@Override
public String toString() {
return number + ".\t " + name + "\t\t" + money + "元/天\t\t载人:" + volume + "人";
}
}
//皮卡
package com.sysDada;
public class Pika {
public int number=3;
public String name="皮卡雪6";
public int money=450;
public int volume1=4;
public int volume2=2;
@Override
public String toString() {
return number + ".\t " + name+ "\t\t" + money + "元/天\t\t载人:" + volume1 + "人,载重:"+volume2+"吨";
}
}
//金龙
package com.sysDada;
public class Jinlong {
public int number=4;
public String name="金龙";
public int money=800;
public int volume=20;
@Override
public String toString() {
return number + ".\t " + name + "\t\t" + money + "元/天\t\t载人:" + volume + "人";
}
}
//松花江
package com.sysDada;
public class Song {
public int number=5;
public String name="松花江";
public int money=400;
public int volume2=4;
@Override
public String toString() {
return number + ".\t " + name + "\t\t" + money + "元/天\t\t载货:" + volume2 + "吨";
}
}
//依维柯
package com.sysDada;
public class Yi {
public int number=6;
public String name="依维柯";
public int money=1000;
public int volume2=20;
@Override
public String toString() {
return number + ".\t " + name + "\t\t" + money + "元/天\t\t载货:" + volume2 + "吨";
}
}
运行结果