使用枚举、方法和用户输入更新的Java应用程序
除了最后一部分(第5部分),我已经看完了这篇文章。我已经附上了这篇文章的图片,这是我正在努力解决的部分 当运行我的应用程序测试仪时,当我按3时,什么也没有发生。有人知道为什么吗?我的课程代码如下。注意:测试人员在MakeChangeTouit方法中调用的update布兰德()方法位于morningSuit和suit类中 在这件事上,如果有任何帮助,我将不胜感激使用枚举、方法和用户输入更新的Java应用程序,java,if-statement,input,enums,menu,Java,If Statement,Input,Enums,Menu,除了最后一部分(第5部分),我已经看完了这篇文章。我已经附上了这篇文章的图片,这是我正在努力解决的部分 当运行我的应用程序测试仪时,当我按3时,什么也没有发生。有人知道为什么吗?我的课程代码如下。注意:测试人员在MakeChangeTouit方法中调用的update布兰德()方法位于morningSuit和suit类中 在这件事上,如果有任何帮助,我将不胜感激 package SuitProg; import java.util.Scanner; public abstract class
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());
}
}
}
}
package-SuitProg;
导入java.util.Scanner;
公开抽象类诉讼{
//实例变量
私人弦色;
私人双重每日成本;
私人长度;
私家车尺寸;
私有布尔值可用;
保护双重总价;
//建造师
公共套装(字符串颜色、双dailyCost、整数裤长、整数jacketChestSize、布尔值可选){
超级();
颜色=颜色;
this.dailyCost=dailyCost;
this.trouserLength=trouserLength;
this.jacketTestSize=jacketTestSize;
this.available=可用;
this.totalPrice=totalPrice;
}
//存取器和变异器
公共字符串getColor(){
返色;
}
公共双getDailCost(){
返回每日成本;
}
public int gettruerlength(){
返回槽长度;
}
public int getJacketChestSize(){
返回jacketChestSize;
}
公共布尔getAvailability(){
返回可用;
}
公共双getTotalPrice(){
返回总价;
}
公共无效设置每日成本(双倍每日成本){
this.dailyCost=dailyCost;
}
公共void setTrouserLength(int trouserLength){
this.trouserLength=trouserLength;
}
公共无效设置JacketchTestSize(int JacketchTestSize){
this.jacketTestSize=jacketTestSize;
}
public void setAvailability(布尔值可用){
this.available=可用;
}
//方法
公共字符串toString(){
return“Suit[color:+getcolor()+”,dailycost:+String.format(“%.2f”,getDailyCost())
+\n传送带长度:“+GetTruserLength()+”,夹克胸围尺寸:“+GetJacketTestSize()
+“它可用吗?”+getAvailability();
}
公共无效calcTotalPrice(国际努姆代谢德){
totalPrice=totalPrice+(getDailCost()*numDaysHired);
}
公共字符串printDailyCost(){
getDailyCost();
返回“£”+String.format(“%.2f”,getDailCost());
}
公共void makeChange(扫描仪输入){
布尔有效=假;
做{
System.out.println(“您确定要更改西装的品牌吗?”);
字符串响应=input.nextLine().toLowerCase();
if(响应等信号情况(“Y”)){
有效=真;
updateBrand(null);
}
其他的
if(响应等信号情况(“N”)){
有效=真;
系统出口(0);
打破
}
}而(!有效);
}
公共无效更新带(扫描程序输入){
布尔有效=假;
int选择;
System.out.println(“可用品牌列表如下:”);
System.out.println(“1-”+Brand.Highstreet);
System.out.println(“2-”+Brand.TedBaker);
System.out.println(“3-”+品牌.法语连接);
做{
System.out.println(“请输入您希望更改的品牌编号”);
if(input.hasNextInt()){
selection=input.nextInt();
如果(选择<1 | |选择>3){
有效=错误;
System.out.println(“请输入介于1和3之间的数字”);
}否则
有效=真;
System.out.println(“您已选择编号:+选择”);
如果(选择==1){
System.out.println(“请输入要进行的更改”);
System.out.println(“新品牌名称:”);
//
}
}
}而(!有效);
}
}
包SuitProg;
导入java.util.Scanner;
公共类晨衣扩展套装实现Brandable{
//实例变量
私人布尔花束;
私有布尔顶帽;
公共品牌;
//建造师
公共晨衣(字符串颜色、双dailyCost、int trouserLength、int jacketChestSize、布尔值可用、布尔值花束、布尔值帽子){
super(颜色、每日成本、裤子长度、jacketChestSize,可选);
this.boutoniere=花束;
this.topHat=topHat;
}
//存取器和变异器
公共布尔getBout(){
返回花束;
}
公共布尔getTopHat(){
返回帽子;
}
公共空白收进(布尔花束){
this.boutoniere=花束;
}
public void setTopHat(布尔topHat){
this.topHat=topHat;
}
公共品牌(品牌){
这个品牌=品牌;
}
//方法
公共字符串toString(){
返回“晨装[Boutoniere”+getBout()+“TopHat”+getTopHat()+“颜色:”+GetColor()+“,每日成本:£”+String.format(%.2f),GetDailCost())
+\n传送带长度:“+GetTruserLength()+”,夹克胸围尺寸:“+GetJacketTestSize()
+“它可用吗?”+getAvailability()+“]”;
}
公共无效calcTotalPrice(国际努姆代谢德){
if(getBout()){
总价=总价+3;
}
if(getTopHat()){
总价=总价+10;
}
totalPrice=totalPrice+(numDaysHired*getDailyCost());
System.out.println(“晨衣是为”+numD租用的
public static void makeChangeToSuits(ArrayList<Suit> suits, Scanner input) {
for (Suit suit : suits) {
suit.updateBrand(input);
}
}
public static void displayAvailableSuits(ArrayList<Suit> suits) {
for (Suit suit : suits) {
if (suit.getAvailability()) {
System.out.println(suit.toString());
}
}
}