英文:
Input from one class to another
问题
以下是翻译好的部分:
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 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());
}
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;
}
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 class ViewLeads extends Lead {
}
}
英文:
I'm receiving input in class Main
, sorting it to different ArrayList
s in Lead.java
and want to make it so View.java
is able to view all the information stored in Lead.java
, triggered by the correct input from class Main
- however I understand there is no such thing as extending two classes. How do I do it ?
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
得分: 1
public class View {
leadReferenceObj = new Lead();
// 假设你的类都在同一个包中,所有公共属性和公共方法都可以通过 leadReferenceObj 访问
// 要访问 Lead.java 中的 primLead 方法:
leadReferenceObj.primLead();
}
> 这是 Java 面向对象编程的基本规则之一。由于 primeLead() 方法被声明为 public 访问修饰符,你可以从另一个类中访问它。primeLead() 方法使用了从父类 Main.java 分配的 Lead.java 类中的值。
想要了解更多:
https://stackify.com/oops-concepts-in-java/#:~:text=OOP%20concepts%20in%20Java%20are,encapsulation%2C%20inheritance%2C%20polymorphism.&text=Basically%2C%20Java%20OOP%20concepts%20let,of%20them%20without%20compromising%20security.
英文:
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();
}
> This one of the basic rules of OOP programming in java. Since the method primeLead() is declared with a public access modifier, you can access it from another class. primeLead() method is using values from the Lead.java class that are assigned from the parent Main.java class.
To read a little more:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论