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

  

 

 

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