2013-06-23 101 views
-1

所以我一直在看這段代碼整個下午,我看不到錯誤(S)。這是我應該做的:Java - 調用從其他類計算字段的方法

創建一個Delivery類爲一個送貨服務。該類包含以下字段:

  • 包含八位數字的投遞號碼。前四位數字代表年份,最後四位數字代表送貨編號。
  • 代表交貨區域的代碼。本地配送是代碼1,長途配送是代碼2.
  • 重量(磅),要交付的物品。
  • 用於遞送的費用,如下:

創建用於Delivery類接受參數的一年一個構造函數,年,輸送距離碼,和重量的包內 交貨編號。構造函數決定了八位數的遞送編號和遞送費用。還包括顯示每個Delivery對象字段的 方法。將該文件保存爲Delivery.java。

接下來,創建一個提示用戶輸入數據的應用程序。保持 提示以下每個值的用戶,直到它們是有效的:

  • 2001年至2025年之間的四位數年份包容
  • 1和9999包
  • A之間的全年投放量0.10磅和100磅包容
  • 的輸送距離的代碼,或者是1或2

當所有的數據項是有效的,構造一個之間包重對象,然後顯示其值 值。將該文件保存爲CreateDelivery.java

因此,這裏是我的交付類

import javax.swing.*; 
import java.util.*; 


class Delivery 
{ 
    //variables 
    private int year; 
    private int deliveryNumber; //deliveryNo 
    private double weight; 
    private int distanceCode; //code 

     //constructor 
     //Delivery() 
     //{ 
     // year = year; 
     // deliveryNumber = deliveryNumber; 
     // weight = weight; 
     // distanceCode = distanceCode; 
     //} 

    //get year 
    public int getYear() 
    { 
     return year; 
    } 
    //set year 
    public int setYear (int newYear) 
    { 
     year = newYear; 
     return year; 
    } 

    //get deliveryNumber 
    public int getDeliveryNumber() 
    { 
     return deliveryNumber; 
    } 
    //set deliveryNumber 
    public int setDeliveryNumber (int newDeliveryNumber) 
    { 
     deliveryNumber = newDeliveryNumber; 
     return deliveryNumber; 
    } 

    //get weight 
    public double getWeight() 
    { 
     return weight; 
    } 
    //set Weight 
    public double setWeight (double newWeight) 
    { 
     weight = newWeight; 
     return weight; 
    } 


    //get distanceCode 
    public int getDistanceCode() 
    { 
     return distanceCode; 
    } 
    //set distanceCode 
    public int setDistanceCode (int newDistanceCode) 
    { 
     distanceCode = newDistanceCode; 
     return distanceCode; 
    } 

    //calculate fee 
    public double displayFees(int distance, double w) //distance = c 
    { 
     double fees = 0; 

    if(distance == 1) 
    { 
      if(w < 5) 
     { 
      fees = 12; 
     } 
     else if((w < 20)&&(w > 5)) 
     { 
      fees = 16.50; 
     } 
     else if(w > 20) 
     { 
      fees = 22; 
     } 
    } 
    else if(distance == 2) 
    { 
     if(w < 5) 
     { 
      fees = 35; 
     } 
     else if(w >= 5) 
     { 
      fees = 47.95; 
     } 
    } 
     return fees; 
    } 

    //display method 
    public void display(int year, int deliveryNumber, double weight, int distanceCode) 
     { 
     System.out.println("Year: " + year + '\n' 
      + "Delivery Number: " + deliveryNumber + '\n' 
      + "Weight of the package: " + weight + '\n' 
      + "Delivery code: " + distanceCode); 
     } 
} 

這裏是我的CreateDelivery類

import javax.swing.JOptionPane; 
import java.util.Scanner; 

