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

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.

0

1个回答

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

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

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!