Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- List list = new List();
- int value;
- int color;
- Scanner scan = new Scanner(System.in);
- System.out.println("Menu");
- System.out.println("1 - Make list" + "n" + "2 - Show massive" + "n" + "3 - Show number of elements" + "n" +
- "4 - Show cards by the value" + "n" + "5 - Show cards by color" + "n" + "6 - Remove" + "n" + "7 - Quit" + "n");
- byte functuion = scan.nextByte();
- boolean isQuit = false;
- while (!isQuit){
- switch (functuion){
- case 1 : list.makeList();
- break;
- case 2 :
- if (!list.isEmpty())
- list.showList();
- break;
- case 3 :
- System.out.println("Number of elements: ");
- list.showNumberOfElements();
- break;
- case 4 :
- System.out.println("Enter value for search: ");
- scan.nextLine();
- value = scan.nextInt();
- list.showByValue();
- break;
- case 5 :
- System.out.println("Enter color for search: ");
- scan.nextLine();
- color = scan.nextInt();
- list.showByColor();
- break;
- case 6 :
- System.out.println("Enter value of card that you want to remove: ");
- value = scan.nextInt();
- System.out.println("Enter color of card that you want to remove: ");
- color = scan.nextInt();
- Card key = new Card(value, color);
- list.remove(key);
- break;
- case 7 :
- isQuit = true;
- break;
- }
- }
- }
- }
- import java.util.Random;
- public class List {
- private Element first;
- public List() {
- first = null;
- }
- public boolean isEmpty(){
- return (first == null);
- }
- public void makeList() {
- Random random = new Random();
- int mValue = random.nextInt(14) + 1;
- int mColor = random.nextInt(3);
- insertFirst(new Card(mValue, mColor));
- }
- public void insertFirst(Card cd) {
- Element newElement = new Element(cd);
- newElement.next = first;
- first = newElement;
- }
- public void showList() {
- Element current = first;
- int i = 1;
- while (current != null){
- System.out.println(i + " element: ");
- current.showElement();
- current = current.next;
- i++;
- }
- }
- public void showNumberOfElements() {
- }
- public void showByValue() {
- }
- public void showByColor() {
- }
- public Element remove(Card key) {
- Element current = first;
- Element previous = first;
- while (current.cardData != key){
- if (current.next == null)
- return null;
- else {
- previous = current;
- current = current.next;
- }
- }
- if (current == first)
- first = first.next;
- else
- previous.next = current.next;
- return current;
- }
- }
- public class Element {
- public Card cardData;
- public Element next;
- public Element(Card cd) {
- this.cardData = cd;
- }
- protected void showElement(){
- System.out.println("Value: " + cardData.getCardValue() + "n" + "Color: " + cardData.getCardColor() + "n");
- }
- }
- public class Card {
- int cardValue;
- int cardColor;
- public Card(int value, int color) {
- this.cardValue = cardValue;
- this.cardColor = cardColor;
- }
- public int getCardValue() {
- return cardValue;
- }
- public int getCardColor() {
- return cardColor;
- }
- }
Add Comment
Please, Sign In to add comment