package dadazu;
/*
* 1:声明一个车辆类
*/
public class MainVehicle {
int number;
String name;
int rent;
int per;
double weight;
public int getPer() {
return per;
}
public void setPer(int per) {
this.per = per;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
MainVehicle(){
}
MainVehicle(int number,String name,int rent,int per){
this.number = number;
this.name = name;
this.rent = rent;
this.per = per;
}
MainVehicle(int number,String name,int rent,double weight){
this.number = number;
this.name = name;
this.rent = rent;
this.weight = weight;
}
MainVehicle(int number,String name,int rent,int per,double weight){
this.number = number;
this.name = name;
this.rent = rent;
this.weight = weight;
this.per = per;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getinfo(){
if(weight==0){
return number+"\t"+name+"\t"+rent+"元/天"+"\t"+"载人:"+per+"人";
}
else if(per==0){
return number+"\t"+name+"\t"+rent+"元/天"+"\t"+"载货:"+weight+"吨";
}
else{
return number+"\t"+name+"\t"+rent+"元/天"+"\t"+"载人:"+per+"人"+" "+"载货:"+weight+"吨";
}
}
}
、、、、、、、、
//运行主类
package dadazu;
import java.util.Scanner;
public class Vehicle {
public static void main(String[] args){
int a,b,day;
int q=0,w=0;
int sper=0;
double swei = 0;
double price=0;
Scanner in = new Scanner(System.in);
System.out.println("欢迎使用哒哒租车系统!");
System.out.println("您是否要租车:1是 0否");
a = in.nextInt();
if(a==1){//a=1进入租车系统!
System.out.println("您可租车的类型及其价目表如下:");
MainVehicle veh[] = new MainVehicle[6];//初始化租车对象
veh[0] = new MainVehicle(1, "奥迪A4", 500, 4);
veh[1] = new MainVehicle(2, "马自达6", 400, 4);
veh[2] = new MainVehicle(3, "皮卡雪6", 450, 4,2.0);
veh[3] = new MainVehicle(4, "金龙", 800, 20);
veh[4] = new MainVehicle(5, "松花江", 400, 4.0);
veh[5] = new MainVehicle(6, "兰博基尼", 1000, 20.0);
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量"+"\t");
for(int i=0;i<veh.length;i++){//打印租车信息
System.out.println(veh[i].getinfo());
}
System.out.println("请输入您要租汽车的数量:");
b = in.nextInt();
int c[] = new int[b];//声明一个汽车数量大小的数组
for(int i=0;i<b;i++){//选择车辆序号,存入到数组里面
System.out.println("请输入第"+(i+1)+"辆车的序号:");
c[i] = in.nextInt();
}
System.out.println("请输入租车天数:");
day = in.nextInt();
System.out.println();
System.out.println();
System.out.println();
System.out.println("您的账单:");
MainVehicle pers[] = new MainVehicle[b];//声明一个汽车数量大小的数组对象
for(int i=0, k=0;i<veh.length;i++){//将选择的序号的车辆对象存入到pers[]
for(int j=0;j<c.length;j++){
if(i+1==c[j]){
pers[k]=veh[i];
k++;
}
}
}
for(int i=0;i<pers.length;i++){//遍历选择的车辆对象
if(pers[i].getPer()!=0){
q++;//统计载人的车辆数
}
if(pers[i].getWeight()!=0){
w++;//统计载货的车辆数
}
}
System.out.println(q+" -- "+w);
MainVehicle spers[] = new MainVehicle[q];//声明载人的车辆对象数组
MainVehicle wu[] = new MainVehicle[w];//声明载货的车辆对象数组
for(int j=0,i=0;j<pers.length;j++){//遍历pers[],找到载人的车辆赋值给新的spers[]
if(pers[j].getPer()!=0){
spers[i]=pers[j];
i++;
}
}
for(int j=0, i=0;j<pers.length;j++){//遍历pers[],找到载货的车辆赋值给新的wu[]
if(pers[j].getWeight()!=0){
wu[i]=pers[j];
i++;
}
}
System.out.println("***可载人的车有:");
for(int i=0;i<spers.length;i++){
System.out.print(spers[i].getName()+"\t");
sper = spers[i].getPer()+sper;
price = spers[i].getRent()*day+price;
}
System.out.println("共载人:"+sper);
System.out.println("***载货的车有:");
for(int i=0;i<wu.length;i++){
System.out.print(wu[i].getName()+"\t");
swei = wu[i].getWeight()+swei;
if(wu[i].getWeight()!=0&&wu[i].getPer()==0)
price = wu[i].getRent()*day+price;
}
System.out.println("共载货:"+swei+"吨");
System.out.println("租车总价格:"+price+"元");
}
else if(a==0){
System.out.println("退出系统!");
System.exit(-1);
}
else{
System.out.println("输入有误!,正在退出系统");
System.exit(-1);
}
}
}
还是建议加子类比较好,因为如果之后写一个自动添加车的功能而不是手动的话,那么人数肯定是整数,载重如果也是一个整数,别人又写的是"20",而不是"20.0",按照这种写法是肯定会把货车新建成一个客车的实例。
其次就是一个代码重复的问题,如果你写了之类的话后面的几个循环是完全不用写的,完全可以这样:
String keChe = ""; int zaiRen = 0; String huoChe = ""; double zaiWu = 0; // 如果想要加入皮卡 String piKa = ""; double zongJia = 0; // 总价 for (Car car: c) { if (car instanceof KeChe) { keChe += car.name + " "; zaiRen += car.per; } else if (car instanceof HuoChe) { huoche += car.name + " "; zaiWu += car.weight; } else if (car instanceof PiKa) { piKa += car.name + " "; zaiRen += car.per; zaiWu += car.weight; } zongJia += car.price; } System.out.println("***可载人的车有:"); System.out.println(keChe); // 后面直接代印载人多少,货车和皮卡同样的道理进行打印
打印出来的结果是一样的,但是只有一次遍历(循环)。
我觉得写得不错了,建议你用集合框架写,可以省很多代码。