Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package list;
  2.  
  3. // A menu-driven friend accumulation and listing program
  4.  
  5. import java.util.*;  // Java library for linked lists referenced here
  6.  
  7. class Friend
  8. {
  9.     String name, colour;
  10.  
  11.     //A method to allow a new friends name and favourite colour to be added
  12.     public Friend(String friendsName, String favouriteColour)
  13.     {
  14.         name = friendsName;
  15.         colour = favouriteColour;
  16.     }
  17.  
  18.     // method to respond to a message sent
  19.     // to a rectangle to set its length
  20.     public void setName(String nameValue)
  21.     {
  22.         name = nameValue;
  23.     }
  24.  
  25.     // method to respond to a message sent
  26.     // to a rectangle to set its breadth
  27.     public void setColour(String colourValue)
  28.     {
  29.         colour = colourValue;
  30.     }
  31.  
  32.  
  33.     // method to allow a Friend to respond
  34.     // to a System.out.println
  35.     public String toString()
  36.     {
  37.         return("\n" + name + "\t\t\t" + " " + colour);
  38.     }
  39.  
  40.  
  41.     // method to allow a Friend object to say
  42.     // whether it's equal to another rectangle or
  43.     // not
  44.     public boolean equals(Friend rIn)
  45.     {
  46.         return name.equals(rIn.name) && colour.equals(rIn.colour);
  47.     }
  48.    
  49.     public String getName()
  50.     {
  51.         return name;
  52.     }
  53.     public String getColour()
  54.     {
  55.         return colour;
  56.     }
  57. }
  58.  
  59.  
  60. public class People
  61. {
  62.     public static void main(String [] args)
  63.     {
  64.         Scanner scanner = new Scanner(System.in);
  65.  
  66.         //A LinkedList for storage of friends names and favourite colour
  67.         List<Friend> friends = new LinkedList<Friend>();
  68.        
  69.         ListIterator<Friend> iterator;
  70.         String friendName, faveColour, friendsName;
  71.         int choice;
  72.  
  73.         do
  74.         {
  75.             System.out.println("\nPlease choose your menu choice");
  76.             System.out.println("Add a friend and their favourite colour \t\t 1");
  77.             System.out.println("See a list of your friends and their favourite colour \t 2");
  78.             System.out.println("Remove a friend \t\t\t\t\t 3");
  79.             System.out.println("Search for a named friend's favourite colour \t\t 4");
  80.             System.out.println("Quit \t\t\t\t\t\t\t 0");
  81.             choice = scanner.nextInt();
  82.  
  83.             if(choice == 1) //Adding a friend and their favourite colour
  84.             {
  85.                 // ask the user for the length and the breadth of the new Rectangle
  86.                 System.out.println("\nPlease type in your friends first name and favourite colour");
  87.                 System.out.println("First name");
  88.                 friendName = scanner.next();
  89.                 System.out.println("Favourite colour");
  90.                 faveColour = scanner.next();
  91.                 iterator = friends.listIterator();
  92.                
  93.                 if(iterator.hasNext() && iterator.next().getName().equals(friendName))
  94.                 {
  95.                     System.out.println(friendName + " is already a friends name, please choose another name for your friend.");
  96.                 }
  97.                 else
  98.                 {              
  99.                 friends.add(new Friend(friendName, faveColour));
  100.                 }
  101.                
  102.                
  103.             }
  104.             else if (choice == 2) //Seeing a list of all of your friends and their favourite colour
  105.             {
  106.                 int i = friends.size();
  107.                 if (i > 0)
  108.                 {
  109.                     //code for printing out the list of your friends and their favourite colours.
  110.                     System.out.println("Friends name:\t\t Favourite colour: \n" + friends);
  111.                 }
  112.                 else
  113.                 {
  114.                     System.out.println("You don't have any friends to list!");
  115.                 }
  116.             }
  117.            
  118.             else if (choice == 3) //Removing a friend and their favourite colour
  119.             {
  120.                 friendsName = null;
  121.                 int i = friends.size();
  122.                 if (i == 0)
  123.                 {
  124.                     System.out.println("You don't have any friends to remove!");
  125.                 }
  126.                 else
  127.                 {
  128.                     System.out.println("Who would you like to remove as a friend?");
  129.                     friendsName = scanner.next();
  130.                    
  131.                     iterator = friends.listIterator();
  132.                     if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
  133.                     {
  134.                     System.out.println(friendsName + " isn't in your list of friend(s).");
  135.                     }
  136.                     else
  137.                     {
  138.                     iterator.remove();
  139.                     System.out.println(friendsName + " has been removed as a friend!");
  140.                     }
  141.                    
  142.                 }
  143.             }
  144.            
  145.             else if (choice == 4) //Searching for a friends favourite colour
  146.             {
  147.                 int i = friends.size();
  148.                 if (i == 0)
  149.                 {
  150.                     System.out.println("You don't have any friends to search!");
  151.                 }
  152.                 else
  153.                 {
  154.                     friendsName = null;
  155.                     faveColour = null;
  156.                     System.out.println("Name the friend whos favourite colour you would like to know.");
  157.                     friendsName = scanner.next();
  158.                
  159.                     iterator = friends.listIterator();
  160.                     if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
  161.                     {
  162.                         System.out.println(friendsName + " isn't a friend of yours.");
  163.                     }
  164.                     else
  165.                     {
  166.                         faveColour = iterator.next().getColour();
  167.                         System.out.println(friendsName + "'s favourite colour is " + faveColour);
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.         while(choice != 0);
  173.         System.out.println("\nThank you for using this program. Come back soon!\n\n");
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement