2017-07-14 2 views
0

Ich habe eine Reihe von verschiedenen Stack-Überlauf-Fragen gelesen, die ein ähnliches Problem haben, aber nicht, was ich gerade mit kämpfen. Ich habe insgesamt 4 Klassen erstellt, eine Bill-Klasse, eine Money-Klasse und eine Date-Klasse sowie einen Treiber, um meine Ausgabe zu testen. Diese Klassen sollen für die Verwaltung einer Reihe von ausstehenden und bezahlten Rechnungen implementiert werden. Wenn ich versuche, mein Hauptprogramm auf meiner Treiberklasse auszuführen, erhalte ich "Inkompatible Typen, Geld kann nicht in int konvertiert werden" und es zeigt auf meine getDollars-Methode. Ich bin ziemlich neu in Java, so dass meine Lösung einfach sein kann, aber ich sehe nicht, was ich falsch mache. Sollte ich ein Objekt zurückgeben?Inkompatible Typen in Getter-Methode in Java mit Unterklassen

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); 
    } 
    } 
} 

Dies ist die Klasse, die einen Fehler hat, wenn ich es in meinem Fahrer laufen. Die anderen Klassen sind

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); 
    } 
    } 
} 

Datum Klasse

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)); 
    } 
} 

Treiber

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

Ich zeigte deutlich die Fehler in Zitat aus oben markiert und zeigte auf, wo es war. –

+0

Java bietet eine ['LocalDate'] (https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) -Klasse, also muss man nicht neu erfinden. –

Antwort

0

Dieser Code versucht, ein int zurück, aber Sie eine neue Instanz von Money

public int getDollars(){ 
    return new Money(dollars); 
} 
Bereitstellung

vielleicht möchten Sie (???)

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

Wenn nicht, wo dollars kommen?

0

Scary Wombat korrigiert einige Methoden.Sie müssen auch die getMoney() ändern, wenn Sie das Geld wie xx.xx.

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

Die Getter-Methode sollte die Instanzvariable direkt zurückgeben. Sie haben versucht, das neue Objekt zu erstellen und dann zu versuchen, sie zurückzugeben. Für zB:

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

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