package collection_homework.class7;
import java.util.Scanner;
/**
* @Auther: Yu Panpan
* @Date: 2022/3/28 - 03 - 28 - 8:06
* @Description: collection_homework.class7
* @version: 1.0
*
* 实现商品批发总金额显示
*/
public class Goods {
//定义存储商品名称的数组
String[] products = new String[]{"电风扇","洗衣机","电视机","冰箱","空调机"};
//定义存储商品价格的数组
double[] prices = new double[]{124.23,4500.0,8800.9,5000.88,4456.0};
//登录验证
public boolean login(){
//验证是否成功的一个标识,默认为false
boolean flag = false;
Scanner input = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String password = input.next();
//验证
if("TOM".equals(username) && "123".equals(password)){
//合法
System.out.println("登录成功!");
//修改标识位
flag = true;
}
return flag;
}
//显示商品列表
public void show(){
System.out.println("*********************欢迎进入商品批发城*********************");
System.out.println("\t\t编号\t商品\t价格");
//遍历商品名称
for (int i = 0; i < products.length; i++) {
System.out.print("\t\t" + (i+1));
System.out.print("\t" + products[i]);
System.out.print("\t" + change(prices[i]) + "\n");
}
System.out.println("***********************************************************");
}
//按照指定格式输出价格
public String change(double price){
StringBuffer stringBuffer = new StringBuffer(String.valueOf(price));
//根据传递的价格在合适的位置插入,
for (int i = stringBuffer.indexOf(".") - 3; i>0; i = i-3) {
//插入,
stringBuffer.insert(i,",");
}
return stringBuffer.toString();
}
//测试方法
public static void main(String[] args) {
Goods goods = new Goods();
Scanner input = new Scanner(System.in);
//验证用户是否是合法用户
if(goods.login()){
//显示商品列表
goods.show();
System.out.print("请输入您批发的商品编号:");
int proId = Integer.parseInt(input.next());
System.out.print("请输入批发数量:");
int proNum = Integer.parseInt(input.next());
//计算总价格
double totalPrice = goods.prices[proId-1] * proNum;
System.out.println("您需要付款:" + goods.change(totalPrice));
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)