佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1322|回复: 12

可以拜托帮我看看我的java code吗^_^

[复制链接]
发表于 26-6-2010 07:56 PM | 显示全部楼层 |阅读模式
java assignment的 due date快到了><"
希望佳礼的各位programming前辈们可以帮我看看我的code
请给我一些提示 多多指教

  1. import java.util.Scanner; //import java.util.Scanner class to read user input
  2. import java.util.InputMismatchException;

  3. public class Main  //public class that contain public static void main
  4. {

  5.            private static Scanner scan = new Scanner(System.in);
  6.     private static Assest[] ass = new Assest[100]; // array of objects

  7.     private static int choice = 0;//choice is used to control switch statement
  8.     private static int choice2 = 0;// choice 2 is used to control switch statement
  9.     private static int a, b; //a and b are used to control for loop
  10.     private static int counter = 0; //how many assests user want to enter, and which assest user want to add, amend, write-off, searching
  11.     private static String question; //question ask user is he/she really want to written-off that assest
  12.     private static boolean loop = true; //determine anymore input is needed
  13.    
  14.     private static String mainID, mainDS, mainDT;
  15.     private static int mainOD = 0;
  16.     private static double mainCP = 0.0;

  17.     public static void main(String[] args)
  18.     {
  19.                 for(a = 0; a < 100 ; a++)
  20.                 {
  21.                         ass[a] = new Assest(); // initialize all assests as empty assests by using default constructor
  22.                 }

  23.         do
  24.         {
  25.                 try
  26.                 {
  27.                         System.out.println("How many assest do you want to enter (only 1-100)? : "); //prompt user how many assest user want to enter
  28.                         counter = scan.nextInt();
  29.                         loop=false;
  30.                 }
  31.                 catch(InputMismatchException e)
  32.                 {
  33.                         System.out.println("Error Occured: only accept number without decimal");
  34.                         scan.nextLine();
  35.                         loop = true;
  36.                   }
  37.         }while(loop || counter < 1 || counter > 100);

  38.         for(a = 0; a < counter; a++) //prompt user to key-in all the details of assest
  39.         {
  40.                 scan.nextLine();
  41.                 System.out.println((a+1) + " assest - assest ID :");
  42.                 mainID = scan.nextLine();
  43.                 System.out.println((a+1) + " assest - description :");
  44.                 mainDS = scan.nextLine();
  45.                 System.out.println((a+1) + " assest - date purchased :");
  46.                 mainDT = scan.nextLine();
  47.                 do
  48.                 {
  49.                         try
  50.                         {
  51.                                 System.out.println((a+1) + " assest - order number :");
  52.                                 mainOD = scan.nextInt();
  53.                                 System.out.println((a+1) + " assest - cost price :");
  54.                                 mainCP = scan.nextDouble();
  55.                                 loop=false;
  56.                         }
  57.                         catch(InputMismatchException e)
  58.                         {
  59.                                 System.out.println("Error Occured: only numbers are accepted");
  60.                                 scan.nextLine();
  61.                                 loop=true;
  62.                           }
  63.                        }while(loop);
  64.                 System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  65.                 ass[a] = new Assest(mainID , mainDS , mainDT , mainOD ,mainCP);
  66.         }

  67.                 //do-while to control the remaining program, it will continue running unless user change the value of choice to -1
  68.                 do
  69.                 {
  70.                         do
  71.                         {
  72.                                 try
  73.                                 {
  74.                                        System.out.println("\n----------------------------");
  75.                                        System.out.println("1. add new assest (1-100)");
  76.                                        System.out.println("2. amend an assest (1-100)");
  77.                                        System.out.println("3. search assest (1-100)");
  78.                                        System.out.println("4. write-off an asset (1-100)");
  79.                                        System.out.println("5. list all assest (1-100)");
  80.                                        System.out.println("6. equality checking (1-100)");
  81.                                        System.out.println(">>>There is " + Assest.getNo() + " Assest stored<<<");
  82.                                        System.out.println("---------(-1 to quit)--------");
  83.                                        choice = scan.nextInt();
  84.                                        loop=false;
  85.                                        if(choice == -1)
  86.                                                break;
  87.                                 }
  88.                                 catch(InputMismatchException e)
  89.                         {
  90.                                 System.out.println("Error Occured: only numbers are accepted");
  91.                                 scan.nextLine();
  92.                                 loop=true;
  93.                           }
  94.                         }while(loop);

  95.                         switch (choice)
  96.                         {

  97.                                case 1: Add();
  98.                                break;

  99.                                case 2: Amend();
  100.                                break;

  101.                                case 3: Search();
  102.                                break;

  103.                                case 4: WriteOff();
  104.                                break;

  105.                                case 5: ListAll();
  106.                                break;

  107.                                case 6: Equality();
  108.                                break;
  109.                        }
  110.                }while(choice != -1); // while user didn't enter -1, this program will continue running
  111.                System.out.println("------------------\nThe End , Good bye\n------------------\n");
  112.     }

  113.     public static void Add()
  114.     {
  115.             do
  116.             {
  117.                 try
  118.                 {
  119.                         System.out.println("How many assest do you want to enter (only 1-100 or -1 to leave)? : "); //allow to add new assest
  120.                         counter = scan.nextInt();
  121.                         loop=false;
  122.                         if(counter == -1)
  123.                                 break;
  124.                         if(counter > 0 && counter < 101){
  125.                                 if(ass[counter-1].getST().equals("Written-Off")) //don't allow user to add existing assest
  126.                                        {
  127.                                                scan.nextLine();
  128.                                                System.out.println((counter) + " assest - assest ID :");
  129.                                         mainID = scan.nextLine();
  130.                                         System.out.println((counter) + " assest - description :");
  131.                                               mainDS = scan.nextLine();
  132.                                         System.out.println((counter) + " assest - date purchased :");
  133.                                         mainDT = scan.nextLine();
  134.                                         do
  135.                                         {
  136.                                                 try
  137.                                                 {
  138.                                                         System.out.println((counter) + " assest - order number :");
  139.                                                         mainOD = scan.nextInt();
  140.                                                         System.out.println((counter) + " assest - cost price :");
  141.                                                         mainCP = scan.nextDouble();
  142.                                                         loop=false;
  143.                                                 }
  144.                                                 catch(InputMismatchException e)
  145.                                                 {
  146.                                                         System.out.println("Error Occured: only numbers are accepted");
  147.                                                         scan.nextLine();
  148.                                                         loop=true;
  149.                                                   }
  150.                                                }while(loop);
  151.                                         System.out.println("@@@@@@@@@@@@@@@@@@@@@>>ADDED<<@@@@@@@@@@@@@@@@@@@@@");
  152.                                         ass[counter-1] = new Assest(mainID , mainDS , mainDT , mainOD , mainCP);
  153.                                        }
  154.                                        else
  155.                                                System.out.println("\n!!!!!Not allow to add existing assest!!!!!");
  156.                                        }
  157.                            }
  158.                       catch(InputMismatchException e)
  159.                 {
  160.                         System.out.println("Error Occured: only accept number without decimal");
  161.                         scan.nextLine();
  162.                         loop = true;
  163.                   }
  164.                }while(loop || counter < 1 || counter > 100);
  165.     }
复制代码
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 26-6-2010 07:57 PM | 显示全部楼层
接下去上面的code

  1. public static void Amend(){
  2.             do
  3.                {
  4.                 try
  5.                 {
  6.                         System.out.println("Which assest do you want to modify (only 1-100 or -1 to leave)? : "); //allow user to amend existing assest
  7.                         counter = scan.nextInt();
  8.                         loop=false;
  9.                         if(counter == -1)
  10.                                 break;
  11.                         if(counter > 0 && counter < 101)
  12.                                 {
  13.                                 System.out.println("\n" + "---------- " + (counter) + " ASSEST ----------");
  14.                                        ass[counter-1].list_search();
  15.                                        if(ass[counter-1].getST().equals("ON")) //don't allow user to amend not existing assest
  16.                                        {
  17.                                                do
  18.                                                {
  19.                                                        System.out.println("Which detail do you want to modify : 1.ID 2.description 3.date purchased 4.order number 5.cost price (-1 to quit):");
  20.                                                        choice2 = scan.nextInt();
  21.                                                        switch (choice2)
  22.                                                        {
  23.                                                                case 1: System.out.println((counter) + " assest - assest ID : ");
  24.                                                                                scan.nextLine();
  25.                                                                                mainID = scan.nextLine();
  26.                                                                                ass[counter-1].setID(mainID);
  27.                                                                                System.out.println("You have succesful amended assest " + (counter) + "  ID");
  28.                                                                break;
  29.                                                                case 2:        System.out.println((counter) + " assest - description : ");
  30.                                                                                scan.nextLine();
  31.                                                                                mainDS = scan.nextLine();
  32.                                                                                ass[counter-1].setDS(mainDS);
  33.                                                                                System.out.println("You have succesful amended assest " + (counter) + " description");
  34.                                                                break;
  35.                                                                case 3: System.out.println((counter) + " assest - date purchased : ");
  36.                                                                                scan.nextLine();
  37.                                                                                mainDT = scan.nextLine();
  38.                                                                                ass[counter-1].setDT(mainDT);
  39.                                                                                System.out.println("You have succesful amended assest " + (counter) + " date purchased");
  40.                                                                break;
  41.                                                                case 4: do
  42.                                                                         {
  43.                                                                                 try
  44.                                                                                 {
  45.                                                                                         System.out.println((counter) + " assest - order number : ");
  46.                                                                                                mainOD = scan.nextInt();
  47.                                                                                                ass[counter-1].setOD(mainOD);
  48.                                                                                                System.out.println("You have succesful amended assest " + (counter) + " order number");
  49.                                                                                         loop=false;
  50.                                                                                 }
  51.                                                                                 catch(InputMismatchException e)
  52.                                                                                 {
  53.                                                                                         System.out.println("Error Occured: only numbers are accepted");
  54.                                                                                         scan.nextLine();
  55.                                                                                         loop=true;
  56.                                                                                   }
  57.                                                                                }while(loop);
  58.                                                                break;
  59.                                                                case 5: do
  60.                                                                         {
  61.                                                                                 try
  62.                                                                                 {
  63.                                                                                         System.out.println((counter) + " assest - cost price : ");
  64.                                                                                                mainCP = scan.nextDouble();
  65.                                                                                                ass[counter-1].setCP(mainCP);
  66.                                                                                                System.out.println("You have succesful amended assest " + (counter) + " cost price");
  67.                                                                                         loop=false;
  68.                                                                                 }
  69.                                                                                 catch(InputMismatchException e)
  70.                                                                                 {
  71.                                                                                         System.out.println("Error Occured: only numbers are accepted");
  72.                                                                                         scan.nextLine();
  73.                                                                                         loop=true;
  74.                                                                                   }
  75.                                                                                }while(loop);
  76.                                                                break;
  77.                                                        }
  78.                                                }while(choice2 != -1);
  79.                                        }
  80.                                        else
  81.                                                System.out.println("\n!!!!!Not allow to amend empty assest!!!!!");
  82.                         }
  83.                 }
  84.                 catch(InputMismatchException e)
  85.                 {
  86.                         System.out.println("Error Occured: only accept number without decimal");
  87.                         scan.nextLine();
  88.                                loop = true;
  89.                   }
  90.         }while(loop || counter < 1 || counter > 100);
  91.     }

  92.     public static void Search(){
  93.             do
  94.         {
  95.                 try
  96.                 {
  97.                         System.out.println("Which assest are you looking for (only 1-100)? : "); // search for an assest
  98.                         counter = scan.nextInt();
  99.                         if(counter > 0 && counter < 101)
  100.                         {
  101.                                        System.out.println("\n" + "---------- " + (counter) + " ASSEST ----------");
  102.                                        ass[counter-1].list_search();
  103.                         }
  104.                         else
  105.                                 System.out.println("Out of range");
  106.                         loop=false;
  107.                 }
  108.                 catch(InputMismatchException e)
  109.                 {
  110.                         System.out.println("Error Occured: only accept number without decimal");
  111.                         scan.nextLine();
  112.                         loop = true;
  113.                   }
  114.                 }while(loop || counter < 1 || counter > 100);
  115.     }

  116.     public static void WriteOff(){
  117.             do
  118.         {
  119.                 try
  120.                 {
  121.                         System.out.println("Which assest do you want to write-off (only 1-100 or -1 to leave) : "); //write-off an assest
  122.                                counter = scan.nextInt();
  123.                                if(counter == -1)
  124.                                        break;
  125.                                if(counter > 0 && counter < 101)
  126.                                {
  127.                                        if(ass[counter-1].getST().equals("ON"))
  128.                                        {
  129.                                                System.out.println("\n" + "---------- " + (counter) + " ASSEST ----------");
  130.                                                ass[counter-1].list_search();
  131.                                                System.out.println("Warning!!! you are going to write-off assest " + counter);
  132.                                                System.out.println("Are you sure u want to do this ? (y/n) : ");
  133.                                                question = scan.next();
  134.                                                if(question.equals("y"))
  135.                                                {
  136.                                                        ass[counter-1] = new Assest();
  137.                                                        System.out.println("You have been succesful written-off assest " + counter);
  138.                                                }
  139.                                        }
  140.                                        else
  141.                                                System.out.println("\n!!!!!Not allow to write-off empty assest!!!!!");
  142.                                         loop=false;
  143.                                }
  144.                        }
  145.                        catch(InputMismatchException e)
  146.                        {
  147.                                System.out.println("Error Occured: only accept number without decimal");
  148.                                scan.nextLine();
  149.                                loop = true;
  150.                  }
  151.                 }while(loop || counter < 1 || counter > 100);
  152.     }

  153.     public static void ListAll(){
  154.             for(a = 0; a < 100 ; a++)
  155.                { //list all hundred assest
  156.                         if(ass[a].getST().equals("ON"))
  157.                         {
  158.                                 System.out.println("\n" + "########## " + (a+1) + " ASSEST ##########");
  159.                                 ass[a].list_search();
  160.                                 System.out.println("##############################");
  161.                         }
  162.                }
  163.     }

  164.     public static void Equality(){
  165.             for(a = 0; a < 50 ; a++)
  166.                { // equality checking
  167.                        for(b = a + 1; b < 100; b++)
  168.                        {
  169.                                if( ass[a].getID().equals(ass[b].getID()) && (ass[a].getST().equals("ON")))
  170.                                {
  171.                                        Assest.setBLtrue(); // if equal boolean is true
  172.                                        System.out.println("There is atleast 2 assests contain the same assest ID");
  173.                                        break;
  174.                                }
  175.                        }
  176.                        if(Assest.getBL() == true) //conditional statement that stop this nested loop when found one !!
  177.                                break;
  178.                }
  179.                if(Assest.getBL() == false)
  180.                        System.out.println("There is no assest contain the same assest ID");
  181.                Assest.setBLfalse();
  182.     }
  183. }
