这是这个电话簿代码的后续行动.
此电话簿允许用户输入介于1-3之间的值.
输入1让用户向电话簿中添加一个号码,当处理完成后,用户再次被要求从这3项任务中选择1项。
输入2让用户快速拨号一个号码。如果该号码存在,它将被拨号,否则将打印错误声明。同样,用户将从3个选项中选择一个选项。
输入3个退出程序。
问:我如何使这段代码更容易理解/理解。我可以让它更多地面向反对和如何?有人还告诉我,使用太多的静态方法是不好的做法。为了防止这种情况,我已经将扫描器和数组传递给了类,并且只想确保它是正确的,因为我以前没有这样做过。
package com.java.phonebook;
import java.util.ArrayList; //Import ArrayList
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner; //Import Scanner
public class PhoneBookV2 {
public static void main(String[] args){
final List<String> numbers = new ArrayList<>(); //Initialize new ArrayList
Scanner scan = null;
try {
scan = new Scanner(System.in);
PhoneBook phoneBook = new PhoneBook(scan, numbers);
phoneBook.task();
}catch(InputMismatchException e){
System.out.println("Scanner Not Found");
}finally{
if(scan != null){
scan.close();
}
}
}
}package com.java.phonebook;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PhoneBook {
Scanner scan;
List<String> numbers;
public PhoneBook(Scanner scan, List<String> numbers) {
this.scan = scan;
this.numbers = numbers;
}
public void task() {
boolean cont = true;
do {
//Ask user for input on which task they want to do
System.out.println("Please Pick A Task: \n 1:Add A Number to Speed Dial \n 2:Speed Dial A Number \n 3:Exit");
String choice = scan.nextLine(); //read input
//check user input and continue accordingly
switch (choice) {
case "1":
AddNumber(); //run AddNumber Method
cont = false;
break;
case "2":
CallNumber(); //Run CallNumber method
cont = false;
break;
case "3":
System.out.println("Goodbye!");
System.exit(0); //Terminate Program
cont = false;
break;
default:
System.err.println("Invalid");
}
}while(cont);
}
public void CallNumber() {
boolean cont = true;
//create Keypad
do try {
String[][] keys = {
{" ", "1", " ", "2", " ", "3"},
{" ", "4", " ", "5", " ", "6"},
{" ", "7", " ", "8", " ", "9"},
{" ", " ", " ", "0", " ", " "}
};
//print keypad
printPhoneBook(keys);
//ask user for key number
System.out.println("Pick A Speed Dial Number");
String phoneNum = null; // should not be initialized as "", otherwise dialing message would be displayed even if choice is invalid.
if (scan.hasNextInt()) {
int choice = Integer.parseInt(scan.nextLine()); //convert string to int
if (choice >= 0 && choice <= 9) {
phoneNum = numbers.get((choice + 9) % 10);
} else {
System.err.println("Does Not Exist");
continue;
}
}
//if number exists, Dial number
if (phoneNum != null) {
System.out.println("Dialing " + phoneNum + "....");
cont = false;
}
//or say no number exists
else {
System.out.println("There is No Number At This Location");
break;
}
//ask user tasks again
task();
} catch (IndexOutOfBoundsException e) { //catch possible errors
System.err.println("There is No Number At This Location");
//ask for tasks again
task();
}while(cont);
}
public void AddNumber() {
boolean cont = true; //initialize boolean for loop
do try{
System.out.print("Please Enter The Number You Wish To Save Under Speed Dial: ");
if(scan.hasNextLong()) {
long input = Long.parseLong((scan.nextLine().trim().strip()));
if ((input >= 100) && (input <= 999_999_999)) {
// Add the next number to the ArrayList
numbers.add(String.valueOf(input));
}else if((input < 1000) || (input > 999_999_999)){
System.out.println("Invalid Entry");
continue;
}
}else if (scan.hasNextLine()) {
scan.nextLine();
System.out.println("Invalid Entry");
continue;
}
System.out.print("Would you like to add another? Yes or No: ");
String answer = scan.nextLine().toLowerCase(); //take input and convert to lowercase
if (answer.equals("yes")){
continue; //if they want to continue, allow it
}else if (answer.equals("no")) { //if not, print arraylist and exit loop
print((ArrayList<String>) numbers);
cont = false;
}else if(answer.equals("")){
System.out.println("Invalid Entry");
}else{
System.out.println("Invalid");
}
}catch(NumberFormatException e){
}while(cont);
//ask user tasks again
task();
}
public static void printPhoneBook(String[][] keys){
// Loop to print array
for(String[] row : keys){
for(String s : row){
System.out.print(s);
}
System.out.println();
}
}
public void print(){
//loop to print array numbers
for(int i = 0; i< numbers.size(); i++){
System.out.println((i+1) + ") " + numbers.get(i));
}
}
}发布于 2021-05-26 08:21:40
您似乎所做的主要事情是:( a)共享扫描仪;( b)将处理移到类中。您还添加了输入的一些验证。这还不是OOP。
import java.util.Scanner; //Import Scanner根本没有任何价值。注释应该增加价值,通常是通过解释为什么代码是原样的。这里有一个建议,让您开始一种面向对象的方法。
在这个设计中,我假设您的PhoneBook应该按照输入的顺序存储无限数量的电话号码,并且前10个条目(索引0到9)应该可以作为快速拨号号码使用。
首先,编写一个不执行I/O的PhoneBook类-它应该实现Iterable<String>并提供以下内容:
现在为这个类编写一些单元测试。理想情况下使用Junit,因为这是Java中的规范,学习它将给您提供宝贵的技能。确保代码编译,并通过单元测试。您应该考虑提供这个类和单元测试以供评审。
现在编写交互式代码来使用这个类,同时考虑到已经给出的反馈,并遵循Java命名和布局标准。编译和测试你的程序。然后提出一个新的审查请求。
下面是PhoneBook类的框架代码
import java.util.Iterator;
public class PhoneBook implements Iterable<String> {
public PhoneBook() {
// Add code to create the internal storage for phone numbers
}
public void addPhoneNumber(String phoneNumber) {
// Add the phone number to the storage
}
public String getPhoneNumber(int index) {
return null; // Change this to return the appropriate phone number, or null if none exists
}
public Iterator<String> iterator() {
return null; // change this to return a suitable Iterator
}
}https://codereview.stackexchange.com/questions/261211
复制相似问题