java - 尝试使用类对象作为方法的参数时出现“变量可能未初始化”的错误,尝试使用 IF 来避免该问题(JAVA)
问题描述
我正在学习 JAVA,我正在为商店中的产品销售系统编写代码作为示例。所以我创建了产品、客户和销售类。
在我的菜单中,我检查是否有已注册的产品或客户,如果是,我调用具有这两个参数的销售方法。但是,NetBeans 会通知一个错误,即变量尚未初始化,即使之前未完成注册的逻辑也不开始销售。
我的基本代码:“变量产品可能尚未初始化”“变量客户可能尚未初始化”
//Main
//Reg a client and a product, then perform a sale.
package store;
import java.util.Random;
import java.util.Scanner;
public class Store {
static boolean flagProduct=false, flagCustomer=false;
public static void main(String[] args) {
menu();
}
public static void menu() {
System.out.println("\n Choose option:\n"
+ "1 - Product Registration\n"
+ "2 - Customer Registration\n"
+ "3 - Sell\n"
+ "4 - End\n"
);
Scanner scanner = new Scanner(System.in);
int select = scanner.nextInt();
switch (select) {
case 1:
Product product = RegistProduct();
System.out.print("\n Cod product : " + product.getCodProduct());
System.out.print("\n Price product : " + product.getPriceProduct());
System.out.print("\n Quantity product : " + product.getQuantProduct());
menu();break;
case 2:
Customer customer = RegistCustomer();
System.out.print("\n Cod Customer : " + customer.getCodCustomer());
System.out.print("\n Customer name: " + customer.getName());
menu();break;
case 3:
if(flagProduct == true && flagCustomer==true){
Sale sale = sell(product, customer); // *****where the error happens*****
System.out.print("\n Sale code : " + sale.getCodSale());
System.out.print("\n Customer name: " + customer.getName());
System.out.print("\n Cod product : " + product.getCodProduct()+ " -- Quantity:" + sale.getQuantSale()+ " -- Total:" + sale.getValueSale());
} else if(flagProduct == true && flagCustomer==false){
System.out.println("First register the customer ");
menu();break;
} else if(flagProduct == false && flagCustomer==true){
System.out.println("First register the product");
menu();break;
} else
System.out.println("First register the customer and product");
menu();break;
case 4:
break;
default:
System.out.println("Error");
menu();
}
}
public static Product RegistProduct(){
Product product = new Product();
java.util.Scanner scanner = new Scanner(System.in);
System.out.print("Product code:\n");
product.setCodProduct(scanner.nextInt());
System.out.print("Product price:\n");
product.setPriceProduct(scanner.nextFloat());
System.out.print("Product quantity:\n");
product.setQuantProduct(scanner.nextInt());
flagProduct = true;
return product;
}
public static Customer RegistCustomer(){
Customer customer = new Customer();
java.util.Scanner scanner = new Scanner(System.in);
System.out.print("Customer name:\n");
customer.setName(scanner.nextLine());
System.out.print("Customer code:\n");
customer.setCodCustomer(scanner.nextInt());
flagCustomer=true;
return customer;
}
public static Sale Sell(Product product, Customer customer){
java.util.Scanner scanner = new Scanner(System.in);
Random num = new Random();
int codSale = num.nextInt(9999);
Sale sale = new Sale();
sale.setCodSale(codSale);
System.out.print("Customer code:");
int codSaleCustomer = scanner.nextInt();
while (codSaleCustomer != customer.getCodCustomer()){
System.out.print("Non-existent code. Please enter a valid code:\n");
codSaleCustomer = scanner.nextInt();
}
System.out.print("Product code:");
int codSaleProduct = scanner.nextInt();
while (codSaleProduct != product.getCodProduct()){
System.out.print("Non-existent code. Please enter a valid code:\n");
codSaleProduct = scanner.nextInt();
}
System.out.print("Quantity of products purchased:\n");
sale.setQuantSale(scanner.nextInt());
sale.setValueSale(sale.getQuantSale()*product.getPriceProduct());
product.setQuantProduct(product.getQuantProduct() - sale.getQuantSale());
System.out.print("Remaining quantity in stock: "+ product.getQuantProduct());
return sale;
}
}
//Class Product
package store;
public class Product {
private int codProduct;
private float priceProduct;
private int quantProduct;
public int getCodProduct() {
return codProduct;
}
public void setCodProduct(int codProduct) {
this.codProduct = codProduct;
}
public float getPriceProduct() {
return priceProduct;
}
public void setPriceProduct(float priceProduct) {
this.priceProduct = priceProduct;
}
public int getQuantProduct() {
return quantProduct;
}
public void setQuantProduct(int quantProduct) {
this.quantProduct = quantProduct;
}
}
//Class customer
package store;
public class Customer {
private int codCustomer;
private String name;
public int getCodCustomer() {
return codCustomer;
}
public void setCodCustomer(int codCustomer) {
this.codCustomer = codCustomer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Class sale
package store;
public class Sale extends Product{ //I dnno if extends is necessary
private int codSale;
private int quantSale;
private float valueSale;
public int getCodSale() {
return codSale;
}
public void setCodSale(int codSale) {
this.codSale = codSale;
}
public int getQuantSale() {
return quantSale;
}
public void setQuantSale(int quantSale) {
this.quantSale = quantSale;
}
public float getValueSale() {
return valueSale;
}
public void setValueSale(float valueSale) {
this.valueSale = valueSale;
}
}
解决方案
推荐阅读
- ruby-on-rails - Rails 6迁移到对不同于rails标准的列的引用
- php - 是否可以在 php 中创建对象时通过引用传递变量?
- security - 如何在 Spring Webflux 中启用无需身份验证的授权
- python - 简单的神经网络给出了 NaN 损失
- html - 为什么 CSS 设计文件无法正确显示?
- google-cloud-platform - 谷歌云 OAuth API 验证
- jenkins - 在并行阶段跨多个节点重用工作空间
- python - AttributeError:模块“tensorboard.summary”没有属性“pr_curve”
- c# - 该程序不包含适合作为入口点的静态 main 方法
- redis - Redigo 连接池 - 如何获得更多连接?