新手,望指教。
package test;
public class CarInfo {
private int pNum;
private String type;
private String name;
private double weight;
private int rentment;
public int getpNum(){
return pNum;
}
public void setpNum(int newPnum){
pNum=newPnum;
}
public String getName(){
return name;
}
public void setName(String newName){
name=newName;
}
public String getType(){
return type;
}
public void setType(String newType){
type=newType;
}
public double getWeight(){
return weight;
}
public void setWeight(double newWeight){
weight=newWeight;
}
public int getRentment(){
return rentment;
}
public void setRentment(int newRentment){
rentment=newRentment;
}
public String toString() {
return type+" "+name+" "+rentment+"元/天 "+weight+"吨"+pNum+"人";
}
}
package test;
public class HuoCar extends CarInfo implements IzaiHuo{
private double weight;
private String type;
private String name;
private int rentment;
public HuoCar(){};
public HuoCar(String newName,String newType,double newWeight,int newRentment){
this.weight=newWeight;
this.type=newType;
this.name=newName;
this.rentment=newRentment;
};
@Override
public String toString() {
return type+" "+name+" "+rentment+"元/天 "+weight+"吨";
}
public double getWeight(){
return weight;
}
public void setWeight(double newWeight){
weight=newWeight;
}
public String getName(){
return name;
}
public void setName(String newName){
name=newName;
}
public String getType(){
return type;
}
public void setType(String newType){
type=newType;
}
public int getRentment(){
return rentment;
}
public void setRentment(int newRentment){
rentment=newRentment;
}
public void zaihuo() {
System.out.println("可载货");
}
}
package test;
public class KeCar extends CarInfo implements IzaiKe{
private int pNum;
private String type;
private String name;
private int rentment;
public KeCar(){};
public KeCar(String newName,String newType,int newPnum, int newRentment){
this.pNum=newPnum;
this.type=newType;
this.name=newName;
this.rentment=newRentment;
};
public int getpNum(){
return pNum;
}
public void setpNum(int newPnum){
pNum=newPnum;
}
public String getName(){
return name;
}
public void setName(String newName){
name=newName;
}
public String getType(){
return type;
}
public void setType(String newType){
type=newType;
}
public int getRentment(){
return rentment;
}
public void setRentment(int newRentment){
rentment=newRentment;
}
@Override
public String toString() {
return type+" "+name+" "+rentment+"元/天 "+pNum+"人";
}
public void zaike() {
System.out.println("可载人");
}
}
package test;
public class KeHuoCar extends CarInfo implements IzaiHuo,IzaiKe{
private int pNum;
private String type;
private String name;
private double weight;
private int rentment;
public KeHuoCar(){};
public KeHuoCar(String newName,String newType,int newPnum,double newWeight,int newRentment){
this.pNum=newPnum;
this.type=newType;
this.name=newName;
this.weight=newWeight;
this.rentment=newRentment;
};
public int getpNum(){
return pNum;
}
public void setpNum(int newPnum){
pNum=newPnum;
}
public String getName(){
return name;
}
public void setName(String newName){
name=newName;
}
public String getType(){
return type;
}
public void setType(String newType){
type=newType;
}
public double getWeight(){
return weight;
}
public void setWeight(double newWeight){
weight=newWeight;
}
public int getRentment(){
return rentment;
}
public void setRentment(int newRentment){
rentment=newRentment;
}
public void zaike() {
System.out.println("可载人");
}
public void zaihuo() {
System.out.println("可载货");
}
@Override
public String toString() {
return type+" "+name+" "+rentment+"元/天 "+weight+"吨"+pNum+"人";
}
}
package test;
public interface IzaiHuo {
public void zaihuo();
}
package test;
public interface IzaiKe {
public void zaike();
}
package test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
CarInfo car[]={new HuoCar("松花江","1",2,400),
new HuoCar("依维柯","2",20,1000),
new KeCar("奥迪A4","3",4,500),
new KeCar("金龙","4",20,800),
new KeHuoCar("皮卡","5",4,2,450)};
Scanner flag=new Scanner(System.in);
Scanner num=new Scanner(System.in);
Scanner no=new Scanner(System.in);
Scanner day=new Scanner(System.in);
int num2,day2,sum=0,ton=0,person=0;//租车数量,时间,总价,总载货吨数,总载人数
System.out.println("欢迎使用大大租车系统:");
System.out.println("您是否要租车:1是 0否");
if(flag.nextInt()==1){
System.out.println("您可以租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量 ");
System.out.println(car[0].toString() );
System.out.println(car[1].toString() );
System.out.println(car[2].toString() );
System.out.println(car[3].toString() );
System.out.println(car[4].toString() );
System.out.println("请输入你要租车的数量(不超过5):");//可修改(超过5)
num2=num.nextInt();
CarInfo[] arrCur=new CarInfo[num2];
for(int i=0;i<num2;i++){
System.out.println("请输入你要租的车序号:");//可修改(输入序号重复)
int j=no.nextInt();
arrCur[i]=car[j-1];
}
System.out.println("请输入租车天数:");//可修改(不同序号车租不同时间)
day2=day.nextInt();
for(int i=0;i<num2;i++){
sum+=arrCur[i].getRentment()*day2; //租车费用
}
System.out.println("你的账单");
System.out.println("***可载人的车有:");
for(CarInfo c: arrCur ){
if(c instanceof KeCar || c instanceof KeHuoCar){
System.out.print(c.getName()+" ");
person+=c.getpNum();
}
}
System.out.println("可载人:"+person+"人");
System.out.println("***可载货的车有:");
for(CarInfo c: arrCur ){
if(c instanceof HuoCar || c instanceof KeHuoCar){
System.out.print(c.getName()+" ");
ton+=c.getWeight();
}
}
System.out.println("可载货:"+ton+"吨");
System.out.println("***租车总价格:"+sum+"元");
}else System.out.println("程序结束");
}
}