8 Replies - 469 Views - Last Post: 04 March 2012 - 11:13 AM Rate Topic: -----

#1 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Question about, arraylist and calling methods

Posted 03 March 2012 - 12:39 PM

Hello everyone, im having a problem calling my methods to my man class, and making a menu to use my methods. I guess im just having a brain fart right now, pretty tired. Im supposed to make a database that stores an owners info, and than display it when i want the owners info. Im i doing it right so far, any errors? Much appreciated thanks.

import java.util.Scanner;
import java.util.ArrayList;

public class Owner {
	
	ArrayList<String> name = new ArrayList<String>();
	ArrayList<Integer> phoneNum = new ArrayList<Integer>();
	ArrayList<String> address = new ArrayList<String>();
	Scanner kb = new Scanner(System.in);

	public Owner(ArrayList<String> name, ArrayList<Integer> phoneNum,ArrayList<String> address) {
		this.name = name;
		this.phoneNum = phoneNum;
		this.address = address;
	}
	
	public ArrayList<String> getOwnerNam() {
		
		System.out.println("Please enter Owners name: ");
		name.add(kb.next());
		return name;
	}

	public ArrayList<Integer> getOwnerPhone() {
		System.out.println("Please enter Owners phone nubmer: ");
		phoneNum.add(kb.nextInt());
		return phoneNum;
	}

	public ArrayList<String> getOwnerAddress() {
		System.out.println("Please enter the address of Owner:");
		address.add(kb.next());
		return address;

	}
	public String toString(){
		return "Owner name: " + getOwnerNam() + "\nOwners phone number: " + getOwnerPhone() + "\nOwners address: " + getOwnerAddress();
		
	}

}



public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		int answer;
		
		System.out.println("Welcome to the Vet office");
		System.out.println("What would you like to do?");
		System.out.println("1. Add new Owner");
		
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Question about, arraylist and calling methods

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 01:03 PM

Hold it right there. 'Owner' should not contain List of anything (perhaps address but that's theoretical). Something might have a List<Owner>. Nor should Owner have a Scanner.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,851
  • Joined: 18-April 07

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 01:09 PM

Lots of problems here. First of all why are you passing an arraylist for something as simple as "name"? This should be a simple string right? The name of the owner. The same with the phoneNumber. This can also be a string as well as Address.

ArrayList is a list of items. Be it a list of things to get at the grocery store or a list of contact names etc. Owner on the other hand takes simple data types like ONE name, a single phone number and one address.

// Notice no arraylist here, just simple data types.
public Owner(String OwnerName, String PhoneNumber, String OwnerAddress) {


}



Now when you have defined an arraylist as a member of a class, it is visible to all functions inside of that class. So you don't need to return it from a function. Let's take the getOwnerNam() function you have defined. What should it return? A single name for the owner. Again a simple string. This means that "name" should also be defined as a String, not an arraylist.

// Defined as a string in the class...
private String name;

// Method to return the name
public String getOwnerName() {
   return name;
}



So fix the rest of your methods here. Keep in mind that an arraylist is again a list of items. An owner is a single person. Define it as such. Back in main we can then have a list of owners.


// Define an owner object in Main
Owner theOwner = new Owner("Tom","123-456-7890","123 Fake Street");

// Now we can ask things using his methods...
// What is your name owner?
System.out.println("My name is: " + theOwner.getOwnerName());



Now that we have shown one owner, we can create an list of owners. This is where the arraylist can come in...

ArrayList<Owner> listOfOwners = new ArrayList<Owner>();



We can then add to this list by using listOfOwners methods and do all sorts of things. Hope you understand. Define one owner class, then create a list of Owner objects using the ArrayList.

:)
Was This Post Helpful? 1
  • +
  • -

#4 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 01:22 PM

yea but the owner class is supposed to store all the owner's info, as in many different owners.
than i have to so all the different owners with their info

i dont see how any of that would work, i would need something to store the info in, so thats why i used the arraylist