复制代码
回复

使用道具 举报

 楼主| 发表于 26-6-2010 07:59 PM | 显示全部楼层
另外一个含有constructor的class

  1. class Assest
  2. {
  3.     private String assestID, description, datePurchased, status;
  4.     private int orderNumber;
  5.     private double costPrice;
  6.     private static boolean equal;
  7.     private static int assestNo = 0 ;
  8. //no argument constructor that used to written-off or set assest to empty assest
  9.     public Assest()
  10.     {
  11.             assestID = "Empty";
  12.             description = "Empty";
  13.             datePurchased = "Empty";
  14.             status = "Written-Off";
  15.             orderNumber = 0;
  16.             costPrice = 0;
  17.             if(assestNo != 0){ //while the beginning of initializing (assestNo is equal to 0 no need to minus it)
  18.                     assestNo--;
  19.             }
  20.     }
  21. //---parameter constructor that used to set the value for all the details---
  22.     public Assest(String ID, String DS, String DT , int OD, double CP)
  23.     {
  24.             assestID = ID;
  25.             description = DS;
  26.             datePurchased = DT;
  27.                 status = "ON";
  28.             orderNumber = OD;
  29.             costPrice = CP;
  30.             assestNo++;
  31.     }
  32. //----------------All 8 getter methods, some is not in use----------------
  33.     public static boolean getBL()
  34.     {
  35.                 return equal;
  36.         }
  37.         public static int getNo()
  38.         {
  39.                 return assestNo;
  40.         }
  41.     public String getID()
  42.         {
  43.             return assestID;
  44.     }
  45.     public String getDS()
  46.         {
  47.         return description;
  48.     }
  49.     public String getDP()
  50.         {
  51.         return datePurchased;
  52.     }
  53.     public int getOD()
  54.         {
  55.         return orderNumber;
  56.     }
  57.     public double getCP()
  58.         {
  59.         return costPrice;
  60.     }
  61.         public String getST()
  62.         {
  63.         return status;
  64.     }
  65. //-------------7 setter methods to change the value of private datas---------
  66.     public static void setBLtrue()
  67.     {
  68.         equal = true;
  69.     }
  70.     public static void setBLfalse()
  71.     {
  72.         equal = false;
  73.     }
  74.     public void setID(String ID)
  75.     {
  76.             assestID = ID;
  77.     }
  78.     public void setDS(String DS)
  79.     {
  80.             description = DS;
  81.     }
  82.     public void setDT(String DT)
  83.     {
  84.             datePurchased = DT;
  85.     }
  86.     public void setOD(int OD)
  87.     {
  88.             orderNumber = OD;
  89.     }
  90.     public void setCP(double CP)
  91.     {
  92.             costPrice = CP;
  93.     }
  94. //------------------method used to print details of one assest---------------
  95.     void list_search()
  96.     {
  97.                         System.out.println("assest - assest ID : " + assestID + "\n" +
  98.                                 "assest - description : " + description + "\n" +
  99.                                         "assest - date purchased : " + datePurchased + "\n" +
  100.                                                 "assest - order number : " + orderNumber + "\n" +
  101.                                                         "assest - cost price : " + costPrice + "\n" +
  102.                                                                 "assest - status : " + status);
  103.         }
  104. //----------------------------------------------------------------------------
  105. }
