Java 如何使代码更灵活
我已经为我的类项目编写了一个java类,我所有的方法都是无效的,我基本上什么也不做,只是在我的主方法中调用这些方法 该准则基本上帮助学生管理他们的月收入,包括租金和贷款支付 有人能告诉我我做错了什么吗? 在编码习惯方面有什么建议吗 类别代码:Java 如何使代码更灵活,java,class,methods,usability,Java,Class,Methods,Usability,我已经为我的类项目编写了一个java类,我所有的方法都是无效的,我基本上什么也不做,只是在我的主方法中调用这些方法 该准则基本上帮助学生管理他们的月收入,包括租金和贷款支付 有人能告诉我我做错了什么吗? 在编码习惯方面有什么建议吗 类别代码: import java.io.*; import java.util.*; public class Finance{ private double rentExpenses, tuition, totalCost, totCost, rent;
import java.io.*;
import java.util.*;
public class Finance{
private double rentExpenses, tuition, totalCost, totCost, rent;
private double payInput;
private boolean status, liveWithParent;
private int pay;
//totalCost=Final cost per month
//totCost=cost of tuition and rent per month
//Living with parents?
public void liveWithParents(){
Scanner in=new Scanner(System.in);
System.out.println("Are you living with your parents?");
String parents= in.nextLine();
if(parents.charAt(0)=='y' || parents.charAt(0)=='Y'){
status=true;}
else{
status=false;}}
//If yes, do you pay them rent?, if yes how much? else -, else How much is your monthly rent anyway?
public void amountRent(){
double rent;
char valid;
String validIn;
Scanner in=new Scanner(System.in);
if(status){
System.out.println("Do you need to pay them rent?");
validIn=in.nextLine();
valid= validIn.charAt(0);
if(valid=='y' || valid=='Y'){
System.out.println("How much is your rent?");
rent=in.nextDouble();}}
else{
System.out.println("How much is your monthly rent?");
rent=in.nextDouble();}}
//What is your college tuition, $/term
public void collegeTuition(){
System.out.println("What what is your college tuition in $ per term?");
Scanner in=new Scanner(System.in);
tuition= in.nextDouble();}
//Total cost of tuition and rent per month
public void getMonthlyCost(){
totCost= rentExpenses + tuition/3.75;
System.out.println("Your rent expenses and college tuition are: $"+totCost+" per month");}
//Method of paying for expenses
public void payMethod(){
Scanner in=new Scanner(System.in);
System.out.println("How will you pay for your expenses?"
+ "\n 1 -Savings\n 2 -Loans\n 3 -Freelance Work");
pay=in.nextInt();
while(pay<=0 || pay>3){
System.out.println("You need to enter a number coresponding to the three choiches.\n\t Try again:");
System.out.println("How will you pay for your expenses?"
+ "\n 1 -Savings\n 2 -Loans\n 3 -Freelance Work");
pay=in.nextInt();}}
//Gets the amount of savings the user has and converts
//that value to a monthly value
public void inputPayMethod(){
Scanner in=new Scanner(System.in);
if(pay==1){
System.out.println("What amount of savings do you have in total for the school year?");
payInput=in.nextDouble();
payInput=payInput/9;}
else if(pay==2){
System.out.println("What amount of loans did you acquire for this school year?");
payInput=in.nextDouble();
payInput=payInput/9;}
else if(pay==3){
System.out.println("How much revenue does your Freelane business get per month?");
payInput=in.nextDouble();}}
//Calculates the total cost that the user needs
//for renting and tuition solely
public void getTotalCost(){
totalCost=(payInput/3.75)-(rentExpenses + tuition/4.348);}
//Outputs the total cost
public void outputCost(){
System.out.println("Your balance per month after expenses is: $"
+totalCost);
if(totalCost<0){
System.out.println("You still need $"+(-totalCost)+" per months");}
if(totalCost>0){
System.out.println("In other words you should be A-O-KAY");}
//Balance calculation for an entire school year
System.out.println("For an entire school year, your expenses would be: "+
(totalCost*2));}
//Create a file with the information entered
//and the information processed
public void outputFile() throws IOException{
String payFileOutput=null;
Scanner in=new Scanner(System.in);
System.out.println("Enter the name of the file you wish to store this"+
"information in: ");
String fileName= in.nextLine();
PrintWriter file= new PrintWriter(fileName);
file.println("Your rent expenses are :"+rentExpenses);
file.println("Your college tuition in dollars per month is:"+tuition);
file.println(" -----");
file.println("Your rent expenses and college tuition are :"+(rentExpenses + tuition));
if(pay==1)
payFileOutput="Savings";
else if(pay==2)
payFileOutput="Loans";
else if(pay==3)
payFileOutput="Freelance Work";
else
;
file.println("\n\nYou choose "+payFileOutput+"as your income source");
file.println("Your balance per month after expenses is: $"+totalCost);
if(totalCost<0){
file.println("You still need $"+(-totalCost)+"per month");}
if(totalCost>0){
file.println("\n\n\nYour budget seems good");}
file.close();
System.exit(0);}
}
谢谢首先想到的是你需要把你的顾虑分开。这意味着你的金融课应该只做与金融相关的事情。它不应该执行从命令行读取输入之类的操作 实现这种分离的一种方法是创建另一个类,比如FinanceDataReader之类的,并让它管理所有的用户交互。它将从命令行获取数据,并将其提供给财务实例。如果您真的想变得有趣,可以创建一个用于读取财务数据的接口,然后使用CommandLineFinanceDataReader实现。这样你就可以改变你将来获取数据的方式,而不必改变你的金融课程 因此,换一种说法,将读取输入的任何功能移动到另一个类中,以使财务更小、更易于维护。构建封装所有功能的类,但根据要解决的问题对它们进行分组 您可以做的另一件大事是使用JUnit之类的框架对代码进行单元测试。这将需要一笔前期投资,但它将帮助您节省时间,因为您将在运行过程中测试所有小部件。换句话说,你不会写300行代码,然后必须弄清楚为什么它不工作;编写每个方法时进行测试将有助于确保方法/类执行您想要的操作
别担心,这类事情会随着时间的推移而发生——如果这是针对您的第一个Java类,可能是OO类,那么您将犯错误,并且设计有限。如果你坚持下去,这会随着时间的推移而改善。首先想到的是,你需要分离你的顾虑。这意味着你的金融课应该只做与金融相关的事情。它不应该执行从命令行读取输入之类的操作 实现这种分离的一种方法是创建另一个类,比如FinanceDataReader之类的,并让它管理所有的用户交互。它将从命令行获取数据,并将其提供给财务实例。如果您真的想变得有趣,可以创建一个用于读取财务数据的接口,然后使用CommandLineFinanceDataReader实现。这样你就可以改变你将来获取数据的方式,而不必改变你的金融课程 因此,换一种说法,将读取输入的任何功能移动到另一个类中,以使财务更小、更易于维护。构建封装所有功能的类,但根据要解决的问题对它们进行分组 您可以做的另一件大事是使用JUnit之类的框架对代码进行单元测试。这将需要一笔前期投资,但它将帮助您节省时间,因为您将在运行过程中测试所有小部件。换句话说,你不会写300行代码,然后必须弄清楚为什么它不工作;编写每个方法时进行测试将有助于确保方法/类执行您想要的操作
别担心,这类事情会随着时间的推移而发生——如果这是针对您的第一个Java类,可能是OO类,那么您将犯错误,并且设计有限。如果你坚持下去,这会随着时间的推移而改善。对于初学者来说,有很多重复的代码。你只有几次的问题遵循相同的模式,所以你应该把它抽象出来
class QuestionIO {
private Scanner in = new Scanner(System.in);
public boolean boolQuestion(String question) {
System.out.println(question);
String result= in.nextLine();
return (result.charAt(0)=='y' || result.charAt(0)=='Y');
}
//other types for doubles or ints
}
这也有助于对代码进行分区。您可以采用MVC类型的设计,一个类处理IO,另一个类处理数据,另一个类控制交互。对于初学者来说,有很多重复的代码。你只有几次的问题遵循相同的模式,所以你应该把它抽象出来
class QuestionIO {
private Scanner in = new Scanner(System.in);
public boolean boolQuestion(String question) {
System.out.println(question);
String result= in.nextLine();
return (result.charAt(0)=='y' || result.charAt(0)=='Y');
}
//other types for doubles or ints
}
这也有助于对代码进行分区。您可以采用MVC类型的设计,一个类处理IO,另一个类处理数据,另一个类控制交互。大多数方法都是将值存储在变量中的问题。您可以创建一个问题/规则界面,该界面有一个提示用户输入的方法、一个计算结果的方法和一个输出方法。然后为每个问题创建一个具有自己独特逻辑的实现。您的主要方法只需要循环浏览这些问题的列表,以便提问和报告。大多数方法都是在变量中存储值的问题。您可以创建一个问题/规则界面,该界面有一个提示用户输入的方法、一个计算结果的方法和一个输出方法。
然后为每个问题创建一个具有自己独特逻辑的实现。您的主要方法只需要循环浏览这些问题的列表,以便提问和报告。灵活、非僵化的代码是随着时间的推移而学习的,并结合了设计模式和通用设计原则的使用。以下是一些关于从何处开始的提示。您希望从对抽象的深入理解和使用DIP设计反转原理开始 使用通用设计模式是实现灵活性的一个很好的方法。首先有两个很好的例子:战略模式和可观察模式。
您还需要遵循最佳实践和原则,如单一责任原则、最少知识原则、开放-关闭原则等。。这几点应该让你开始,但你必须掌握这项技术 建筑灵活、非僵化的代码是随着时间的推移而学习的,并结合了设计模式和通用设计原则的使用。以下是一些关于从何处开始的提示。您希望从对抽象的深入理解和使用DIP设计反转原理开始 使用通用设计模式是实现灵活性的一个很好的方法。首先有两个很好的例子:战略模式和可观察模式。
您还需要遵循最佳实践和原则,如单一责任原则、最少知识原则、开放-关闭原则等。。这几点应该让你开始,但你必须掌握这项技术 有人能告诉我我做错了什么我们怎么知道你做错了没有错误信息,没有stackttrace,或者描述一下你所期望的和你所看到的?我相信Andrei是说,根据OO范式和社区观察的编码标准,他认为他的方法是不可接受的。确切地说,Joel。我只想从更有经验的用户那里得到一些关于应该如何做的信息。但我认为这个问题将在我当前章节的两章中得到解决,这本书将重新讨论类和方法。。。但我还是想知道如何使我的代码更灵活。有人能给我指出我做错了什么的正确方向吗,或者描述一下你所期望的和你所看到的?我相信Andrei是说,根据OO范式和社区观察的编码标准,他认为他的方法是不可接受的。确切地说,Joel。我只想从更有经验的用户那里得到一些关于应该如何做的信息。但我认为这个问题将在我当前章节的两章中得到解决,这本书将重新讨论类和方法。。。但我仍然想知道如何使我的代码更灵活。你创建一个用于读取金融数据的接口是什么意思?@andrei,如果这是针对一个类的,你可能还没有做到这一点。看到了吗?创建一个用于读取金融数据的接口是什么意思?@andrei,如果这是一个类的接口,那么您可能还没有实现。看到这个了吗