如题,上代码:
package com.mxy.HomeWork;
import java.util.Scanner;
/**
* 2:登录验证通过后,显示批发商品信息; 输入批发商品编号和数量,以指定格式显示总金额
* <p>
* 实现思路
* A、创建类Goods
* B、创建方法show()
* C、创建方法change()
*
* @author MXHstrat
* @create 2021 - 11 - 03 20:44
*/
public class Test02 {
public static void main(String[] args) {
Goods g1 = new Goods();
if (g1.register()) {
g1.login();
g1.show();
g1.change();
}
}
}
class Goods {
String name;
String psw;
String logname;
String logpsw;
commodity[] comd = new commodity[1000];
public void login() {//登录
while (true) {
System.out.println();
System.out.println("----------------------------------");
System.out.print("请输入您登录的用户名:");
logname = new Scanner(System.in).next();
System.out.print("请输入您登录的密码:");
logpsw = new Scanner(System.in).next();
if (logname.equals(name) && logpsw.equals(psw))
break;
else System.err.println("账号与密码不匹配,请重新输入!");
System.out.println();
}
System.out.println("登录成功!");
System.out.println("----------------------------------");
}
public boolean register() {//注册
System.out.println("----------------------------------");
System.out.println("欢迎使用,请注册!");
System.out.print("请输入您的用户名:");
name = new Scanner(System.in).next();
System.out.print("请输入您的密码:");
psw = new Scanner(System.in).next();
if (name != null && psw != null) {
System.out.println("注册成功!请牢记您的密码");
System.out.println("name = " + name);
System.out.println("psw = " + psw);
System.out.println("----------------------------------");
return true;
}
return false;
}
public void show() {
commodity s1 = new commodity();
s1.cdname = "娃哈哈";
s1.cdprice = 10;
commodity s2 = new commodity();
s2.cdname = "百事可乐";
s2.cdprice = 8;
commodity s3 = new commodity();
s3.cdname = "N95口罩";
s3.cdprice = 15;
comd[0] = s1;
comd[1] = s2;
comd[2] = s3;
System.out.println();
System.out.println("----------------------------------");
System.out.println("商品名称\t\t商品价格");
for (int i = 0; i < comd.length; i++) {
if (comd[i] != null) {
System.out.println(comd[i].cdname + "\t" + "\t" + comd[i].cdprice + "元");
} else break;
}
System.out.println("----------------------------------");
System.out.println();
}
public void change() {
String pfname;
int count = 0;
while (true) {
System.out.println("----------------------------------");
System.out.println("请输入您要批发商品的名称:(按0结束)");
pfname = new Scanner(System.in).next();
if (pfname.equals("0")) break;
System.out.println("请输入您要批发商品的数量:");
count = new Scanner(System.in).nextInt();
for (int i = 0; i < comd.length; i++) {
if (comd[i] != null) {
if (comd[i].cdname.equals(pfname) && count > 0) {
System.out.println("您的订单总金额为:" + comd[i].cdprice * count);
break;
}
} else {
System.err.println("找不到你输入的商品信息,请重新输入!");
break;
}
}
}
System.out.println("----------------------------------");
}
}
class commodity {
String cdname;
int cdprice;
// int cdcount;
}