I really need help here. This is for a class assignment, yet I'm not getting any help from anyone, anywhere, including my instructor. This is an online class, and he is never available. Since this is for a class, I need to keep things the way they are. Meaning that I need the outside class, and all of the setter/getter methods. This is for a basic address book. I need the Storage class to calculate numdonationAmount*numDonationsYTD. This calculation cannot be within found anywhere in AddressBook.java. It must be found in Storage.java. I have damn near most of it. Also, I can't get the Decimal formatter to work properly. I can't get it to work properly using the printf command, either. I need the output to be the name, address, city, state, zip, how many times this person donated, and the total amount of all donations (donate amount * how many times this person donated). Here is my code. I am on the verge of giving up. I just can't seem to understand java, at least in the way that I am being taught. The first part is AddressBook.java, and the second is a class I created, Storage.java

/*Program: Address Book
Week 04 Assignment 01 IT/215
Jordan Sinclair
Last Modified on 20091031 */
import java.util.*;                                                                                      //program uses java utilities package                                                                                        //program uses java I/O package
//import java.text.*;                                                                                      //programs uses java text package
//import java.io.*;
public class AddressBook {                                                                               //class AddressBook declaration
        public static void main(String args[]) {                                                         //begin main()
            Storage entry = new Storage ();
            Scanner input = new Scanner( System.in );                                                    //program uses scanner class to evalute strings of text
            //DecimalFormat money = new DecimalFormat("$00.00");                                            //program uses decimalFormat class to evaluate currency
            System.out.println( "Welcome to Jordan's Address Book Program!" );                           //introductory line
            final String stopP = "Stop";                                                                 //string stopP declaration is created and not reconfigurable
            do{                                                                                           //begin do clause
                /* 20091018 update changed all input variables to 'id<variableName> and took out last name prompt */
                System.out.println( "Please Enter A Name.  Otherwise, enter Stop to exit: " );           //program asks for user to enter a first name
                entry.idName = input.nextLine();                                                               //scanner variable idName is created
                if (entry.idName.equals(stopP)) {
                    System.out.println("Thank you for using Jordan's Address Book.  Program will now exit.");
                    }else{
                System.out.println();                                                                    //blank line for readability
               
           
           
                System.out.println( "Please Enter Street Address for "+entry.idName+":" );                     //program asks user to enter the street address
                entry.idAddress = input.nextLine();                                                     //user input 'idAddress' variable created
                System.out.println();                                                                    //blank line for readability
            
                System.out.println( "Please Enter City:");                                               //program asks user to enter a city 
                entry.idCity = input.nextLine();                                                        //user input 'idCity' variable created
                System.out.println();                                                                    //blank line for readability                    
            
                System.out.println("Please Enter Two Letter State Abbreviation (i.e., TX):");            //program asks for user to enter a state in a two letter format
                entry.idState = input.nextLine();                                                       //user input 'idState' variable created
                System.out.println();                                                                    //blank line for readability
            
                System.out.println("Please Enter a 5-digit Zip Code:");                                  //program asks user to enter a zip code
                entry.idZip = input.nextLine();                                                         //user input 'idZip' variable created
                System.out.println();                                                                    //blank line for readability                    
                
                String array[]={entry.idName,entry.idAddress,entry.idCity,entry.idState,entry.idZip};
                
                // 20091017 update: changed "donationNumber" to "donationYTD". numDYTD stems from "Number Donations Year To Date".  this is the integer number format for the variable "donationYTD
                
                //int numDYTD;                                                                             //declares variable 'numDYTD' as an integer
                System.out.println("Please Enter Number of Donations This Year:");                       //program asks user to enter number of donation frequency year to date
                String donationsYTD = input.nextLine();                                                  //user input 'donations year to date' as a string
                entry.numDonationsYTD = Double.parseDouble(donationsYTD);                                                //'donations year to date' string is recognized as int numDYTD
                while (entry.numDonationsYTD <= 0) {                                                                   //begin if clause  
                    System.out.println("ERROR!  FLAGRANT WINDOWS ERROR!!  Dunno what you did, mate, but you sure screwed things up over here! You're lucky I don't throw the Blue Screen of DEATH at you!!  Now enter a positive number!!!(Greater Than 0)");               //print error message1
                    System.out.println("Please Enter Number of Donations This Year (Positive Numbers Only):");//program re-asks user to enter number of donation frequency year to date
                    donationsYTD = input.nextLine();                                                     //user input 'donations year to date' as a string
                    entry.numDonationsYTD = Double.parseDouble(donationsYTD);                                            //'donations year to date' string is recognized as int numDYTD
                    continue;                                                                            //restart while() and re-evaluate while condition        
                   }                                                                                    //end while()
                //double numDonateAmount;                                                                  //declare variable numDonateAmount as a double-precision floating-point number
                System.out.println("Please Enter Amount of Each Donation This Year (format X.XX)");          //program asks for donation amount numeric total
                String donationAmount = input.nextLine();                                                //user input total donation amount in numerical value
                entry.numDonateAmount = Double.parseDouble(donationAmount);                                    //'donationAmount' is recognized as a double numDonateAmount
                while (entry.numDonateAmount <=0) {                                                            //begin while clause
                    System.out.println("ERROR!  FLAGRANT WINDOWS ERROR!!  Dunno what you did, mate, but you sure screwed things up over here! You're lucky I don't throw the Blue Screen of DEATH at you!!  Now enter a positive number!!!(Greater Than 0)");               //print error message2
                    System.out.println("Please Enter Amount of Each Donation This Year (Positive Numbers Only, Format X.XX):");//program re-asks for donation amount numeric total
                    donationAmount = input.nextLine();                                                   //user input total donation amount numeric total
                    entry.numDonateAmount = Double.parseDouble(donationAmount);                                //'donationAmount' is recognized as a double numDonateAmount
                    continue;                                                                            //restart while() and re-evaluate while condition
                    }
                    
                /* both print statements below reflect the returned output based upon previous input */
                 System.out.printf( entry.idName+" lives at "+entry.idAddress+" "+entry.idCity+", "+entry.idState+" "+entry.idZip+" and donated "+entry.numDonationsYTD+" time(s) this year.");
                 System.out.println();
                //double grandTotal=entry.numDonateTotal;                                       //create a new variable to equal the product of the donation amount and frequency
                //double numDonateTotal = entry.numDonateTotal;
                System.out.printf("The total of all donations amount to "+entry.numDonateTotal+".");//placed this new total in USD currency format to display a grand total
                System.out.println();
                }}while (! (entry.idName.equals(stopP)));
    } //end main()

} //end class AddressBook

