一道Java编程题 求源代码
希望各位帮帮忙,题目:创建称为Invoice的类,硬件商店要用它来代表店内所卖产品的一张发票。Invoice应该包括4部分信息,作为实例变量:编号部分(String类型),说明部分(String类型),所购买产品的数量(int类型)和每个产品的价格(double类型)。这个类应该有一个构造函数,初始化这4个实例变量。为每个实例变量提供一个set方法和一个get方法,另外,提供一个名为getInvoiceAmount的方法,计算发票总计数额(产品数量乘以每个产品的价格),然后将这个数额作为double值返回。如果数量不是正数,它应该被设置为0.如果每个产品的价格不是正数,他应该被设置为0.0.编写一个名为InvoiceTest的应用程序,演示类Invoice的功能。
追问:InvoiceTest.java:9: 不兼容的类型
找到: Invoice
需要: InvoiceTest
InvoiceTest invoiceTest = new Invoice("编号","一个商品的 ^
InvoiceTest.java:13: 找不到符号
符号: 方法 getInvoiceAmount()
位置: 类 InvoiceTest
double priceTotal = invoiceTest.getInvoiceAmount();
^
2 错误
答案://主类
public class Invoice {
//编号部分
String id;
//说明部分
String description;
//数量
int numbers;
//价格
double price;
//初始化4个参数
public Invoice(String id, String description, int numbers, double price) {
this.id = id;
this.description = description;
this.numbers = numbers;
this.price = price;
}
//下面是get和set方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getNumbers() {
return numbers;
}
public void setNumbers(int numbers) {
//如果数目是负数 则=0
if(numbers<0) this.numbers = 0;
else this.numbers = numbers;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
//如果是负数 则=0.0
if(price <= 0) this.price = 0.0;
else this.price = price;
}
//计算价格的函数
public double getInvoiceAmount(){
return this.numbers*price;
}
}
测试类
public class InvoiceTest {
//程序入口
public static void main(String[] args){
//初始化
InvoiceTest invoiceTest = new Invoice("编号","一个商品的描速",10,20.3);
//计算价格
double priceTotal = invoiceTest.getInvoiceAmount();
//打印价格
System.out.println("价格为 " + priceTotal);
}
}
/***Invoice 类**/
public class Invoice {
?private String sno;//编号
?private String mark;//说明
?private int number;//数量
?private double price;//单价;
?/********提供set, get 方法***************/
?public String getSno() {
?return sno;
?}
?public void setSno(String sno) {
?this.sno = sno;
?}
?public String getMark() {
?return mark;
?}
?public void setMark(String mark) {
?this.mark = mark;
?}
?public int getNumber() {
?return number;
?}
?public void setNumber(int number) {
?if(number<0){//小于0
?this.number = 0;
?}else{
?this.number = number;
?}
?}
?public double getPrice() {
?return price;
?}
?public void setPrice(double price) {
?if(price<0){//小于0.0
?this.price = 0.0;
?}else{
?this.price = price;
?}
?}
?
?public double getInvoiceAmount(){//返回金额总数
??
?return this.getNumber()*this.getPrice();
?}
?
?private Invoice(){//设为私有, 不允许构造没有参数的构造方法,?
?}//只能通过下面的构造方法new
?/**
? * 构造时, 初始化这4个实例变量
? */
?public Invoice(String sno, String mark, int number, double price) {
??
?this.sno = sno;
?this.mark = mark;
?if(number<0){//小于0
?this.number = 0;
?}else{
?this.number = number;
?}
?if(price<0){//小于0.0
?this.price = 0.0;
?}else{
?this.price = price;
?}
?}
}
/************ InvoiceTest 类************/
import java.text.DecimalFormat;
public class InvoiceTest {
?public static void main(String[] args) throws Exception{
??
?Invoice ie = new Invoice("B001","毛笔",7,2.3);//构造对象初始化
??
?System.out.println("产品编号: "+ie.getSno());
?System.out.println("产品说明: "+ie.getMark());
?System.out.println("产品数量: "+ie.getNumber());
?System.out.println("产品单价: "+ie.getPrice());
?System.out.println("发票总计数额: "+ie.getInvoiceAmount());//可以注释
??
?DecimalFormat dft=(DecimalFormat) DecimalFormat.getInstance();?
?dft.applyPattern("0.00");//设置小数点位数 (两位) 四舍五入
?System.out.println("发票总计数额: "+dft.format(ie.getInvoiceAmount()));
?}
}
/********** 以下是输出结果 *********/
产品编号: B001
产品说明: 毛笔
产品数量: 7
产品单价: 2.3
发票总计数额: 16.099999999999998
发票总计数额: 16.10
上一个:高分求java socket 上传图片并保存
下一个:java的简介及其语言特点