public class CreateDelivery 
    { 
     public static void main(String []args) 

     { 

      Delivery delivery1 = new Delivery(); 

      //scanner year 
      Scanner input = new Scanner(System.in); 
      System.out.print("Please enter the current year, format (yyyy) >>> "); 
      delivery1.setYear(input.nextInt()); 

      //loop year 
        while((delivery1.getYear() <= 2000)||(delivery1.getYear() >= 2026)) 
        { 
         System.out.println('\n'+ "Error, year should be in the range of (2010 - 2025). Please enter a valid option >> "); 
         delivery1.setYear(input.nextInt()); 
        } 

      //scanner for delivery number 
      System.out.print('\n'+ "Please enter a delivery number: "); 
      delivery1.setDeliveryNumber(input.nextInt()); 

      //loop for delivery number 
        while((delivery1.getDeliveryNumber() <= 0001)||(delivery1.getDeliveryNumber() >= 10000)) 
        { 
         System.out.println("Error, the delivery number is a 4 digit number between 0001 and 9999, please enter a valid option >> "); 
         delivery1.setDeliveryNumber(input.nextInt()); 
        } 

      //scanner for weight 
      System.out.print("Please enter the weight of the package (in pounds): "); 
      delivery1.setWeight(input.nextDouble()); 

        //loop for weight 
        while((delivery1.getWeight() <= .09)||(delivery1.getWeight() >= 101)) 
        { 
         System.out.println("Error, the minimum allowed weight is 0.10 pounds and the maximum is 100 pounds. Please enter a valid weight >> "); 
         delivery1.setWeight(input.nextDouble()); 
        } 

      //scanner for delivery code 
      System.out.print("Please enter 1 for local or 2 for long distance deliveries >> "); 
      delivery1.setDistanceCode(input.nextInt()); 

        //loop for delivery code 
        while((delivery1.getDistanceCode() == 1) && (delivery1.getDistanceCode() == 2)) 
        { 
         System.out.println("Error, please enter a valid distance code (1 for local deliveries and 2 for long distance deliveries) >> "); 
         delivery1.setDistanceCode(input.nextInt()); 
        } 

      //turn int to string 
      String n = Integer.toString(delivery1.getDeliveryNumber()); 
      String y = Integer.toString(delivery1.getYear()); 

      String trackingNumber = n + y; 
      System.out.println(delivery1.getDistanceCode() + " " 
      + trackingNumber + " " + delivery1.getWeight() + " " + fees); 

    } 

} 

所以我做了你們建議的修改,但現在我不能從交貨拉費類。有什麼想法嗎?

+0

您面臨什麼錯誤? –

+0

'Delivery'和'CreateDelivery'是否在同一個包中? – Genzer

+0

//掃描儀的距離代碼 \t \t \t System.out.print(「請輸入1爲本地交貨,2爲長途交貨>>」); \t \t \t delivery1.setDistanceCode(input.nextInt()); \t \t \t \t \t \t \t \t //循環用於遞送代碼 \t \t \t \t \t而(((delivery1.getDistanceCode()<= 1)||(delivery1.getDistanceCode()> = 2))) \t \t \t \t \t { \t \t \t \t \t \t的System.out.println(「發生錯誤,請輸入一個有效的距離碼(1對於本地交付和2對於長途交貨)>>「); \t \t \t \t \t \t delivery1.setDistanceCode(input.nextInt()); \t \t \t \t \t} –

回答

1
Delivery() 
    { 
     year = year; 
     deliveryNumber = deliveryNumber; 
     weight = weight; 
     distanceCode = distanceCode; 
    } 

沿東西線替換它:

Delivery(int year, int deliveryNumber, int weight, int distanceCode) 
    { 
     this.year = year; 
     this.deliveryNumber = deliveryNumber; 
     this.weight = weight; 
     this.distanceCode = distanceCode; 
    } 

從那裏,我會避免使用set方法。相反,當您從System.in加載它們時,將所有值存儲到各個字段中。獲得所有字段後,請創建Delivery對象。

+0

雖然我被要求在交付類中設置構造函數。 –

+0

是的,我修正了這個問題。 – 2013-06-23 04:03:09

+0

?避免設置方法?所以你建議我們應該放棄一個基本的面向對象原則?雖然我可以理解爲什麼你可以用這樣一個小例子來提出它,但問題的目的是教給學生適當的範例。而不知道這個問題可能導致我(個人)發現這個建議令人擔憂 – MadProgrammer

1

我認爲您在while循環中的方法末尾丟失了(),例如getDeliveryNumbergetYear等。 並且您還在末尾使用了未聲明的變量,例如getDeliveryNumbergetYear等。

+0

我注意到了,我修復了它,但是現在我無法得到距離環路,即使在輸入接受變量時也會提示錯誤 –

+0

請在while循環中從兩個條件中刪除'='標記 –

+0

@ClaudiaBenitezdeArmstrong條件應該是'while(delivery1.getDistanceCode()<1 || delivery1.getDistanceCode()> 2)' –

相關問題