交作業,類分的比較細,用了接口,參考了大神的代碼用了List集合
各種名字起的不是很好..
車類Auto.java
package com.imooc;
public class Auto {
private int autoNum;
private String autoName;
private int autoRentprice;
public int getAutoNum() {
return autoNum;
}
public void setAutoNum(int autoNum) {
this.autoNum = autoNum;
}
public String getAutoName() {
return autoName;
}
public void setAutoName(String autoName) {
this.autoName = autoName;
}
public int getAutoRentprice() {
return autoRentprice;
}
public void setAutoRentprice(int autoRentprice) {
this.autoRentprice = autoRentprice;
}
}
小汽車類car.java
package com.imooc;
public class car extends Auto implements ImannedAuto {
private int mannedNum;
public car(int autoNum,String autoName,int autoRentprice,int mannedNum){
? ? ? ? this.setAutoName(autoName);
? ? ? ? this.setAutoNum(autoNum);
? ? ? ? this.setAutoRentprice(autoRentprice);
? ? ? ? this.mannedNum=mannedNum;
? ? }
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
this.mannedNum=mannedNum;
}
public int getmannedNum(){
return mannedNum;
}
}
大客車類Motorcoach.java:
package com.imooc;
public class Motorcoach extends Auto implements ImannedAuto {
private int mannedNum;
public Motorcoach(int autoNum,String autoName,int autoRentprice,int mannedNum){
? ? ? ? this.setAutoName(autoName);
? ? ? ? this.setAutoNum(autoNum);
? ? ? ? this.setAutoRentprice(autoRentprice);
? ? ? ? this.mannedNum=mannedNum;
? ? }
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
if(mannedNum<10)
{
System.out.println("大客車載人數應為10人以上!");
}
else this.mannedNum=mannedNum;
}
public int getmannedNum(){
return mannedNum;
}
}
小貨車類Smalltruck.java:
package com.imooc;
public class Smalltruck extends Auto implements IlabenAuto {
private int labenNum;
public Smalltruck(int autoNum,String autoName,int autoRentprice,int labenNum){
? ? ? ? this.setAutoName(autoName);
? ? ? ? this.setAutoNum(autoNum);
? ? ? ? this.setAutoRentprice(autoRentprice);
? ? ? ? this.labenNum=labenNum;
? ? }
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
if(labenNum>5)
{
System.out.println("小貨車的載貨量應為5噸以下!");
}
else this.labenNum=labenNum;
}
public int getlabenNum(){
return labenNum;
}
}
大貨車類Bigtruck.java:
package com.imooc;
public class Bigtruck extends Auto implements IlabenAuto {
private int labenNum;
public Bigtruck(int autoNum,String autoName,int autoRentprice,int labenNum){
? ? ? ? this.setAutoName(autoName);
? ? ? ? this.setAutoNum(autoNum);
? ? ? ? this.setAutoRentprice(autoRentprice);
? ? ? ? this.labenNum=labenNum;
? ? }
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
if(labenNum<10)
{
System.out.println("大貨車的載貨量應為10噸以上!");
}
else this.labenNum=labenNum;
}
public int getlabenNum(){
return labenNum;
}
}
皮卡類Pickup.java:
package com.imooc;
public class Pickup extends Auto implements IlabenAuto, ImannedAuto {
private int labenNum;
private int mannedNum;
public Pickup(int autoNum,String autoName,int autoRentprice,int labenNum,int mannedNum){
? ? ? ? this.setAutoName(autoName);
? ? ? ? this.setAutoNum(autoNum);
? ? ? ? this.setAutoRentprice(autoRentprice);
? ? ? ? this.labenNum=labenNum;
? ? ? ? this.mannedNum=mannedNum;
? ? }
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
this.mannedNum=mannedNum;
}
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
this.labenNum=labenNum;
}
public int getmannedNum(){
return mannedNum;
}
public int getlabenNum(){
return labenNum;
}
}
載人接口ImannedAuto.java:
package com.imooc;
public abstract interface ImannedAuto {
abstract void setmannedNum(int mannedNum);
}
載貨接口IlabenAuto.java;
package com.imooc;
public abstract interface IlabenAuto {
abstract void setlabenNum(int labenNum);
}
主函數類initialTest.java:
package com.imooc;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class initialTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用嗒嗒租車系統!");
? ? ? ? System.out.println("您是否要租車? :1 是 ?0 否");
? ? ? ? Scanner c=new Scanner(System.in);
? ? ? ? int k=c.nextInt();
? ? ? ? if(k == 1){
? ? ? ? System.out.println("您可租車的類型及其價目表:");
? ? ? ? Auto [] carForRent={new car(1, "奧迪A4", 500, 4),
? ? ? ? new car(2, "馬自達6", 400, 4),
? ? ? ? new Pickup(3, "皮卡雪6", 450, 4, 2),
? ? ? ? new Smalltruck(4, "金龍", 800, 2),
? ? ? ? new Smalltruck(5, "松花江", 400, 4),
? ? ? ? new Bigtruck(6, "依維柯", 1000, 20)};
? ? ? ? System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"\t"+"容量");
? ? ? ? //打印輸出可供租的汽車清單
? ? ? ? for(Auto Auto: carForRent){
? ? ? ? if(Auto instanceof car){
? ? ? ? System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
? ? ? ? Auto.getAutoRentprice()+"/天"+"\t"+"載人:"+((car) Auto).getmannedNum());
? ? ? ? }
? ? ? ? if(Auto instanceof Motorcoach){
? ? ? ? System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
? ? ? ? Auto.getAutoRentprice()+"/天"+"\t"+"載人:"+((Motorcoach) Auto).getmannedNum());
? ? ? ? }
? ? ? ? if(Auto instanceof Smalltruck){
? ? ? ? System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
? ? ? ? Auto.getAutoRentprice()+"/天"+"\t"+"載貨:"+((Smalltruck) Auto).getlabenNum());
? ? ? ? }
? ? ? ? if(Auto instanceof Bigtruck){
? ? ? ? System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
? ? ? ? Auto.getAutoRentprice()+"/天"+"\t"+"載貨:"+((Bigtruck) Auto).getlabenNum());
? ? ? ? }
? ? ? ? if(Auto instanceof Pickup){
? ? ? ? System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
? ? ? ? Auto.getAutoRentprice()+"/天"+"\t"+"載人:"+((Pickup) Auto).getmannedNum()+
? ? ? ? "載貨:"+((Pickup) Auto).getlabenNum());
? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("請輸入您要租的汽車的數量");
? ? ? ? ? ? int totalNum=c.nextInt(); //用于接收汽車的總數量
? ? ? ? ? ? //調用集合框架List用于存儲客戶租車的清單
? ? ? ? ? ? List<Auto> autoList=new ArrayList<Auto>();
? ? ? ? ? ? int p=0; //p變量用來拓展carList的容量
? ? ? ? ? ? ?
? ? ? ? ? ? List<Auto> autoListPassenger=new ArrayList<Auto>(); //用來儲存可載人的車輛
? ? ? ? ? ? List<Auto> autoListTrunk=new ArrayList<Auto>(); ?//用來儲存可載貨的車輛
? ? ? ? ? ? ?
? ? ? ? ? ? int totalPassenger=0;; //用來存儲總的載人數
? ? ? ? ? ? int totalCarrayCargo=0; //用來存儲總的載貨量
? ? ? ? ? ? int totalMoney=0; //用來計算租車的總資金
? ? ? ? ? ? ?
? ? ? ? ? ? /*
? ? ? ? ? ? ?* 主要思想:用循環遍歷在添加客戶租車的序號,用集合框架List來存儲客戶租的車輛。
? ? ? ? ? ? ?* 同時在客戶輸入序號的時候與三種車輛進行比對,計算相應車輛的載人數,載貨量以及租車的總資金
? ? ? ? ? ? ?* 同時添加到載人車輛或者載貨車輛當中
? ? ? ? ? ? ?*/
? ? ? ? ? ? for(int i=0;i<totalNum;i++){
? ? ? ? ? ? ? ? System.out.println("請輸入第"+(i+1)+"輛車的序號:");
? ? ? ? ? ? ? ? int num=c.nextInt(); //用于接收輸入的汽車的序號
? ? ? ? ? ? ? ? autoList.add((Auto) carForRent[num-1]);
? ? ? ? ? ? ? ? System.out.println("成功添加:"+autoList.get(p).getAutoName());
? ? ? ? ? ? ? ? if(carForRent[num-1] instanceof car){
? ? ? ? ? ? ? ? ? ? totalPassenger+=((car)autoList.get(p)).getmannedNum();
? ? ? ? ? ? ? ? ? ? totalMoney+=((car)autoList.get(p)).getAutoRentprice();
? ? ? ? ? ? ? ? ? ? autoListPassenger.add(autoList.get(p));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(carForRent[num-1] instanceof Motorcoach){
? ? ? ? ? ? ? ? ? ? totalPassenger+=((Motorcoach)autoList.get(p)).getmannedNum();
? ? ? ? ? ? ? ? ? ? totalMoney+=((Motorcoach)autoList.get(p)).getAutoRentprice();
? ? ? ? ? ? ? ? ? ? autoListPassenger.add(autoList.get(p));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(carForRent[num-1] instanceof Pickup){
? ? ? ? ? ? ? ? ? ? totalPassenger+=((Pickup)autoList.get(p)).getmannedNum();
? ? ? ? ? ? ? ? ? ? totalCarrayCargo+=((Pickup)autoList.get(p)).getlabenNum();
? ? ? ? ? ? ? ? ? ? totalMoney+=((Pickup)autoList.get(p)).getAutoRentprice();
? ? ? ? ? ? ? ? ? ? autoListPassenger.add(autoList.get(p));
? ? ? ? ? ? ? ? ? ? autoListTrunk.add(autoList.get(p));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(carForRent[num-1] instanceof Smalltruck){
? ? ? ? ? ? ? ? ? ? totalCarrayCargo+=((Smalltruck)autoList.get(p)).getlabenNum();
? ? ? ? ? ? ? ? ? ? totalMoney+=((Smalltruck)autoList.get(p)).getAutoRentprice();
? ? ? ? ? ? ? ? ? ? ?autoListTrunk.add(autoList.get(p));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(carForRent[num-1] instanceof Bigtruck){
? ? ? ? ? ? ? ? ? ? totalCarrayCargo+=((Bigtruck)autoList.get(p)).getlabenNum();
? ? ? ? ? ? ? ? ? ? totalMoney+=((Bigtruck)autoList.get(p)).getAutoRentprice();
? ? ? ? ? ? ? ? ? ? ?autoListTrunk.add(autoList.get(p));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? p++;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("請輸入租車的天數:");
? ? ? ? ? ? int carRenDay=c.nextInt();
? ? ? ? ? ? totalMoney=totalMoney*carRenDay;
? ? ? ? ? ? ?
? ? ? ? ? ? System.out.println("********************可載人的車有********************");
? ? ? ? ? ? for (Auto auto : autoListPassenger) {
? ? ? ? ? ? ? ? System.out.println(auto.getAutoName());
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? ? ? System.out.println("********************可載貨的車有********************");
? ? ? ? ? ? for (Auto auto : autoListTrunk) {
? ? ? ? ? ? ? ? System.out.println(auto.getAutoName());
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? ? ? System.out.println("********************租車的總價格********************");
? ? ? ? ? ? System.out.println(totalMoney*carRenDay);
? ? ? ? }
? ? ? ? else if(k==0){
? ? ? ? ? ? System.out.println("感謝您的再次使用!再見!");
? ? ? ? ? ? ?
? ? ? ? }else{
? ? ? ? ? ? System.out.println("您輸入的信息有錯!");
? ? ? ? }
}
}
2015-12-15
受教了