2016-04-06 19 views
1

enter image description hereJava的套裝應用程序更新

我已經經歷了這些文件的獲得除了最後一個部分(第5部分)我已附上紙,因爲我與掙扎的部分圖片以上。

運行我的應用程序測試儀時,當我按下3時,什麼也沒有發生。有誰知道爲什麼?我的課程代碼如下。注:makeChangeToSuit方法中在測試程序中調用的updateBrand()方法位於morningSuit和suit類中。

我將不勝感激在這個問題上的任何幫助。

package SuitProg; 

import java.util.Scanner; 

public abstract class Suit { 

    //instance variables 
    private String Colour; 
    private double dailyCost; 
    private int trouserLength; 
    private int jacketChestSize; 
    private boolean available; 
    protected double totalPrice; 

    //constructor 
    public Suit(String colour, double dailyCost, int trouserLength, int jacketChestSize, boolean available) { 
     super(); 
     Colour = colour; 
     this.dailyCost = dailyCost; 
     this.trouserLength = trouserLength; 
     this.jacketChestSize = jacketChestSize; 
     this.available = available; 
     this.totalPrice = totalPrice; 
    } 

    //accessors & mutators 
    public String getColour() { 
     return Colour; 
    } 

    public double getDailyCost() { 
     return dailyCost; 
    } 

    public int getTrouserLength() { 
     return trouserLength; 
    } 

    public int getJacketChestSize() { 
     return jacketChestSize; 
    } 

    public boolean getAvailability() { 
     return available; 
    } 

    public double getTotalPrice() { 
     return totalPrice; 
    } 

    public void setDailyCost(double dailyCost) { 
     this.dailyCost = dailyCost; 
    } 

    public void setTrouserLength(int trouserLength) { 
     this.trouserLength = trouserLength; 
    } 

    public void setJacketChestSize(int jacketChestSize) { 
     this.jacketChestSize = jacketChestSize; 
    } 

    public void setAvailability(boolean available) { 
     this.available = available; 
    } 

    //methods 
    public String toString() { 
     return " Suit [ Colour: " + getColour() + ", Daily Cost: " + String.format("%.2f", getDailyCost()) 
     + "\nTrouser Length: " + getTrouserLength() + ", Jacket Chest Size: " + getJacketChestSize() 
     + " Is it available? " + getAvailability(); 
    } 

    public void calcTotalPrice (int numDaysHired) { 
     totalPrice = totalPrice + (getDailyCost() * numDaysHired); 
    } 

    public String printDailyCost() { 
     getDailyCost(); 
     return "£" + String.format("%.2f", getDailyCost()); 
    } 

    public void makeChange(Scanner input) { 
     boolean valid = false; 
     do { 
      System.out.println("Are you sure you want to change the branding of a suit?"); 
      String response = input.nextLine().toLowerCase(); 
      if (response.equalsIgnoreCase("Y")) { 
       valid = true; 
       updateBrand(null); 
      } 
      else 
       if (response.equalsIgnoreCase("N")) { 
        valid = true; 
        System.exit(0); 
        break; 
       } 
     } while (!valid); 
    } 

    public void updateBrand(Scanner input) { 
     boolean valid = false; 
     int selection; 
     System.out.println("The list of available brands are below:"); 
     System.out.println("1 - " + Brand.Highstreet); 
     System.out.println("2 - " + Brand.TedBaker); 
     System.out.println("3 - " + Brand.FrenchConnection); 
     do { 
      System.out.println("Please enter the number of the Brand you wish to change."); 
      if (input.hasNextInt()) { 
       selection = input.nextInt(); 
       if (selection < 1 || selection > 3) { 
        valid = false; 
       System.out.println("Please enter a number betwen 1 and 3"); 
       } else 
        valid = true; 
        System.out.println("You have selected number: " + selection); 
        if (selection == 1) { 
         System.out.println("Please enter the changes you want to make"); 
         System.out.println("New brand name : "); 
         // 
        } 
      } 

     } while (!valid); 
    } 


} 

package SuitProg; 

import java.util.Scanner; 

public class MorningSuit extends Suit implements Brandable { 

    //instance variables 
    private boolean boutonniere; 
    private boolean topHat; 
    public Brand brand; 

    //constructor 
    public MorningSuit(String colour, double dailyCost, int trouserLength, int jacketChestSize, boolean available, boolean boutonniere, boolean topHat) { 
     super(colour, dailyCost, trouserLength, jacketChestSize, available); 
     this.boutonniere = boutonniere; 
     this.topHat = topHat; 
    } 

