本文介绍了将userInput添加到EmployeeStore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我让一个EmployeeStore明显存储员工详细信息,如姓名,ID和电子邮件。我设置了一个菜单,现在每当用户想要添加一个员工时,我都会停下来做什么。
这里是我的代码:
MenuMethods:
//进口
import java.util 。扫描器;
// ******************************************** ************************
公共类MenuMethods
{
private static Scanner keyboard = new扫描器(System.in);
//公司应用程序菜单的方法。
//验证选择的方法。
public static int getMenuChoice(String menuString,int limit,String prompt,String errorMessage)
{
System.out.println(menuString);
int choice = inputAndValidateInt(1,limit,prompt,errorMessage);
回报选择;
}
// *************************************** *****************************
//此方法用于getMenuChoice方法。
public static int inputAndValidateInt(int min,int max,String prompt,String errorMessage)
{
int number;
布尔型有效;
做{
System.out.print(提示);
number = keyboard.nextInt();
valid = number if(!valid){
System.out.println(errorMessage);
}
} while(!valid);
返回号码;
}
// *************************************** *****************************
public void userInput()
{
}
// ****************************************** **************************
}
添加方法:
//添加到散列图:员工。
public void add(Employee employee)
{
map.put(employee.getEmployeeName(),employee);
}
// *************************************** *****************************
MainApp:
//导入。
import java.util.Scanner;
// ******************************************** ************************
公共类MainApp
{
//扫描器在此声明以供整个使用整个MainApp。
private static Scanner keyboard = new Scanner(System.in);
public static void main(String [] args)
{
new MainApp()。start();
$ b public void start()
{
//创建一个名为Store的商店并将Employee添加到商店。
EmployeeStore Store = new EmployeeStore();
Store.add(新员工(James O'Carroll,18,hotmail.com));
Store.add(新员工(Andy Carroll,1171,yahoo.com));
Store.add(新员工(Luis Suarez,7,gmail.com));
// ******************************************** ************************
/ *测试代码。
Store.searchByName(James O'Carroll);
Store.print();
Store.searchByEmail(gmail.com);
Employee andy = Store.searchByEmail(hotmail.com);
System.out.println(andy);
Employee employee = Store.searchByName(James O'Carroll);
if(employee!= null)
{
employee.setEmployeeName(Joe);
employee.setEmployeeId(1);
employee.setEmployeeEmail(webmail.com);
Store.edit(员工);
Store.print();
} * /
// ************************************* *******************************
int选择;
System.out.println(欢迎使用公司数据库。);
do
{
choice = MenuMethods.getMenuChoice(
1.\tView All+
\\\
2.\tAdd+
\ n3.\tDelete+
\ n4.\tDelete All+
\\\
5.\tEdit+
\\\
6.\\ \\ tSearch+
\\\
7.\tPrint+
\\\
8.\tExit,8,请输入您的选择:,错误[1,8] );
// String temp = keyboard.nextLine();这阻止了进入选择。
switch(选择)
{
案例1:
System.out.println(View All);
Store.print();
break;
案例2:
System.out.println(Add);
//Store.add();
break;
案例3:
System.out.println(删除);
//Store.delete();
中断;
案例4:
System.out.println(全部删除);
Store.clear();
break;
情况5:
System.out.println(Edit);
break;
案例6:
System.out.println(Search);
Store.clear();
break;
案例7:
System.out.println(Print);
Store.print();
break;
案例8:
System.out.println(Exit);
break;
}
} while(choice!= 8);
$ b
解决方案我认为在添加员工之前,您错过了接下来的步骤。向用户询问员工姓名和电子邮件地址,然后才将其添加到您的商店。
I made an EmployeeStore to obviously store employee details such as name,id and email. I set up a menu and now whenever the user wants to add an employee i get stuck on what to do.Here is my code:
MenuMethods: //Imports import java.util.Scanner; //******************************************************************** public class MenuMethods { private static Scanner keyboard = new Scanner(System.in); //Methods for the Company Application menu. //Method for validating the choice. public static int getMenuChoice(String menuString, int limit, String prompt, String errorMessage) { System.out.println(menuString); int choice = inputAndValidateInt(1, limit, prompt, errorMessage); return choice; } //******************************************************************** //This method is used in the getMenuChoice method. public static int inputAndValidateInt(int min, int max, String prompt, String errorMessage) { int number; boolean valid; do { System.out.print(prompt); number = keyboard.nextInt(); valid = number <= max && number >= min; if (!valid) { System.out.println(errorMessage); } } while (!valid); return number; } //******************************************************************** public void userInput() { } //******************************************************************** }
Add Method:
//Add to the Hashmap : Employee. public void add(Employee employee) { map.put(employee.getEmployeeName(), employee); } //********************************************************************
MainApp:
//Imports. import java.util.Scanner; //******************************************************************** public class MainApp { //The Scanner is declared here for use throughout the whole MainApp. private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { new MainApp().start(); } public void start() { //Create a Store named Store and add Employee's to the Store. EmployeeStore Store = new EmployeeStore(); Store.add(new Employee ("James O' Carroll", 18,"hotmail.com")); Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com")); Store.add(new Employee ("Luis Suarez", 7,"gmail.com")); //******************************************************************** /*Test Code. Store.searchByName("James O' Carroll"); Store.print(); Store.searchByEmail("gmail.com"); Employee andy = Store.searchByEmail("hotmail.com"); System.out.println(andy); Employee employee = Store.searchByName("James O' Carroll"); if (employee != null) { employee.setEmployeeName("Joe"); employee.setEmployeeId(1); employee.setEmployeeEmail("webmail.com"); Store.edit(employee); Store.print(); }*/ //******************************************************************** int choice ; System.out.println("Welcome to the Company Database."); do { choice = MenuMethods.getMenuChoice( "1.\tView All" + "\n2.\tAdd" + "\n3.\tDelete" + "\n4.\tDelete All " + "\n5.\tEdit" + "\n6.\tSearch" + "\n7.\tPrint"+ "\n8.\tExit", 8, "Please enter your choice:", "Error [1,8] Only"); //String temp = keyboard.nextLine(); This prevented entering the choice. switch (choice) { case 1: System.out.println("View All"); Store.print(); break; case 2: System.out.println("Add"); //Store.add(); break; case 3: System.out.println("Delete"); //Store.delete(); break; case 4: System.out.println("Delete All"); Store.clear(); break; case 5: System.out.println("Edit"); break; case 6: System.out.println("Search"); Store.clear(); break; case 7: System.out.println("Print"); Store.print(); break; case 8: System.out.println("Exit"); break; } } while (choice != 8); } }
解决方案I think you're missing the next steps, before adding the employee. Ask to the user about the Employee name and email, and only after that, add it to your Store.
这篇关于将userInput添加到EmployeeStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!