从一个班级输入到另一个班级

时间:2020-09-18 18:40:49

标签: java class

我正在类Main中接收输入,并将其分类到ArrayList中的不同Lead.java s并希望输入,以便View.java能够查看所有信息存储在Lead.java中,由类Main的正确输入触发-但是我知道没有扩展两个类的事情。我该怎么办?

Main.Java:

import java.util.Scanner;

public class Main {
    boolean exit = false;

    public void runMenu() {
        printHeader();
        while (!exit) {
            mainMenu();
            int choice = getInput();
            performAction(choice);
        }
    }

    private void performAction(int choice) {
        switch(choice){
            case 1:
                Lead lead1 = new Lead();
                lead1.primeLead();
            case 2:
            case 3:
            case 4:
            case 5:
                exit = true;
                System.out.println("Bye!");
                break;
        }
    }

    public void printHeader() {
        System.out.println("===========================================");
        System.out.println("                  Hello user!              ");
        System.out.println("               Welcome to our lead         ");
        System.out.println("                 Management tool           ");
        System.out.println("===========================================");
    }

    public void mainMenu() {
        System.out.println("\nPlease select one of the following options: ");
        System.out.println("1) Create a new lead");
        System.out.println("2) View all the leads");
        System.out.println("3) Connect ");
        System.out.println("4) View statistics");
        System.out.println("5) Exit   ");
    }

    private int getInput() { // Scanner takes input from user, returns his choice.
        Scanner kb = new Scanner(System.in);
        int choice = -1;
        while (choice < 0 || choice > 5) {
            try {
                System.out.print("\nEnter your choice:  ");
                choice = Integer.parseInt(kb.nextLine()); // What is Integer.parseInt ? what is . next line ?
            }
            catch (NumberFormatException e) {
                System.out.println("Invalid selection, please try again.");
            }
        }
        return choice;
    }

    public static void main(String[] args) {
        Main menu = new Main();
        menu.runMenu();
    }
}

Lead.java:

import java.util.ArrayList;
import java.util.Scanner;

public class Lead extends Main {
    String nameLead;
    int ageLead;
    int phoneLead;
    String cityLead;
    String email;
    String otherNotes;
    int choicelead;
    int indexOfLead = 0;
    int i = indexOfLead;

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> ages = new ArrayList<Integer>();
    ArrayList<Integer> phones = new ArrayList<Integer>();
    ArrayList<String> cities = new ArrayList<String>();
    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> notes = new ArrayList<String>();

    Scanner leads = new Scanner(System.in);
    
    Lead() {
        i = 0;
        // Need to create an ArrayList that has all the Arraylists above.
    }

    public  void primeLead() {
        i = 0;
        System.out.println("============================================");
        System.out.println("  Please enter by the following order : ");
        System.out.println("     Name, age, phone , city, mail     ");
        System.out.println("============================================");
    
        System.out.println("Please enter the name of the Lead :  ");
        names.add(leads.nextLine());
    
        System.out.println("Age? :  ");
        ages.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println("Phone number? ");
        phones.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println("Would you like to add ... ");
        System.out.println("1) City? ");
        System.out.println("2) Email? ");
        System.out.println("3) Notes?  ");
        System.out.println("4) All of the above?");

        if (leads.nextLine().equals("1")) {
            System.out.println("Please add City: ");
            cities.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals("2")) {
            System.out.println("Please add email : ");
            emails.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals("3")) {
            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
            runMenu();
        }

        if (leads.nextLine().equals("4")) {
            System.out.println("Please add a City: ");
            cities.add(leads.nextLine());

            System.out.println("Please add email : ");
            emails.add(leads.nextLine());

            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
        }
    }
}

View.java:

public class View extends Main {
    public ViewLeads extends Lead {
    
    }
}

1 个答案:

答案 0 :(得分:1)

public class View{
       leadReferenceObj = new Lead();
       //Assuming your classes are in the same package, all the public attributes
       //and public methods are accessible using leadReferenceObj
       //to access the primeLead method in Lead.java;
       leadReferenceObj.primLead();
}

这是Java中OOP编程的基本规则之一。由于方法primeLead()是使用公共访问修饰符声明的,因此您可以从另一个类访问它。 primeLead()方法使用的是Lead.java类的值,这些值是从父Main.java类分配的。

要阅读更多内容:

https://stackify.com/oops-concepts-in-java/#:~:text=OOP%20concepts%20in%20Java%20are,encapsulation%2C%20inheritance%2C%20and%20polymorphism.&text=Basically%2C%20Java%20OOP%20concepts%20let,of%20them%20without%20compromising%20security