. Here is Storage.java. I created this class.

/* Storage Class
Jordan Sinclair
IT/215
Address Book Part Three
20091025*/
                                                     //programs uses java text package
public class Storage {                               //class definition
    public String idName;                            //program creates variable idName
    public String idAddress;                         //program creates variable idAddress
    public String idCity;                            //program creates variable idCity
    public String idState;                           //program creates variable idState
    public String idZip;                             //program creates variable idZip
    public String donationsYTD;                      //program creates double precision floating variable donateYTD
    public String donationAmount;                    //program creates double precision floating variable donateAmount
    public double numDonateAmount;
    public double numDonationsYTD;
    public double numDonateTotal;
    
    public Storage () {
    }
    public Storage(String name, String address, String city, String state, String zip, double donationsYTD, double donateAmount, double donateTotal) {
        this.idName=name;
        this.idAddress=address;
        this.idCity=city;
        this.idState=state;
        this.idZip=zip;
        this.numDonationsYTD=donationsYTD;
        this.numDonateAmount=donateAmount;
        //numDonateTotal=numDonationsYTD*numDonateAmount;
        this.numDonateTotal=donateTotal;
        

    }
   
    public void setNumDonationsYTD (double donationsYTD) {
        numDonationsYTD=donationsYTD;
    }
    public double getNumDYTD () {
        return numDonationsYTD;
    }
    public void setNumDonateAmount (double donateAmount) {
        numDonateAmount=donateAmount;
    }
    public double getNumDonateAmount () {
        return numDonateAmount;
    }
    //public void setNumDonateTotal (double donateTotal){
    	//numDonateTotal=donateTotal;
    //}
    public double NumDonateTotal(){
    	numDonateTotal=numDonateAmount*numDonationsYTD;
    	return numDonateTotal;
    }
    public void setIdName (String name) {            //set idName
        idName=name;
    }
    public String getIdName () {                     //get idName
        return idName;
    }
    public void setIdAddress (String address) {      //set idAddress
        idAddress=address;
    }
    public String getIdAddress () {                  //get idAddress
        return idAddress;
    }
    public void setIdCity (String city) {            //set idCity
        idCity=city;
    }
    public String getIdCity () {                     //get idCity
        return idCity;
    }
    public void setIdState (String state) {          //set idState
        idState=state;
    }
    public String getIdState () {                    //get idState
        return idState;
    }
    public void setIdZip (String zip) {              //set idZip
        idZip=zip;
    }
    public String getIdZip () {                      //get idZip
        return idZip;
    }
   
}

The formatted text needs to be in the output in a USD format. Output needs to be like this:
idName lives at idAddress, idCity, idState, idZip, and has donated numDonateAmount times. The total of all donations amount to numDonateTotal.

Now, I need to grab numDonateAmount from Storage class, and numDonateTotal needs to be in dollar format. Please tell me what I'm doing wrong.

Recommended Answers

All 2 Replies

<deleted this post>

System.out.println("ERROR! FLAGRANT WINDOWS ERROR!! Dunno what you did, mate, but you sure screwed things up over here! You're lucky I don't throw the Blue Screen of DEATH at you!! Now enter a positive number!!!(Greater Than 0)"); //print error message1

Haha. I have to say you have a great sense of humor. And I can promise you that this site has many capable java programmers who are more than willing to help you with your project, provided you put in the effort yourself, which it is clear that you are doing. Don't give up. Anyway:

I noticed that you are using nextLine() to read in a double. Use nextDouble() instead. Remember that when the user enters a number such as "5.67" or whatever, when they hit enter, that puts two things in the input stream: 5.67 and '\n' (the newline character, from hitting enter). So after you call double whatever = nextDouble(), on the next line, just call nextLine() which will get rid of the '\n'.

Anyway, in order to print things out the way you want to, override the toString() method in your Storage class. For example, drop the following method into your Storage class:

public String toString(){
String s = donationsYTD + " " + donationAmount;
return s;
}

Then from within AddressBook class, call System.out.println(entry.toString()).

Other than that, I don't know what you mean by the "decimal formatter not working properly". Are you trying to make sure that your value is printed out with only two decimal places? If so,

[CODE=Java]
double d = 1.234567;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(d));

As for this: "I need the Storage class to calculate numdonationAmount*numDonationsYTD." Your Storage class already has a method that does that. What's the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.