gpt4 book ai didi

java - 如何让代码更灵活

转载 作者:行者123 更新时间:2023-11-29 07:21:12 25 4
gpt4 key购买 nike

我为我的类项目写了一个java类,我所有的方法都是void,我基本上什么都不做,只是调用我的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);}


}

<p></p>

<code>//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();
}

}
</code>


谢谢

最佳答案

首先想到的是您需要分离您的关注点。这意味着您的金融类应该只做与金融相关的事情。它不应该 执行诸如从命令行读取输入之类的操作。

实现这种分离的一种方法是创建另一个类,例如 FinanceDataReader 之类的,并让它管理所有用户交互。它将从命令行获取数据并将其提供给您的财务实例。如果您真的想要花哨的东西,您可以创建一个用于读取财务数据的接口(interface),然后有一个 CommandLineFinanceDataReader 实现。这样你就可以改变你在未来获取数据的方式,而不必改变你的财务类。

因此,换句话说,将任何读取输入的功能移动到另一个类中,以使 Finance 更小且更易于维护。构建封装所有功能的类,但根据您要解决的问题对它们进行分组。

您可以做的另一件大事是使用像 JUnit 这样的框架来对您的代码进行单元测试。这将需要前期投资,但可以帮助您节省时间,因为您将在进行过程中测试所有的小部分。换句话说,您不会写了 300 行代码,然后必须弄清楚为什么它不起作用;在您编写每个方法时进行测试将帮助您确保您的方法/类正在执行您想要的操作。

别担心,这种事情会随着时间的推移而发生——如果这是您的第一个 Java 和可能的 OO 类,您会犯错误并且设计有限。如果您坚持下去,这会随着时间的推移而改善。

关于java - 如何让代码更灵活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4249804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com