0
package michael.week.pkg5;

class Employee {
String Fname ;
        String Lname;
        String PhoneNum;
        String Address;
         void setFirst(String First){
             Fname = First ;
         }
         void setlast(String Last){
             Lname  = Last ;
         }
          void setAddress (String address){
             Address = address ;
         }
         void setPhone (String Phone){
             PhoneNum  = Phone ;
         }          
         void display (){
             System.out.println ("the Fist name is :"+ Fname + "  , the last name is :        " + Lname  + "  ,the address is : "+ Address+ "  ,the phone is : "+ PhoneNum);
         }
         }

    package michael.week.pkg5;

     import java.io.BufferedReader;
     import java.io.IOException;
      import java.io.InputStreamReader;





   public class MichaelWeek5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    class Stck {
    Employee stck [] = new Employee[10];
    int x ;
     void stck (){
        x= -1 ;
    }
    Employee push (Employee item){
        if (x == 9){
            System.out.println ("Stack is full");
        }else stck[++x] = item ;
        return stck[x];
    }
        Employee pop (){
        if (x <0){
            System.out.println (" Stack underflow");
           return stck [x];
        }else 
            return stck[x++];
    }
    }
    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    String info2 = null ;
    Stck obj = new Stck();
    Employee obj2 = new Employee ();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();
   if(choice.equals("add")){
       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.stck ();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();

       System.out.println ("please enter Employee last name name :");
       info = br.readLine();

       System.out.println ("please enter Employee address :");
       info = br.readLine();

       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       }
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

    } 

   }

问候,我是 Java 新手,我正在开发这个程序.......我需要知道如何将数据推送到 stck ?注意:push 的参数是类型employee,Employee 包含First、Last、Phone 和address。我怎样才能推动他们每个人?这是我做的,我的导师拒绝了

package week.pkg5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Week5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
      class Employee {


     String [] Fname = new String [10];
        String[] Lname= new String [10];
        String[] PhoneNum= new String [10];
        String[] Address = new String [10];
        int x= -1 ;
         void increment(){
           x++;

        }
         void PushFirst(String First){
             Fname[x] = First ;
         }
         void Pushlast(String Last){
             Lname [x] = Last ;
         }
         void PushPhone (String Phone){
             PhoneNum [x] = Phone ;
             System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
         }
         void PushAddress (String address){
             Address [x] = address ;
         }
         void pop (){
             if (x < 0){
                 System.out.println (" No Empolyee !");
             }
             else {

        System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
        x--;
        }
}
 void display (){
    if (x < 0){
        System.out.println (" No Empolyee !");
    }
    else {
        for (int q = 0 ; q <=x ; q++){
        System.out.println ((q+1)+"- "+"the First name is :"+ Fname [q]+ "  , the last name is : " + Lname [q] + "  ,the address is : "+ Address[q] + "  ,the phone is : "+ PhoneNum[q]);
        }
    }
}
 }
 InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    Employee obj = new Employee();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter display  to display all employees list");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();

   if(choice.equals("add")){


       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.increment();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();
       obj.PushFirst(info);
       System.out.println ("please enter Employee last name name :");
       info = br.readLine();
       obj.Pushlast(info);
       System.out.println ("please enter Employee address :");
       info = br.readLine();
       obj.PushAddress(info);
       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       obj.PushPhone(info);}
   }  else if(choice.equals("display")){
           obj.display();
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

} 
 }
4

3 回答 3

0

您需要创建一个员工堆栈,然后您可以将整个员工对象推入堆栈。

import java.util.Stack;
...
Employee emp = new Employee();
Stack<Employee> stack = new Stack<Employee>();
stack.push(emp);
于 2012-08-04T05:20:00.303 回答
0

在为类创建一个对象之后说(这里是stck),它将通过调用如下所示的方法来保存数据

class std
{

  int id;
  string name;
  public:
  void set_id(int i)
  {
      id=i;
  }
  void set_name(string n)
  {
      name = n;
   }
  void disp()
  {
     system.out.println("name"+name+" and id = "+id);
  }
 };

  main()
  {
      std s = new std();
      s.set_id(1);
      s.set_name("sunmoon");
      s.disp();
 }

所以在这里你可以观察到 s 被认为是堆栈,我们推送了一个人的详细信息,就像你可以创建对象数组并推送 n 个人员的详细信息一样。

于 2012-08-04T05:35:28.370 回答
0

您在如何对数据建模方面存在几个问题。

问题#1(最大的问题):员工不应该有任何堆栈的内部表示。想想现实世界中的物体。在您的示例中,每个 Employee 应该具有:

  • 名字
  • 地址
  • 电话号码

所以成员字段:

 String [] Fname = new String [10];
 String[] Lname= new String [10];
 String[] PhoneNum= new String [10];
 String[] Address = new String [10];
 int x= -1 ;

不属于。这导致我们...

问题 #2:您为 Stack 建模的方式仅允许固定数量的条目 - 在您的情况下为 10 个)。这不是堆栈的工作方式。如果您不确定,请阅读堆栈是什么。您可以使用LinkedList在 Java 中对 Stack 建模- 或使用 Java 提供的内置Stack。如果您决定创建自己的版本,则需要创建的重要操作是:

  • 流行音乐
  • 是空的

我的建议是首先对员工进行建模,然后从 JDK 的内置堆栈中推送和弹出它们,直到您真正了解操作,然后再尝试创建自己的操作。

于 2012-08-04T06:59:15.213 回答