package didizuche;
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用滴滴租车系统"+"\n"+"您是否要租车:1是 0否");
Scanner judge=new Scanner(System.in);
int a=judge.nextInt();
if(a==1){
System.out.println("您可租车的类型及价目表"+"\n"+"序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
Car test1=new Aodi();test1.car();
Car test2=new Mazida();test2.car();
Car test3=new Pikaxue();test3.car();
Car test4=new Jinlong();test4.car();
Car test5=new Songhuajia();test5.car();
Car test6=new Yiweike();test6.car();
System.out.println("请输入你要租汽车的数量");
Scanner amount=new Scanner(System.in);
int b=amount.nextInt();
int c;int allpeople=0;int allthing=0;int allprice=0;String Person=null;String Cars=null;
for(int i=1;i<=b;i++){
do{
System.out.println("请输入第"+i+"辆车的序号:");
Scanner xuhao=new Scanner(System.in);
c=xuhao.nextInt();
}while(c<1||c>6);
if(c==1){
allpeople+=4;allthing+=0;allprice+=500;Person+="奥迪A4";
}else if(c==2){
allpeople+=4;allprice+=400;Person+="马自达6";
}else if(c==3){
allpeople+=4;allthing+=2;allprice+=450;Person+="皮卡雪6";Cars+="皮卡雪6";
}else if(c==4){
allpeople+=20;;allprice+=800;Person+="金龙";
}else if(c==5){
allthing=4;allprice+=400;Cars+="松花江";
}else{
allthing+=20;allprice+=1000;Cars+="依维柯";
}
}
System.out.println("请输入要租的天数:");
Scanner days=new Scanner(System.in);
int d=days.nextInt();
System.out.println("你的账单"+"\n"+"***可载人的车有:"+"\n"+Person+"\t"+"共载人:"+allpeople+"人");
System.out.println("***载货的车有:"+"\n"+Cars+"\t"+"共载货:"+allthing+"吨");
System.out.println("***租车总价格:"+allprice*d+"元");
}
}
}
String Person = null; String Cars=null; Cars赋值null ,在拼接字符串的时候 就是 "null" ,应该赋值为"",这样在拼接车名字的时候不会出现null了;
if (c == 1) {
allpeople += 4;
allthing += 0;
allprice += 500;
Person += "奥迪A4";
}
车的属性到成员变量了这里就不用手动加 常量了。直接加对象的成员变量就好了;
import java.util.*;
public class Test {
public static void main(String[] args) {
//所有车放在这里,可以根据序号取对应的车
List<Car> cars = new ArrayList<Car>();
Car aodi = new Aodi();
cars.add(aodi);
Car mazida = new Mazida();
cars.add(mazida);
Car pikaxue = new Pikaxue();
cars.add(pikaxue);
Car jinlong = new Jinlong();
cars.add(jinlong);
Car songhuajia = new Songhuajia();
cars.add(songhuajia);
Car yiweike = new Yiweike();
cars.add(yiweike);
System.out.println("欢迎使用滴滴租车系统" + "\n" + "您是否要租车:1是 0否");
Scanner judge = new Scanner(System.in);
int a = judge.nextInt();
if (a == 1) {
System.out.println("您可租车的类型及价目表" + "\n" + "序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量");
aodi.car(1);
mazida.car(2);
pikaxue.car(3);
jinlong.car(4);
songhuajia.car(5);
yiweike.car(6);
System.out.println("请输入你要租汽车的数量");
Scanner amount=new Scanner(System.in);
int b=amount.nextInt();
int c;
int allpeople = 0;
int allthing = 0;
int allprice = 0;
String Person = "";
String Cars = "";
for(int i=1;i<=b;i++){
do {
System.out.println("请输入第" + i + "辆车的序号:");
Scanner xuhao = new Scanner(System.in);
c = xuhao.nextInt();
}while(c<1||c>6);
//根据序号在List中取相应的车
Car cc = cars.get(c-1);
//如果该车能装人
if(cc.people>0){
allpeople+=cc.people;
Person += cc.name+" ";
}
//如果该车能装货
if(cc.thing>0){
allthing+=cc.thing;
Cars += cc.name+" ";
}
allprice += cc.price;
}
System.out.println("请输入要租的天数:");
Scanner days=new Scanner(System.in);
int d=days.nextInt();
System.out.println("你的账单"+"\n"+"***可载人的车有:"+"\n"+Person+"\t"+"共载人:"+allpeople+"人");
System.out.println("***载货的车有:"+"\n"+Cars+"\t"+"共载货:"+allthing+"吨");
System.out.println("***租车总价格:"+allprice*d+"元");
}
}
}
class Car{
String name = "";
int people = 0;
int thing = 0;
float price = 0;
public void car(int index){
System.out.println(index + "\t" + name + "\t" + price + "\t" + people + "人" + thing + "货");
}
}
class Aodi extends Car{{
name = "奥迪A4";
people = 4;
thing = 0;
price = 500;
}}
class Mazida extends Car{{
name = "马自达6";
people = 4;
thing = 0;
price = 400;
}}
class Pikaxue extends Car{{
name = "皮卡雪6";
people = 4;
thing = 2;
price = 450;
}}
class Jinlong extends Car{{
name = "金龙";
people = 20;
thing = 0;
price = 800;
}}
class Songhuajia extends Car{{
name = "松花江";
people = 0;
thing = 4;
price = 400;
}}
class Yiweike extends Car{{
name = "依维柯";
people = 20;
thing = 0;
price = 1000;
}}//以奥迪为例
public static class Aodi extends Car {
String name = "奥迪A4"; //车的名字
int people = 4; //容纳人数
int thing = 0; //容纳货物数量
float price = 500; //价格
@Override
public void car() {
System.out.println("1" + "\t" + name + "\t" + price + "\t" + people + "人" + thing + "货");
}
}
这样在选车的时候 就可以写成
if (c == 1) {
allpeople += ((Aodi)test1).getPeople();
allthing += ((Aodi)test1).getThing();
allprice += ((Aodi)test1).getPrice();
Person += ((Aodi)test1).getName();
}