Java: study demo

/**
 * 1.某小夥想定一份外賣,商家的優惠方式如下:
 * 魚香肉絲ShreddedPork單點24元,油炸花生米FriedPeanuts單點8元,米飯 CookedRice單點3元。
 * 訂單滿30元8折優惠。  discountedPrice
 * 魚香肉絲優惠價16元,但是優惠價和折扣不能同時使用。preferentialPrice
 * 那麼這個小夥要點這三樣東西,最少要花多少錢?
 * */

package org.example;
/**
 *就餐食物實體類
 * @author geovindu
 * @version  1.0
 *
 * */
public class FoodInfo {
    /**
     *魚香肉絲價
     * */
    private  int shreddedPork;
    /**
     * 魚香肉絲優惠價
     * */
    private int preferentialPork;
    /**
     *油炸花生米價
     * */
    private  int friedPeanuts;
    /**
     *米飯價
     * */
    private  int cookedRice;
    /**
     *折扣價
     * */
    private  double discountedPrice;
    /**
     *優惠價
     *
     * */
    private  double preferentialPrice;
    /**
     *是否選擇折扣
     *
     * */
    private  boolean isDiscount;
    /**
     * 選擇優惠價還是折扣價
     * 真爲折扣價
     * */
    private  boolean checkDiscount;

    /**
     *
     * */
    public FoodInfo()
    {

    }
    /**
     *
     * */
    public FoodInfo(int shreddedPork,int friedPeanuts,int cookedRice, double discountedPrice,double preferentialPrice,boolean isDiscount)
    {
        this.shreddedPork=shreddedPork;
        this.preferentialPork=16;
        this.friedPeanuts=friedPeanuts;
        this.cookedRice=cookedRice;
        this.discountedPrice=discountedPrice;
        this.preferentialPork=preferentialPork;
        this.isDiscount=isDiscount;
    }
    /**
     *魚香肉絲價
     * @param shreddedPork 輸入參數
     *
     * */
    public void setShreddedPork(int shreddedPork) {
        this.shreddedPork = shreddedPork;
    }
    /**
     *魚香肉絲價
     * */
    public int getShreddedPork() {
        return shreddedPork;
    }
    /**
     *輸入油炸花生米價
     * @param friedPeanuts  輸入參數
     * */
    public void setFriedPeanuts(int friedPeanuts) {
        this.friedPeanuts = friedPeanuts;
    }
    /**
     *返回油炸花生米價
     * @return  返回油炸花生米價
     * */
    public int getFriedPeanuts() {
        return friedPeanuts;
    }
    /**
     *設置米飯價格
     * @param cookedRice  輸入參數
     * */
    public void setCookedRice(int cookedRice) {
        this.cookedRice = cookedRice;
    }
    /**
     *返回米飯價
     * @return  返順米飯價格
     * */
    public int getCookedRice() {
        return cookedRice;
    }
    /**
     *設置折扣價
     * @param discountedPrice  輸入參數
     *
     * */
    public void setDiscountedPrice(double discountedPrice) {
        this.discountedPrice = discountedPrice;
    }
    /**
     *折扣價格
     * @return  返回折扣價格
     * */
    public double getDiscountedPrice() {
        return discountedPrice;
    }
    /**
     *設置優惠價
     * @param preferentialPrice  輸入參數
     * */
    public void setPreferentialPrice(double preferentialPrice) {
        this.preferentialPrice = preferentialPrice;
    }
    /**
     *優惠價
     * @return  返回優惠價
     * */
    public double getPreferentialPrice() {
        return preferentialPrice;
    }
    /**
     *設置是否折扣價
     * @param discount  輸入參數
     * */
    public void setDiscount(boolean discount) {
        isDiscount = discount;
    }
    /**
     *是否折扣價
     * @return  返回折扣價
     * */
    public boolean getIsDiscount() {
        return isDiscount;
    }
    /**
     *魚香肉絲優惠價
     * @param preferentialPork  輸入參數
     *
     * */
    public void setPreferentialPork(int preferentialPork) {
        this.preferentialPork = preferentialPork;
    }
    /**
     *香肉絲優惠價
     * @return  返回魚香肉絲優惠價
     *
     * */
    public int getPreferentialPork() {
        return preferentialPork;
    }

