返回错误值的Java方法

返回错误值的Java方法,java,Java,我正在努力学习用Java正确地编写方法。我看过很多例子,看不出我做错了什么。我有一个名为InvoiceApp的主类,它使用一个名为Validator的类来筛选输入,然后是一个名为Invoice的类来处理用户输入。当InvoiceApp运行时,用户输入'r'或'c'作为客户类型,并输入双精度值作为“小计”。应用程序创建Invoice类的对象以使用getInvoice()方法,该方法返回格式化的发票。 将返回格式化的发票,但字符串的值为空,所有数字的值为零(用户在第一位输入的数字除外)。Invoic

我正在努力学习用Java正确地编写方法。我看过很多例子,看不出我做错了什么。我有一个名为InvoiceApp的主类,它使用一个名为Validator的类来筛选输入,然后是一个名为Invoice的类来处理用户输入。当InvoiceApp运行时,用户输入'r'或'c'作为客户类型,并输入双精度值作为“小计”。应用程序创建Invoice类的对象以使用getInvoice()方法,该方法返回格式化的发票。 将返回格式化的发票,但字符串的值为空,所有数字的值为零(用户在第一位输入的数字除外)。Invoice类显然没有为变量赋值。我用了很多很多不同的方法,但都不能让它正常工作

有人能看出我做错了什么吗?这是我的密码:

InvoiceApp类:

import java.util.Scanner;

public class InvoiceApp
{    
public static void main(String[] args)
{
    // display a welcome message
    System.out.println("Welcome to the Invoice Total Calculator");
    System.out.println();  // print a blank line

    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while(choice.equalsIgnoreCase("y"))
    {
        // get user entries
        String customerType = Validator.getString(sc,
            "Enter customer type (r/c):   ");
        double subtotal = Validator.getDouble(sc,
            "Enter subtotal:              ", 0, 10000);

        Invoice i;
        i = new Invoice(customerType, subtotal);
        System.out.println();
        System.out.println(i.getInvoice());

        System.out.println();
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }
} 
}
发票类别:

import java.text.NumberFormat;

public class Invoice {
private final String customerType;
private final double subtotal;
private double discountAmount;
private double discountPercent;
private double invoiceTotal;
private String sCustomerType;

public Invoice(String customerType, double subtotal){
    this.customerType = customerType;
    this.subtotal = subtotal;
}
public void setDiscountPercent(double discountPercent){
    if (customerType.equalsIgnoreCase("r"))
    {
        if (subtotal >= 500)
            discountPercent = .2;
        else if (subtotal >= 250 && subtotal < 500)
            discountPercent =.15;
        else if (subtotal >= 100 && subtotal < 250)
            discountPercent =.1;
        else if (subtotal < 100)
            discountPercent =.0;
    }
    else if (customerType.equalsIgnoreCase("c"))
    {
            discountPercent = .2;
    }
    else
    {
        discountPercent = .05;
    }
}
public double getDiscountPercent(){
    return discountPercent;
}
public void setDiscountAmount(double discountAmount){
    discountAmount = subtotal * (getDiscountPercent());
}
public double getDiscountAmount(){
    return discountAmount;
}
public void setInvoiceTotal(double invoiceTotal){
    invoiceTotal = subtotal - (getDiscountAmount());
}
public double getInvoiceTotal(){
    return invoiceTotal;
}
public void setCustomerType(String sCustomerType){
    sCustomerType = "Unknown";
        if (customerType.equalsIgnoreCase("r"))
            sCustomerType = "Retail";
        else if (customerType.equalsIgnoreCase("c"))
            sCustomerType = "College";
}
public String getCustomerType(){
    return sCustomerType;
}
public String getInvoice(){
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();
    sCustomerType = this.getCustomerType();
    discountPercent = this.getDiscountPercent();
    discountAmount = this.getDiscountAmount();
    invoiceTotal = this.getInvoiceTotal();
    // Create a string representation of the full invoice and return
    String invoice = ("Subtotal:         " + currency.format(subtotal) + "\n"
                   + "Customer type:    " + sCustomerType + "\n"
                   + "Discount percent: " + percent.format(discountPercent)+ "\n"
                   + "Discount amount:  " + currency.format(discountAmount)+ "\n"
                   + "Total:            " + currency.format(invoiceTotal) + "\n");
    return invoice;
}
}
    import java.text.NumberFormat;

public class Invoice {
private final String customerType;
private double subtotal;
private double discountAmount;
private double discountPercent;
private double invoiceTotal;
private String sCustomerType;

public Invoice(String customerType, double subtotal){
    this.customerType = customerType;
    this.subtotal = subtotal;
}
public void setSubtotal(double subtotal){
    this.subtotal = subtotal;
}
public double getSubtotal(){
    return subtotal;
}
public void setDiscountPercent(double discountPercent){
    this.discountPercent = discountPercent;
}
public double getDiscountPercent(){
    if (customerType.equalsIgnoreCase("r"))
    {
        if (subtotal >= 500)
            discountPercent = .2;
        else if (subtotal >= 250 && subtotal < 500)
            discountPercent =.15;
        else if (subtotal >= 100 && subtotal < 250)
            discountPercent =.1;
        else if (subtotal < 100)
            discountPercent =.0;
    }
    else if (customerType.equalsIgnoreCase("c"))
    {
            discountPercent = .2;
    }
    else
    {
        discountPercent = .05;
    }
    return discountPercent;
}
public void setDiscountAmount(double discountAmount){
    this.discountAmount = discountAmount;
}
public double getDiscountAmount(){
    discountAmount = subtotal * (getDiscountPercent());
    return discountAmount;
}
public void setInvoiceTotal(double invoiceTotal){
    this.invoiceTotal = invoiceTotal;
}
public double getInvoiceTotal(){
    invoiceTotal = subtotal - (getDiscountAmount());
    return invoiceTotal;
}
public void setCustomerType(String sCustomerType){
    this.sCustomerType = sCustomerType;
}
public String getCustomerType(){
    sCustomerType = "Unknown";
        if (customerType.equalsIgnoreCase("r"))
            sCustomerType = "Retail";
        else if (customerType.equalsIgnoreCase("c"))
            sCustomerType = "College";
    return sCustomerType;
}
public String getInvoice(){
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();
    // Create a string representation of the full invoice and return
    String invoice = ("Subtotal:         " + currency.format(getSubtotal()) + "\n"
                   + "Customer type:    " + getCustomerType() + "\n"
                   + "Discount percent: " + percent.format(getDiscountPercent())+ "\n"
                   + "Discount amount:  " + currency.format(getDiscountAmount())+ "\n"
                   + "Total:            " + currency.format(getInvoiceTotal()) + "\n");
    return invoice;
}
}
导入java.text.NumberFormat;
公共类发票{
私有最终字符串customerType;
私人最终双倍小计;
私人双折扣;
私人双折;
私人双重发票总额;
私有字符串sCustomerType;
公共发票(字符串customerType,双倍小计){
this.customerType=customerType;
这个。小计=小计;
}
公共无效设置折扣百分比(双倍折扣百分比){
if(customerType.equalsIgnoreCase(“r”))
{
如果(小计>=500)
折扣百分比=.2;
否则如果(小计>=250&&小计<500)
折扣百分比=.15;
否则如果(小计>=100&&小计<250)
折扣百分比=.1;
否则,如果(小计<100)
折扣百分比=.0;
}
else if(customerType.equalsIgnoreCase(“c”))
{
折扣百分比=.2;
}
其他的
{
折扣百分比=.05;
}
}
公共双getDiscountPercent(){
退货折扣百分比;
}
公共无效设置折扣挂载(双折扣挂载){
折扣金额=小计*(getDiscountPercent());
}
公共双getDiscountAmount(){
退货折扣额;
}
公共void setInvoiceTotal(双重invoiceTotal){
invoiceTotal=小计-(getDiscountAmount());
}
公共双精度getInvoiceTotal(){
退货发票总额;
}
public void setCustomerType(字符串sCustomerType){
sCustomerType=“未知”;
if(customerType.equalsIgnoreCase(“r”))
sCustomerType=“零售”;
else if(customerType.equalsIgnoreCase(“c”))
sCustomerType=“学院”;
}
公共字符串getCustomerType(){
返回sCustomerType;
}
公共字符串getInvoice(){
NumberFormat currency=NumberFormat.getCurrencyInstance();
NumberFormat percent=NumberFormat.getPercentInstance();
sCustomerType=this.getCustomerType();
折扣百分比=this.getDiscountPercent();
discountAmount=this.getDiscountAmount();
invoiceTotal=this.getInvoiceTotal();
//创建完整发票和退货的字符串表示形式
字符串发票=(“小计:+货币.格式(小计)+”\n
+客户类型:“+sCustomerType+”\n
+折扣百分比:“+百分比.格式(折扣百分比)+”\n
+折扣金额:“+货币.格式(折扣金额)+”\n
+“总计:“+货币.格式(发票总计)+”\n”);
退货发票;
}
}
验证程序类:

