I'm currently working on this project:
Create a simple Friends class with, as a minimum, the following:
-name and age fields
-appropriate constructors
-get/set methods
-toString() method
Create an ArrayList of Friends.
Write a program to manage your list of friends.
Run the program from a menu with the following options:
-Add a Friend
-Remove a Friend
-Display all Friends
-Exit
and I've come a ways, but I'm not sure I'm heading in the right direction.
Here's what I've got so far:
import java.util.ArrayList;
import java.util.Scanner;
public class Friends
{
public static void main( String[] args )
{
int menu;
menu = 0;
int choice;
choice = 0;
Scanner input = new Scanner(System.in);
ArrayList< Friends > name = new ArrayList< >();
ArrayList< Friends > age = new ArrayList< >();
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
while(menu != 4)
{
switch(menu)
{
case 1:
while(choice != 2)
{
System.out.println("Enter Friend's Name: ");
name.add = input.next();
System.out.println("Enter Friend's Age: ");
age.add(input.nextInt());
System.out.println("Enter another? 1: Yes, 2: No");
choice = input.nextInt();
} break;
case 2:
System.out.println("Enter Friend's Name to Remove: ");
name.remove(input.next()); break;
case 3:
for(int i = 0; i < name.size(); i++)
{
System.out.println(name.get(i));
}
for(int k = 0; k < age.size(); k++)
{
System.out.println(age.get(k));
}break;
}
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
}
System.out.println("Thank you and goodbye!");
}
public String name;
public int age;
public Friends( String friendsName, int friendsAge )
{
this.name = friendsName;
this.age = friendsAge;
}
public String toString()
{
return super.toString();
}
public void setName( String friendsName )
{
name = friendsName;
}
public void setAge( int friendsAge )
{
age = friendsAge;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
I have a few questions:
- How do I utilize the Friends class to store user input? (What is wrong with line 34 and 36?)
When I display the friends it shows this:
John
Jen
Jeff
22
24
26
I'd like to have the name and age next to each other rather than all down a line. Any help would be greatly appreciated!
This is where I'm at now, but I messed something up and now it won't allow me to put an argument in "FriendsTest f = new FriendsTest();", but when I don't my friends list just says "null 0"
package friends;
import java.util.ArrayList;
import java.util.Scanner;
public class FriendsTest
{
public static void main( String[] args )
{
int menu;
int choice;
choice = 0;
Scanner input = new Scanner(System.in);
ArrayList< FriendsTest > friends = new ArrayList< >();
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
while(menu != 4)
{
switch(menu)
{
case 1:
while(choice != 2)
{
System.out.println("Enter Friend's Name: ");
String name = input.next();
System.out.println("Enter Friend's Age: ");
int age = input.nextInt();
FriendsTest f = new FriendsTest(name, age);
friends.add(f);
System.out.println("Enter another? 1: Yes, 2: No");
choice = input.nextInt();
} break;
case 2:
System.out.println("Enter Friend's Name to Remove: ");
friends.remove(input.next()); break;
case 3:
for(int i = 0; i < friends.size(); i++)
{
System.out.println(friends.get(i).name + " " + friends.get(i).age);
}
break;
}
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
}
System.out.println("Thank you and goodbye!");
}
public String name;
public int age;
}