Wednesday, October 03, 2012

Java範例程式 : package

將之前的車輛生產程式拆分成不同的 package.

程式碼
pCARb 目錄下 的 C13P315.java
package pCARb;
import pCAR2.Sub.*;

class C13P315
{
 public static void main(String[] args)
 {
  String querystr = "[[[查詢車輛資訊]]]";
  String setupstr = "[[[設定車輛資訊]]]";

  System.out.println("\n [[[ GM 汽車生產陣列 ]]]");
  Car[] GM = new Car[12];
  int BASEID = 6600;
  for(int i = 0; i < GM.length; i++){
   if( i < 6 ){
    GM[i] = new Car(i+BASEID, 1688.88888);
   } else if( i >= GM.length - 2 ){
    //GM[i] = new Car(i+6600, 0.0); //OK
    GM[i] = new Car(i+BASEID);
    //GM[i].setCar(i+6600);
   } else {
    GM[i] = new Car(i+BASEID, 1666.666);
   }
  }
  Car.showCarSum();

  System.out.println("\n [[[ GM ]]] " + setupstr);
  GM[2].setCar(1888.88);
  GM[0].setCar(1888.88);
  GM[8].setCar(1888.88);
  System.out.println("\n [[[ GM ]]] " + querystr);
  for(int i = 0; i < GM.length; i++){
   GM[i].showCar();
  }

  Car.showCarSum();

  System.out.println("\n\n打造第一台賽車");
  RacingCar rc1 = new RacingCar();
  rc1.setSpeed(600);
  rc1.setCar(666, 2500.0);
  rc1.showCar();
  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ LOTUS 汽車/賽車生產陣列 ]]]");
  RacingCar[] LOTUS = new RacingCar[6];
  BASEID = 8800;
  for(int i = 0; i < LOTUS.length; i++){
   if( i/3 == 0 ){
    ////LOTUS[i] = new RacingCar();
    ////LOTUS[i].setCar(i+BASEID, 2000.0);
    //LOTUS[i] = new RacingCar(i+BASEID, 2000.0);
    //LOTUS[i].setSpeed(500);
    LOTUS[i] = new RacingCar(i+BASEID, 2000.0, 500);
   } else {
    ////LOTUS[i] = new RacingCar();
    ////LOTUS[i].setCar(i+BASEID, 2500.0);
    //LOTUS[i] = new RacingCar(2500.0, i+BASEID);
    //LOTUS[i].setSpeed(660);
    LOTUS[i] = new RacingCar(2500.0, i+BASEID, 600);
   }
   System.out.print("調整渦輪加速級數 : ");
   LOTUS[i].setTurbo(i%3*2+1+i/3);
  }

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ LOTUS ]]] " + querystr);
  for(int i = 0; i < LOTUS.length; i++){
   LOTUS[i].showCar();
  }

  Car.showCarSum();
  RacingCar.showCarSum();
 }
}

pCAR2/sub 目錄下的 Car.java 及 RacingCar.java
package pCAR2.Sub;

public class Car
{
 private int id;
 private double gasoline;
 private double MAXGAS = 2500;
 private static int carsum = 0;

 public Car()
 {
  id = 0;
  gasoline = 0.0;
  carsum++;
  this.manufactCar();
 }

 public Car(int id)
 {
  this();
  this.setCar(id);
 }

 public Car(int id, double gasoline)
 {
  this();
  this.setCar(id, gasoline);
 }

 public Car(double gasoline, int id)
 {
  this();
  this.setCar(gasoline, id);
 }

 private void prepareParts()
 {
  System.out.println("pCAR2 Sub");
  System.out.println("備料");
 }

 private void manufactCar()
 {
  this.prepareParts();
  System.out.println("製造一輛車");
 }

 public int getID()
 {
  return this.id;
 }

 public double getGas()
 {
  return this.gasoline;
 }

 public boolean setCar(int id)
 {
  if( id <= 0 ){
   System.out.println("!!! 車號 (" + id + ") 不可小於或等於 0. !!!");
   return false;
  } else {
   this.id = id;
   System.out.println("設定車號 " + this.id);
   return true;
  }
 }

 public boolean setCar(double gasoline)
 {
  if( gasoline < 0 || gasoline > this.MAXGAS ){
   System.out.println("!!! 油量 (" + gasoline + ") 不可小於 0 或大於 " + this.MAXGAS + ". !!!");
   return false;
  } else {
   this.gasoline = gasoline;
   System.out.println("車號 " + this.id + " 設定油量 " + this.gasoline);
   return true;
  }
 }

