weixin_42656574 2018-11-02 18:37 采纳率: 60%
浏览 449
已采纳

有大佬能帮我用Java写一下吗

abstract class: “item”

every item has a unique item_id (a positive integer, auto-incremented for each new item), a price (double), and quantity (integer).

Every item can be “Displayed()”, an abstract method, and “Purchased()” (that is removed from the inventory).

concrete class: “book”

a book is-an item. It adds the attributes “author”, “title” and “year”.

concrete class: “gift”

a gift is-an item. It adds the attributes “label”, and “manufacturer”.

Write an application that maintains a list of items entered from the user by means of an interactive menu. The user can:

Add item to the inventory, entering its type (book or gift), and necessary attributes along with quantity.

Display all items.

Display only books sorted by author name.

Display only gifts sorted by label.

Delete an item by item_id

Purchase an item by removing the purchased quantity from the inventory

Bonus: Save items to a file, and loading them back, using object serialization.

Write all necessary classes and methods to ensure the responsibility driven assignment of classes in a pure object oriented approach enforcing all business rules. Document all the rules and classes using Javadoc compatible documentation.

  • 写回答

1条回答

  • threenewbee 2018-11-03 01:47
    关注

    菜单我这里不方便写,我把调用写给你,你自己完善下:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    class Untitled {
        public static void main(String[] args) throws Exception {
            ArrayList<item> list = new ArrayList<>();
            book book1, book2, book3;
            book1 = new book(32.0, 100, "tom", "java programming", 2001);
            book2 = new book(45.0, 200, "jack", "data structure via c++", 2003);
            book3 = new book(47.0, 150, "jim", "linux system programming", 2007);
            gift gift1, gift2, gift3;
            gift1 = new gift(101.0, 50, "toy tank", "company1");
            gift2 = new gift(145.0, 100, "toy plane", "company2");
            gift3 = new gift(9.7, 250, "puzzle", "company3");
            list.add(book1);
            list.add(book2);
            list.add(book3);
            list.add(gift1);
            list.add(gift2);
            list.add(gift3);
            System.out.println("-----all items:");
            for (item i : list)
                i.Displayed();
            System.out.println("-----books (sort by author):");
            ArrayList<book> booklist = new ArrayList<>();
            for (item i : list)
            {
                if (i instanceof book)
                {
                    booklist.add((book)i);
                }
            }
            Collections.sort(booklist, new Comparator<book>(){
                public int compare(book b1, book b2) {
                    return b1.getauthor().compareTo(b2.getauthor());
                }
            });
            for (book i : booklist)
                i.Displayed();
            int toremove = 3;
            for (item i : list)
                if (i.getitem_id() == toremove)
                {
                    list.remove(i);
                    break;
                }
            int topurchase = 1;
            for (item i : list)
                if (i.getitem_id() == topurchase)
                {
                    i.Purchased();
                    break;
                }
            System.out.println("-----removed and purchased:");
            for (item i : list)
                i.Displayed();
        }
    }
    
    abstract class item
    {
        static private int maxitem_id = 0;
        private int item_id;
        public int getitem_id()
        {
            return item_id;
        }
        public item()
        {
            item_id = ++item.maxitem_id;
        }
        public item(double p, int q)
        {
            this();
            price = p;
            quantity = q;
        }
        private double price;
        public double getprice()
        {
            return price;
        }
        public void setprice(double v)
        {
            price = v;
        }
        private int quantity;
        public int getquantity()
        {
            return quantity;
        }
        public void setquantity(int v)
        {
            quantity = v;
        }
        public abstract void Displayed();
        public void Purchased() throws Exception
        {
            if (quantity <= 0) 
                throw new Exception("quantity couldn't be 0 or less than 0.");
            quantity--;
        }
    }
    
    class book extends item
    {
        private String author;
        public String getauthor()
        {
            return author;
        }
        public void setauthor(String v)
        {
            author = v;
        }
        private String title;
        public String gettitle()
        {
            return title;
        }
        public void settitle(String v)
        {
            title = v;
        }
        private int year;
        public int getyear()
        {
            return year;
        }
        public void setyear(int v)
        {
            year = v;
        }
        public void Displayed()
        {
            System.out.println("Book itemid = " + getitem_id() + 
                               ", price = " + getprice() +
                              ", quantity = " + getquantity() +
                              ", author = " + getauthor() +
                              ", title = " + gettitle() +
                              ", year = " + getyear());
        }
        public book(double p, int q, String a, String t, int y)
        {
            super(p, q);
            author = a;
            title = t;
            year = y;
        }
    }
    class gift extends item
    {
        private String label;
        public String getlabel()
        {
            return label;
        }
        public void setlabel(String v)
        {
            label = v;
        }   
        private String manufacturer;
        public String getmanufacturer()
        {
            return manufacturer;
        }
        public void setmanufacturer(String v)
        {
            manufacturer = v;
        }   
        public void Displayed()
        {
            System.out.println("Gift itemid = " + getitem_id() + 
                               ", price = " + getprice() +
                              ", quantity = " + getquantity() +
                              ", label = " + getlabel() +
                              ", manufacturer = " + getmanufacturer());
        }
        public gift(double p, int q, String l, String m)
        {
            super(p, q);
            label = l;
            manufacturer = m;
        }
    }
    

    运行结果
    -----all items:
    Book itemid = 1, price = 32.0, quantity = 100, author = tom, title = java programming, year = 2001
    Book itemid = 2, price = 45.0, quantity = 200, author = jack, title = data structure via c++, year = 2003
    Book itemid = 3, price = 47.0, quantity = 150, author = jim, title = linux system programming, year = 2007
    Gift itemid = 4, price = 101.0, quantity = 50, label = toy tank, manufacturer = company1
    Gift itemid = 5, price = 145.0, quantity = 100, label = toy plane, manufacturer = company2
    Gift itemid = 6, price = 9.7, quantity = 250, label = puzzle, manufacturer = company3
    -----books (sort by author):
    Book itemid = 2, price = 45.0, quantity = 200, author = jack, title = data structure via c++, year = 2003
    Book itemid = 3, price = 47.0, quantity = 150, author = jim, title = linux system programming, year = 2007
    Book itemid = 1, price = 32.0, quantity = 100, author = tom, title = java programming, year = 2001
    -----removed and purchased:
    Book itemid = 1, price = 32.0, quantity = 99, author = tom, title = java programming, year = 2001
    Book itemid = 2, price = 45.0, quantity = 200, author = jack, title = data structure via c++, year = 2003
    Gift itemid = 4, price = 101.0, quantity = 50, label = toy tank, manufacturer = company1
    Gift itemid = 5, price = 145.0, quantity = 100, label = toy plane, manufacturer = company2
    Gift itemid = 6, price = 9.7, quantity = 250, label = puzzle, manufacturer = company3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制