我已经为我的类项目编写了一个java类,并且我的所有方法都是无效的,除了在main方法中调用方法之外我基本上什么都不做。
该代码基本上可以帮助学生管理他们的月收入,包括租金和贷款支付。
有人能指出我正确的方向,我做错了什么? 关于编码习惯的一般建议吗?
班级代码:
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);}
}
//The main method:
import java.io.*;
public class UseClass {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
Finance fin=new Finance();
fin.liveWithParents();
fin.amountRent();
fin.collegeTuition();
fin.getMonthlyCost();
fin.payMethod();
fin.inputPayMethod();
fin.getTotalCost();
fin.outputCost();
fin.outputFile();
}
}
//The main method:
import java.io.*;
public class UseClass {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
Finance fin=new Finance();
fin.liveWithParents();
fin.amountRent();
fin.collegeTuition();
fin.getMonthlyCost();
fin.payMethod();
fin.inputPayMethod();
fin.getTotalCost();
fin.outputCost();
fin.outputFile();
}
}
谢谢
答案 0 :(得分:3)
首先想到的是你需要分开你的顾虑。这意味着您的财务类应该只执行与财务相关的事情。它应该不执行从命令行读取输入等操作。
实现这种分离的一种方法是创建另一个类,比如FinanceDataReader或类似东西,并让它管理所有用户交互。它将从命令行获取数据并将其提供给您的Finance实例。如果您真的想要获得幻想,可以创建一个用于读取财务数据的界面,然后使用CommandLineFinanceDataReader实现。这样,您可以更改将来获取数据的方式,而无需更改财务类。
因此,换句话说,将任何读取输入的功能移动到另一个类中,以使财务更小,更易于维护。构建封装所有功能的类,但根据您要解决的问题对它们进行分组。
您可以做的另一件大事是使用像JUnit这样的框架对您的代码进行单元测试。它需要一笔前期投资,但它会帮助您节省时间,因为您将随时测试所有小部分。换句话说,你不会写300行代码然后必须弄清楚它为什么不起作用;在编写每个方法时进行测试将帮助您确保您的方法/类正在执行您想要的操作。
别担心,这类事情会随着时间而来 - 如果这是你的第一个Java和可能的OO课程,你会犯错误并且设计有限。如果你坚持下去,那将随着时间的推移而改善。
答案 1 :(得分:3)
对于初学者来说,有很多重复的代码。你只有几次问题遵循相同的模式,所以你应该把它抽象出来。
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,另一个类处理数据,另一个类控制交互。
答案 2 :(得分:0)
您的大部分方法都是将值存储在变量中的问题。您可以创建一个问题/规则界面,其中包含一个提示用户输入的方法,一个用于计算结果的方法和一个输出方法。然后使用自己独特的逻辑为每个问题创建一个实现。您的主要方法只需要循环查看这些问题的列表以进行询问和报告。
答案 3 :(得分:0)
构建灵活,非刚性的代码是随着时间的推移而学习的,并结合了设计模式和通用设计原则的使用。以下是从哪里开始的一些提示。您希望从对抽象的充分理解和使用DIP(设计倒置原则)开始。
使用常见的设计模式是实现灵活性的好方法。一个很好的例子是“战略模式”和可观察模式。 您还希望遵循最佳实践和原则,例如单一责任原则,最少知识原则,开放式原则等等。那些少数应该让您开始,但它必须取决于您掌握这项技术。