    public void setCheckDiscount(boolean checkDiscount) {
        this.checkDiscount = checkDiscount;
    }

    public boolean isCheckDiscount() {
        return checkDiscount;
    }

    /**
     *自然實際選擇的最低的價格
     * @return  返回自然選擇最低價格
     * */
    public  double getPrice()
    {
        double price;
        double issum=this.shreddedPork+this.friedPeanuts+this.cookedRice;
        double psum=this.preferentialPork+this.friedPeanuts+this.cookedRice;
        //總價大於30可以有折扣
        if(issum>30)
        {

            if(psum<(issum*0.8))
            {
                setCheckDiscount(false);
                price=psum;
            }
            else
            {
                setCheckDiscount(true);
                price=issum*0.8;
            }

        }
        else //不大於30 無折扣
        {
            if(psum<issum)
            {
                setCheckDiscount(false);
                price=psum;
            }
            else
            {
                setCheckDiscount(true);
                price=issum;
            }
        }
        return price;

    }
}



/**
 * 2.身高是具有遺傳性的,子女的身高和父母的身高有一定的關係。假定,父母和子女的身高遺傳關係如下: *
 * 兒子身高(釐米)=(父親身高+母親身高) ×1.08÷2 *
 * 女兒身高(釐米)=(父親身高×0.923+母親身高) ÷2 *
 * 現有父親身高177CM,母親身高165CM。求子女身高分別預計爲多少?
 * **/

package org.example;

/**
 * 父母實體類
 * @author geovindu
 * @version  1.0
 * */
public class ParentsInfo {

    /**
     *
     *
     * */
    private  int fatherBodyHeight;
    /**
     *
     *
     * */
    private  int motherBodyHeight;
     public  ParentsInfo()
     {}
    /**
     *
     * @param fatherBodyHeight 輸入父親身高
     * @param motherBodyHeight 輸入母親身高
     *
     * */
    public ParentsInfo(int fatherBodyHeight, int motherBodyHeight)
    {
        this.fatherBodyHeight=fatherBodyHeight;
        this.motherBodyHeight=motherBodyHeight;
    }

    /**
     * 設置父樣身高
     * @param fatherBodyHeight  設置輸入父親身高參數
     *
     * */
    public void setFatherBodyHeight(int fatherBodyHeight) {
        this.fatherBodyHeight= fatherBodyHeight;
    }
    /**
     * 得到父親身高
     * @param
     *  @return 得到父親身高
     * */
    public int getFatherBodyHeight() {
        return fatherBodyHeight;
    }
    /**
     *設置母親身高
     * @param motherBodyHeight 輸入母親身高參數
     *
     * */
    public void setMotherBodyHeight(int motherBodyHeight) {
        this.motherBodyHeight= motherBodyHeight;
    }
    /**
     *得到母親身高
     * @return  返回母親身高
     * */
    public int getMotherBodyHeight() {
        return motherBodyHeight;
    }
}


/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

/**
 *兒女的身高
 * @author geovindu
 * @version 1.0
 * */
public class SonDaughterInfo {
    /**
     *
     * */
    private  double sonBodyHeight;
    /**
     *
     * */
    private  double daughterBodyHeight;

    public SonDaughterInfo(){

    }
    /**
     *
     * @param sonBodyHeight 輸入兒子身高
     * @param daughterBodyHeight 輸入女兒身高
     * */
    public SonDaughterInfo(double sonBodyHeight,double daughterBodyHeight)
    {
        this.sonBodyHeight=sonBodyHeight;
        this.daughterBodyHeight=daughterBodyHeight;
    }
    /**
     *
     * @param sonBodyHeight 輸入兒子的身高
     *
     * */
    public void setSonBodyHeight(double sonBodyHeight) {
        this.sonBodyHeight = sonBodyHeight;
    }
    /**
     *
     * @return 返回兒子身高
     * */
    public double getSonBodyHeight()
    {
        return  sonBodyHeight;
    }
    /**
     *
     * @param daughterBodyHeight  輸入女兒的身高
     *
     * */
    public void setDaughterBodyHeight(double daughterBodyHeight) {
        this.daughterBodyHeight = daughterBodyHeight;
    }
    /**
     *
     * @return  返回女兒的身高
     * */
    public double getDaughterBodyHeight() {
        return daughterBodyHeight;
    }
    /**
     * 得到女兒的身高
     * @param parentsInfo 輸入父母的資料實體類
     * @return  得到女兒的身高
     * */
    public double getDaughterHeight(ParentsInfo parentsInfo)
    {
       double height= (parentsInfo.getFatherBodyHeight()*0.923+parentsInfo.getMotherBodyHeight())/2;
       return height;
    }
    /**
     *得到兒子的身高
     * @param parentsInfo 輸入父母的資料的實體類
     * @return  得到兒子的身高
     * */
    public double getSonHeight(ParentsInfo parentsInfo)
    {

        double height=(parentsInfo.getFatherBodyHeight()+parentsInfo.getMotherBodyHeight())*1.08/2;
        return height;
    }

}


/**
 *3.紅茶妹妹有21元錢,她攢了幾天錢之後自己的錢比原來的兩倍還多三塊。綠茶妹妹有24元錢,
 * 她攢了幾天錢之後自己的錢正好是原來的兩倍。
 * 那麼紅茶和綠茶現在的錢一樣多,請問對麼?
 *
 * */


package org.example;
/**
 *
 * */
public class TeaMoneyInfo {

   /**
    *紅茶女原價格
    * */
   private  int redTeaSister;
   /**
    *綠茶女原價格
    * */
   private int greenTeaSister;
   /**
    *紅茶女計算的價
    * */
   private  int redMoney;
   /**
    *綠茶女計算的價
    * */
   private  int greenMoney;

   public  TeaMoneyInfo()
   {

   }

   public TeaMoneyInfo(int redTeaSister,int greenTeaSister,int redMoney,int greenMoney)
   {
      this.redTeaSister=redTeaSister;
      this.greenTeaSister=greenTeaSister;
      this.redMoney=redMoney;
      this.greenMoney=greenMoney;

   }

   /**
    *
    * */
   public void setRedTeaSister(int redTeaSister) {
      this.redTeaSister = redTeaSister;
   }
   /**
    *
    * */
   public int getRedTeaSister() {
      return redTeaSister;
   }
   /**
    *
    * */
   public void setGreenTeaSister(int greenTeaSister) {
      this.greenTeaSister = greenTeaSister;
   }
   /**
    *
    * */
   public int getGreenTeaSister() {
      return greenTeaSister;
   }
   /**
    *
    * */
   public void setRedMoney(int redMoney) {
      this.redMoney = redMoney;
   }
   /**
    *
    * */
   public int getRedMoney() {
      return redMoney;
   }
   /**
    *
    * */
   public void setGreenMoney(int greenMoney) {
      this.greenMoney = greenMoney;
   }
   /**
    *
    * */
   public int getGreenMoney() {
      return greenMoney;
   }

   public String getMoney()
   {
      String str="";
      this.redMoney=this.redTeaSister*2+3;
      this.greenMoney=this.greenTeaSister*2;
      if(this.redMoney==this.greenMoney)
      {
         str="兩位妹妹一樣多!爲:"+this.redMoney;
      }
      else
      {
         if(this.redMoney>this.greenMoney)
         {
            str="紅茶妹妹多,紅茶妹妹爲:"+this.redMoney+" 綠茶妹妹爲:"+this.greenMoney;
         }
         else
         {
            System.out.println("綠茶妹妹多,紅茶妹妹爲:"+this.redMoney+" 綠茶妹妹爲:"+this.greenMoney);
         }
      }

      return str;
   }

}

  

/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

/**
 * 兒女枚舉類
 * */
public enum SonDaughter {

    /**
     * 兒子
     * */
    Son,
    /**
     * 女兒
     * */
    Daughter;
}


/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

/**
 * 業務處理類
 * @author geovindu
 * @version  1.0
 *
 * */
public class FoodDal {


