这就是我要做的
Making·a·new·object·of·class·Invoice:↵Enter
·the·part·number:32-RJK-45↵Enter
·the·part·description:Phillips-Screw↵Enter
·the·Quantity·of·Items:20↵Enter·
the·Price·Per·Item·with·cents·in·decimals:↵4.50↵↵
Invoice·price·and·amount·together:↵
我尝试以多种方式使用编译器,使用字符串和双精度和整数作为数量。
这是我的发票页面
package week2chapter3;
public class Invoice {
private String partNumber; // instance variable
private String partDescription; // instance variable
private double priceperitem; // double va1rible
private int itemBeingPurchesed;// used to total
private double invoiceAmount;// double that adds invoice amount
// Part Number
public Invoice (String partNumber,String partDescription,double priceperitem,
int itemBeingPurchesed,double invoiceAmount)
{this.partNumber = partNumber;// assign it to instance variable name
// method to get the part number
}
{this.partDescription = partDescription;// assign it to instance variable name
// method to get the part number
}
// Part Price
// validate that the priceperitem is greater than 0.0; if it is not,
// instance variable priceperitem keeps its default initial value of 0.0
public void setpriceperitem(double priceperitem) {
if (priceperitem > 0.0) {// if the balance is valid
this.priceperitem = priceperitem; // assign it to instance variable balance
}
}
// Item purchased
// validate that the itemBeingPurchesed is greater than 0; if it is not,
// instance variable itemBeingPurchesed keeps its default initial value of 0
public void setitemBeingPurchesed(int itemBeingPurchesed) {
if (itemBeingPurchesed > 0) {// if the balance is valid
this.itemBeingPurchesed = itemBeingPurchesed;// assign it to instance variable balance
}
}
// getInvoiceAmount multiply the total if nothing or negative it sets to 0
public void invoiceAmount(double invoiceAmount) {
if (invoiceAmount > 0) {// if the balance is valid
invoiceAmount = itemBeingPurchesed * priceperitem; // assign it to instance variable balance
}
}
// method that returns the partDescription
public String getpartNumber() {
return partNumber;
}
// method that returns the partDescription
public String getpartDescription() {
return partDescription;
}
// method returns the account priceperitem
public double getpriceperitem() {
return priceperitem;
}
public int getitemBeingPurchesed() {
return itemBeingPurchesed;
}
// method returns the account priceperitem
public double getinvoiceAmount() {
return invoiceAmount;
}
}
这是我的测试页面
package week2chapter3;
import java.util.Scanner;
public class getInvoiceAmount {
public static void main(String[] args) {
Invoice invoice1 = new Invoice();
Invoice invoice2 = new Invoice();
Invoice invoice3 = new Invoice();
Invoice invoice4 = new Invoice();
// display initial balance of each object
System.out.printf("%s Part number is: %n", invoice1.getpartNumber());
System.out.printf("%s Part description: %n", invoice2.getpartDescription());
System.out.printf("%s Part price: $%.2f%n", invoice3.getpriceperitem());
System.out.printf("%s Part number is: $%.2f%n", invoice4.getitemBeingPurchesed());
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
System.out.print("Enter the Part number:"); // prompt
String partNumber = input.nextLine(); // obtain user input
invoice1.partNumber(partNumber);
System.out.print("Enter the part description:"); // prompt
String partDescription = input.nextLine(); // obtain user input
System.out.print("Enter the quantity of items :"); // prompt
int itemBeingPurchesed = input.nextInt(); // obtain user input
System.out.print("Enter the pric per item·with cents in decimalse:"); // prompt
double priceperitem = input.nextDouble(); // obtain user input
System.out.printf("%s Toatal for items: $%.2f%n", invoice1.getinvoiceAmount(), invoice1.getinvoiceAmount());
}
}
我收到几个错误。 当我解决一个问题时,我得到未定义的构造函数,当我修复时,我的源页面中出现错误,当我修复这些错误时,我得到格式错误。 现在,部件号在测试页面上读取为错误,部分描述读取为未定义。
这修复了代码中的"错误",但您的代码无法理解,我无法为您提供更多帮助。
public class getInvoiceAmount { public static void main(String[] args) {
Invoice invoice1 = new Invoice();
Invoice invoice2 = new Invoice();
Invoice invoice3 = new Invoice();
Invoice invoice4 = new Invoice();
// display initial balance of each object
System.out.printf("Part number is: %sn", invoice1.getpartNumber());
System.out.printf("Part description: %sn", invoice2.getpartDescription());
System.out.printf("Part price: %.2fn", invoice3.getpriceperitem());
System.out.printf("Part number is: %dn", invoice4.getitemBeingPurchesed());
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
System.out.print("Enter the Part number:"); // prompt
String partNumber = input.nextLine(); // obtain user input
invoice1.setPartNumber( partNumber);
System.out.print("Enter the part description:"); // prompt
String partDescription = input.nextLine(); // obtain user input
System.out.print("Enter the quantity of items :"); // prompt
int itemBeingPurchesed = input.nextInt(); // obtain user input
System.out.print("Enter the pric per item·with cents in decimalse:"); // prompt
double priceperitem = input.nextDouble(); // obtain user input
System.out.printf("Toatal for items: %.2fn", invoice1.getinvoiceAmount());
}
}
class Invoice {
private String partNumber; // instance variable
private String partDescription; // instance variable
private double priceperitem; // double va1rible
private int itemBeingPurchesed;// used to total
private double invoiceAmount;// double that adds invoice amount
public Invoice(){
}
// Part Number
public Invoice (String partNumber,String partDescription,double priceperitem,
int itemBeingPurchesed,double invoiceAmount)
{this.partNumber = partNumber;// assign it to instance variable name
// method to get the part number
}
{this.partDescription = partDescription;// assign it to instance variable name
// method to get the part number
}
// Part Price
// validate that the priceperitem is greater than 0.0; if it is not,
// instance variable priceperitem keeps its default initial value of 0.0
public void setpriceperitem(double priceperitem) {
if (priceperitem > 0.0) {// if the balance is valid
this.priceperitem = priceperitem; // assign it to instance variable balance
}
}
// Item purchased
// validate that the itemBeingPurchesed is greater than 0; if it is not,
// instance variable itemBeingPurchesed keeps its default initial value of 0
public void setitemBeingPurchesed(int itemBeingPurchesed) {
if (itemBeingPurchesed > 0) {// if the balance is valid
this.itemBeingPurchesed = itemBeingPurchesed;// assign it to instance variable balance
}
}
// getInvoiceAmount multiply the total if nothing or negative it sets to 0
public void invoiceAmount(double invoiceAmount) {
if (invoiceAmount > 0) {// if the balance is valid
invoiceAmount = itemBeingPurchesed * priceperitem; // assign it to instance variable balance
}
}
public void setPartNumber(String part_number){
partNumber = part_number;
}
// method that returns the partDescription
public String getpartNumber() {
return partNumber;
}
// method that returns the partDescription
public String getpartDescription() {
return partDescription;
}
// method returns the account priceperitem
public double getpriceperitem() {
return priceperitem;
}
public int getitemBeingPurchesed() {
return itemBeingPurchesed;
}
// method returns the account priceperitem
public double getinvoiceAmount() {
return invoiceAmount;
}
}