0

我已经搜索过,但不明白如何将以前的答案与我自己的代码结合起来。我有一个任务是为学校的“银行/奖学金”系统创建一个程序草案。这里我们需要创建很多不同的方法,这些方法应该被调用而不是直接进行更改。

问题是,我和其他一些人不断收到该主题中的错误消息,但我不知道如何解决它。如果有人能为我解释如何以及为什么要更改我的一种方法,我将非常感激,然后我假设我将能够自己重写所有其他方法。到目前为止,这是我的代码:

班级地址

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);

}}

这是我所有的三个课程。一些功能被注释掉了,其他的只是不能正常工作(代码也有点乱,我昨天来回改变以试图让它工作)。任何帮助将不胜感激!:)

4

1 回答 1

0

好的,根据您的评论,您的问题来自(有帮助的)注释掉的行

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

这是因为您Customer没有“拥有” Address,您需要一个地址实例而不是String

private Address address;

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

为了清楚起见,我已经编辑了其他变量。顺便说一句,我会使用构建器模式而不是一个巨大的构造器。
现在你有Address一个Customer

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

我建议您阅读有关和变量之间差异的链接。staticinstance

于 2013-02-17T11:47:00.530 回答