主类
public abstract class Vehicle { public int getPeople() { return people; } public void setPeople(int people) { this.people = people; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int people; private int cargo; private int price; private String name; public abstract void show(); }
子类1
public class car extends Vehicle { public void show(){ System.out.println(" "+getName()+" "+getPrice()+"元/天 载人"+getPeople()); } }
子类2
public class pickup extends Vehicle { public void show(){ System.out.println(" "+getName()+" "+getPrice()+"元/天 载人"+getPeople()+" 载货"+getCargo());} }
子类3
public class truck extends Vehicle { public void show(){ System.out.println(" "+getName()+" "+getPrice()+"元/天 载货"+getCargo()+"吨");} }
main类
import java.util.Scanner; import java.util.Arrays; public class main { public static void main(String[] args) { System.out.println("欢迎使用大大租车系统"); System.out.println("您是否要租车:1是 0否"); Scanner input=new Scanner(System.in); int i=input.nextInt(); switch (i){ case 0: System.exit(0); break; case 1: System.out.println("您可租车的类型及其价格表如下"); System.out.println("序号 汽车类型 租金 容量"); Vehicle[] t=new Vehicle[6];//6是代表数组总个数,代表六种车子可以租 t[0]=new car(); t[0].setName("奥迪A4"); t[0].setPrice(500); t[0].setPeople(4); t[1]=new car(); t[1].setName("马自达6"); t[1].setPrice(400); t[1].setPeople(4); t[2]=new pickup(); t[2].setName("皮卡雪6"); t[2].setPrice(450); t[2].setPeople(4); t[2].setCargo(2); t[3]=new car(); t[3].setName("金龙"); t[3].setPrice(800); t[3].setPeople(20); t[4]=new truck(); t[4].setName("松花江"); t[4].setPrice(400); t[4].setCargo(4); t[5]=new truck(); t[5].setName("依维柯"); t[5].setPrice(1000); t[5].setCargo(20); for (int j=0;j<6;j++){ System.out.print(j+1+" "); t[j].show(); } System.out.println("请输入您要租车的数量:"); Scanner n=new Scanner(System.in); int nn=n.nextInt(); int zairen=0; int zaihuo=0; int sumprice=0; //String []sumname1=new String[nn]; //String []sumname2=new String[nn]; String sumname1=""; String sumname2=""; for (int xx=1;xx<=nn;xx++){ System.out.println("请输入第"+xx+"辆车的序号"); Scanner b=new Scanner(System.in); int bb=b.nextInt(); zairen=zairen+t[bb-1].getPeople(); zaihuo=zaihuo+t[bb-1].getCargo(); sumprice=sumprice+t[bb-1].getPrice(); if (t[bb-1].getPeople()!=0){ sumname1=sumname1+t[bb-1].getName()+" "; } if(t[bb-1].getCargo()!=0){ sumname2=sumname2+t[bb-1].getName()+" "; } } System.out.println("请输入租车的天数"); Scanner tianshu=new Scanner(System.in); int day=tianshu.nextInt(); sumprice=day*sumprice; System.out.println("载人车: "+sumname1); System.out.println(("载货车: "+sumname2)); System.out.println("载人总数为"+zairen); System.out.println("载货总数为"+zaihuo); System.out.println("***租车总价为"+sumprice); } } }
也可以放在构造函数里
你这个一眼看去就是太繁杂了,其实那些参数可以放到一个带参数方法中
。。。