Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package list;
- // A menu-driven friend accumulation and listing program
- import java.util.*; // Java library for linked lists referenced here
- class Friend
- {
- String name, colour;
- //A method to allow a new friends name and favourite colour to be added
- public Friend(String friendsName, String favouriteColour)
- {
- name = friendsName;
- colour = favouriteColour;
- }
- // method to respond to a message sent
- // to a rectangle to set its length
- public void setName(String nameValue)
- {
- name = nameValue;
- }
- // method to respond to a message sent
- // to a rectangle to set its breadth
- public void setColour(String colourValue)
- {
- colour = colourValue;
- }
- // method to allow a Friend to respond
- // to a System.out.println
- public String toString()
- {
- return("\n" + name + "\t\t\t" + " " + colour);
- }
- // method to allow a Friend object to say
- // whether it's equal to another rectangle or
- // not
- public boolean equals(Friend rIn)
- {
- return name.equals(rIn.name) && colour.equals(rIn.colour);
- }
- public String getName()
- {
- return name;
- }
- public String getColour()
- {
- return colour;
- }
- }
- public class People
- {
- public static void main(String [] args)
- {
- Scanner scanner = new Scanner(System.in);
- //A LinkedList for storage of friends names and favourite colour
- List<Friend> friends = new LinkedList<Friend>();
- ListIterator<Friend> iterator;
- String friendName, faveColour, friendsName;
- int choice;
- do
- {
- System.out.println("\nPlease choose your menu choice");
- System.out.println("Add a friend and their favourite colour \t\t 1");
- System.out.println("See a list of your friends and their favourite colour \t 2");
- System.out.println("Remove a friend \t\t\t\t\t 3");
- System.out.println("Search for a named friend's favourite colour \t\t 4");
- System.out.println("Quit \t\t\t\t\t\t\t 0");
- choice = scanner.nextInt();
- if(choice == 1) //Adding a friend and their favourite colour
- {
- // ask the user for the length and the breadth of the new Rectangle
- System.out.println("\nPlease type in your friends first name and favourite colour");
- System.out.println("First name");
- friendName = scanner.next();
- System.out.println("Favourite colour");
- faveColour = scanner.next();
- iterator = friends.listIterator();
- if(iterator.hasNext() && iterator.next().getName().equals(friendName))
- {
- System.out.println(friendName + " is already a friends name, please choose another name for your friend.");
- }
- else
- {
- friends.add(new Friend(friendName, faveColour));
- }
- }
- else if (choice == 2) //Seeing a list of all of your friends and their favourite colour
- {
- int i = friends.size();
- if (i > 0)
- {
- //code for printing out the list of your friends and their favourite colours.
- System.out.println("Friends name:\t\t Favourite colour: \n" + friends);
- }
- else
- {
- System.out.println("You don't have any friends to list!");
- }
- }
- else if (choice == 3) //Removing a friend and their favourite colour
- {
- friendsName = null;
- int i = friends.size();
- if (i == 0)
- {
- System.out.println("You don't have any friends to remove!");
- }
- else
- {
- System.out.println("Who would you like to remove as a friend?");
- friendsName = scanner.next();
- iterator = friends.listIterator();
- if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
- {
- System.out.println(friendsName + " isn't in your list of friend(s).");
- }
- else
- {
- iterator.remove();
- System.out.println(friendsName + " has been removed as a friend!");
- }
- }
- }
- else if (choice == 4) //Searching for a friends favourite colour
- {
- int i = friends.size();
- if (i == 0)
- {
- System.out.println("You don't have any friends to search!");
- }
- else
- {
- friendsName = null;
- faveColour = null;
- System.out.println("Name the friend whos favourite colour you would like to know.");
- friendsName = scanner.next();
- iterator = friends.listIterator();
- if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
- {
- System.out.println(friendsName + " isn't a friend of yours.");
- }
- else
- {
- faveColour = iterator.next().getColour();
- System.out.println(friendsName + "'s favourite colour is " + faveColour);
- }
- }
- }
- }
- while(choice != 0);
- System.out.println("\nThank you for using this program. Come back soon!\n\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement