Utilice Java para escribir una clase de producto, 1. Atributos: número de producto, nombre del producto, precio unitario del producto, inventario de productos, 2. Método de construcción.
clase?ChanPinClass{
//Número de producto
private?String?ChanPinBianHao;
//Nombre del producto
private?String?ChanPinName;
//Precio unitario del producto
private?String?ChanPinDanJia;
//Inventario de productos
private?long?ChanPinKuCun;
/**
?*?Constructor privado para uso singleton
?*/
privado?ChanPinClass(){
}
privado?estático?ChanPinClass?cc?=?null;
/**
?*?Constructor extensible
?*?@param?obj
?*?@throws?Exception?
?*/
público?estático?ChanPinClass?getChanPinClass(Objeto...?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("¡El parámetro no coincide!");
}
return?cc;
}
/**
?*?Información del producto de salida
?*/
public?void?ChanPinMessagePrint(){
StringBuffer?sb?=?new?StringBuffer();
sb.append(this.getChanPinBianHao()?== ? null?||?"".equals(this.getChanPinBianHao())"Número de producto: Ninguno":"Número de producto:" ?this.getChanPinBianHao()).append("\n");
sb.append(this.getChanPinName()?==?null?||?"".equals(this.getChanPinName())"Nombre del producto: Ninguno":"Nombre del producto:"? ?this.getChanPinName( ) ).append("\n");
sb.append(this.getChanPinDanJia()?==?null?||?"".equals(this.getChanPinDanJia())" Precio unitario del producto :None":"Precio unitario del producto:"? ?new?java.text.DecimalFormat("#.00").format(Double.parseDouble(this.getChanPinDanJia()))).append("\n");
sb.append(this.getChanPinKuCun()?lt;=?0"Inventario de productos: Ninguno":"Inventario de productos:"??this.getChanPinKuCun()).append("\n")
System.out.println(sb.toString());
}
public?String?getChanPinBianHao()?{
return?ChanPinBianHao;
}
public?void?setChanPinBianHao(String?chanPinBianHao)?{
ChanPinBianHao?=?chanPinBianHao;
}
¿público?String?getChanPinName()?{
return?ChanPinName;
}
¿público?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, "Baicai", 120.02);
//Agregar inventario
ss.setChanPinKuCun(12);
//Mensaje de salida
ss.ChanPinMessagePrint();
}
}