Guest User

Untitled

a guest
Mar 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. List list = new List();
  8.  
  9. int value;
  10. int color;
  11.  
  12. Scanner scan = new Scanner(System.in);
  13.  
  14. System.out.println("Menu");
  15. System.out.println("1 - Make list" + "n" + "2 - Show massive" + "n" + "3 - Show number of elements" + "n" +
  16. "4 - Show cards by the value" + "n" + "5 - Show cards by color" + "n" + "6 - Remove" + "n" + "7 - Quit" + "n");
  17.  
  18. byte functuion = scan.nextByte();
  19.  
  20. boolean isQuit = false;
  21.  
  22. while (!isQuit){
  23.  
  24. switch (functuion){
  25.  
  26. case 1 : list.makeList();
  27. break;
  28.  
  29. case 2 :
  30. if (!list.isEmpty())
  31. list.showList();
  32. break;
  33.  
  34. case 3 :
  35. System.out.println("Number of elements: ");
  36. list.showNumberOfElements();
  37. break;
  38.  
  39. case 4 :
  40. System.out.println("Enter value for search: ");
  41. scan.nextLine();
  42. value = scan.nextInt();
  43. list.showByValue();
  44. break;
  45.  
  46. case 5 :
  47. System.out.println("Enter color for search: ");
  48. scan.nextLine();
  49. color = scan.nextInt();
  50. list.showByColor();
  51. break;
  52.  
  53. case 6 :
  54. System.out.println("Enter value of card that you want to remove: ");
  55. value = scan.nextInt();
  56. System.out.println("Enter color of card that you want to remove: ");
  57. color = scan.nextInt();
  58. Card key = new Card(value, color);
  59. list.remove(key);
  60. break;
  61.  
  62. case 7 :
  63. isQuit = true;
  64. break;
  65.  
  66. }
  67. }
  68.  
  69. }
  70. }
  71.  
  72. import java.util.Random;
  73.  
  74. public class List {
  75.  
  76. private Element first;
  77.  
  78. public List() {
  79.  
  80. first = null;
  81.  
  82. }
  83.  
  84. public boolean isEmpty(){
  85.  
  86. return (first == null);
  87.  
  88. }
  89.  
  90. public void makeList() {
  91.  
  92. Random random = new Random();
  93.  
  94. int mValue = random.nextInt(14) + 1;
  95. int mColor = random.nextInt(3);
  96.  
  97. insertFirst(new Card(mValue, mColor));
  98.  
  99. }
  100.  
  101. public void insertFirst(Card cd) {
  102.  
  103. Element newElement = new Element(cd);
  104. newElement.next = first;
  105. first = newElement;
  106.  
  107. }
  108.  
  109. public void showList() {
  110.  
  111. Element current = first;
  112.  
  113. int i = 1;
  114.  
  115. while (current != null){
  116.  
  117. System.out.println(i + " element: ");
  118. current.showElement();
  119. current = current.next;
  120. i++;
  121.  
  122. }
  123.  
  124. }
  125.  
  126. public void showNumberOfElements() {
  127.  
  128.  
  129.  
  130. }
  131.  
  132. public void showByValue() {
  133.  
  134.  
  135.  
  136. }
  137.  
  138. public void showByColor() {
  139.  
  140.  
  141.  
  142. }
  143.  
  144. public Element remove(Card key) {
  145.  
  146. Element current = first;
  147. Element previous = first;
  148.  
  149. while (current.cardData != key){
  150.  
  151. if (current.next == null)
  152. return null;
  153. else {
  154.  
  155. previous = current;
  156. current = current.next;
  157.  
  158. }
  159.  
  160. }
  161.  
  162. if (current == first)
  163. first = first.next;
  164. else
  165. previous.next = current.next;
  166.  
  167. return current;
  168. }
  169. }
  170.  
  171. public class Element {
  172.  
  173. public Card cardData;
  174. public Element next;
  175.  
  176. public Element(Card cd) {
  177.  
  178. this.cardData = cd;
  179.  
  180. }
  181.  
  182. protected void showElement(){
  183.  
  184. System.out.println("Value: " + cardData.getCardValue() + "n" + "Color: " + cardData.getCardColor() + "n");
  185.  
  186. }
  187. }
  188.  
  189. public class Card {
  190.  
  191. int cardValue;
  192. int cardColor;
  193.  
  194. public Card(int value, int color) {
  195.  
  196. this.cardValue = cardValue;
  197. this.cardColor = cardColor;
  198.  
  199. }
  200.  
  201. public int getCardValue() {
  202. return cardValue;
  203. }
  204.  
  205. public int getCardColor() {
  206. return cardColor;
  207. }
  208. }
Add Comment
Please, Sign In to add comment