```这个是main方法主程序
import java.util.Scanner;
public class A6_30 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in=new Scanner(System.in);
System.out.println("**欢迎使用达达租车系统**");
System.out.println("您是否要租车:1是 2否");
int number=in.nextInt();
if(number==1){
System.out.println("您可租车的类型及其价目表");
System.out.println("序号\t"+"汽车名称"+"\t租金\t"+"容量");
QiChe[] cars={new ZaiRen(1, "奥迪", 500, 4),new ZaiRen(2,"马自达",400,4),
new ZaiRen(3,"金龙",800,20),new ZaiHuo(4,"松花江",400,4),
new ZaiHuo(5,"依维客",1000,20),new RenYvHuo(6,"皮卡",450,4,2)};
for(int i=0;i<cars.length;i++){
System.out.println(cars[i].toString());
}
System.out.println("请输入您要租车的数量:");
int number0=in.nextInt();
int[] num=new int[number0];
String[] kkc=new String[6];//空载客车数组
String[] khc=new String[6];//空载货车数组
int sumZaiRen=0;//载客总数
double sumZaiHuo=0;//载货总数
double sumMoney=0;//租金总额
for(int i=0;i<num.length;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
num[i]=in.nextInt();
}
for(int i=0;i<num.length;i++){
if(num[i]==1){
kkc[i]=cars[0].name;
sumZaiRen=sumZaiRen+cars[0].zairen;
sumMoney=sumMoney+cars[0].money;
}else if(num[i]==2){
kkc[i]=cars[1].name;
sumZaiRen=sumZaiRen+cars[1].zairen;
sumMoney=sumMoney+cars[1].money;
}else if(num[i]==3){
kkc[i]=cars[2].name;
sumZaiRen=sumZaiRen+cars[2].zairen;
sumMoney=sumMoney+cars[2].money;
}else if(num[i]==4){
khc[i]=cars[3].name;
sumZaiHuo=sumZaiHuo+cars[3].zaihuo;
sumMoney=sumMoney+cars[3].money;
}else if(num[i]==5){
khc[i]=cars[4].name;
sumZaiHuo=sumZaiHuo+cars[4].zaihuo;
sumMoney=sumMoney+cars[4].money;
}else if(num[i]==6){
kkc[i]=cars[5].name;
khc[i]=cars[5].name;
sumZaiRen=sumZaiRen+cars[5].zairen;
sumZaiHuo=sumZaiHuo+cars[5].zaihuo;
sumMoney=sumMoney+cars[5].money;
}
}
System.out.println("请输入租车天数:");
int days=in.nextInt();
System.out.println("您的帐单:");
System.out.println("可载人的车有:");
for(int i=0;i<kkc.length;i++){
if(kkc[i]!=null){
System.out.print(kkc[i]+" ");
}else{continue;}
}
System.out.print("共载人"+sumZaiRen+"人");
System.out.println("\n"+"可载货的车有:");
for(int i=0;i<khc.length;i++){
if(khc[i]==null){
continue;
}else{System.out.print(khc[i]+" ");}
}
System.out.print("共载货"+sumZaiHuo+"吨");
System.out.print("\n"+"***租车总价格为:"+sumMoney*3);
}else if(number==2){
System.exit(0);
}else{
System.out.println("输入错误,系统退出");
System.exit(0);
}
}
}
下面是定义的类
主类
public abstract class QiChe {
public int id;
public String name;
public int money;
public int zairen;
public int zaihuo;
public abstract String toString();
}
继承类
public class RenYvHuo extends QiChe {
public RenYvHuo(int id,String name,int money,int zairen,int zaihuo){
this.id=id;this.name=name;this.money=money;this.zaihuo=zaihuo;this.zairen=zairen;
}
public String toString(){
return id+"\t"+name+"\t"+money+"元/天"+"\t"+"\t载人:"+zairen+"人"+" "+"载货:"+zaihuo+"吨";
}
}
继承类
public class ZaiHuo extends QiChe {
public ZaiHuo(int id,String name,int money,int zaihuo){
this.id=id;
this.name=name;
this.money=money;
this.zaihuo=zaihuo;
}
public String toString(){
return (id+"\t"+name+"\t"+money+"元/天"+"\t"+"载货:"+zaihuo+"吨");
}
}
继承类
public class ZaiRen extends QiChe {
public ZaiRen(int id,String name,int money,int zairen){
this.id=id;
this.name=name;
this.money=money;
this.zairen=zairen;
}
public String toString(){
return (id+"\t"+name+"\t"+money+"元/天"+"\t"+"\t载人:"+zairen+"人");
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章