package com.tc.shop;
import java.util.Scanner;
/**
* 初始化超市中的商品信息
* @author Administrator
*
*/
public class Data {
/**
* 初始化所有商品
* @return
*/
public Product[] getAllProduct(){
Product pro1=new Product(1001,"红牛",5,100);
Product pro2=new Product(1002,"红牛1",5,100);
Product pro3=new Product(1003,"红牛2",5,100);
//int []nums=new int[100];nums[i]
Product[] pros=new Product[100];
pros[0]=pro1;
pros[1]=pro2;
pros[2]=pro3;
return pros;
}
// /**
// * 根据商品编号,查询商品信息
// *
// * @param no
// * @return
// */
// public boolean getProductByNo(int no){
// Product[] pros=getAllProduct();
// for (int i = 0; i < pros.length; i++) {
// if(pros[i]!=null){
// if(pros[i].getProdNo()==no){
//
// }
// }
// }
// }
/**
* 显示商品信息列表
*/
public void showProductList(Product []pro){
System.out.println("商品编号\t商品名称\t商品价格\t库存");
for (int i = 0; i < pro.length; i++) {
if (pro[i] != null) {
Product p = pro[i];
System.out.println(p.getProdNo() + "\t" + p.getProdName()
+ "\t" + p.getPrice()+"\t"+p.getCount());
}
}
}
/**
* 结账功能
*/
public void jieZhang(Product[] customer){
Scanner input=new Scanner(System.in);
double totalPay=0;
System.out.println("商品编号\t商品名称\t商品价格\t库存");
for (int i = 0; i < customer.length; i++) {
if (customer[i] != null) {
Product p = customer[i];
System.out.println(p.getProdNo() + "\t" + p.getProdName()
+ "\t" + p.getPrice()+"\t"+p.getCount());
totalPay+=p.getPrice()*p.getCount();
}
}
boolean isSuccess=false;
do{
System.out.println("需要支付:"+totalPay);
System.out.println("请输入支付金额:");
double money=input.nextDouble();
if(money<totalPay){
System.out.println("哥们,也太抠了吧");
isSuccess=false;
}else{
System.out.println("支付成功");
System.out.println("找零:"+(money-totalPay));
isSuccess=true;
}
}while(!isSuccess);
}
/**
* 购买商品
* @param pro
* @param customer
* @return
*/
public Product[] buy(Product[] pro,Product[] customer){
Scanner input=new Scanner(System.in);
String isGO="y";
do{
System.out.println("请输入要购买的产品编号");
int pid=input.nextInt();
System.out.println("请输入要购买的商品数量");
int count=input.nextInt();
Product product=null;
//选购商品
for (int i = 0; i < pro.length; i++) {
if(pro[i]!=null){
if(pro[i].getProdNo()==pid){
product=new Product(pro[i].getProdNo(),pro[i].getProdName(),pro[i].getPrice(),count);
//减去已经购买的数量
pro[i].setCount(pro[i].getCount()-count);
break;
}
}
}
if(product!=null){
for (int i = 0; i < customer.length; i++) {
if(customer[i]!=null){
//第二次购买,数量累加
if(customer[i].getProdNo()==product.getProdNo()){
customer[i].setCount(customer[i].getCount()+product.getCount());
break;
}
}else{//第一次购买
customer[i]=product;
break;
}
}
}
System.out.println("是否要继续?");
isGO=input.next();
}while(isGO.equals("y"));
return customer;
}
/**
* 购买完毕执行的操作
* @param customer
* @return
*/
public Product[] buyOver(Product[] customer){
Scanner input=new Scanner(System.in);
System.out.println("您购买的商品信息如下:");
//打印商品列表
showProductList(customer);
System.out.println("是否要退货?");
String isTui=input.next();
if(isTui.equals("y")){//退货
System.out.println("请输入要退货的商品编号:");
//退货编号
int tui_no=input.nextInt();
System.out.println("请输入要退货的商品数量:");
int tui_count=input.nextInt();
//记录退货的商品是否存在
boolean isExists=false;
for (int i = 0; i < customer.length; i++) {
if(customer[i]!=null){
if(customer[i].getProdNo()==tui_no){
if(customer[i].getCount()>=tui_count){
customer[i].setCount(customer[i].getCount()-tui_count);
}else{
System.out.println("你没有购买如此多的商品");
}
isExists=true;
break;
}else{
isExists=false;
}
}
}
if(isExists==false){
System.out.println("你没有购买此商品");
}
//打印商品列表
showProductList(customer);
}
return customer;
}
}
java 超市收银系统
2星 需积分: 50 127 浏览量
2013-03-13
16:40:39
上传
评论 5
收藏 3KB RAR 举报
zhaozhaoz0
- 粉丝: 0
- 资源: 1