简体   繁体   中英

How can i display all my data on array on object

i want to create a method that displays all the data on the array of objects i made. i manage to create a search specific through the help of people in here. can i re use his code?? how? The method i want to create is at the last part. How can i display all my data on array on object

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;

public class TestMouse {

    private static Dictionary<String, Mouse> mouseList = new Hashtable<String, Mouse>(); 

    public static void main(String[] args) {

        menu();
    }

    @SuppressWarnings("resource")
    public static void menu() {
        Scanner input = new Scanner(System.in);

        int choice;
        do {
            System.out.println("CMPE 325 Student Record Holder System");
            System.out.println("--------------------------------");
            System.out.println("1.Add Student");
            System.out.println("2.View Records");
            System.out.println("3.Update Students");
            System.out.println("4.Get Average");
            System.out.println("5.Exit");
            System.out.println();
            System.out.println("--------------------------------");
            System.out.print("Enter your choice: ");
            choice = input.nextInt();

            switch (choice) {

            case 1:
                record();
                break;
            case 2:
                display();
                // display();
                break;
            case 3:
                menu();
                // update();
                // break();
            case 4:
                menu();
                // average();
                // break();
            case 5:
                break;
            default:
                continue;
            }

        } while (choice != 5);
        System.out.println();
        System.out.println("Good-Bye");

    }

    // end of menu(); method.
    // ------------------------------------------------------------
    @SuppressWarnings({ "resource", "unused" })
    public static void record() {

        Scanner Input = new Scanner(System.in);

        int total;

        System.out.println("How many students you are going to input? ");
        total = Input.nextInt();

        Mouse[] keyboard = new Mouse[total];

        for (int index = 0; index < total; index++) {
            String space;

            keyboard[index] = new Mouse();

            System.out.printf("Student[%d]", index + 1);

            System.out.println();

            System.out.println("");
            space = Input.nextLine();

            System.out.println("ID Number: ");
            keyboard[index].setId(Input.nextLine());

            System.out.println("First Name: ");
            keyboard[index].setFirstName(Input.nextLine());

            System.out.println("Middle Name: ");
            keyboard[index].setMiddleName(Input.nextLine());

            System.out.println("Last Name: ");
            keyboard[index].setLastName(Input.nextLine());

            System.out.println("Degree: ");
            keyboard[index].setDegName(Input.nextLine());

            System.out.println("Year Level: ");
            keyboard[index].setYear(Input.nextInt());

            //Save current object
            mouseList.put(keyboard[index].getId(), keyboard[index]);
        }

        for (int index2 = 0; index2 < total; index2++) {
            System.out.printf("Student[%d]", index2 + 1);
            System.out.println();
            System.out.println("ID Number:" + keyboard[index2].getId());
            System.out.println("Name:" + keyboard[index2].getFirstName() + " "
                    + keyboard[index2].getMiddleName() + " "
                    + keyboard[index2].getLastName());
            System.out.println("Degree:" + keyboard[index2].getDegName());
            System.out.println("Year Level:" + keyboard[index2].getYear());
        }
    }

    public static void specific() {
        String id = "";
        System.out.println("Enter an Id Number");
        Scanner Input = new Scanner(System.in);
        id = Input.nextLine();

        Mouse mouse = mouseList.get(id);
        if (mouse != null) {
            System.out.println();
            System.out.println("ID Number:" + mouse.getId());
            System.out.println("Name:" + mouse.getFirstName() + " "
                    + mouse.getMiddleName() + " "
                    + mouse.getLastName());
            System.out.println("Degree:" + mouse.getDegName());
            System.out.println("Year Level:" + mouse.getYear());
        }
    }
    @SuppressWarnings("resource")
    public static void display(){
        Scanner input = new Scanner(System.in);

        int choice;


            System.out.println("--------------------------------");
            System.out.println("1.View List");
            System.out.println("2.View Specific Record");
            System.out.println("3.Exit");
            System.out.println();
            System.out.println("--------------------------------");
            System.out.print("Enter your choice: ");
            choice = input.nextInt();

            switch (choice) {

            case 1:
                displayall();
                break;
            case 2:
                specific();
                break;
            case 3:
                menu();
                break;

            }
}

 this part is the one im talking about. i hope u can help me
 public static void displayall(){

        String id = "";
        System.out.println("Enter an Id Number");
        Scanner Input = new Scanner(System.in);
        id = Input.nextLine();

        Mouse mouse = mouseList.get(id);
        if (mouse != null) {
            System.out.println();
            System.out.println("ID Number:" + mouse.getId());
            System.out.println("Name:" + mouse.getFirstName() + " "
                    + mouse.getMiddleName() + " "
                    + mouse.getLastName());
            System.out.println("Degree:" + mouse.getDegName());
            System.out.println("Year Level:" + mouse.getYear());
        }
    }
}             

Because you're using the legacy Dictionary interface , this gets a bit annoying, but it's possible to do this.

What you want to do is create an Enumeration of your elements, then iterate over that. The instance is provided by calling Dictionary#elements() , or more specifically, mouseList.elements() .

You then iterate over it as prescribed by Enumeration .

It would actually be a lot easier if Mouse also overrode toString() , as that's the best way to print those sorts of complex objects.

Lastly, you make a lot of unnecessary calls in that method - you don't care about an ID or anything like that. You'll also not need to check for null , since if it's not in the set of elements, it won't be in the Enumeration .

If you want to get a little more modern...then use a Map instead. Hashtable already uses the Map interface, so you won't need to change your concrete object (although I would strongly recommend it). Iterating over the elements of a Map becomes a call to Map#entrySet() with a little more work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM