• JAVA 集合框架 之 ArrayList 應用

    0
    Java C/C++ list 3414 次瀏覽

    房屋信息:房主名稱,  價格, 描述)

    1 . 添加房屋

    2 . 列出房屋的所有信息

    3 . 根據 房主名稱 修改房屋

    4 . 按房主名稱查詢房屋

    5 . 根據 房主名稱  刪除房屋

    http://cn.honoit.com/home/?com=detail&id=11113296120378

     

    import java.util.List;

     

    /*

     * To change this template, choose Tools | Templates

     * and open the template in the editor.

     */

     

    /**

     *

     * @author zhangd

     */

    public class Test {

     

        /**

         * List 實現房地產公司對, 出租的房屋信息的管理

    (房屋信息:房主名稱,  價格, 描述)

    1 . 添加房屋

    2 . 列出房屋的所有信息

    3 . 根據 房主名稱 修改房屋

    4 . 按房主名稱查詢房屋

    5 . 根據 房主名稱  刪除房屋

     

         */

        public static void main(String[] args) {

            //實例化房地產公司

           Company company=new Company("鏈家地產");

           //添加房屋

           company.add(new House("李強",1500,"單間"));

           company.add(new House("王浩",3000,"一室一廳"));

           company.add(new House("張濤",4000,"兩室一廳"));

           

           //根據 房主名稱 修改房屋

           company.updateByOwner(new House("張濤",4500,"兩室一廳"));

           

           //按房主名稱查詢房屋

           House house=company.findByOwner("王浩");

           System.out.println("您查找的王浩房屋信息:"+house.getOwner()+","+house.getDescription()+","+house.getPrice());

           

           //根據 房主名稱  刪除房屋

           company.deleteByOwner("張濤");

           

           //列出房屋的所有信息

           List houseList=company.getHouseList();

           for(int i=0;i<houseList.size();i++)

           {

               House h=(House)houseList.get(i);

               System.out.println(h.getOwner()+","+h.getPrice()+","+h.getDescription());

           }

           

        }

    }

     

     

     

     

    /*

     * To change this template, choose Tools | Templates

     * and open the template in the editor.

     */

     

    /**

     *

     * @author zhangd

     */

    public class House {

        private String owner;

        private double price;

        private String description;

     

        public House(String owner, double price, String description) {

            this.owner = owner;

            this.price = price;

            this.description = description;

        }

     

        public String getOwner() {

            return owner;

        }

     

        public void setOwner(String owner) {

            this.owner = owner;

        }

     

        public double getPrice() {

            return price;

        }

     

        public void setPrice(double price) {

            this.price = price;

        }

     

        public String getDescription() {

            return description;

        }

     

        public void setDescription(String description) {

            this.description = description;

        }

        

        

    }

     

     

     

     

     

     

    import java.util.ArrayList;

    import java.util.List;

     

    /*

     * To change this template, choose Tools | Templates

     * and open the template in the editor.

     */

     

    /**

     *

     * @author zhangd

     */

    public class Company {

        private String name;

        // company 1 對 多 house

        private List houseList;

     

        public Company(String name) {

            this.name = name;

            this.houseList=new ArrayList();

        }

        

        //1 . 添加房屋

        public void add(House h)

        {

            houseList.add(h);

        }

        //2 . 列出房屋的所有信息

        public List getHouseList() {

            return houseList;

        }

        

        //3 . 根據 房主名稱 修改房屋

        public boolean updateByOwner(House h)

        {

            for(int i=0;i<houseList.size();i++)

            {

                House house=(House)houseList.get(i);

                if(house.getOwner().equals(h.getOwner()))

                {

                    //修改

                    houseList.set(i, h);

                    return true;

                }

            }

            return false;

        }

        //4 . 按房主名稱查詢房屋

        public House findByOwner(String owner)

        {

            for(int i=0;i<houseList.size();i++)

            {

                House h=(House)houseList.get(i);

                if(h.getOwner().equals(owner))

                {

                    return h;

                }

            }

            return null;

        }

        //5 . 根據 房主名稱  刪除房屋

        public boolean deleteByOwner(String owner)

        {

            for(int i=0;i<houseList.size();i++)

            {

                House h=(House)houseList.get(i);

                if(h.getOwner().equals(owner))

                {

                    houseList.remove(i);

                    return true;

                }

            }

            return false;

        }

     

        public String getName() {

            return name;

        }

     

        public void setName(String name) {

            this.name = name;

        }

     

        

     

        public void setHouseList(List houseList) {

            this.houseList = houseList;

        }

        

        

        

    }

     

     

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色