Advertisement
Guest User

Untitled

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