2017-07-14 43 views
0

我已经阅读了许多不同的堆栈溢出问题,这些问题有类似的问题,但不是我特别努力的。我创建了总共4个类,一个Bill类,一个Money类和一个Date类以及一个驱动程序来测试我的输出。这些课程应该用于管理一系列未付和付费账单。当我尝试在我的驱动程序类上运行main时,我收到“不兼容的类型,钱不能转换为int”,并指向我的getDollars方法。我对Java相当陌生,所以我的解决方案可能很简单,但我不明白我做错了什么。我应该返回一个对象吗?java中带有子类的getter方法的不兼容类型

public class Money { 
    //private instance variables used for tracking dollars and cents 
    //private to avoid privacy leaks 
    private int dollars; 
    private int cents; 

    //constructor initializing dollars 
    public Money(int dol){ 
     setDollars(dol); 
    } 

    //constructor initializing dollars and cents 
    public Money(int dol, int cent){ 
     setDollars(dol); 
     setCents(cent); 
    } 

    //constructor 
    public Money(Money other){ 
     setDollars(other.dollars); 
     setCents(other.cents); 

    } 

    //setter for dollars 
    public void setDollars(int dol){ 
     invalidDollars(dol); 
     dollars = dol; 
    } 

    //getter for dollars 
    public int getDollars(){ 
     return new Money(dollars); 
    } 

    //setter for cents 
    public void setCents(int cent){ 
     invalidCents(cent); 
     cents = cent; 
    } 

    //getter for cents 
    public int getCents(){ 
     return new Money(cents); 
    } 

    //getter for total monetary amount, returned as a double 
    public double getMoney(){ 
     dollars = (double)dollars; 
     cents = (double)cents; 
     return dollars + cents; 
    } 

    //setter for dollars and cents 
    public void setMoney(int dol, int cent){ 
     setDollars(dol); 
     setCents(cent); 
    } 

    //method to add the passed in int to dollars 
    public void add(int dol){ 
     dollars += dol; 
    } 

    //method that adds the two ints passed in to dollars and cents 
    public void add(int dol, int cent){ 
     //checks if addition caused cents to go over 99 limit 
     if(cents + cent > 99){ 
      cent = cent - 100; 
      dol += dol + 1; 
     } 
     dollars += dol; 
     cents += cent; 
    } 


    //method that adds to this object the dollars and cents stored in 
    //the other object 
    public void add(Money other){ 
     add(other.dollars, other.cents); 
    } 

    //determines if this money object is equal to that money object 
    @Override 
    public boolean equals(Object o) { 
     if(o == null || ! (o instanceof Money)) { 
      return false;   
     } else { 
     Money that = (Money)o;  
     return this.dollars == that.dollars && this.cents == that.cents; 
    } 
    } 

    //prints out a Money object in the form of "$0.00" 
    public String toString(){ 
     return "$"+ dollars +"."+ String.format("%02d", cents); 
    } 

    //checks if value for dollar is greater than zero, 
    //if not the system will print out an error message 
    //and crash 
    public void invalidDollars(int val){ 
     if(val < 0){   
      System.err.println("Invalid cent value: " + val); 
      System.exit(-1); 
    } 
    } 

    //checks if value for cents is greater than zero and less 
    //than 99, if not the system will print out an error 
    //message and crash 
    public void invalidCents(int val){ 
     if(val < 0 || val > 99){   
      System.err.println("Invalid cent value: " + val); 
      System.exit(-1); 
    } 
    } 
} 

这是在我的驱动程序中运行它时出现错误的类。其他类

public class Bill { 
    //private data member initialization 
    private Money amount; 
    private Date dueDate; 
    private Date paidDate = null; 
    private String originator; 

    //constructor 
    public Bill(Money amount, Date dueDate, String originator){ 
     this.amount = new Money(amount); 
     this.dueDate = new Date(dueDate); 
     this.originator = new String(originator); 
    } 

    //copy constructor 
    public Bill(Bill toCopy){ 
     this.amount = new Money(toCopy.amount); 
     this.dueDate = new Date(toCopy.dueDate); 
     this.originator = new String(toCopy.originator); 
    } 

    //method to get dueDate 
    public Date getDueDate(){ 
     return new Date(dueDate); 
    } 

    //method to get originator 
    public String getOriginator(){ 
     return new Bill(originator); 
    } 

    //checking if the bill has been paid 
    public boolean isPaid(Date tempPaidDate){ 
     if(tempPaidDate == null){ 
      return false; 
     } else { 
      return true; 
     } 
    } 

    //method to check if the date the bill was paid was before the dueDate, 
    //if so, it sets the onDay to the paidDate 
    public void setPaid(Date onDay){ 
     if(onDay.precedes(dueDate)){ 
     paidDate = new Date(onDay); 
     } else { 
      setUnpaid(); 
     } 
    } 

    //method to set paidDate to null, meaning unpaid 
    public void setUnpaid(){ 
     paidDate = new Date(null); 
    } 

    //method to set due date. if there is a paidDate (it does not equal null) 
    //then it checks if the new dueDate is before the paidDate using the precedes 
    //method from the date class. If the paidDate precedes the dueDate, then the 
    //dueDate can be changed to the argument nextDate 
    public void setDueDate(Date nextDate){ 
     if(paidDate != null){ 
      if(paidDate.precedes(nextDate)){   
     dueDate = new Date(nextDate); 
     } 
     } 
    } 

    //setter method for money amount 
    public void setAmount(Money tempAmount){ 
     amount = new Money(tempAmount); 
    } 

    //getter method from Money class for the bill amount 
    public Money getAmount(){ 
     return new Bill(amount); 
    } 

    //method to set the originator 
    public void setOriginator(String tempOriginator){ 
     originator = new String(tempOriginator); 
    } 

    //toString method to print out the bill information including the amount, the dueDate, who the money should go to, 
    //if it is paid, and if so, the date it was paid. If it has not been paid, the paidDate will return null 
    public String toString(){ 
     return "Amount: " + amount + " Due: " + dueDate + " To: " + originator + " Paid: " + isPaid(paidDate) + " Date: " + paidDate; 
     // build a string that reports the amount, 
     //when its due, to whom, whether paid, and if paid, the date paid. 
    } 

    //determine if two bills are equal by checking and comparing the amount, dueDate and originator 
    @Override 
    public boolean equals(Object toCompare) { 
     if(toCompare == null || ! (toCompare instanceof Bill)) { 
      return false;   
     } else { 
     Bill that = (Bill)toCompare;   
     return this.amount.equals(that.amount) && this.dueDate.equals(that.dueDate) && this.originator.equals(that.originator); 
    } 
    } 
} 

Date类

public class Date { 
    //private instance variables used for tracking month, day and year. 
    //private to avoid privacy leaks 
    private int month; 
    private int day; 
    private int year; 

    //constructor 
    public Date(){ 
    } 

    //constructor 
    public Date(int month, int day, int year){ 
     setDate(month, day, year); 
    } 

    //copy constructor 
    public Date(Date aDate){ 
     //crashes if date is null 
     if(aDate == null){ 
      System.out.println("Bad Date."); 
      System.exit(0); 
     } 
     setMonth(aDate.month); 
     setDay(aDate.day); 
     setYear(aDate.year); 
    } 

    //setter for date taking in for argument temporary ints for each variable 
    public void setDate(int tempMonth, int tempDay, int tempYear){ 
     setMonth(tempMonth); 
     setDay(tempDay); 
     setYear(tempYear); 
    } 

    //getter method for day 
    public int getDay() { 
     return day; 
    } 

    //setter method for day, first checks if the day 
    //is within the bounds of 1 and 31. If the day is 
    //invalid, the system will crash after printing out 
    //an error message using the method invalidDate 
    public void setDay(int tempDay) { 
     if(tempDay >= 1 && tempDay <= 31) { 
      day = tempDay; 
     } else { 
      invalidDate(tempDay); 
     } 
    } 

    //getter for month 
    public int getMonth() { 
     return month; 
    } 

