Advertisement
Nikki12345671

Bibliogrophy

Apr 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package bibliogrophy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. //Nikki Forehand
  8. public class Program {
  9.  
  10. public static void main(String[] args) {
  11. //A special type of list that can hold Source objects
  12. ArrayList sources = new ArrayList();
  13.  
  14. //Connect to the keyboard
  15. Scanner sc = new Scanner(System.in);
  16.  
  17. //Some temporary variables
  18. String input, f, l, t, c, p;
  19. int y;
  20.  
  21. do {
  22. //Read in details about a source...
  23. System.out.println("Enter source information:");
  24. System.out.print("\tBook title: ");
  25. t = sc.nextLine();
  26.  
  27. System.out.print("\tAuthor first name: ");
  28. f = sc.nextLine();
  29.  
  30. System.out.print("\tAuthor last name: ");
  31. l = sc.nextLine();
  32.  
  33. System.out.print("\tPublishing company: ");
  34. p = sc.nextLine();
  35.  
  36. System.out.print("\tPublisher city: ");
  37. c = sc.nextLine();
  38.  
  39. System.out.print("\tYear published: ");
  40. y = Integer.parseInt(sc.nextLine());
  41.  
  42. //Create an Source object based on the values entered
  43. Source s = new Source(l, f, t, c, p, y);
  44.  
  45. //Add the new source our our list of sources
  46. sources.add(s);
  47.  
  48. System.out.println("Enter another source (Y or N)?");
  49. input = sc.nextLine();
  50.  
  51. }while(input.equalsIgnoreCase("Y"));
  52.  
  53. //Put the list of sources in order based on author last name
  54. Collections.sort(sources);
  55.  
  56. //Print out the list of sources
  57. for(int i=0; i<sources.size(); i++) {
  58. System.out.println(sources.get(i));
  59. }
  60. }
  61. }
  62.  
  63. //Create a new class to hold bibliography sources. It includes code that lets us sort it.
  64. class Source implements Comparable<Source>{
  65. //Source information
  66. String authorLast;
  67. String authorFirst;
  68. String bookTitle;
  69. String publisherCity;
  70. String publisherName;
  71. int yearPublished;
  72.  
  73. public Source(String last, String first, String title, String city, String publisher, int year) {
  74. authorLast = last;
  75. authorFirst = first;
  76. bookTitle = title;
  77. publisherCity = city;
  78. publisherName = publisher;
  79. yearPublished = year;
  80. }
  81.  
  82. //Returns the author info in "Last, First" format.
  83. public String getAuthor() {
  84. return authorLast + ", " + authorFirst + ". " + bookTitle + ". " + publisherCity + ": " + publisherName + ". " + yearPublished + "." + "Print" ;
  85. }
  86.  
  87. //Returns a text string representing this source.
  88. public String toString() {
  89. //Update this line to return (not print out) a String in MLA format.
  90. return authorLast + ", " + authorFirst + ". " + bookTitle + ". " + publisherCity + ": " + publisherName + ". " + yearPublished + "." + " Print" ;
  91. }
  92.  
  93. //Specifies that sources are sorted by author.
  94. public int compareTo(Source s) {
  95. return getAuthor().compareTo(s.getAuthor());
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement