用ArrayList写得一个增删改查 java程序
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type Employ is accessible. Must qualify the allocation with an enclosing instance of type Employ (e.g. x.new A() where x is an instance of Employ).
No enclosing instance of type Employ is accessible. Must qualify the allocation with an enclosing instance of type Employ (e.g. x.new A() where x is an instance of Employ).
at com.yu.Employ.main(Employ.java:120)
代码:
package com.yu;
import java.util.*;
import java.io.*;
public class Employ {
/**
* 职工信息定义类
* @author Yu
*
*/
class Emp{
private String name;
private String age;
private float sal;
private String id;
public Emp(String id,String name,String age,float sal){
this.age=age;
this.id=id;
this.sal=sal;
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
/**
* 职工信息管理类
* @author Yu
*
*/
class EmployM{
ArrayList a=null;
public EmployM(){
a=new ArrayList();
}
//增加一个职工
public void addEmp(Emp emp){
a.add(emp);
}
// 查询
public void showEmp(String id ){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
System.out.println("查询的员工名字是"+emp.getName());
System.out.println("工资"+emp.getSal());
System.out.println("年龄"+emp.getAge());
System.out.println("性别"+emp.getId());
}
}
}
//删除
public void deleteEmp(String id){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
a.remove(i);
a.remove(emp);
}
}
}
//更新
public void updateEmp(String id,float newsal){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
emp.setSal(newsal);
}
}
}
}
/**主函数
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
EmployM employm=new EmployM();//此处显示错误????为什么呀?????????
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入你的选择:");
System.out.println("1添加雇员:");
System.out.println("2删除雇员:");
System.out.println("3查询雇员:");
System.out.println("4修改工资:");
String operType=br.readLine();
if(operType.equals("1")){
System.out.println("请输入编号:");
String id=br.readLine();
System.out.println("请输入名字:");
String name=br.readLine();
System.out.println("请输入性别:");
String age=br.readLine();
System.out.println("请输入工资:");
float sal=Float.parseFloat(br.readLine());
Emp emp=new Emp(id, name, age, sal);//此处显示错误????为什么呀?????????
employm.addEmp(emp);
}else if(operType.equals("2")){
String id=br.readLine();
employm.deleteEmp(id);
}else if (operType.equals("3")){
String id=br.readLine();
employm.showEmp(id);
}else if(operType.equals("4")){
String id=br.readLine();
float sal=Float.parseFloat(br.readLine());
employm.updateEmp(id, sal);
}else if(operType.equals("5")){
System.exit(-1);
}
}
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type Employ is accessible. Must qualify the allocation with an enclosing instance of type Employ (e.g. x.new A() where x is an instance of Employ).
No enclosing instance of type Employ is accessible. Must qualify the allocation with an enclosing instance of type Employ (e.g. x.new A() where x is an instance of Employ).
at com.yu.Employ.main(Employ.java:120)
代码:
package com.yu;
import java.util.*;
import java.io.*;
public class Employ {
/**
* 职工信息定义类
* @author Yu
*
*/
class Emp{
private String name;
private String age;
private float sal;
private String id;
public Emp(String id,String name,String age,float sal){
this.age=age;
this.id=id;
this.sal=sal;
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
/**
* 职工信息管理类
* @author Yu
*
*/
class EmployM{
ArrayList a=null;
public EmployM(){
a=new ArrayList();
}
//增加一个职工
public void addEmp(Emp emp){
a.add(emp);
}
// 查询
public void showEmp(String id ){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
System.out.println("查询的员工名字是"+emp.getName());
System.out.println("工资"+emp.getSal());
System.out.println("年龄"+emp.getAge());
System.out.println("性别"+emp.getId());
}
}
}
//删除
public void deleteEmp(String id){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
a.remove(i);
a.remove(emp);
}
}
}
//更新
public void updateEmp(String id,float newsal){
for(int i=0;i<a.size();i++){
Emp emp=(Emp)a.get(i);
if(emp.getId().equals(id)){
emp.setSal(newsal);
}
}
}
}
/**主函数
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
EmployM employm=new EmployM();//此处显示错误????为什么呀?????????
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入你的选择:");
System.out.println("1添加雇员:");
System.out.println("2删除雇员:");
System.out.println("3查询雇员:");
System.out.println("4修改工资:");
String operType=br.readLine();
if(operType.equals("1")){
System.out.println("请输入编号:");
String id=br.readLine();
System.out.println("请输入名字:");
String name=br.readLine();
System.out.println("请输入性别:");
String age=br.readLine();
System.out.println("请输入工资:");
float sal=Float.parseFloat(br.readLine());
Emp emp=new Emp(id, name, age, sal);//此处显示错误????为什么呀?????????
employm.addEmp(emp);
}else if(operType.equals("2")){
String id=br.readLine();
employm.deleteEmp(id);
}else if (operType.equals("3")){
String id=br.readLine();
employm.showEmp(id);
}else if(operType.equals("4")){
String id=br.readLine();
float sal=Float.parseFloat(br.readLine());
employm.updateEmp(id, sal);
}else if(operType.equals("5")){
System.exit(-1);
}
}
}
}
...全文


