总结:感觉写主页面还是像是在写面向对象的程序,不知道是不是思维转换不好的原因,如果有朋友有更好的方式可以一起交流啊~
主页面:显示所有的流程
package recentcar;
import java.util.Scanner;
import java.util.ArrayList;
public class View {
public static void main(String args[]){
Scanner input=new Scanner(System.in); // 保存用户输入值
/********可租车辆信息*********/
Car cars[]=new Car[7];
cars[1]=new Coach("奥迪A4",500,4);
cars[2]=new Coach("马自达6",400,4);
cars[3]=new Pickup("皮卡雪6",450,4,2);
cars[4]=new Coach("金龙",800,20);
cars[5]=new Truck("松花江",400,4);
cars[6]=new Truck("依维柯",1000,20);
ArrayList<Car> people=new ArrayList<Car>(); //租车中载人车辆动态数组
ArrayList<Car> goods=new ArrayList<Car>(); //租车中载货车辆动态数组
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
int number=input.nextInt();
if(number==1){
System.out.println("您可租车的类型及价目表:");
show(cars);
System.out.println("请输入您要租汽车的数量:");
number=input.nextInt();
int[] order=new int[number+1]; //根据租车数量得到租车序列号数组
/*************获取用户租车序列号并判断是载人还是载货,放入相应的动态数组中**********************/
for(int i=1;i<=number;i++){
System.out.println("请输入第"+i+"辆车的序号:");
order[i]=input.nextInt();
if((cars[order[i]] instanceof Coach)){
people.add(cars[order[i]]);
}
else if((cars[order[i]] instanceof Truck)){
goods.add(cars[order[i]]);
}
else if((cars[order[i]] instanceof Pickup)){
people.add(cars[order[i]]);
goods.add(cars[order[i]]);
}
}
System.out.println("请输入租车天数:");
int day=input.nextInt();//租车天数
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
/*************输出载人车辆名称*********************/
for(int i=0;i<people.size();i++){
Coach outpeople;
Pickup outpickup;
if(people.get(i) instanceof Coach){
outpeople=(Coach)people.get(i);
System.out.print(outpeople.getname());
}
else if(people.get(i) instanceof Pickup){
outpickup=(Pickup)people.get(i);
System.out.print(outpickup.getname());
}
System.out.print("\t");
}
System.out.println("共载人"+calPerson(people)); //总人数
/*************输出载货车辆名称*********************/
System.out.println("***载货的车有:");
for(int i=0;i<goods.size();i++){
Truck outtruck;
Pickup outpickup;
if(goods.get(i) instanceof Truck){
outtruck=(Truck)goods.get(i);
System.out.print(outtruck.getname());
}
else if(goods.get(i) instanceof Pickup){
outpickup=(Pickup)goods.get(i);
System.out.print(outpickup.getname());
}
System.out.print("\t");
}
System.out.println("共载货"+calGoods(goods));//计算载货总量
System.out.println("***租车总价格:"+calMoney(order,cars,day)); //计算总金额
}
input.close();
}
/********************显示可租车辆信息的函数*****************************/
public static void show(Car[] cars){
Coach outpeople;
Pickup outpickup;
Truck outtruck;
System.out.println("序号\t汽车名称\t租金\t容量");
for(int i=1;i<cars.length;i++){
if(cars[i] instanceof Coach){
outpeople=(Coach)cars[i];
System.out.println(i+".\t"+outpeople.getname()+"\t"+outpeople.getmoney()+"元/天\t载人:"+outpeople.getbusload());
}
else if(cars[i] instanceof Truck){
outtruck=(Truck)cars[i];
System.out.println(i+".\t"+outtruck.getname()+"\t"+outtruck.getmoney()+"元/天\t载货:"+outtruck.getboatload());
}
else if(cars[i] instanceof Pickup){
outpickup=(Pickup)cars[i];
System.out.println(i+".\t"+outpickup.getname()+"\t"+outpickup.getmoney()+"元/天\t载人:"+outpickup.getbusload()+" 载货:"+outpickup.getboatload());
}
}
}
/********************计算总人数的函数*****************************/
public static int calPerson(ArrayList<Car> people){
int total=0;
Coach outpeople;
Pickup outpickup;
for(int i=0;i<people.size();i++){
if(people.get(i) instanceof Coach){
outpeople=(Coach)people.get(i);
total=outpeople.getbusload()+total;
}
else if(people.get(i) instanceof Pickup){
outpickup=(Pickup)people.get(i);
total=outpickup.getbusload()+total;
}
}
return total;
}
/********************计算总载货量的函数*****************************/
public static int calGoods(ArrayList<Car> goods){
int total=0;
Truck outtruck;
Pickup outpickup;
for(int i=0;i<goods.size();i++){
if(goods.get(i) instanceof Truck){
outtruck=(Truck)goods.get(i);
total=outtruck.getboatload()+total;
}
else if(goods.get(i) instanceof Pickup){
outpickup=(Pickup)goods.get(i);
total=outpickup.getboatload()+total;
}
}
return total;
}
/********************计算总金额的函数*****************************/
public static int calMoney(int[] order,Car[] cars,int day){
int total=0;
for(int i=1;i<order.length;i++){
total=cars[order[i]].getmoney()+total;
}
total=total*day;
return total;
}
}
父类:
package recentcar;
public class Car {
private String name;
private int money;
Car(String name,int money){
this.name=name;
this.money=money;
}
public void setmoney(int money){
this.money=money;
}
public int getmoney(){
return money;
}
public void setname(String name){
this.name=name;
}
public String getname(){
return name;
}
}
三个子类:
- 客车:Coach
package recentcar;
public class Coach extends Car{
int busload;
Coach(String name,int money,int busload){
super(name,money);
this.busload=busload;
}
public void setbusload(int busload){
this.busload=busload;
}
public int getbusload(){
return busload;
}
}
- 货车:Truck
package recentcar;
public class Truck extends Car{
int boatload;
Truck(String name,int money,int boatload){
super(name,money);
this.boatload=boatload;
}
public void setboatload(int boatload){
this.boatload=boatload;
}
public int getboatload(){
return boatload;
}
}
- 皮卡:Pickup
package recentcar;
public class Pickup extends Car{
int busload;
int boatload;
Pickup(String name,int money,int busload,int boatload){
super(name,money);
this.busload=busload;
this.boatload=boatload;
}
public void setbusload(int busload){
this.busload=busload;
}
public int getbusload(){
return busload;
}
public void setboatload(int boatload){
this.boatload=boatload;
}
public int getboatload(){
return boatload;
}
}