    /**
     *業務處理
     * @param foodInfo
     * @return
     * */
    public double getDiscount(FoodInfo foodInfo)
    {
        double price;
        if(foodInfo.getIsDiscount()==true) {
            if (foodInfo.getShreddedPork() + foodInfo.getFriedPeanuts() + foodInfo.getCookedRice() > 30) {
                price = (foodInfo.getShreddedPork() + foodInfo.getFriedPeanuts() + foodInfo.getCookedRice() )* 0.8;
                foodInfo.setDiscountedPrice((foodInfo.getShreddedPork() + foodInfo.getFriedPeanuts() + foodInfo.getCookedRice()) * 0.8);

            }
            else
            {
                price = foodInfo.getShreddedPork() + foodInfo.getFriedPeanuts() + foodInfo.getCookedRice();
                foodInfo.setDiscountedPrice(foodInfo.getShreddedPork() + foodInfo.getFriedPeanuts() + foodInfo.getCookedRice());

            }
        }
        else
        {
            price= foodInfo.getPreferentialPork()+ foodInfo.getFriedPeanuts()+ foodInfo.getCookedRice();
            foodInfo.setPreferentialPrice(foodInfo.getPreferentialPork()+ foodInfo.getFriedPeanuts()+ foodInfo.getCookedRice());
        }
        return price;

    }

}


/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

/**
 *求兒女身高業務操作類
 * @author geovindu
 * @version 1.0
 *
 * */
public class SonDaughterDal {

    /**
     *
     * */
    public SonDaughterInfo getInfoHeight(ParentsInfo parentsInfo)
    {
        SonDaughterInfo sonDaughterInfo=new SonDaughterInfo();
        double height=(parentsInfo.getFatherBodyHeight()+parentsInfo.getMotherBodyHeight())*1.08/2;
        sonDaughterInfo.setSonBodyHeight(height);
        sonDaughterInfo.getDaughterHeight(parentsInfo);
        sonDaughterInfo.getSonHeight(parentsInfo);
        double height1=(parentsInfo.getFatherBodyHeight()*0.923+parentsInfo.getMotherBodyHeight())/2;
        sonDaughterInfo.setDaughterBodyHeight(height1);


        return sonDaughterInfo;

    }

}


/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

/**
 * 茶妹妹的處理業務
 * @author geovindu
 * @version 1.0
 *
 * */
public class TeaMoneyDal {

    /**
     * 得到誰錢的簡要描述
     *
     * */
    public String getMoney(TeaMoneyInfo teaMoneyInfo)
    {
        String getStr="";
        teaMoneyInfo.setRedMoney(teaMoneyInfo.getRedTeaSister()*2+3);
        teaMoneyInfo.setGreenMoney(teaMoneyInfo.getGreenTeaSister()*2);
        if(teaMoneyInfo.getRedMoney()==teaMoneyInfo.getGreenMoney())
        {
            getStr="兩位妹妹一樣多!爲:"+teaMoneyInfo.getRedMoney();
        }
        else
        {
            if(teaMoneyInfo.getRedMoney()>teaMoneyInfo.getGreenMoney())
            {
                getStr="紅茶妹妹多,紅茶妹妹爲:"+teaMoneyInfo.getRedMoney()+" 綠茶妹妹爲:"+teaMoneyInfo.getGreenMoney();
            }
            else
            {
                getStr="綠茶妹妹多,紅茶妹妹爲:"+teaMoneyInfo.getRedMoney()+" 綠茶妹妹爲:"+teaMoneyInfo.getGreenMoney();
            }
        }
        return getStr;
    }
}

  

調用:

/**
 * geovindu,Geovin Du
 * 2023-03-12
 * */

package org.example;

import java.io.*;
import java.util.*;


public class Main {