    //accessors & mutators 
    public boolean getBout() { 
     return boutonniere; 
    } 

    public boolean getTopHat() { 
     return topHat; 
    } 

    public void setBout(boolean boutonniere) { 
     this.boutonniere = boutonniere; 
    } 

    public void setTopHat(boolean topHat) { 
     this.topHat = topHat; 
    } 

    public void setBrand(Brand brand) { 
     this.brand = brand; 
    } 

    //methods 
    public String toString() { 
     return "Morning Suit [ Boutonniere " + getBout() + " TopHat " + getTopHat() + " Colour: " + getColour() + ", Daily Cost: £" + String.format("%.2f", getDailyCost()) 
     + "\nTrouser Length: " + getTrouserLength() + ", Jacket Chest Size: " + getJacketChestSize() 
     + " Is it available? " + getAvailability() + "]"; 
    } 

    public void calcTotalPrice(int numDaysHired) { 
     if (getBout()) { 
      totalPrice = totalPrice + 3; 
     } 
     if (getTopHat()) { 
      totalPrice = totalPrice + 10; 
     } 
     totalPrice = totalPrice + (numDaysHired * getDailyCost()); 
     System.out.println("The morning suit was hired for " + numDaysHired + " days."); 
     System.out.println("The total cost for the hire was: £" + String.format("%.2f", totalPrice)); 
    } 

    public String getBrand() { 
     return "The brand of this Morning Suit is " + brand.toString().toLowerCase(); 
    } 

    public void makeChange(Scanner input) { 
     boolean valid = false; 
     do { 
      System.out.println("Are you sure you want to change the branding of a suit?"); 
      String response = input.nextLine().toLowerCase(); 
      if (response.equalsIgnoreCase("Y")) { 
       valid = true; 
       updateBrand(input); 
      } 
      else 
       if (response.equalsIgnoreCase("N")) { 
        valid = true; 
        System.exit(0); 
        break; 
       } 
     } while (!valid); 
    } 

    public void updateBrand(Scanner input) { 

     boolean valid = false; 
     int selection; 
     System.out.println("The list of available brands are below:"); 
     System.out.println("1 - " + Brand.Highstreet); 
     System.out.println("2 - " + Brand.TedBaker); 
     System.out.println("3 - " + Brand.FrenchConnection); 
     do { 
      System.out.println("Please enter the number of the Brand you wish to change."); 
      if (input.hasNextInt()) { 
       selection = input.nextInt(); 
       if (selection < 1 || selection > 3) { 
        valid = false; 
       System.out.println("Please enter a number betwen 1 and 3"); 
       } else 
        valid = true; 
        System.out.println("You have selected number: " + selection); 
        if (selection == 1) { 
         System.out.println("Please enter the changes you want to make"); 
         System.out.println("New brand name : "); 
         // 
        } 
      } 

     } while (!valid); 
    } 


} 
package SuitProg; 

public enum Brand { 

    Highstreet,TedBaker,FrenchConnection 
} 

package SuitProg; 

public interface Brandable { 

    public String getBrand(); 
} 

package SuitProg; 

import java.util.Scanner; 

public class EveningSuit extends Suit implements Brandable { 

    //variables 
    private boolean cufflinks; 
    private boolean waistcoat; 
    public Brand brand; 

    public EveningSuit(String colour, double dailyCost, int trouserLength, int jacketChestSize, boolean available, boolean cufflinks, boolean waistcoat) { 
     super(colour, dailyCost, trouserLength, jacketChestSize, available); 
     this.cufflinks = cufflinks; 
     this.waistcoat = waistcoat; 
     this.brand = Brand.Highstreet; 
    } 

    //accessors & mutators 
    public boolean getCuffs() { 
     return cufflinks; 
    } 

    public boolean getWaistcoat() { 
     return waistcoat; 
    } 

    public void setCuffs(boolean cufflinks) { 
     this.cufflinks = cufflinks; 
    } 

    public void setWaistcoat(boolean waistcoat) { 
     this.waistcoat = waistcoat; 
    } 


    //methods 
    public String toString() { 
     return "Evening Suit [ Cufflinks " + getCuffs() + " Waistcoat " + getWaistcoat() + " Colour: " + getColour() + ", Daily Cost: £" + String.format("%.2f", getDailyCost()) 
     + "\nTrouser Length: " + getTrouserLength() + ", Jacket Chest Size: " + getJacketChestSize() 
     + " Is it available? " + getAvailability() + "]"; 
    } 

