前提・実現したいこと
Javaを用いて簡単な登録システムを作成しています。ソースコードは「Registration.java」「RentalMain.java」「User.java」の三種類で構成されています。「Registration.java」の中のRegistrationクラスのprintSignin()メソッドの中にあるScannerインスタンスの出力が重複してしまいます。
以下の写真のように動作して欲しいです。
発生している問題・エラーメッセージ
Enter 1 or 2 (1. New Registration 2. Login): 1 You entered option 1 ********************** Enter Details ********************** User Name: Password: 1 // <= ここが重なって表示されてしまっている。 isVIP(true/false)1 Registration Successful
Registration.java
Registration.java
1 2/* 3 * This file is the registration class for the Vehicle Rental System. 4 * 5 * 6 */ 7 8package model; 9 10import java.io.BufferedReader; 11import java.io.IOException; 12import java.nio.file.Files; 13import java.nio.file.Path; 14import java.nio.file.Paths; 15import java.util.Map; 16import java.util.Scanner; 17import utility.FileLoader; 18 19/** 20 * Represents User Sign-in Object. 21 * 22 * Methods of this class throw NullPointerException if required parameters are null. 23 * 24 * @author Ayumu 25 * @version 1.0 26 */ 27 28public class Registration { 29 30 /** 31 * User Storage File. 32 */ 33 public static final String USERFILE_NAME = "./resources/registeredusers.txt"; 34 35 /** 36 * The registered user list for signin. 37 */ 38 private final Map<String, User> myUserList; 39 40 /** 41 * Constructs a sigin/registration system. 42 * 43 * 44 */ 45 public Registration() { 46 myUserList = FileLoader.readItemsFromFile(USERFILE_NAME); 47 } 48 49 /** 50 * getter for myUserList. 51 * 52 * @return myUserList 53 */ 54 public Map<String, User> getMyUserList() { 55 return myUserList; 56 } 57 58 /** 59 * display sign-in or registration options. 60 */ 61 public void printSignin() { 62 63 final Scanner input = new Scanner(System.in); 64 System.out.print("Enter 1 or 2 (1. New Registration 2. Login): "); 65 final int intVal1 = input.nextInt(); 66 if (intVal1 == 1) { // Registration 67 System.out.println("You entered option 1"); 68 System.out.println(); 69 System.out.println("**********************"); 70 System.out.println(" Enter Details"); 71 System.out.println("**********************"); 72 73 74 System.out.print("User Name: "); 75 String uName = input.nextLine(); 76 System.out.print("Password: "); 77 String uPass = input.nextLine(); 78 79 if (login(uName, uPass)) { //check if the user is already exist or not. 80 while (login(uName, uPass)) { 81 System.out.print("User already exist, enter different user name: "); 82 uName = input.nextLine(); 83 System.out.print("Password: "); 84 uPass = input.nextLine(); 85 } 86 } 87 88 System.out.print("isVIP(true/false)"); 89 final String uVIP = input.nextLine(); 90 91 // create a new user from given information 92 final User newUser = new User(uName, uPass, Boolean.valueOf(uVIP)); 93 // register the User 94 if (register(newUser)) { 95 System.out.println("Registration Successful"); 96 } else { 97 System.out.println("Registration Failed"); 98 99 } 100 101 } else if (intVal1 == 2) { // Login 102 System.out.println("You entered option 2"); 103 System.out.println(); 104 System.out.println("**********************"); 105 System.out.println(" Enter Details"); 106 System.out.println("**********************"); 107 108 // Login 109 System.out.print("User Name: "); 110 String uName = input.nextLine(); 111 System.out.print("Password: "); 112 String uPass = input.nextLine(); 113 114 if (login(uName, uPass)) { 115 System.out.println("Login Successful"); 116 } else { 117 System.out.println("Wrong Credentials"); 118 while (login(uName, uPass)) { //loop until a user type the correct credentials. 119 System.out.print("User Name: "); 120 uName = input.nextLine(); 121 System.out.print("Password: "); 122 uPass = input.nextLine(); 123 } 124 } 125 } else { 126 System.out.println("Please select 1 or 2!"); 127 printSignin(); 128 } 129 input.close(); 130 } 131 132 /** 133 * Verify Sign-in procedure. 134 * 135 * @param theUsername username for sign-in 136 * @param thePassword password for signin 137 * @return sign-in success 138 */ 139 public boolean login(final String theUsername, final String thePassword) { 140 141 final Path file = Paths.get(USERFILE_NAME); 142 try (BufferedReader br = Files.newBufferedReader(file)) { 143 String line; 144 while ((line = br.readLine()) != null) { 145 final String[] tokens = line.split(FileLoader.SPLIT_TOKEN); 146 final String uName = tokens[0]; 147 final String uPwd = tokens[1]; 148 final boolean isVIP = Boolean.parseBoolean(tokens[2]); //just in case I use it maybe 149 // return when the name and pass are matched. 150 return uName.equals(theUsername) && uPwd.equals(thePassword); 151 } 152 } catch (final IOException e) { 153 System.out.println(e); 154 } 155 // return false when the name and pass are not matched for all lines. 156 return false; 157 } 158 159 /** 160 * Adds a user to the registered user list. 161 * 162 * @param theUser an order to add to this shopping cart 163 * @return true/false returns if registration is successful 164 */ 165 public boolean register(final User theUser) { 166 try { 167 FileLoader.writeUserToFile(USERFILE_NAME, theUser); 168 return true; 169 170 } catch (final NullPointerException e) { 171 e.printStackTrace(); 172 return false; 173 } 174 } 175 176 /** 177 * Empties the user list. 178 */ 179 public void clear() { 180 myUserList.clear(); 181 } 182 183 @Override 184 /** 185 * String representation of the object 186 * 187 */ 188 public String toString() { 189 final StringBuilder sb = new StringBuilder(); 190 sb.append("Registered UserList {"); 191 for (String key : myUserList.keySet()) { 192 sb.append(key + "=" + myUserList.get(key) + ", "); 193 } 194 sb.append("}"); 195 final String msg = sb.toString(); 196 return msg; 197 } 198 199} 200 201
javaを触るのは久しぶりで、コードに不備等や改善点が多々見受けられることかと思いますが、そちらの方も是非教えていただけると幸いです。
回答1件
あなたの回答
tips
プレビュー