    //setter for month. first checks if the temporary month 
    //taken in as argument is within the bounds of 1 and 12. 
    //if not the system will crash after printing out an error 
    //message using the invalidDate method. 
    public void setMonth(int tempMonth) { 
     if(tempMonth >= 1 && tempMonth <= 12) { 
      month = tempMonth; 
     } else { 
      invalidDate(tempMonth); 
     } 
    } 

    //getter for year 
    public int getYear() { 
     return year; 
    } 

    //setter for year. first checks if the temporary year taken 
    //in as argument is within the bounds of 2014 and 2024. if not 
    //the system will crash after printing out an error message 
    //using the invalidDate method. 
    public void setYear(int tempYear) { 
     if(tempYear >= 2014 && tempYear <= 2024) { //maybe change this? 
      year = tempYear; 
     } else { 
      invalidDate(tempYear); 
     } 
    } 

    //method to printout an error message of the bad 
    //date component and crash the system. 
    public void invalidDate(int val) { 
     System.err.println("Bad date component: " + val); 
     System.exit(-1); 
    } 

    //string method that returns the date in the format 
    // mm\\dd\\yyyy 
    @Override 
    public String toString() { 
     return month + "\\" + day + "\\" + year; 
    } 

    //equals method checking if each component of the two dates 
    //being compared are equal. returns true or false 
    @Override 
    public boolean equals(Object other) { 
     if(other == null || ! (other instanceof Date)) { 
      return false;   
     } else { 
     Date that = (Date)other;   
     return this.year == that.year && this.month == that.month && this.day == that.day; 
    } 
    } 

    //method to check if one date is before another date. 
    //returns true or false after checking each date component 
    public boolean precedes(Date otherDate){ 
     return ((year < otherDate.year)|| 
       (year == otherDate.year && month < 
       otherDate.month) || 
       (year == otherDate.year && month == otherDate.month 
       && day < otherDate.day)); 
    } 
} 

驱动

public class BillMoneyDateDriver 
{ 

    /** 
    main driver function 
    pre: none 
    post: exercises the methods in Bill, Money, and Date (not done) 
    */ 
    public static void main(String[] args) 
    { 
     //Construct some money 
     Money money1 = new Money(10); 
     Money money2 = new Money(money1); 
     money1.setMoney(30,50); 
     //TODO: do more functional exercises with the money class 


     System.out.println("Money objects output:"); 
     System.out.println(money1); 
     System.out.println(money2); 


     //Construct some bills 
     Money amount = new Money(20); 
     Date dueDate = new Date(4, 30, 2007); 
     Bill bill1 = new Bill(amount, dueDate, "The phone company"); 

     Bill bill2 = new Bill(bill1); 
     bill2.setDueDate(new Date(5, 30, 2007)); 
     amount.setMoney(31, 99); 
     dueDate.setDay(29); 
     Bill bill3 = new Bill(amount, dueDate, "The record company"); 

     System.out.println("Bill objects output:"); 
     System.out.println(bill1); 
     System.out.println(bill2); 
     System.out.println(bill3); 

    } 
} 
+0

我明确指出,在报价错误标志着往上顶,并指出这是在哪里。 –

+0

Java提供['LocalDate'](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)类,因此不需要重新创建。 –

回答

0

此代码试图返回一个int,但是您提供的Money

public int getDollars(){ 
    return new Money(dollars); 
} 
一个新实例

也许你想(???)

public int getDollars(){ 
    return this.dollars; 
} 

如果不是,其中dollars来自哪里?

0

可怕的Wombat纠正了几个方法。如果你想获得像xx.xx这样的钱,你还需要更改getMoney()。

//getter for total monetary amount, returned as a double 
public double getMoney() { 
    return this.dollars + (this.cents/100); 
} 

你的getter方法应该直接返回实例变量。您正在尝试创建新对象,然后尝试返回它们。 对于如:

//getter for dollars 
public int getDollars(){ 
    return new Money(dollars); 
} 

应该

//getter for dollars 
public int getDollars(){ 
    return this.dollars; 
}