내 수업 프로젝트 용 자바 클래스를 작성했으며 모든 메소드가 무효이며 기본적으로 아무 것도하지 않고 내 기본 메소드에서 메소드를 호출합니다.코드를보다 융통성있게 만드는 방법
이 코드는 기본적으로 학생이 월세를 임대료 및 대출 지불과 관련하여 관리하는 데 도움이됩니다.
어떤 사람이 잘못된 방향으로 올바른 방향으로 나를 가리킬 수 있습니까? 일반적으로 코딩 습관에 대한 조언이 있습니까?
클래스 코드 :
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(); } }
가 마음에 오는 첫번째 것은 당신이 당신의 문제를 분리 할 필요가 당신
'잘못된 방향으로 나를 가리키는 사람이 있습니까? '오류 메시지, 스택 래치 또는 예상되는 것에 대한 설명없이 무엇을 잘못하고 있는지 알 수있는 방법은 무엇입니까? 너보고 있니? – Falmarri
Andrei는 OO 패러다임 및 커뮤니티 관측 코딩 표준에 따라 그의 접근 방식이 수용 가능하다고 생각하지 않는다고 말합니다. – Joel
정확히 Joel. 나는 경험이 많은 사용자로부터 어떻게해야하는지에 대한 의견을 얻고 싶습니다. 하지만이 문제는 책이 클래스와 메소드를 다시 방문하는 현재 장의 2 장에서 해결 될 것이라고 생각합니다. 그러나 여전히 코드를 좀 더 유연하게 만드는 방법을 알고 싶습니다. –