import java.util.Scanner;

public class Validator
{
public static String getString(Scanner sc, String prompt)
{
    System.out.print(prompt);
    String s = sc.next();  // read user entry
    sc.nextLine();  // discard any other data entered on the line
    return s;
}

public static int getInt(Scanner sc, String prompt)
{
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.print(prompt);
        if (sc.hasNextInt())
        {
            i = sc.nextInt();
            isValid = true;
        }
        else
        {
            System.out.println("Error! Invalid integer value. Try again.");
        }
        sc.nextLine();  // discard any other data entered on the line
    }
    return i;
}

public static int getInt(Scanner sc, String prompt,
int min, int max)
{
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        i = getInt(sc, prompt);
        if (i <= min)
            System.out.println(
                "Error! Number must be greater than " + min + ".");
        else if (i >= max)
            System.out.println(
                "Error! Number must be less than " + max + ".");
        else
            isValid = true;
    }
    return i;
}

public static double getDouble(Scanner sc, String prompt)
{
    double d = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.print(prompt);
        if (sc.hasNextDouble())
        {
            d = sc.nextDouble();
            isValid = true;
        }
        else
        {
            System.out.println("Error! Invalid decimal value. Try again.");
        }
        sc.nextLine();  // discard any other data entered on the line
    }
    return d;
}

