Et9
2016-06-16 11:12
package com.imooc.dada; //父类 public abstract class Aotu { public String name; public float price; public int manned; public float cargo; //public int number; public abstract void run(); } package com.imooc.dada; //其中一子类 public class Aodi extends Aotu { @Override public void run() { // TODO Auto-generated method stub } Aodi(){ name = "奥迪A4 "; price = 500; manned = 4; cargo = 0; } } package com.imooc.dada; import java.util.Scanner; //主程序入口 public class Dada { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int inputbuf = 0; //输入缓存 int[] inputData; //用户输入数据 float allMoney = 0; //总金额 int day = 0; //天数 String[] takePerson; //载人车名 String[] takeGoods; //载货车名 int loadNumber = 0; //总载人数 float ladenNumber = 0; //总载货数 //创建各个对象 Aotu aodi = new Aodi(); Aotu mazida = new Mazida(); Aotu pikaxue = new Pikaxue(); Aotu jinlon = new Jinlon(); Aotu songhuajiang = new Songhuajiang(); Aotu yiweike = new Yiweike(); //租车流程开始 System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要搭车:1是 0否"); inputbuf = input.nextInt(); if(inputbuf == 1){ //打印车辆信息 System.out.println("您可租车的类型和价格表:"); System.out.println("序号 汽车名称 租金 容量"); System.out.println("1、 "+aodi.name+aodi.price+"元/天"+" "+"载人:"+aodi.manned+"人"+" "+"载货:"+aodi.cargo+"吨"); System.out.println("2、 "+mazida.name+mazida.price+"元/天"+" "+"载人:"+mazida.manned+"人"+" "+"载货:"+mazida.cargo+"吨"); System.out.println("3、 "+pikaxue.name+pikaxue.price+"元/天"+" "+"载人:"+pikaxue.manned+"人"+" "+"载货:"+pikaxue.cargo+"吨"); System.out.println("4、 "+jinlon.name+jinlon.price+"元/天"+" "+"载人:"+jinlon.manned+"人"+" "+"载货:"+jinlon.cargo+"吨"); System.out.println("5、 "+songhuajiang.name+songhuajiang.price+"元/天"+" "+"载人:"+songhuajiang.manned+"人"+" "+"载货:"+songhuajiang.cargo+"吨"); System.out.println("6、 "+yiweike.name+yiweike.price+"元/天"+" "+"载人:"+yiweike.manned+"人"+" "+"载货:"+yiweike.cargo+"吨"); System.out.println(""); //用户输入 System.out.println("请输入您要租车的数量:"); inputbuf = input.nextInt(); inputData = new int[inputbuf]; takeGoods = new String[inputbuf]; takePerson = new String[inputbuf]; //用户输入车序号 for(int i = 0;i < inputbuf;i++){ System.out.println("请输入第"+(i+1)+"辆车的序号:"); inputData[i] = input.nextInt(); } System.out.println("请输入租车天数:"); day = input.nextInt(); input.close(); //清空数据 allMoney = 0; ladenNumber = 0; loadNumber = 0; for(int i = 0,j = 0,z = 0;i < inputData.length;i++){ //计算总载人数和总载货数,获取载人车名和载货车名 switch(inputData[i]){ case 1: allMoney = allMoney + aodi.price*day; loadNumber = loadNumber + aodi.manned; takePerson[j] = aodi.name; j++; break; case 2: allMoney = allMoney + mazida.price*day; loadNumber = loadNumber + mazida.manned; takePerson[j] = mazida.name; j++; break; case 3: allMoney = allMoney + pikaxue.price*day; loadNumber = loadNumber + pikaxue.manned; ladenNumber = ladenNumber + pikaxue.cargo; takePerson[j] = pikaxue.name; takeGoods[z] = pikaxue.name; z++; j++; break; case 4: allMoney = allMoney + jinlon.price*day; loadNumber = loadNumber + jinlon.manned; takePerson[j] = jinlon.name; j++; break; case 5: allMoney = allMoney + songhuajiang.price*day; ladenNumber = ladenNumber + songhuajiang.cargo; takeGoods[z] = pikaxue.name; z++; break; case 6: allMoney = allMoney + yiweike.price*day; ladenNumber = ladenNumber + pikaxue.cargo; takeGoods[z] = pikaxue.name; z++; break; default: } } //打印账单 System.out.println("您的帐单:"); System.out.println("***可载人的车有:"); for(String s:takePerson){ if(s != null){ System.out.print(s + " "); } } System.out.println("共载人:" + loadNumber + "人"); System.out.println("***载货的车有:"); for(String s:takeGoods){ if(s != null){ System.out.print(s + " "); } } System.out.println("共载货:" + ladenNumber + "吨"); System.out.println("租车总价钱:" + allMoney); }else{ System.out.println("再见!"); } } }
package diDiDaChe;
public class Car {
private double money;
private String name;
public Car(String name,double money){
this.money=money;
this.name=name;
}
public void setMoney(double money){
this.money=money;
};
public double getMoney(){
return money;
};
public String getName(){
return name;
};
}
package diDiDaChe;
public class MannedCar extends Car{
static int baseMoney=1000;
private int manned;
public MannedCar(String name,double money,int manned){
super(name,money);
this.manned=manned;
}
public void setManned(int manned){
this.manned=manned;
};
public int getManned(){
return manned;
}
}
package diDiDaChe;
public class Truck extends Car{
//载货量
static int baseMoney=2000;
private int container;
public Truck(String name,double money,int container){
super(name,money);
this.container=container;
}
public void setContainer(int container){
this.container=container;
};
public int getContainer(){
return container;
}
}
package diDiDaChe;
import java.util.InputMismatchException;
import java.util.Scanner;
public class DiDi {
double sumMoney=0;
MannedCar Aodi=new MannedCar("奥迪",500,4);
MannedCar Bingli=new MannedCar("宾利",1000,6);
MannedCar WuLi=new MannedCar("五菱之光",200,12);
MannedCar BMW=new MannedCar("宝马",800,4);
Truck DF=new Truck("东风",1000,40);
Truck DZ=new Truck("大众",800,30);
Truck FT=new Truck("丰田",500,20);
MannedCar[] mannedCar={Aodi,Bingli,WuLi,BMW};
Truck[] truck={DF,DZ,FT};
public double canMoney(Car car,int day){
if(car instanceof MannedCar){
sumMoney=day*car.getMoney()+Truck.baseMoney;
}else{
sumMoney=day*car.getMoney()+MannedCar.baseMoney;
}
return sumMoney;
}
public static int inputDay(){
System.out.println("请输入要租的天数");
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
String s=sc.next();
int k=Integer.parseInt(s.trim());
return k;
}
public Car outPutCar(int i){
if(i==1){
System.out.println("轿车品牌"+" "+"每天单价"+" 载人量");
for(int j=0;j<mannedCar.length;j++){
System.out.println(j+1+mannedCar[j].getName()+mannedCar[j].getMoney()+mannedCar[j].getManned());
}
System.out.println("请输入所需轿车的编号");
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
String s=sc.next();
int k=Integer.parseInt(s.trim());
if(k<0||k>=mannedCar.length){
System.out.println("没有这辆车请重新输入一遍");
outPutCar(i);
}
return mannedCar[k];
}else{
System.out.println("卡车品牌"+" "+"每天单价"+" 载货量");
for(int j=0;j<truck.length;j++){
System.out.println(j+1+truck[j].getName()+truck[j].getMoney()+truck[j].getContainer());
}
System.out.println("请输入所需卡车的编号");
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
String s=sc.next();
int k=Integer.parseInt(s.trim());
if(k<0||k>=truck.length){
System.out.println("没有这辆车请重新输入一遍");
outPutCar(i);
}
return truck[k];
}
}
public static int decideCar(){
System.out.println("请选择载货或载人,载货请输1,载人请输2,并以回车结束");
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
String s=sc.next();
int i=Integer.parseInt(s.trim());
if(i!=1&&i!=2){
System.out.println("输入错误请重新输入");
decideCar();
}
return i;
}
public static void main(String[] args){
DiDi didi=new DiDi();
int i=decideCar();
Car car=didi.outPutCar(i);
int k=inputDay();
System.out.println(car.getName()+"车租用"+k+"天的价格是1"+didi.canMoney(car,k));
}
}
你看把车型放在一个引用数组里面,用的时候调用,是不是可以减少代码量。这样的话,包括你的打印车辆信息以及switch选择部分都可以减少一大段代码量。
打印车辆信息部分添加父类方法,传不同车型打印不同信息。下面case中也在父类定义一个方法,分别传入不同值,计算出不同数据。
(拙见,也是新手)
Java入门第二季 升级版
530649 学习 · 6091 问题
相似问题