當前位置:偏方大全网 - 藥品查詢 - 用java編寫壹個產品類, 1. 屬性:產品編號,產品名稱,產品單價,產品庫存,2.構造方

用java編寫壹個產品類, 1. 屬性:產品編號,產品名稱,產品單價,產品庫存,2.構造方

class?ChanPinClass{

//產品編號

private?String?ChanPinBianHao;

//產品名稱

private?String?ChanPinName;

//產品單價

private?String?ChanPinDanJia;

//產品庫存

private?long?ChanPinKuCun;

/**

?*?私有化構造函數,供單例使用

?*/

private?ChanPinClass(){

}

private?static?ChanPinClass?cc?=?null;

/**

?*?可擴展的構造方法

?*?@param?obj

?*?@throws?Exception?

?*/

public?static?ChanPinClass?getChanPinClass(Object...?obj)?throws?Exception{

if(cc?==?null){

cc?=?new?ChanPinClass();

}

int?obj_length?=?obj.length;

if(obj_length?==?0){

}else?if(obj_length?==?1){

cc.setChanPinBianHao(String.valueOf(obj[0]));

}else?if(obj_length?==?2){

cc.setChanPinBianHao(String.valueOf(obj[0]));

cc.setChanPinName(String.valueOf(obj[1]));

}else?if(obj_length?==?3){

cc.setChanPinBianHao(String.valueOf(obj[0]));

cc.setChanPinName(String.valueOf(obj[1]));

cc.setChanPinDanJia(String.valueOf(obj[2]));

}else?if(obj_length?==?4){

cc.setChanPinBianHao(String.valueOf(obj[0]));

cc.setChanPinName(String.valueOf(obj[1]));

cc.setChanPinDanJia(String.valueOf(obj[2]));

cc.setChanPinKuCun(Long.valueOf(String.valueOf(obj[3])));

}else{

throw?new?Exception("參數不匹配!");

}

return?cc;

}

/**

?*?輸出產品信息

?*/

public?void?ChanPinMessagePrint(){

StringBuffer?sb?=?new?StringBuffer();

sb.append(this.getChanPinBianHao()?==?null?||?"".equals(this.getChanPinBianHao())"產品編號:無":"產品編號:"?+?this.getChanPinBianHao()).append("\n");

sb.append(this.getChanPinName()?==?null?||?"".equals(this.getChanPinName())"產品名稱:無":"產品名稱:"?+?this.getChanPinName()).append("\n");

sb.append(this.getChanPinDanJia()?==?null?||?"".equals(this.getChanPinDanJia())"產品單價:無":"產品單價:"?+?new?java.text.DecimalFormat("#.00").format(Double.parseDouble(this.getChanPinDanJia()))).append("\n");

sb.append(this.getChanPinKuCun()?<=?0"產品庫存:無":"產品庫存:"?+?this.getChanPinKuCun()).append("\n");

System.out.println(sb.toString());

}

public?String?getChanPinBianHao()?{

return?ChanPinBianHao;

}

public?void?setChanPinBianHao(String?chanPinBianHao)?{

ChanPinBianHao?=?chanPinBianHao;

}

public?String?getChanPinName()?{

return?ChanPinName;

}

public?void?setChanPinName(String?chanPinName)?{

ChanPinName?=?chanPinName;

}

public?String?getChanPinDanJia()?{

return?ChanPinDanJia;

}

public?void?setChanPinDanJia(String?chanPinDanJia)?{

ChanPinDanJia?=?chanPinDanJia;

}

public?long?getChanPinKuCun()?{

return?ChanPinKuCun;

}

public?void?setChanPinKuCun(long?hchanPinKuCunanPinKuCun)?{

ChanPinKuCun?=?hchanPinKuCunanPinKuCun;

}

}

public?class?ChanPinClassMain?{

public?static?void?main(String[]?args)?throws?Exception?{

ChanPinClass?ss?=?ChanPinClass.getChanPinClass(001,"娃娃菜",120.02);

//添加庫存

ss.setChanPinKuCun(12);

//輸出信息

ss.ChanPinMessagePrint();

}

}

  • 上一篇:疾病預防控制中心所屬單位
  • 下一篇:湖北省實施《中華人民共和國廣告法》辦法(2021年修正)
  • copyright 2024偏方大全网