public static double getDouble(Scanner sc, String prompt,
double min, double max)
{
    double d = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        d = getDouble(sc, prompt);
        if (d <= min)
            System.out.println(
                "Error! Number must be greater than " + min + ".");
        else if (d >= max)
            System.out.println(
                "Error! Number must be less than " + max + ".");
        else
            isValid = true;
    }
    return d;
}
}
import java.util.Scanner;
公共类验证器
{
公共静态字符串getString(扫描程序sc、字符串提示)
{
系统输出打印(提示);
字符串s=sc.next();//读取用户条目
sc.nextLine();//放弃在该行中输入的任何其他数据
返回s;
}
公共静态int getInt(扫描程序sc,字符串提示)
{
int i=0;
布尔值isValid=false;
while(isValid==false)
{
系统输出打印(提示);
if(sc.hasnetint())
{
i=sc.nextInt();
isValid=true;
}
其他的
{
System.out.println(“错误!无效整数值。请重试”);
}
sc.nextLine();//放弃在该行中输入的任何其他数据
}
返回i;
}
公共静态int getInt(扫描程序sc、字符串提示、,
整数最小值,整数最大值)
{
int i=0;
布尔值isValid=false;
while(isValid==false)
{
i=getInt(sc,提示符);
如果(i=最大值)
System.out.println(
错误!数字必须小于“+max+”;
其他的
isValid=true;
}
返回i;
}
公共静态双getDouble(扫描程序sc,字符串提示)
{
双d=0;
布尔值isValid=false;
while(isValid==false)
{
系统输出打印(提示);
if(sc.hasNextDouble())
{
d=sc.nextDouble();
isValid=true;
}
其他的
{
System.out.println(“错误!十进制值无效。请重试”);
}
sc.nextLine();//放弃在该行中输入的任何其他数据
}
返回d;
}
公共静态双getDouble(扫描程序sc、字符串提示、,
双最小值,双最大值)
{
双d=0;
布尔值isValid=false;
while(isValid==false)
{
d=getDouble(sc,提示符);
如果(d=最大值)
System.out.println(
错误!数字必须小于“+max+”;
其他的
isValid=true;
}
返回d;
}
}
谢谢任何能帮忙的人


谢谢大家。我从setter方法中删除了参数并添加了这个。给施工人员。显然,我对两个主要方面感到困惑:

  • set方法中应该包含哪些参数?我试图插入set方法实际使用的变量,但也没有成功

  • 我认为get方法只是用来返回set方法生成的值。因为二传手都是空的,所以他们
        import java.text.NumberFormat;
    
    public class Invoice {
    private final String customerType;
    private double subtotal;
    private double discountAmount;
    private double discountPercent;
    private double invoiceTotal;
    private String sCustomerType;
    
    public Invoice(String customerType, double subtotal){
        this.customerType = customerType;
        this.subtotal = subtotal;
    }
    public void setSubtotal(double subtotal){
        this.subtotal = subtotal;
    }
    public double getSubtotal(){
        return subtotal;
    }
    public void setDiscountPercent(double discountPercent){
        this.discountPercent = discountPercent;
    }
    public double getDiscountPercent(){
        if (customerType.equalsIgnoreCase("r"))
        {
            if (subtotal >= 500)
                discountPercent = .2;
            else if (subtotal >= 250 && subtotal < 500)
                discountPercent =.15;
            else if (subtotal >= 100 && subtotal < 250)
                discountPercent =.1;
            else if (subtotal < 100)
                discountPercent =.0;
        }
        else if (customerType.equalsIgnoreCase("c"))
        {
                discountPercent = .2;
        }
        else
        {
            discountPercent = .05;
        }
        return discountPercent;
    }
    public void setDiscountAmount(double discountAmount){
        this.discountAmount = discountAmount;
    }
    public double getDiscountAmount(){
        discountAmount = subtotal * (getDiscountPercent());
        return discountAmount;
    }
    public void setInvoiceTotal(double invoiceTotal){
        this.invoiceTotal = invoiceTotal;
    }
    public double getInvoiceTotal(){
        invoiceTotal = subtotal - (getDiscountAmount());
        return invoiceTotal;
    }
    public void setCustomerType(String sCustomerType){
        this.sCustomerType = sCustomerType;
    }
    public String getCustomerType(){
        sCustomerType = "Unknown";
            if (customerType.equalsIgnoreCase("r"))
                sCustomerType = "Retail";
            else if (customerType.equalsIgnoreCase("c"))
                sCustomerType = "College";
        return sCustomerType;
    }
    public String getInvoice(){
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();
        // Create a string representation of the full invoice and return
        String invoice = ("Subtotal:         " + currency.format(getSubtotal()) + "\n"
                       + "Customer type:    " + getCustomerType() + "\n"
                       + "Discount percent: " + percent.format(getDiscountPercent())+ "\n"
                       + "Discount amount:  " + currency.format(getDiscountAmount())+ "\n"
                       + "Total:            " + currency.format(getInvoiceTotal()) + "\n");
        return invoice;
    }
    }
    
    public void setDiscountPercent(double discountPercent){
        ...
            if (subtotal >= 500)
                discountPercent = .2;
        ...
    
    public void setDiscountPercent(double discountPercent){
        ...
            if (subtotal >= 500)
                this.discountPercent = .2;
        ...
    
    public void setDiscountAmount(double discountAmount){
        discountAmount = subtotal * (getDiscountPercent());
    }
    
    public void setDiscountAmount(double discountAmount){
        this.discountAmount = subtotal * (getDiscountPercent());
    }