Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的选项和验证_Java - Fatal编程技术网

Java中的选项和验证

Java中的选项和验证,java,Java,我想在我的系统上添加一个新选项,其中我想添加两个文本文件,rental.txt和customer.txt。每个文本中都有客户的身份证号码、他们需要的录像带和价格 我想把它作为我的代码的一个选项。现在我有: 添加客户 租金申报表 视图列表 搜寻 出口 我想把这个作为我的第六个选择。比如说,我订购了一个视频,它会显示价格,让我确认价格,以及我是否要购买它 这是我目前的代码: import java.io.*; import java.util.ArrayList; import

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

我想把它作为我的代码的一个选项。现在我有:

  • 添加客户
  • 租金申报表
  • 视图列表
  • 搜寻
  • 出口
  • 我想把这个作为我的第六个选择。比如说,我订购了一个视频,它会显示价格,让我确认价格,以及我是否要购买它

    这是我目前的代码:

      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("                  - - - - - - - - - - - - - - - - - - - - - - - - -");
            }
    
    }
    
    import java.io.*;
    导入java.util.ArrayList;
    导入静态java.lang.System.out;
    公共类租赁系统{
    静态BufferedReader输入=新的BufferedReader(新的InputStreamReader(System.in));
    静态文件=新文件(“File.txt”);
    静态ArrayList=新建ArrayList();
    静态int行;
    公共静态void main(字符串[]args)引发异常{
    导言();
    系统输出打印(“\n\n”);
    登录();
    System.out.print(“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n”);
    导言();
    字符串重复;
    做{
    loadFile();
    System.out.print(“\n您想做什么?\n\n”);
    System.out.print(“\n----”;
    System.out.print(“\n\n | 1.添加客户| 2.租金返还| \n”);
    System.out.print(“\n----”;
    System.out.print(“\n\n | 3.查看列表| 4.搜索| \n”);
    System.out.print(“\n----”;
    系统输出打印(“\n\n | 5.退出| \n”);
    系统输出打印(“\n---”);
    系统输出打印(“\n\n选择:”);
    int choice=Integer.parseInt(input.readLine());
    开关(选择){
    案例1:
    writeData();
    打破
    案例2:
    rentData();
    打破
    案例3:
    视图列表();
    打破
    案例4:
    搜索();
    打破
    案例5:
    System.out.println(“再见!”);
    系统出口(0);
    违约:
    系统输出打印(“无效选择:”);
    打破
    }
    系统输出打印(“\n是否执行其他任务?[y/n]”);
    repeat=input.readLine();
    }while(重复.equals(“y”));
    if(重复!=“y”)System.out.println(“\nGoodbye!”);
    }
    public static void writeData()引发异常{
    系统输出打印(“\n名称:”);
    字符串cname=input.readLine();
    系统输出打印(“地址:”);
    字符串add=input.readLine();
    系统输出打印(“电话号码:”);
    字符串pno=input.readLine();
    系统输出打印(“租金金额:”);
    字符串ramount=input.readLine();
    系统输出打印(“磁带编号:”);
    字符串tno=input.readLine();
    系统输出打印(“标题:”);
    字符串标题=input.readLine();
    系统输出打印(“借用日期:”);
    字符串dborrow=input.readLine();
    系统输出打印(“到期日:”);
    字符串ddate=input.readLine();
    创建行(cname、add、pno、ramount、tno、title、DBORROWER、ddate);
    rentData();
    }
    公共静态void createLine(字符串名称、字符串地址、字符串电话、字符串出租、字符串tapenumber、字符串标题、字符串借用、字符串到期)引发异常{
    FileWriter fw=新的FileWriter(文件,true);
    写(“\n姓名:“+name+”\n地址:“+address+”\n电话号码:“+phone+”\n电话号码:“+rentalamount+”\n电话号码:“+tapenumber+”\n标题:“+title+”\n借用日期:“+borroed+”\n借用日期:“+due+”:\r\n”);
    fw.close();
    }
    公共静态void loadFile()引发异常{
    试一试{
    list.clear();
    FileInputStream fStream=新的FileInputStream(文件);
    BufferedReader br=新的BufferedReader(新的InputStreamReader(fStream));
    行=0;
    while(br.ready())
    {
    list.add(br.readLine());
    行++;
    }
    br.close();
    }捕获(例外e){
    System.out.println(“列表尚未加载”);
    }
    }
    公共静态void viewList(){
    System.out.print(“\n~~~~~~~~~”;
    系统输出打印(“|所有客户列表|”);
    System.out.print(“~~~~~~~~~~~~”);
    
    对于(int i=0;i我建议进行枚举:

    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。

    我想添加另一个名为“租电影”的选项在那里,我会看到电影的id号、片名和价格。它会问我应该买以下哪一个,并确认价格。我在其中添加了另一个公共静态void类…但我不知道如何完成。如果你使用像mysql2这样的数据库,我会容易得多,所以你不知道添加2的逻辑问题是文本文件还是仅仅添加一个选项?添加它们并为它们生成函数。