java - 将用户插入用于 getter 时出现问题
问题描述
这段代码接下来会做:
用户显示一个航班列表,每个都有自己的 ID,并选择输入 ID。我通过检查用户选择了哪个 ID 来打印该航班。
public void chooseFlight(ArrayList<Flight> flightList) {
System.out.println("Choose one flight by ID: ");
int userChoiceFlight = scanner.nextInt();
scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
}
}
}
如您所见,这是主要部分:
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
现在我正在创建名为 的新方法promoCode
,其中发生了什么:
我问用户是否有促销代码,如果有我用他选择的 ID 航班检查该代码,每个航班都有自己的促销代码。
我的问题是什么,在这种方法中我无法访问用户选择的航班。这是方法:
@Override
public void promoCode(ArrayList<Flight> flightList) {
System.out.println("Do you have promo code? (yes / no) ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userPromoCode.contains(tempUserChoiceFlight.getPromoCode())) {
System.out.println("Cool you have discount!");
break;
} else if (yesNo.contains("no")) {
System.out.println("Find one, you have no discount!");
break;
} else if (!userPromoCode.contains(tempUserChoiceFlight.getPromoCode())) {
System.out.println("Promo code is not valid!");
break;
} else {
System.out.println("Wrong input!");
}
}
}
}
这是一种多合一的方法,我乘坐用户航班并检查促销代码,这里工作正常,我想用两种方法分开用户选择航班和检查促销代码。
此方法有效:
public void chooseFlight(ArrayList<Flight> flightList) {
System.out.println("Choose one flight by ID: ");
int userChoiceFlight = scanner.nextInt();
scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
System.out.println("Do you have promo code? (yes / no) ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
if (userPromoCode.contains(tempUserChoiceFlight.getPromoCode())) {
System.out.println("Cool you have discount!");
break;
} else {
System.out.println("Promo code is not valid!");
break;
}
} else if (yesNo.contains("no")) {
System.out.println("Find one, you have no discount!");
} else {
System.out.println("Wrong input!");
}
}
我在这里检查促销代码:
if (userPromoCode.contains(tempUserChoiceFlight.getPromoCode())) {
再次,在新方法中,我无法获取用户选择的航班 ID 来比较所选航班的促销代码和用户输入的促销代码。
解决方案
假设你的类的名字是FlightSelector并且“ chooseFlight ”和“ promoCode ”方法都在这个类中,使用下面的代码:
public class FlightSelector {
private Flight selectedFlight;
public void chooseFlight(ArrayList<Flight> flightList) {
System.out.println("Choose one flight by ID: ");
int userChoiceFlight = scanner.nextInt();
scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
selectedFlight = temUserChoiceFlight;
promoCode(flightList);
break;
}
}
}
@Override
public void promoCode(ArrayList<Flight> flightList) {
System.out.println("Do you have promo code? (yes / no) ");
String yesNo = scanner.nextLine();
if (yesNo.equalsIgnoreCase("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
if (userPromoCode.equalsIgnoreCase(selectedFlight.getPromoCode())) {
System.out.println("Cool you have discount!");
} else {
System.out.println("Promo code is not valid!");
}
} else {
System.out.println("Find one, you have no discount!");
}
}
}
我还假设您无法更改promoCode方法的签名,因此我不将selectedFlight作为参数传递。否则,我建议将签名更改为:
public void promoCode(Flight selectedFlight)
在这种情况下,您不需要:
private Flight selectedFlight;
推荐阅读
- node.js - 代理不适用于快递。好像没有定义
- codeigniter - 使用 SMTP Codeigniter 发送的电子邮件在不同的电子邮件提供商收件箱中中断
- sql-server - 在 SQL Server 的表中插入 2 个外键
- android - 将 Firestore 项目变为计数 - Android Studio
- javascript - NgRx 效果,出现失败,不接受新动作
- terminal - 如何从终端删除powerlevel9
- javascript - 在 MUI 中使用 makeStyles() 进行条件渲染
- typescript - 如何根据另一个属性的值限制属性中的建议值?
- node.js - 使用 Bot Framework 通过 NodeJS 发布到 Microsoft Teams 频道
- sql - 在数据库字段中搜索特定格式值