一道Java编程题 求源代码

希望各位帮帮忙,题目:创建称为Invoice的类,硬件商店要用它来代表店内所卖产品的一张发票。Invoice应该包括4部分信息,作为实例变量:编号部分(String类型),说明部分(String类型),所购买产品的数量(int类型)和每个产品的价格(double类型)。这个类应该有一个构造函数,初始化这4个实例变量。为每个实例变量提供一个set方法和一个get方法,另外,提供一个名为getInvoiceAmount的方法,计算发票总计数额(产品数量乘以每个产品的价格),然后将这个数额作为double值返回。如果数量不是正数,它应该被设置为0.如果每个产品的价格不是正数,他应该被设置为0.0.编写一个名为InvoiceTest的应用程序,演示类Invoice的功能。

第1个回答  2010-04-01
public class Invoice {

String bianhao = null;

String shuoming = null;

int count = 0;

double price = 0.0;

public Invoice(String bianhao, String shuoming, int count, double price) {
this.bianhao = bianhao;
this.shuoming = shuoming;

if (count < 0) {
this.count = 0;
} else {
this.count = count;
}

if (price < 0.0) {
this.price = 0.0;
} else {
this.price = price;
}

}

public double getInvoiceAmount() {
return count * price;
}

public String getBianhao() {
return bianhao;
}

public void setBianhao(String bianhao) {
this.bianhao = bianhao;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getShuoming() {
return shuoming;
}

public void setShuoming(String shuoming) {
this.shuoming = shuoming;
}

}

public class InvoiceTest {

/**
* @param args
*/
public static void main(String[] args) {
Invoice invoice = new Invoice("010220", "Desk", 50, 53.9);

System.out.println(invoice.getInvoiceAmount());
}

}本回答被提问者采纳
相似回答