    public static void main(String[] args) {


          /**
             * 魚香肉絲全價
        * */
        int fish=24;
        /**
         *魚香肉絲優惠價
         * */
        int fish1=16;
        /**
         *油炸花生米
         * */
        int peanut=8;
        /**
         *米飯
         * */
        int rise=3;
        /**
         *全價
         * */
        int sum=0;
        /**
         *魚香優惠全價
         * */
        int sum1=0;
        /**
         *
         * */
        sum=fish+peanut+rise; //總和價,便打折扣  Discount
        sum1=fish1+peanut+rise; //優惠魚香肉絲價  Preferential
        /**
         *折扣價
         * */
        double price=0;
        /**
         *優惠價
         * */
        double price2=0;

        if(sum>30)
        {
            price=sum*(1-0.2);
        }
        else {
            price=sum;
        }
        price2=sum1;
        System.out.println("優惠價:"+price2+"; 折扣價:"+price);
        if(price2>price)
        {
            System.out.println("選擇折扣");
        }
        else {

            System.out.println("選擇魚香肉絲優惠價");
        }

        price=getDiscount(fish,peanut,rise,true);
        price2=getDiscount(fish1,peanut,rise,false);
        System.out.println("方法 優惠價:"+price2+"; 折扣價:"+price);
        if(price2>price)
        {
            System.out.println("方法 選擇折扣");}
        else
        {
            System.out.println("方法 選擇魚香肉絲優惠價");}

        //身高
        int father=177;
        int mother=165;
        double son=getBodyHeight(father,mother,SonDaughter.Son);
        System.out.println("生兒子身高"+son);
        double daughter=getBodyHeight(father,mother,SonDaughter.Daughter);
        System.out.println("生女兒身高"+daughter);
        int redtea=21;
        int greentea=24;
        boolean istea=getMoney(redtea,greentea);
        if(istea)
        {
            System.out.println("一樣多");
        }
        else {
            System.out.println("不一樣多");
        }
        //實體類操作
        //不折扣
        FoodInfo foodInfoIsNo=new FoodInfo();
        foodInfoIsNo.setDiscount(false);
        foodInfoIsNo.setShreddedPork(24);
        foodInfoIsNo.setPreferentialPork(16);
        foodInfoIsNo.setFriedPeanuts(8);
        foodInfoIsNo.setCookedRice(3);
        double noDiscountPrice=new FoodDal().getDiscount(foodInfoIsNo);
        String ischeck=foodInfoIsNo.isCheckDiscount()?"折扣價":"優惠價";
        System.out.println(noDiscountPrice+"/ 實體操作"+foodInfoIsNo.getPrice()+"/"+ischeck);
        //折扣
        FoodInfo foodInfo=new FoodInfo();
        foodInfo.setDiscount(true);
        foodInfo.setShreddedPork(24);
        foodInfo.setPreferentialPork(16);
        foodInfo.setFriedPeanuts(8);
        foodInfo.setCookedRice(3);
        double discountPrice=new FoodDal().getDiscount(foodInfo);
        ischeck=foodInfo.isCheckDiscount()?"折扣價":"優惠價";
        System.out.println(discountPrice+"/ 實體操作 "+foodInfo.getPrice()+"/"+ischeck);
        if(noDiscountPrice>discountPrice)
        {
            System.out.println("業務操作方法 選擇折扣,折扣價爲:"+foodInfo.getDiscountedPrice()+" 優惠價爲:"+foodInfo.getPreferentialPrice());
        }
        else
        {
            System.out.println("業務操作方法 選擇魚香肉絲優惠價,折扣價爲:"+foodInfo.getDiscountedPrice()+" 優惠價爲:"+foodInfoIsNo.getPreferentialPrice());
        }
        //1
        ParentsInfo parentsInfo=new ParentsInfo();
        SonDaughterInfo sonDaughterInfo=new SonDaughterInfo();
        parentsInfo.setFatherBodyHeight(177);
        parentsInfo.setMotherBodyHeight(165);
        System.out.println("女兒身高:"+sonDaughterInfo.getDaughterHeight(parentsInfo)+" 兒子身高:"+sonDaughterInfo.getSonHeight(parentsInfo));
        //2
        SonDaughterInfo sonDaughter=new SonDaughterInfo();
        sonDaughter=new SonDaughterDal().getInfoHeight(parentsInfo);
        System.out.println("業務操作  女兒身高:"+sonDaughter.getDaughterBodyHeight()+" 兒子身高:"+sonDaughter.getSonBodyHeight());

        //1
        TeaMoneyInfo teaMoneyInfo=new TeaMoneyInfo();
        teaMoneyInfo.setRedTeaSister(21);
        teaMoneyInfo.setGreenTeaSister(24);
        System.out.println(teaMoneyInfo.getMoney());

        //2
        TeaMoneyDal teaMoneyDal=new TeaMoneyDal();
        System.out.println("業務操作 "+teaMoneyDal.getMoney(teaMoneyInfo));


    }

    /**
     * A比B的大的邏輯結果: 真 否則爲假
     * @param a 輸入參數
     * @param b 輸入參數
     * @return  返回邏輯真假
     * */
    private  static  boolean  getMore(int a,int b)
    {
        boolean isok=false;
        if(a>b)
        {
            isok=true;
        }
        else
        {
            isok=false;
        }
        System.out.println("a="+a+", b="+b);
        return  isok;
    }
    /**
     * 計算折扣價和優惠價
     * @param fish 魚香肉絲價 價格
     * @param peanut 油炸花生米 價格
     * @param rise 米飯價格
     * @param isDiscount 是否選擇折扣價,否則是優惠價
     * @return 返回價格
     * */
    private static double getDiscount(int fish,int peanut,int rise,boolean isDiscount)
    {

        int disfish=16; //魚香肉絲優惠價

        double price=0;
        if(isDiscount==true)
        {
            if((fish+peanut+rise)>30)
                price=(fish+peanut+rise)*0.8;
            else
                price=fish+peanut+rise;
        }
        else
        {
            price=(disfish+peanut+rise);
        }
        return price;
    }

    /**
     * 根據父母身高求兒女的身高方法
     * @param fatherBodyHeight 父親身高
     * @param motherBodyHeight 母親身高
     * @param sonDaughter  輸入選擇是兒子還是女兒
     * @return  返回身高
     * */
    private static double getBodyHeight(int fatherBodyHeight,int motherBodyHeight, SonDaughter sonDaughter)
    {
        double bodyHeithg=0;
        if(sonDaughter==SonDaughter.Son)
        {
            bodyHeithg=(fatherBodyHeight+motherBodyHeight)*1.08/2;
        }
        else
        {
            bodyHeithg=(fatherBodyHeight*0.923+motherBodyHeight)*2;

        }
        return  bodyHeithg;
    }
    /**
     * 紅茶妹和綠茶妹是否一樣多,真爲一樣多,假爲不一樣多
     * @param redTeaSister 紅茶妹妹的初始錢
     * @param greenTeaSister 綠茶妹妹的初始錢
     * @return  返回真假
     * */
    private  static boolean getMoney(int redTeaSister,int greenTeaSister)
    {
        boolean isMore=false;
        int redmoney=0;
        int greenmoney=0;
        redmoney=redTeaSister*2+3;
        greenmoney=greenTeaSister*2;
        if(redmoney==greenmoney)
        {
            isMore=true;
            System.out.println("兩位妹妹一樣多!爲:"+redmoney);
        }
        else
        {
            if(redmoney>greenmoney)
            {
                System.out.println("紅茶妹妹多,紅茶妹妹爲:"+redmoney+" 綠茶妹妹爲:"+greenmoney);
            }
            else
            {
                System.out.println("綠茶妹妹多,紅茶妹妹爲:"+redmoney+" 綠茶妹妹爲:"+greenmoney);
            }
            isMore=false;
        }
        return isMore;
    }
}

  輸出:

優惠價:27.0; 折扣價:28.0
選擇魚香肉絲優惠價
方法 優惠價:27.0; 折扣價:28.0
方法 選擇魚香肉絲優惠價
生兒子身高184.68
生女兒身高656.742
綠茶妹妹多,紅茶妹妹爲:45 綠茶妹妹爲:48
不一樣多
27.0/ 實體操作27.0/優惠價
28.0/ 實體操作 27.0/優惠價
業務操作方法 選擇魚香肉絲優惠價,折扣價爲:28.0 優惠價爲:27.0
女兒身高:164.1855 兒子身高:184.68
業務操作  女兒身高:164.1855 兒子身高:184.68
綠茶妹妹多,紅茶妹妹爲:45 綠茶妹妹爲:48

業務操作 綠茶妹妹多,紅茶妹妹爲:45 綠茶妹妹爲:48

  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章