user2989139
user2989139

Reputation: 71

Java methods and classes

I'm having a bit of trouble with one of my assignments given to me by my professor. Here is the prompt:

Modify the Customer class to include changeStreetO, changeCityO, changeStateO, and changeZipO methods. Modify the Account class to include a changeAddressO method that has street, city, state, and zip parameters. Modify the Bank application to test the changeAddressO method.

I have the changeStreetO, changeCityO, and changeStateO, and changeZipO. However, I'm confused on the section in bold with the ChangeAddressO method. I think I understand how to write the parameters, but I'm unsure of what is supposed to go within the actual method, and how it fits in with the rest of the program. Here is my current code.

import java.text.NumberFormat;
import java.io.*;
import java.util.*;
public class BankModification
{
    public class Account 
    { 
        private double balance; 
        private Customer cust;

        public Account(double bal, String fName, String lName,  String str, String city, String st, String zip)
        {
            balance = bal;
            cust = new Customer(fName, lName, str, city, st, zip);
            /**
             * Returns the current balance
             * pre: none
             * post: The account balance has been returned
             */
        }

        public double getBalance()
        {
            return (balance);
        }

        /**
         * A deposit is made to the account
         * pre: none
         * post: The balance has been increased by the amount of the deposit.
         */
         public void deposit(double amt)
         {
            balance += amt;

            /**
             * A withdrawal is made from the account if there is enough money
             * pre: none
             * post: The balance has been decreased by the amount withdrawn.
             */
        }

        public void withdrawal(double amt)
        {
            if (amt <= balance) 
            balance -= amt;
            else 
            System. out. println ("Not enough money in account.");
        }


        /**
         * Returns a String that represents the Account object. 
         * pre: none
         * post: A string representing the Account object has been returned.
         */
        public String toString()
        {
            String accountString;
            NumberFormat money = NumberFormat.getCurrencyInstance();
            accountString = cust.toString();
            accountString += "Current balance is " + money.format (balance);
            return (accountString) ;
        }

        public changeAddressO(String street, String city, String state, zip_)
        {
        }

        public class Customer 
        {
            private String firstName, lastName, street, city,state, zip_;

            /**
             * constructor
             * pre: none
             * post: A Customer object has been created.
             * Customer data has been initialized with parameters
             */

            public Customer(String fName, String lName, String str,
        String c, String s, String z) 
            {
                firstName = fName;
                lastName = lName;
                street = str;
                city = c; 
                state = s; 
                zip_ = z;
            }

            /** 
             * Returns a String that represents the Customer Object 
             * pre: none
             * post: A string representing the Account object has been returned.
             */

            public String toString() 
            { 
                String custString;
                custString=firstName + " " + lastName + "\n";
                custString +=street + "\n";
                custString += city + ", "+state+ "  " + zip_ + "\n";
                return(custString);
            }

            public void ChangeStreetO(String newStreet)
            {
                street = newStreet;
            }

            public void ChangeCityO(String newCity)
            {
                city = newCity;
            }

            public void ChangeStateO(String newState)
            {
                state = newState;
            }

            public void ChangeZipO(String newZip)
            {
                zip_ = newZip;
            }
        }
    }
}

I'm not sure if I'm missing something essential, but without the changeAddressO method, the program compiles. I'm simply unable to figure out what I need to put in the changeAddressO since I made methods with the chageStreetO, changeCityO, changeStateO, and changeZipO. I'm still having a bit of difficulty with classes and methods. Could someone provide some insight/guidance to help figure out this problem? Thank you for your time.

Upvotes: 0

Views: 1103

Answers (3)

Jonas
Jonas

Reputation: 11

    public void changeAddressO(String street, String city, String state, String zip_)
    {
        this.street = street;
        this.city = city;
        this. state = state;
        this.zip_ = zip_;

    }

And you are probably going to want to put it inside of your Customer class since you made the street, city, state, and zip private.

Upvotes: 1

ltalhouarne
ltalhouarne

Reputation: 4636

You just need to change the address of the customer:

    public void changeAddressO(String street, String city, String state, String zip)
            {
            cust.changeAddress(street);
            cust.changeCity(city);
            cust.changeState(state); 
            cust.changeZip(zip);
            }

Upvotes: 1

aliteralmind
aliteralmind

Reputation: 20163

First of all, change this

public changeAddressO(String street, String city, String state, zip_)
{
}

to

public void changeAddressO(String street, String city, String state, String zip_)
{
}

All functions need a return type, and all parameters need a type before the name. This function's return type is void meaning it returns nothing to the calling function. Change it to String or int or something else as necessary.

At the very least, now your code will compile.

In addition, all of the nested classes are very confusing. Your professor didn't give it to you this way I hope.

Start by changing this:

public class BankModification
{
    public class Account
    {

      //Lots of stuff

        public class Customer
        {

            //Lots of stuff
        }
    }
}

to this (eliminating all publics except the first):

public class BankModification
{
}
class Account
{
   //Lots of stuff
}
class Customer
{
   //Lots of stuff
}

This doesn't change anything functionally, but at least it's less confusing (notice there's nothing in BankModification which isn't very helpful.)

"Modify the Bank application to test the changeAddressO method."

What bank application? I would expect to see a

public static final void main(String[] as_cmdLineParams)

somewhere in there. This is the function that actually executes when you type java BankModification on the command line (since BankModification is the public class, only its main will be executed).

Some random observations to at least get you thinking.

Upvotes: 0

Related Questions