通过方法重载优化商品信息查询功能(只实现了商品查询功能)
未使用方法重载的代码实现:
分析:该方法使用Goods用于中转,通过调用Goods方法,进一步调用Goods1,Goods2或Goods3中的来实现分别通过不同的条件查询商品信息。
public class Test11 {
public static void main(String[] args) {
int[] arr = {1001, 1002, 1003, 1004, 1005};
String[] strings = {"华为", "苹果", "小米", "oppo", "鸭梨"};
double[] arr1 = {300.00,200.00,300.00,400.00,500.00};
while (true) {
System.out.println("1:商品信息展示,2:商品信息录入,3:商品信息查询,4:退出的功能");
Scanner sc = new Scanner(System.in);
switch (sc.nextInt()) {
case 1:
System.out.println("商品信息展示");
break;
case 2:
System.out.println("商品信息录入");
break;
case 3:
System.out.println("商品信息查询");
Goods(strings, arr, arr1);
break;
case 4:
System.out.println("系统退出");
return;
default:
System.out.println("请输入1到4的数字");
break;
}
}
}
//查询商品信息的方法
private static void Goods(String[] strings, int[] arr, double[] arr1) {
System.out.println("请选择你要通过什么条件查询商品");
Scanner sc = new Scanner(System.in);
System.out.println("1,商品名称;2,商品编号;3,商品价格:");
String a = sc.next();
if ("1".equals(a)) {
System.out.println("请输入商品名称:");
Goods1(sc.next(), strings, arr, arr1);
} else if ("2".equals(a)) {
System.out.println("请输入商品编号:");
Goods2(sc.nextInt(), strings, arr, arr1);
} else if ("3".equals(a)) {
System.out.println("请输入商品价格:");
Goods3(sc.nextDouble(), strings, arr, arr1);
} else {
System.out.println("输入错误,返回菜单");
}
}
//通过价格查询商品信息的方法
private static void Goods3(double jiaGe, String[] strings, int[] arr, double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < arr1.length; i++) {
if (jiaGe == arr1[i]) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
//通过编号查询商品信息的方法
private static void Goods2(int bianHao, String[] strings, int[] arr, double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (bianHao == arr[i]) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
//通过名称查询商品信息的方法
private static void Goods1(String str, String[] strings, int[] arr, double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < strings.length; i++) {
if (str.equals(strings[i])) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
}
结果:
使用方法重载的代码实现:
分析:使用方法重载把所有的商品查询的方法定义为Goods,通过不同的查询条件,从而调用不同的方法,虽然方法名相同,但编译器可以通过参数类型的不同从而精确的找到需要调用的方法。
代码实现:
public class Test10 {
public static void main(String[] args) {
int[] arr = {1001, 1002, 1003, 1004, 1005};
String[] strings = {"华为", "苹果", "小米", "oppo", "鸭梨"};
double[] arr1 = {300.00,200.00,300.00,400.00,500.00};
while (true) {
System.out.println("1:商品信息展示,2:商品信息录入,3:商品信息查询,4:退出的功能");
Scanner sc = new Scanner(System.in);
switch (sc.nextInt()) {
case 1:
System.out.println("商品信息展示");
break;
case 2:
System.out.println("商品信息录入");
break;
case 3:
System.out.println("商品信息查询");
System.out.println("请选择你要通过什么条件查询商品");
System.out.println("1,商品名称;2,商品编号;3,商品价格:");
String a = sc.next();
if ("1".equals(a)) {
System.out.println("请输入商品名称:");
Goods(sc.next(), strings, arr, arr1);
} else if ("2".equals(a)) {
System.out.println("请输入商品编号:");
Goods(sc.nextInt(), strings, arr, arr1);
} else if ("3".equals(a)) {
System.out.println("请输入商品价格:");
Goods(sc.nextDouble(), strings, arr, arr1);
} else {
System.out.println("输入错误,返回菜单");
}
break;
case 4:
System.out.println("系统退出");
return;
default:
System.out.println("请输入1到4的数字");
break;
}
}
}
//通过名称查询商品信息的方法
private static void Goods(String str, String[] strings,int[] arr,double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < strings.length; i++) {
if (str.equals(strings[i])) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
//通过编号查询商品信息的方法
private static void Goods(int bianHao, String[] strings,int[] arr,double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (bianHao == arr[i]) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
//通过价格查询商品信息的方法
private static void Goods(double qian, String[] strings,int[] arr,double[] arr1) {
System.out.println();
int count = 0;
for (int i = 0; i < arr1.length; i++) {
if (qian == arr1[i]) {
System.out.println("商品编号;" + arr[i]);
System.out.println("商品名称;" + strings[i]);
System.out.println("商品价格;" + arr1[i]);
count++;
System.out.println();
}
}
System.out.println("总共查询到" + count + "条信息");
}
}
结果:
结论:对于这种需要实现相同功能,但使用的场景不同的方法可以使用方法重载。而且java是面向对象的,一个对象有多种形态,每个状态的处理和变现都是不一样的,这一点与自然界的物体相同,重载的好处在于,对象在不同状态下(多态)调用重载函数得到的结果可以不一样。
暑期编程PK赛 得CSDN机械键盘等精美礼品!


