2013-02-17 88 views
0

我已經搜索,但不知道如何將以前的答案與我自己的代碼。我有一個assigment創建一個學校的「銀行/獎學金」系統的計劃草案,在這裏我們需要創建很多不同的方法,應該被稱爲而不是直接進行更改(非靜態方法更改(字符串,字符串,字符串)不能從靜態上下文中引用

事情是,我不斷收到主題中的錯誤消息以及其他一些內容,我不知道如何解決它,如果有人能夠向我解釋如何以及爲什麼要改變我的方法之一,我會非常感激,那麼我認爲我能夠自己重寫所有其他人。以下是我的代碼到目前爲止:

類地址

public class Address { 

public String street, zip, post; 

public Address (String street, String zip, String post) { 

    this.street = street; 
    this.zip = zip; 
    this.post = post; 
} 

// these are setters (mutators) 
public void setStreet (String street) { 
    this.street = street; } 

public void setZip (String zip) { 
    this.zip = zip; } 

public void setPost (String post) { 
    this.post = post; } 

// these are getters 
public String getStreet() { 
    return this.street; } 

public String getZip() { 
    return zip; } 

public String getPost() { 
    return post; } 

// methods 
//public Address Change(Address newAddress) { 
//return newAddress; 
//} 

// output 
public String toString() { 
    return (street + ", " + zip + ", "+ post); } 

} 

類客戶

import java.text.DecimalFormat; 

public class Customer { 

DecimalFormat twoD = new DecimalFormat("0.00"); 
Address address = new Address("Vik ", "6393 ", "Tomrefjord"); 

public String name, campus, newCampus, newAddress; 
public double loan, increase,decrease,regMaster; 
public boolean finished; 

public Customer (String name,Address address, String campus, double loan, boolean finished) { 
    this.name = name; 
    this.campus = campus; 
    this.loan = loan; 
    this.address = address; 
    this.finished = finished; 
} 

// these are setters 
public void setName (String name) { 
    this.name = name; } 

public void setCampus (String campus) { 
    this.campus = campus; } 

public void setLoan (double loan) { 
    this.loan = loan; } 

public void setFinished (boolean finished) { 
    this.finished = finished; } 

// these are getters 
public String getName() { 
    return name; } 

public String getCampus() { 
    return campus; } 

public double getLoan() { 
    return loan; } 

// methods 
public void RegMaster() { 
this.loan = loan * 0.90;} 

//public void changeAddress (Address newAddress) { 
//Address.Change(newAddress); } 

public double decreaseLoan (double currentBalance, double decrease) { 
currentBalance = currentBalance - decrease; 
return currentBalance; } 

public double getNewLoan (double currentBalance, double newLoan) { 
    newLoan = currentBalance + newLoan; 
    return newLoan; } 

public String getNewCampus (String newCampus) { 
    campus = newCampus; 
    return campus; }  

public static boolean Ended() { 
    return true; } 

// output 
public String toString() { 
     return (name + "\t\n" + address + "\t\n" + campus + "\t\n" + twoD.format(loan) + "\t\n" + finished); } 






} 

類TestCustomer

import java.util.Scanner; 

public class TestCustomer { 

public static void main (String[] args) { 

    String student, school, answer, street, zip, post; 
    double balance, master; 
    boolean finished = false; 
    double done = 0; //this is for the loop 

    Scanner scan = new Scanner(System.in); 

    // a new student receives a loan 
    System.out.println("Write your name"); 
    student = scan.nextLine(); 
    System.out.println("Enter your street name and number"); 
    street = scan.nextLine(); 
    System.out.println("What is your zip code?"); 
    zip = scan.nextLine(); 
    System.out.println("What is your post area?"); 
    post = scan.nextLine(); 
    System.out.println("Where do you study?"); 
    school = scan.nextLine(); 
    System.out.println("Input your current loan"); 
    balance = scan.nextDouble(); 

    Address residence = new Address(street, zip, post); 
    Customer customer = new Customer(student, residence, school, balance, finished); 
    while(done < 1) { //the variable done will have the value 0 until you are finished. 
    // change address 
    System.out.println("Do you wish to change your address? (yes/no)"); 
    answer = scan.nextLine(); 
    if (answer.equalsIgnoreCase("yes")) { //this made it case-insensitive, and now it is not skipping anymore... 
     System.out.println("Write your street name and house number"); 
       street = scan.nextLine(); 
       System.out.println("Write your zip code"); 
       zip = scan.nextLine(); 
       System.out.println("Write your post area"); 
       post = scan.nextLine(); 
       //Address newAddress = new Address(street, zip, post); 
       //residence = Customer.changeAddress(newAddress); 
    } 

    // increase the loan 
    System.out.println("Do you want to increase your loan? (yes/no)"); 
    answer = scan.nextLine(); 
       if (answer.equalsIgnoreCase("yes")) { 
       System.out.println("How much do you need?"); 
       double increaseLoan = scan.nextDouble(); 
       //customer.balance = getNewLoan(customer.balance, moreLoan); 
      } 

    // decrease the loan 
    System.out.println("Do you want to make a downpayment on your loan?"); 
    answer = scan.nextLine(); 
       if (answer.equalsIgnoreCase("yes")) { 
        System.out.println("How much do you intend to pay?"); 
        double downpayment = scan.nextDouble(); 
        //customer.balance = decreaseLoan(customer.balance, downpayment); 
       } 


    // change school 
    System.out.println("Do you study at the same school? (yes/no"); 
    answer = scan.nextLine(); 
       if (answer.equalsIgnoreCase("no")) { 
       System.out.println("Write the name of the new school"); 
       school = scan.nextLine(); } 
       //school.getNewCampus(customer.campus); 

    // master check 
    System.out.println("Have you finished your master? (yes/no)"); 
    answer = scan.nextLine(); 
       if (answer.equalsIgnoreCase("yes")) { 
       customer.finished = Customer.Ended() ; //this will set finished to true, using the method like intended 
       //customer.balance = Customer.RegMaster();  // dont know how to invoke it... me neither -_- 
       } 
      System.out.println("Are you done? yes/no"); 
      answer = scan.nextLine(); 
         if (answer.equalsIgnoreCase("yes")) { 
        done = 1; } // this adds 1 to the variable done, thus ending the loop. 
      } 
      // output 
      System.out.println(""); 
      System.out.println("The data we have so far is:"); 
      System.out.println(customer); 

}} 

這裏是我的三個班。其中一些功能被註釋掉了,另外一些功能卻不能正常工作(代碼也有點雜亂,我昨天試着去改變它的工作方式)。任何幫助將不勝感激! :)

