求大神给这道java题的代码,谢了
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(login())show();
}
//金额格式化
public static StringBuffer change(double d){
StringBuffer str = new StringBuffer(String.valueOf(d));
for(int i=str.indexOf(".")-3;i>0;i=i-3){
str.insert(i, ',');
}
return str;
}
/**
* 小样例
*/
public static boolean login(){
Boolean login = false;
while (true) {
Scanner in=new Scanner(System.in);
System.out.println("请输入用户名:");
String name=in.next();
System.out.println("请输入密码:");
String pwd=in.next();
if(name.toLowerCase().equals("tom") && pwd.equals("123")){
System.out.println("登陆成功!!");
login=true;
break;
}else{
System.out.println("用户名或密码错误!!");
login=false;
}
}
return login;
}
/**
* 展示信息
*/
public static void show(){
String [][] price = {{"电风扇","124.23"},{"洗衣机","4500.0"},{"电视机","8800.0"}};
Scanner in=new Scanner(System.in);
System.out.println("欢迎进入~");
while(true){
System.out.println("编号 商品 价格");
for (int i = 0; i < price.length; i++) {
System.out.println((i+1)+" "+price[i][0].toString()+" "+change(Double.parseDouble(price[i][1].toString())));
}
System.out.println("请输入要批发的商品编号:");
Integer num=in.nextInt();
if(num<=0 || num>price.length){
System.out.println("输入商品编号错误!!");
continue;
}
System.out.println("请输入要批发数量:");
Integer count=in.nextInt();
System.out.println("您需付款:"+change(count*Double.parseDouble(price[num-1][1].toString())));
System.out.println("再见~");
break;
}
}
}
//闲着没得干给你写一个.望采纳...
然后自动生成setId() getId方法,钱定义double就行了
public class Goods {
private int no;
private String name;
private Double price;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// TODO Auto-generated method stub
return getNo() + " " + getName() + " "
+ String.format("%,f%n", getPrice());
}
}
商品类
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean flag = true;
String user = null;
String pwd = null;
while (flag) {
System.out.println("请输入用户名:");
Scanner sc = new Scanner(System.in);
user = sc.nextLine();
System.out.println("请输入密码:");
pwd = sc.nextLine();
if (login(user, pwd)) {
flag = false;
System.out.println("登陆成功!");
show(sc);
} else
System.out.println("登录失败!");
}
}
private static boolean login(String user, String password) {
return "tom".equals(user) && "123".equals(password);
}
private static void show(Scanner sc) {
int no;
int shuliang;
StringBuffer sb = new StringBuffer(1000);
sb.append("************欢迎进入商品批发城*************\n");
sb.append("编号 商品 价格\n");
List<Goods> good=getAllGoods();
for (Goods goods : good) {
sb.append(goods);
}
sb.append("**************************************\n");
System.out.println(sb.toString());
System.out.println("请输入您批发的商品编号:");
no = sc.nextInt();
System.out.println("请输入批发数量:");
shuliang = sc.nextInt();
change(no, shuliang);
}
private static void change(int no, int shuliang) {
Goods good = findGoods(no);
if (good == null) {
System.out.println("没有该商品!");
return;
}
System.out.println("您需要付款:" + good.getPrice() * shuliang);
}
private static Goods findGoods(int no) {
for (Goods good : getAllGoods()) {
if (good.getNo() == no)
return good;
}
return null;
}
private static List<Goods> getAllGoods() {
List<Goods> goods = new ArrayList<Goods>();
Goods g1 = new Goods();
g1.setNo(1);
g1.setPrice(124.23);
g1.setName("电风扇");
Goods g2 = new Goods();
g2.setNo(2);
g2.setPrice(4500.0);
g2.setName("洗衣机");
Goods g3 = new Goods();
g3.setNo(3);
g3.setPrice(8800.9);
g3.setName("电视机");
Goods g4 = new Goods();
g4.setNo(4);
g4.setPrice(5000.88);
g4.setName("冰箱");
Goods g5 = new Goods();
g5.setNo(5);
g5.setPrice(2400.1);
g5.setName("空调");
goods.add(g1);
goods.add(g2);
goods.add(g3);
goods.add(g4);
goods.add(g5);
return goods;
}
}
主程序类