Java中的选项和验证

时间:2012-12-20 04:37:07

标签: java

我想在我的系统上添加一个新选项,我想添加两个文本文件,包括rental.txt和customer.txt。每个文本里面都有客户的身份证号码,他们需要的录像带和价格。

我想将它作为我的代码的选项。现在我有:

  1. 添加客户
  2. 租金回报
  3. 查看列表
  4. 搜索
  5. 退出
  6. 我想将此添加为我的第六个选项。比方说,我订购了一个视频,它会显示价格并让我确认价格以及我是否打算购买。

    这是我目前的代码:

      import java.io.*;
        import java.util.ArrayList;
        import static java.lang.System.out;
    
        public class RentalSystem{
        static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        static File file = new File("file.txt");
        static ArrayList<String> list = new ArrayList<String>();
        static int rows;
    
        public static void main(String[] args) throws Exception{
            introduction();
            System.out.print("\n\n");
            login();
            System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            introduction();
            String repeat;
            do{
                loadFile();
                System.out.print("\nWhat do you want to do?\n\n");
                System.out.print("\n                    - - - - - - - - - - - - - - - - - - - - - - -");
                System.out.print("\n\n                    |     1. Add customer    |   2. Rent Return |\n");
                System.out.print("\n                    - - - - - - - - - - - - - - - - - - - - - - -");
                System.out.print("\n\n                    |     3. View List       |   4. Search      |\n");
                System.out.print("\n                    - - - - - - - - - - - - - - - - - - - - - - -");
                System.out.print("\n\n                                             |   5. Exit        |\n");
                System.out.print("\n                                              - - - - - - - - - -");
                System.out.print("\n\nChoice:");
                int choice = Integer.parseInt(input.readLine());
                switch(choice){
                    case 1:
                        writeData();
                        break;
                    case 2:
                        rentData();
                        break;
                    case 3:
                        viewList();
                        break;
                    case 4:
                        search();
                        break;
                    case 5:
                        System.out.println("Goodbye!");
                        System.exit(0);
                    default:
                        System.out.print("Invalid choice: ");
                        break;
                }
                System.out.print("\nDo another Task? [y/n] ");
                repeat = input.readLine();
            }while(repeat.equals("y"));
    
            if(repeat!="y") System.out.println("\nGoodbye!");
    
        }
    
        public static void writeData() throws Exception{
            System.out.print("\nName: ");
            String cname = input.readLine();
            System.out.print("Address: ");
            String add = input.readLine();
            System.out.print("Phone no.: ");
            String pno = input.readLine();
            System.out.print("Rental amount: ");
            String ramount = input.readLine();
            System.out.print("TapeNumber: ");
            String tno = input.readLine();
            System.out.print("Title: ");
            String title = input.readLine();
            System.out.print("Date Borrowed: ");
            String dborrowed = input.readLine();
            System.out.print("Due Date: ");
            String ddate = input.readLine();
            createLine(cname, add, pno, ramount,tno, title, dborrowed, ddate);
            rentData();
        }
    
        public static void createLine(String name, String address, String phone , String rental, String tapenumber, String title, String borrowed, String due) throws Exception{
            FileWriter fw = new FileWriter(file, true);
            fw.write("\nName: "+name + "\nAddress: " + address +"\nPhone no.: "+ phone+"\nRentalamount: "+rental+"\nTape no.: "+ tapenumber+"\nTitle: "+ title+"\nDate borrowed: "+borrowed +"\nDue date: "+ due+":\r\n");
            fw.close();
        }
    
        public static void loadFile() throws Exception{
            try{
                list.clear();
                FileInputStream fStream = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fStream));
                rows = 0;
                while( br.ready())
                {
                    list.add(br.readLine());
                    rows++;
                }
                br.close();
            } catch(Exception e){
                System.out.println("List not yet loaded.");
            }
        }
    
        public static void viewList(){
            System.out.print("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            System.out.print(" |List of all Costumers|");
            System.out.print("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            for(int i = 0; i <rows; i++){
                System.out.println(list.get(i));
            }
        }
            public static void rentData()throws Exception
        {   System.out.print("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            System.out.print(" |Rent Data List|");
            System.out.print("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            System.out.print("\nEnter Customer Name: ");
            String cname = input.readLine();
            System.out.print("Date Borrowed: ");
            String dborrowed = input.readLine();
            System.out.print("Due Date: ");
            String ddate = input.readLine();
            System.out.print("Return Date: ");
            String rdate = input.readLine();
            System.out.print("Rent Amount: ");
            String ramount = input.readLine();
    
            System.out.print("You pay:"+ramount);
    
    
        }
        public static void search()throws Exception
        {   System.out.print("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            System.out.print(" |Search Costumers|");
            System.out.print("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
            System.out.print("\nEnter Costumer Name: ");
            String cname = input.readLine();
            boolean found = false;
    
            for(int i=0; i < rows; i++){
                String temp[] = list.get(i).split(",");
    
                if(cname.equals(temp[0])){
                System.out.println("Search Result:\nYou are " + temp[0] + " from " + temp[1] + "."+ temp[2] + "."+ temp[3] + "."+ temp[4] + "."+ temp[5] + " is " + temp[6] + "."+ temp[7] + " is " + temp[8] + ".");
                    found = true;
                }
            }
    
            if(!found){
                System.out.print("No results.");
            }
    
        }
    
            public static boolean evaluate(String uname, String pass){
            if (uname.equals("admin")&&pass.equals("12345")) return true;
            else return false;
        }
    
        public static String login()throws Exception{
            BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
            int counter=0;
            do{
                System.out.print("Username:");
                String uname =input.readLine();
                System.out.print("Password:");
                String pass =input.readLine();
    
                boolean accept= evaluate(uname,pass);
    
                if(accept){
                    break;
                    }else{
                        System.out.println("Incorrect username or password!");
                        counter ++;
                        }
            }while(counter<3);
    
                if(counter !=3) return "Login Successful";
                else return "Login Failed";
                }
            public static void introduction() throws Exception{
    
            System.out.println("                  - - - - - - - - - - - - - - - - - - - - - - - - -");
            System.out.println("                  !                  R E N T A L                  !");
            System.out.println("                   ! ~ ~ ~ ~ ~ !  =================  ! ~ ~ ~ ~ ~ !");
            System.out.println("                  !                  S Y S T E M                  !");
            System.out.println("                  - - - - - - - - - - - - - - - - - - - - - - - - -");
            }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我建议制作一个枚举:

public enum ActionEnum{
    AddCustomer(1) { 
              public void action(){
                 RentalSystem.writeData();
               }
     },
    RentReturn(2){ 
              public void action(){
                 RentalSystem.rentData();
               }
     },
    ViewList(3){ 
              public void action(){
                 RentalSystem.viewList();
               }
     },
    Search(4){ 
              public void action(){
                 RentalSystem.search();
               }
     },
    Exit(5){ 
              public void action(){
                 RentalSystem.exit();
               }
     };
     private final int index;
     private ActionEnum(int index){
         this.index = index;
     }


    public abstract void action();

    public static ActionEnum fromIndex(int ix){
            for(ActionEnum action:values()){
                            if(action.index==ix) {
                                            return action;
                            }
           }
       return null;
     }

    };

在你的代码中,你打电话而不是开关:

int choice = Integer.parseInt(input.readLine());

ActionEnum actionEnum = ActionEnum.fromIndex(choice);

if(actionEnum==null){

 System.out.print("Invalid choice: ");

}else{

  actionEnum.action();

}

现在您只能扩展ActionEnum类 - 只修改一个代码位。 请看“有效Java:编程语言指南”Joshua Bloch。