复制代码


拜托各位前辈们帮我看看谢谢^_^
回复

使用道具 举报

发表于 27-6-2010 05:49 PM | 显示全部楼层
你 的 ERROR CONSOLE 那边 出 什么 ERROR 哦 ?
回复

使用道具 举报

 楼主| 发表于 27-6-2010 06:40 PM | 显示全部楼层
本帖最后由 qiqimon5566 于 27-6-2010 06:43 PM 编辑

没有error.. 只是想知道这样的code 符合标准吗=="
因为code 只有1人在写 我已经很累了
很懒惰去加更多的command block 或者 line 或者加强了
所以我的code 连一个JFrame dialog box GUI都没有
我知道 这其实还蛮差 和 不负责任
可是我还是很希望听取各位前辈们的金玉良言
如果可以 我想交了>_<"
回复

使用道具 举报

发表于 28-6-2010 03:44 PM | 显示全部楼层
其实 你 可以 把 那些 GET - SET 写在 另外 一个 CLASS FILE 里面 ( 通常我们叫 BEAN FILE ) ..

可以的话 , 就 尽量 用 ABSTRACT 或者 INTERFACE , 因为 JAVA 的 OOP concept 不鼓励 一次过 写完 全部 CODE 在 同一个 FILE 里面
回复

使用道具 举报

Follow Us
 楼主| 发表于 5-7-2010 07:41 PM | 显示全部楼层
其实 你 可以 把 那些 GET - SET 写在 另外 一个 CLASS FILE 里面 ( 通常我们叫 BEAN FILE ) ..

可以的话 ...
兔仙人 发表于 28-6-2010 03:44 PM



谢谢你的建议
学校刚开始要教inheritance 和 abstract 所以将加在assignment2 ^^
目前的是assignment1 虽然学校教的唯一一个gui只有joptionpane
可是看到有一个人 做了全gui jtextfield jframe jmenubar 等等
再看自己的 感觉到 自己好失败/.\
回复

使用道具 举报

发表于 5-7-2010 10:25 PM | 显示全部楼层
所以你要學習看 API
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 6-7-2010 08:28 PM | 显示全部楼层
所以你要學習看 API
兔仙人 发表于 5-7-2010 10:25 PM



请问什么是API?
还没学到== wiki解释看不太懂==
回复

使用道具 举报

发表于 6-7-2010 09:40 PM | 显示全部楼层
API , 學校 是不會 教的 。

不過 我給個例子你 , 你 自己 看看

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

這個就是 STRING 的 API , 裡面 有完 全部 用法 。 只要 好好 學習 , 以後 “錢途” 不是問題 ^^
回复

使用道具 举报

 楼主| 发表于 7-7-2010 05:24 AM | 显示全部楼层
API , 學校 是不會 教的 。

不過 我給個例子你 , 你 自己 看看



這個就是 STRING 的 API , 裡 ...
兔仙人 发表于 6-7-2010 09:40 PM



谢谢你哦
我会加油的^^
那我想请问你
还有哪些是重要
又是学校不会教的吗?
回复

使用道具 举报

发表于 7-7-2010 09:10 AM | 显示全部楼层
本帖最后由 兔仙人 于 7-7-2010 09:12 AM 编辑

學校只是 教 基本 的 知識 而已 。。

例如 JAVA , 只是教基本的 用法 。 如果 想 繼續 ADVANCE , 就 得 選擇 你 要 走 的 方向 。

PS : J2SE , J2ME , J2EE 。  本仙人 認為 EE 比較有錢途
回复

使用道具 举报

 楼主| 发表于 7-7-2010 09:45 AM | 显示全部楼层
我刚刚把 j2se j2me j2ee j2fx 都下完了=_="
呵呵 想问j2ee 和 j2se有不同吗?
我是买了本 算是教得还蛮全面的java书
可是好像没教我分类 和选择@_@
谢谢你的建议哦^_^ 我也想学enterprise edition ^_^
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 27-11-2025 02:10 AM , Processed in 0.112053 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表