 public boolean setCar(int id, double gasoline)
 {
  return this.setCar(id) && this.setCar(gasoline);
 }

 public boolean setCar(double gasoline, int id)
 {
  return this.setCar(id) && this.setCar(gasoline);
 }

 public void showCar()
 {
  if( this.id == 0 ){
   System.out.print("車號 尚未設定");
  } else {
   System.out.print("車號 " + this.id);
  }

  if( this.gasoline <= 0 ){
   System.out.print(" ( 尚未設定油量 )\n");
  } else {
   System.out.print(" ( 油量 " + this.gasoline + " )\n");
  }
 }

 public static void showCarSum()
 {
  System.out.println("###### 總共生產 " + carsum + " 輛車. ######");
 }
}

package pCAR2.Sub;

public class RacingCar extends Car
{
 private int turbo = 1;
 private int speed;
 private static int carsum = 0;

 public RacingCar()
 {
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車");
 }

 public RacingCar(int id)
 {
  super(id);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.getID() + " 預設速度 " + speed + ")");
 }

 public RacingCar(int id, double gasoline)
 {
  super(id, gasoline);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.getID() + " 油量 " + this.getGas() + " 預設速度 " + speed + ")");
 }

 public RacingCar(double gasoline, int id)
 {
  super(gasoline, id);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.getID() + " 油量 " + this.getGas() + " 預設速度 " + speed + ")");
 }

 public RacingCar(int id, double gasoline, int speed)
 {
  super(id, gasoline);
  this.setSpeed(speed);
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.getID() + " 油量 " + this.getGas() + " 速度 " + this.speed + ")");
 }

 public RacingCar(double gasoline, int id, int speed)
 {
  super(gasoline, id);
  this.setSpeed(speed);
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.getID() + " 油量 " + this.getGas() + " 速度 " + this.speed + ")");
 }

 public void setSpeed(int speed)
 {
  int oldspeed = this.speed;
  if( oldspeed == speed ){
   System.out.println("賽車速度維持 " + this.speed);
  } else {
   this.speed = speed;
   System.out.println("將賽車速度由 " + oldspeed + " 調整為 " + this.speed);
  }
 }

 public void setTurbo(int turbo)
 {
  int oldturbo = this.turbo;
  if( oldturbo == turbo ){
   System.out.println("渦輪加速級數維持 " + this.turbo);
  } else {
   this.turbo = turbo;
   System.out.println("將渦輪加速級數由 " + oldturbo + " 調整為 " + this.turbo);
  }
 }

 public void showCar()
 {
  System.out.print("賽車");
  if( this.getID() == 0 ){
   System.out.print("車號 尚未設定");
  } else {
   System.out.print("車號 " + this.getID());
  }

  if( this.getGas() <= 0 ){
   System.out.print(" ( 尚未設定油量 ");
  } else {
   System.out.print(" ( 油量 " + this.getGas() + " ");
  }

  System.out.print("渦輪加速級數 " + this.turbo + " ");

  if( this.speed <= 0 ){
   System.out.print("尚未設定速度 )\n");
  } else {
   System.out.print("速度 " + this.speed + " )\n");
  }
 }

 public static void showCarSum()
 {
  System.out.println("###### 總共生產 " + carsum + " 輛賽車. ######");
 }
}

編譯及執行 script
#!/bin/bash

PATH=$PATH

PKGPATH="pCAR2/Sub"
PKGPATHb="pCARb"
SRC="C13P315.java"
CLASS="C13P315.class"
CarCLASS="Car.class"
RCarCLASS="RacingCar.class"

if [ ! -d $PKGPATH ];then
 echo "Package folder $PKGPATH does not existed."
 exit 1
fi

if [ ! -d $PKGPATHb ];then
 echo "Package folder $PKGPATHb does not existed."
 exit 1
fi

if [ -f ./$PKGPATHb/$SRC ]; then
 if [ ! -f ./$PKGPATHb/$CLASS ]||[ ! -f ./$PKGPATH/$CarCLASS ]||[ ! -f ./$PKGPATH/$RCarCLASS ]; then
  echo "Compile $SRC"
  javac $PKGPATHb/$SRC
 fi
fi

if [ -f ./$PKGPATHb/$CLASS ]&&[ -f ./$PKGPATH/$CarCLASS ]&&[ -f ./$PKGPATH/$RCarCLASS ]; then
 java pCARb.C13P315
else
 echo "Can not find $CLASS $CarCLASS $RCarCLASS"
fi

No comments: