Demonstration.java and Maths.java files.  
Part 1
You are to write a StockItem class.   The attributes of StockItem are:<!--[if !supportLists]-->·         <!--[endif]-->A unique Identifier which is a String.<!--[if !supportLists]-->·         <!--[endif]-->A name which is a String<!--[if !supportLists]-->·         <!--[endif]-->A stock quantity which is a whole number<!--[if !supportLists]-->·         <!--[endif]-->A unit price which is a double The methods of StockItem are:<!--[if !supportLists]-->·         <!--[endif]-->A constructor which allows input of attribute values<!--[if !supportLists]-->·         <!--[endif]-->display()  to show the record in a window<!--[if !supportLists]-->·         <!--[endif]-->a toString() method All input should be validated using the try and catch mechanismdisplay() should use a textArea and showMessageDialog() method for output.The toString() method will be required as usual. Note that you will not need tio.* Part 2 Write a SList class, a list of StockItemsSlist should use only javax.swing components.All input and output should be through the gui interface.All input should be validated by the try catch mechanism.In addition the operations of adding to a full list and finding non existent objects, should also use the try catch method. Part 3You must write a FrontEnd class which uses the other classes and GUIInput.java to produce a menu which presents the options of <!--[if !supportLists]-->·         <!--[endif]-->Add to the list<!--[if !supportLists]-->·         <!--[endif]-->Display the list<!--[if !supportLists]-->·         <!--[endif]-->Quit Part 4The functionality of delete() should be added to the Slist class and a new option should be added to the FrontEnd class to add the delete() function. Delete should request a reference, and the Item with that reference should be removed.

解决方案 »

  1.   

    Part1
    import javax.swing.*;
    public class StockItem 

        private  final String str;
        private  String name;
        private  int    num;
        private  double price; 
        public StockItem(){
         this.str=JOptionPane.showInputDialog("please input a String!");
         this.name=JOptionPane.showInputDialog("please input a name!");
         this.num=Integer.parseInt(JOptionPane.showInputDialog("please input a Integer!"));
         this.price=Double.parseDouble(JOptionPane.showInputDialog("please input a price!"));
        }
        public void display(){
         JOptionPane.showMessageDialog(null,""+str+" "+name+" "+num+" "+price,"nei bu xin xi!",JOptionPane.DEFAULT_OPTION);
        }
        public static void main(String[] args)
        {
            StockItem a=new StockItem();
            a.display();
        }
        public String toString(){
         return super.toString();
     }
    }
      

  2.   

    我把程序改了一下
    import javax.swing.*;
    import java.io.*;
    public class StockItem 

        private  final String str;
        private  String a=" ";
        private  String name;
        private  int    num;
        private  double price; 
        public StockItem(){
           BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
           try {
          System.out.println("please input a String!");
          
          a=dataIn.readLine();
          
          System.out.println("please input a name!");
          name=dataIn.readLine();
          System.out.println(name);
          System.out.println("please input a Integer!");
          num=Integer.parseInt(dataIn.readLine());
          System.out.println("please input a price(double)!");
          price=Double.parseDouble(dataIn.readLine());
     }
     catch (IOException ex) {
    System.out.println(ex);
     }
         str=a;
        }
        public void display(){
         JOptionPane.showMessageDialog(null,""+str+" "+name+" "+num+" "+price,"nei bu xin xi!",JOptionPane.DEFAULT_OPTION);
        }
        public static void main(String[] args)
        {
            StockItem a=new StockItem();
            a.display();
        }
        public String toString(){
         return super.toString();
     }
    }