the program should has the user if he/she wants to add a owner, then is asks whats owners name, address, phone number, then i should store all those, then when the user wants to see all the different owners it should display all of them
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 02:18 PM

View Postsarmen, on 03 March 2012 - 03:22 PM, said:

yea but the owner class is supposed to store all the owner's info, as in many different owners.
than i have to so all the different owners with their info

Then, make a Owner class that contains Name, Address, ...
and a OwnerBook that contains one single ArrayList<Owner>
Your driver will create a new OwnerBook
create new Owner and add them to the the OwnerBook object
Was This Post Helpful? 1
  • +
  • -

#6 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 06:53 PM

Ive already restarted 3 times, I i dont know what im doing wrong, the approch, logic i have no idea, but its getting really frustrating.

im supposed to do this, but as of know i want to get the owner class and the main method working. What im trying to do know is have the user input their name, address, and number, but i dont know how to store it in the arraylist.

Appointments are their own entity, so this should captured with a class, containing date and time, and client.

You should also have a class for owners with information like name, address, and phone number.

import java.util.ArrayList;
import java.util.Scanner;

public class Owner {

	private String name;
	private int number;
	private String address;
	
	public Owner(String name, int number, String address) {
		this.name = name;
		this.number = number;
		this.address = address;
	}
    
	ArrayList<Owner> OwnerBook = new ArrayList<Owner>();
	Owner o = new Owner(name, number, address);

	
}

Was This Post Helpful? 0
  • +
  • -

#7 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 07:17 PM

Im i on the right track, how would i call this in main to have the user use it?


import java.util.ArrayList;
import java.util.Scanner;

public class Owner {

	private String name;
	private int number;
	private String address;
	Scanner kb = new Scanner(System.in);

	public Owner(String name, int number, String address) {
		this.name = name;
		this.number = number;
		this.address = address;
	}

		ArrayList<Owner> OwnerBook = new ArrayList<Owner>();
		Owner o = new Owner(name, number, address);
		{
			for (int i = 0; i >= number; i++) {
				System.out.println("Enter owners number:");
				number = kb.nextInt(2);
			}
			System.out.println("[i+1]");
		}
	}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Question about, arraylist and calling methods

Posted 03 March 2012 - 07:37 PM

The Owner class should only model an Owner, so as to make it more modular and reusable. It should not have an Owner variable, List<Owner>, or user input. Those should be in your main() method, or ideally better handled in another class. Check out my tutorial Moving Away From Parallel Arrays. It covers class design in more detail.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Question about, arraylist and calling methods

Posted 04 March 2012 - 11:13 AM

You almost het it :^:
Just that the ArrayList<Owner> should not be in the Owner class or ALL Owner will have that ArrayList
And a class does not have a Scanner. A class has to be independant of the source(s) that feeds it: console, GUI, Database

Owner.java
public class Owner {

	private String name;
	private int number;
	private String address;

	public Owner(String name, int number, String address) {
		this.name = name;
		this.number = number;
		this.address = address;
	}
	
	public String toString() {
		return name + " " + number + " " + address;
	}
}



OwnerBook.java
class OwnerBook {
		ArrayList<Owner> al;
		
		OwnerBook() {
			al = new ArrayList<Owner>();
		}

		void addOwner(Owner o) {
		    al.add(o);
		}
		
		void addOwner(String name, int number, String address) {
		    al.add(new Owner(name, number, address));
		}
		
		void listOwners() {
			for(int i = 0; i < al.size(); ++i)
				System.out.println((i+1) + ") " + al.get(i));
		}

		// main method to test the OwnerBook and Owner class with a Scanner
		public static void main(String[] args) {

			Scanner kb = new Scanner(System.in);
            OwnerBook book = new OwnerBook();
            System.out.print("Enter name: ");
            String name = kb.nextLine();
            System.out.print("Enter number: ");
            int num = kb.nextInt();
            kb.nextLine();
            System.out.print("Enter address: ");
            String address = kb.nextLine();
            book.addOwner(name, num, address);
            book.listOwners();
            		
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1