+1

你得到了什麼錯誤,在哪裏? – Abubakkar 2013-02-17 11:08:00

+0

從您的問題標題[this](http://www.geom.uiuc.edu/~daeron/docs/javaguide/java/anatomy/static.html)來判斷可能有幫助。 – 2013-02-17 11:21:01

+0

「更改」功能在哪裏出錯? – BobTheBuilder 2013-02-17 11:26:00

回答

0

好,由你對此有何評論那麼你的問題來自於(有益)註釋掉線

//public void changeAddress (Address newAddress) { 
//Address.Change(newAddress); } 

這是因爲你的Customer沒有這樣做「有」 Address,你需要一個地址的實例,而不是a String

private Address address; 

public Customer (final Address address) { 
    this.address = address; 
} 

爲了清楚起見,我編輯了其他變量。順便提一句,我會用一個builder pattern比一個巨大的構造函數。
現在,你在你的CustomerAddress

public void changeAddress (Some parameters to change) { 
    address.setStuff(); 
} 

我建議你閱讀this linkstaticinstance變量之間的差異。

+0

謝謝,那真是啓發!在做了一些工作之後,我按照預期工作! :) – 2013-02-22 22:51:41

+0

如果它是有用的,你可以接受答案。謝謝。 – 2013-02-23 09:57:58

相關問題