为什么我的代码这么多,,,,
//汽车类 package com.oop; public class Auto { private int id; private String name; private int perRent; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPerRent() { return perRent; } public void setPerRent(int perRent) { this.perRent = perRent; } public Auto(int id, String name, int perRent) { this.id = id; this.name = name; this.perRent = perRent; } public Auto() { } }
//小汽车客车类
package com.oop;
public class Car extends Auto {
private int manned;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public Car() {
super();
}
public Car(int id, String name, int perRent,int manned) {
super(id, name, perRent);
this.manned=manned;
}
}
//卡车类
package com.oop;
public class Truck extends Auto {
private int load;
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public Truck() {
super();
// TODO 自动生成的构造函数存根
}
public Truck(int id, String name, int perRent,int load) {
super(id, name, perRent);
this.load=load;
// TODO 自动生成的构造函数存根
}
}
//皮卡类
package com.oop;
public class Packup extends Auto {
private int manned;
private int load;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public Packup() {
super();
// TODO 自动生成的构造函数存根
}
public Packup(int id, String name, int perRent,int manned,int load) {
super(id, name, perRent);
this.manned=manned;
this.load=load;
// TODO 自动生成的构造函数存根
}
}
//汽车库类
package com.oop;
public class AutoDemo {
Auto []auto=new Auto[10];
public AutoDemo(){
Auto auto1=new Car(1,"奥迪A4", 500, 4);
Auto auto2=new Car(2,"马自达6",400,4);
Auto auto3=new Packup(3,"皮卡雪6",450,4,2);
Auto auto4=new Car(4,"金龙", 800, 20);
Auto auto5=new Truck(5,"松花江",400,4);
Auto auto6=new Truck(6,"依维柯", 1000, 20);
auto[0]=auto1;
auto[1]=auto2;
auto[2]=auto3;
auto[3]=auto4;
auto[4]=auto5;
auto[5]=auto6;
}
public void show(){
for(int i=0;i<auto.length;i++){
if(auto[i]instanceof Car)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载人:"+((Car) auto[i]).getManned());
else if(auto[i]instanceof Truck)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载货"+((Truck) auto[i]).getLoad());
else if(auto[i]instanceof Packup)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载人:"+((Packup) auto[i]).getManned()+"载货"+((Packup) auto[i]).getLoad());
continue;
}
}
}
//计算类
package com.oop;
import java.util.Scanner;
public class Count {
int number;
int autoid1;
int autoid2;
int day;
public void Flow(){
System.out.println("请输入要租汽车的数量");
Scanner input=new Scanner(System.in);
number=input.nextInt();
System.out.println("请输入第一辆车的序号");
autoid1=input.nextInt();
System.out.println("请输入第二辆车的序号");
autoid2=input.nextInt();
System.out.println("请输入租车天数");
day=input.nextInt();
}
public void show(){
AutoDemo a=new AutoDemo();
int rent=(a.auto[autoid1-1].getPerRent()+a.auto[autoid2-1].getPerRent())*day;
System.out.println("您的账单:");
System.out.println("可载人的有");
if(a.auto[autoid1-1] instanceof Car||a.auto[autoid1-1] instanceof Packup){
System.out.println(a.auto[autoid1-1].getName());
}
if(a.auto[autoid2-1] instanceof Car||a.auto[autoid2-1] instanceof Packup){
System.out.println(a.auto[autoid2-1].getName());
}else{
System.out.println("无");
}
System.out.println();
System.out.println("载货的有");
if(a.auto[autoid1-1] instanceof Truck||a.auto[autoid1-1] instanceof Packup){
System.out.println(a.auto[autoid1-1].getName());
}
if(a.auto[autoid2-1] instanceof Truck||a.auto[autoid2-1] instanceof Packup){
System.out.println(a.auto[autoid2-1].getName());
}else{
System.out.println("无");
}
System.out.println("总价为"+rent);
}
}
//测试类
package com.oop;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("欢迎使用哒哒租车系统");
Scanner input=new Scanner(System.in);
System.out.println("您是否要租车1.是 2.否");
if(input.nextInt()==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
AutoDemo ad=new AutoDemo();
ad.show();
Count ct=new Count();
ct.Flow();
ct.show();
}
else{
System.out.println("谢谢使用");
}
}
}