    public void calcTotalPrice (int numDaysHired) { 
     if (getCuffs()) { 
      totalPrice = totalPrice + 5; 
     } 
     if (getWaistcoat()) { 
      totalPrice = totalPrice + 10; 
     } 
     totalPrice = totalPrice + (getDailyCost() * numDaysHired); 
     System.out.println("The evening suit was hired for " + numDaysHired + " days."); 
     System.out.println("The total cost for the hire was: £" + String.format("%.2f", totalPrice)); 
    } 

    public String getBrand() { 
     return "The brand of this Evening Suit is " + brand.toString().toLowerCase(); 
    } 

    public void makeChange(Scanner input) { 
     boolean valid = false; 
     do { 
      System.out.println("Are you sure you want to change the branding of a suit?"); 
      String response = input.nextLine().toLowerCase(); 
      if (response.equalsIgnoreCase("Y")) { 
       valid = true; 
       System.out.println("You can not change the brand name of an evening suit."); 
      } 
      else 
       if (response.equalsIgnoreCase("N")) { 
        valid = true; 
        System.exit(0); 
        break; 
       } 
     } while (!valid); 
    } 
} 

package SuitProg; 

import java.util.ArrayList; 
import java.util.Scanner; 

public class Tester05 { 

    public static void main(String[] args) { 

     //create arrayList of suits 
     ArrayList<Suit> suits = new ArrayList<Suit>(); 

     //create morningSuit object 
     MorningSuit MorningSuit1 = new MorningSuit("Black", 80.00, 32, 36, true, true, false); 
     MorningSuit1.setBrand(Brand.FrenchConnection); 
     //create evening suit 
     EveningSuit EveningSuit1 = new EveningSuit("White", 70.25, 34, 36, true, true, true); 


     //add suits to arrayList 
     suits.add(MorningSuit1); 
     suits.add(EveningSuit1); 

     //print all details of arrayList 
     for (Suit eachSuit : suits) { 
      System.out.println(eachSuit .toString()+"\n"); 
     } 

     System.out.println(MorningSuit1.getBrand()); 
     System.out.println(EveningSuit1.getBrand()); 

     printMenu(suits); 
    } 

    public static void printMenu(ArrayList<Suit> suits) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("----------------Suit Hire-----------------"); 
     System.out.println("What would you like to do?"); 
     System.out.println("\n1)Display all suits\n2)Display available suits\n3)Change Suit brand\n4)Finished"); 
     System.out.println("Please select an option: "); 
     int selection = input.nextInt(); 
     if (selection == 1) { 
      displayAllSuits(suits); 
     } else 
      if (selection == 2) { 
      displayAllSuits(suits); 
      } 
      else 
       if (selection ==3) { 
        makeChangeToSuits(suits, input); 
       } 
       else 
        if (selection ==4) { 
         System.out.println("You are now exitting the system."); 
         System.exit(0); 
        } 
    } 

    public static void makeChangeToSuits(ArrayList<Suit> suits, Scanner input) { 
       for (int i = 0; i > suits.size(); i ++) { 
        suits.get(i).updateBrand(input); 
       } 
    } 

    public static void displayAllSuits(ArrayList<Suit> suits) { 
     for (Suit eachSuit : suits) { 
      System.out.println(eachSuit .toString()+"\n"); 
     } 
    } 

    public static void displayAvailableSuits(ArrayList<Suit> suits) { 
     for (int i = 0; i > suits.size(); i++) { 
      if (suits.get(i).getAvailability()) { 
       System.out.println(suits.get(i).toString()); 
      } 
     } 
    } 

} 
+4

這是不是一個壞的問題,但你會得到更好的接待這個問題,如果你可以修剪下來的張貼示範*確切*你的問題在哪裏 – ControlAltDel

+0

在那裏我對帖子進行了更改 – Liam

回答

0

問題出在您迭代列表的makeChangeToSuits方法中。它應該看起來像:

public static void makeChangeToSuits(ArrayList<Suit> suits, Scanner input) { 
      for (Suit suit : suits) { 
       suit.updateBrand(input); 
      } 
    } 

此外,您displayAvailableSuits方法應該是這樣的:

public static void displayAvailableSuits(ArrayList<Suit> suits) { 
     for (Suit suit : suits) { 
      if (suit.getAvailability()) { 
       System.out.println(suit.toString()); 
      } 
     } 
    }