菜单我这里不方便写,我把调用写给你,你自己完善下:
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