我正在做我的期中作业,为一个简单的java程序创建一个序列图。然而,我遇到了一个问题,我无法决定将哪个元素放在图中,以及忽略哪个元素。如果可以帮助回答这个问题,我将发布一个JAVA代码。
public class TestPOS {
public static void main(String[] args) {
// POST ê°ì²´ë¥¼ ì¤ë¹
Store store = new Store();
ProductCatalog catalog = new ProductCatalog();
catalog.addSpec(1, new ProductSpec(1, "pencil", 1000));
catalog.addSpec(2, new ProductSpec(2, "eraser", 500));
catalog.addSpec(3, new ProductSpec(3, "fountain pen", 50000));
POST post = new POST(store, catalog);
// 첫 ë²ì§¸ í매
post.enterItem(1, 12);
post.enterItem(2, 4);
post.enterItem(3, 1);
post.makePayment();
post.endSale();
// ë ë²ì§¸ í매
post.enterItem(1, 2);
post.enterItem(2, 1);
post.makePayment();
post.endSale();
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ì½ë
for (Sale sale : store.completedSales) {
System.out.println(sale.getDate());
sale.printLineItems();
System.out.println("total = " + sale.getTotal());
}
}
}
这是它调用enterItem()、makePayment()和endSale()的主要位置。任务是为上述三个功能创建序列图。我会把每节课发到下面。
-----------------------------------------------------------
import java.util.Date;
public class POST {
private Store store;
private ProductCatalog catalog;
private Sale sale = null;
public POST(Store store, ProductCatalog catalog) {
this.store = store;
this.catalog = catalog;
}
public void enterItem(int upc, int qty) {
if (sale == null) {
Date date = new Date(System.currentTimeMillis());
sale = new Sale(date);
}
ProductSpec s = catalog.spec(upc);
sale.makeLineItem(s, qty);
}
public void makePayment() {
if (sale != null) sale.makePayment();
}
public void endSale() {
store.addCompleteSale(sale);
sale = null;
}
}
-----------------------------------------------------------
import java.util.ArrayList;
public class Store {
protected ArrayList<Sale> completedSales = null;
public Store() {
completedSales = new ArrayList<Sale>();
}
public void addCompleteSale(Sale sale) {
completedSales.add(sale);
}
}
-----------------------------------------------------------
import java.util.ArrayList;
import java.util.Date;
public class Sale {
private Date date;
private ArrayList<SalesLineItem> lineItem = null;
private Payment payment = null;
public Sale(Date date)
{
this.date = date;
lineItem = new ArrayList<SalesLineItem>();
}
public void makeLineItem(ProductSpec s, int qty) {
SalesLineItem item = new SalesLineItem(s, qty);
lineItem.add(item);
}
public int getTotal() {
int total = 0;
for (SalesLineItem item : lineItem) {
total += item.getSubTotal();
}
return total;
}
public void makePayment() {
int total = this.getTotal();
payment = new Payment(total);
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public Date getDate() {
return date;
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public void printLineItems() {
for (SalesLineItem item : lineItem) {
System.out.println("upc : " + item.getItemUpc() +", name : " + item.getItemName() + ", price : "
+ item.getItemPrice() + ", quantity : " + item.getQuantity());
}
}
}
-----------------------------------------------------------
public class Payment {
private int amount;
public Payment(int amount) {
this.amount = amount;
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public int getAmount() {
return amount;
}
}
-----------------------------------------------------------
public class SalesLineItem {
private int quantity;
private ProductSpec spec;
public SalesLineItem(ProductSpec spec, int quantity) {
this.spec = spec;
this.quantity = quantity;
}
public int getSubTotal() {
int price = spec.getPrice();
return price * quantity;
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public int getItemUpc() {
return spec.getUpc();
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public String getItemName() {
return spec.getName();
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public int getItemPrice() {
return spec.getPrice();
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public int getQuantity() {
return quantity;
}
}
-----------------------------------------------------------
import java.util.HashMap;
public class ProductCatalog {
private HashMap<Integer, ProductSpec> specTable = new HashMap<Integer, ProductSpec>();
public void addSpec(int upc, ProductSpec spec) {
specTable.put(upc, spec);
}
public ProductSpec spec(int upc) {
return specTable.get(upc);
}
}
-----------------------------------------------------------
public class ProductSpec {
private int upc;
private String name;
private int price;
public ProductSpec(int upc, String name, int price) {
this.upc = upc;
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public int getUpc() {
return upc;
}
// ì¶ë ¥ì ë³´ì¬ì£¼ì´ ì´í´ë¥¼ ë기ìí ë©ìë, í´ëì¤ ë¤ì´ì´ê·¸ë¨ì ë°ìíì§ ìì
public String getName() {
return name;
}
}
不要介意代码中的韩语,它们只是为了一些描述。下面的图片是我画的图表,无论它是否正确。
主要的问题是,我不知道是否应该绘制在终止过程中使用的每个元素、交互和实例。我知道我听起来有点杂乱无章,但请有人帮我解决我的问题。