假设我们正在开发一个收银系统,需要实现根据商品类型和数量计算价格的功能。在这个系统中,我们有三种商品类型:水果、蔬菜和零食。
首先,我们定义一个抽象的商品类型接口Product,其方法包含了计算价格的价格getPrice和获取商品名称的方法getName。我们还需要定义三个具体的商品类型实现类:Fruit、Vegetable和Snack,并在其中实现getPrice和getName方法。
接着,我们使用工厂模式,实现一个工厂类ProductFactory,其中包含一个静态的方法createProduct,其参数包括商品类型和数量,返回值为Product接口类型。在createProduct方法中,我们根据商品类型的不同,创建相应的商品类型实例,并返回Product接口类型。这样,我们可以根据用户输入的商品类型和数量,使用ProductFactory创建相应的商品实例,计算价格并输出。
最后,我们需要实现构造函数和toString方法,用于初始化商品的属性和输出商品信息。在商品实例中,我们定义了三个属性:商品类型type、商品名称name和商品单价price,并初始化它们的值。同时,我们重写了toString方法,用于输出商品的详细信息。
// 定义Product接口
public interface Product {
String getName();
double getPrice(int quantity);
}
Fuit.Java
// 定义Fruit类,实现Product接口
public class Fruit implements Product {
private String name;
private double price;
public Fruit(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String getName() {
return name;
}
@Override
public double getPrice(int quantity) {
return price * quantity;
}
@Override
public String toString() {
return "Fruit: " + name + ", Price: " + price;
}
}
Vegetable.java
// 定义Vegetable类,实现Product接口
public class Vegetable implements Product {
private String name;
private double price;
public Vegetable(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String getName() {
return name;
}
@Override
public double getPrice(int quantity) {
return price * quantity;
}
@Override
public String toString() {
return "Vegetable: " + name + ", Price: " + price;
}
}
Snack.java
// 定义Snack类,实现Product接口
public class Snack implements Product {
private String name;
private double price;
public Snack(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String getName() {
return name;
}
@Override
public double getPrice(int quantity) {
return price * quantity;
}
@Override
public String toString() {
return "Snack: " + name + ", Price: " + price;
}
}
ProductFactory.java
// 定义ProductFactory工厂类,创建商品实例
public class ProductFactory {
public static Product createProduct(String type, String name, double price) {
if (type.equalsIgnoreCase("Fruit")) {
return new Fruit(name, price);
} else if (type.equalsIgnoreCase("Vegetable")) {
return new Vegetable(name, price);
} else if (type.equalsIgnoreCase("Snack")) {
return new Snack(name, price);
} else {
throw new IllegalArgumentException("Invalid product type: " + type);
}
}
}
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 获取商品类型和数量
System.out.print("Enter product type (Fruit, Vegetable, Snack): ");
String type = input.nextLine();
System.out.print("Enter product name: ");
String name = input.nextLine();
System.out.print("Enter product price: ");
double price = Double.parseDouble(input.nextLine()); // 将字符串转为double
System.out.print("Enter product quantity: ");
int quantity = Integer.parseInt(input.nextLine()); // 将字符串转为int
// 根据商品类型和数量,使用ProductFactory创建相应的商品实例
Product product = ProductFactory.createProduct(type, name, price);
// 输出商品价格和详细信息
System.out.println("Total price: " + product.getPrice(quantity));
System.out.println(product.toString());
}
}
根据用户输入的商品类型、名称、价格和数量,在Main类中使用ProductFactory创建相应的商品实例,并计算其价格和输出其详细信息。在Product、Fruit、Vegetable和Snack类中,我们分别定义了Product接口和其实现类,并实现了两个方法getName和getPrice,在toString方法中输出了商品类型、名称和价格。在ProductFactory类中,我们定义了一个静态方法createProduct,根据商品类型的不同,创建不同类型的商品实例,并返回Product接口类型。
另一个main方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueAddingProducts = true; // 默认为继续添加新商品
// 如果用户输入"q",退出程序
while (continueAddingProducts) {
System.out.println("--- Enter q to quit the program. ---");
// 获取商品类型,并判断是否退出程序
String type = "";
while (!type.equalsIgnoreCase("Fruit") && !type.equalsIgnoreCase("Vegetable") && !type.equalsIgnoreCase("Snack") && !type.equalsIgnoreCase("q")) {
System.out.print("Enter product type (Fruit, Vegetable, Snack): ");
type = input.nextLine();
}
if (type.equalsIgnoreCase("q")) {
System.out.println("Exiting program...");
break;
}
// 获取商品名称、价格和数量
System.out.print("Enter product name: ");
String name = input.nextLine();
double price = 0;
while (price <= 0) {
System.out.print("Enter product price (must be positive): ");
try {
price = Double.parseDouble(input.nextLine());
} catch (NumberFormatException ex) {
System.out.println("Invalid input. Please enter a positive number.");
}
}
int quantity = 0;
while (quantity <= 0) {
System.out.print("Enter product quantity (must be positive): ");
try {
quantity = Integer.parseInt(input.nextLine());
} catch (NumberFormatException ex) {
System.out.println("Invalid input. Please enter a positive integer.");
}
}
// 根据商品类型和数量,使用ProductFactory创建相应的商品实例
Product product = ProductFactory.createProduct(type, name, price);
// 输出商品价格和详细信息
System.out.println("Total price: " + product.getPrice(quantity));
System.out.println(product.toString());
// 询问用户是否继续添加新商品
System.out.print("Add another product? (y/n) ");
String choice = input.nextLine();
if (!choice.equalsIgnoreCase("y")) {
continueAddingProducts = false;
}
}
}
}
在这个修改后的代码中,我们加入了一个新的boolean类型变量continueAddingProducts,用于控制是否继续添加新商品。在主程序的while循环中,如果用户选择"q",则通过break语句退出整个程序;否则,程序在每次输出完成商品价格和详细信息后,都会询问用户是否继续添加新商品,并根据用户的回答修改continueAddingProducts的值,确保程序能够循环向用户请求添加新商品。