java购物车系统源代码

import java.util.Scanner;

public class ShopCar_Client {
ShopCar_Manager manager = new ShopCar_Manager();
Scanner input = new Scanner(System.in);

// 购物车
int saveNo[] = new int[10]; // 存储商品编号
String saveName[] = new String[10]; // 存储商品名称
int savePrice[] = new int[10]; // 存储商品价格
String saveInfo[] = new String[10]; // 存储商品信息
int saveAmount[] = new int[10]; // 存储商品数量
int shuliang = 0; // 购买商品的数量
int Num; // 购买商品的编号

// 显示仓库中的商品
public void showShop(int[] quotID, String[] quotName, int[] quotNum,
String[] quotInfo, int[] quotPrice) {
System.out.println("现在库存里的商品数量有:");
System.out.println("商品编号\t\t" + "商品名称\t\t" + "商品数量" + "\t\t商品信息"
+ "\t\t" + "商品价格");
for (int i = 0; i < quotID.length; i++) {
if (quotID[i] == 0) {
break;
}
System.out.println(quotID[i] + "\t\t" + quotName[i] + "\t\t"
+ quotNum[i] + "\t\t" + quotInfo[i] + "\t" + quotPrice[i]);
}
}

// 购买商品
public void goumai(int[] quotID, String[] quotName, int[] quotNum,
String[] quotInfo, int[] quotPrice) { // 购买商品
String answer = "";
do {
System.out.println("********************************************");
System.out.println("请选择 1.购买商品 2.查询购物车 3.保存商品订单 4.退出");
System.out.println("********************************************");
int num1 = input.nextInt();
switch (num1) {
case 1: // 购买商品
System.out.print("请输入你要够买的商品的编号:");
Num = input.nextInt();
int index = -1;
for (int x = 0; x < quotID.length; x++) { // 在库存中循环找到需购买商品的编号
if (quotID[x] == 0) {
break;
}
if (quotID[x] == Num) {
index = x;
break;
}
}
if (index != -1) {
System.out.print("请输入你要够买的商品的数量:"); // 若有此商品就提示用户输入购买的数量
shuliang = input.nextInt();
if (shuliang > 0) { // 判断输入的数量是否大于0
for (int i = 0; i < quotNum.length; i++) {
if (saveNo[i] == Num) { // 判断购物车里是否有此商品
quotNum[index] = quotNum[index] + saveAmount[i]; // 货架上商品数量
saveAmount[i] = saveAmount[i] + shuliang; // 购物车内商品数量

manager.quotNum[index] = manager.quotNum[index]- saveAmount[index]; // 购买商品后台减少数量
showShop(manager.quotID, manager.quotName,
manager.quotNum, manager.quotInfo,
manager.quotPrice);
break;
}
if (saveNo[i] == 0) { // 找到存储商品的空数组
saveNo[i] = quotID[index]; // 编号
saveName[i] = quotName[index]; // 名称
savePrice[i] = quotPrice[index]; // 价格
saveInfo[i] = quotInfo[index]; //

信息
if (shuliang <= quotNum[index]) {
saveAmount[i] = saveAmount[i] + shuliang; // 购物车内的商品数量
manager.quotNum[index] = manager.quotNum[index]- saveAmount[i]; // 购买商品后台减少数量
System.out.println("操作完成!!");
showShop(manager.quotID, manager.quotName,
manager.quotNum, manager.quotInfo,
manager.quotPrice);
} else {
System.out.println("抱歉,商品数量只有:"
+ quotNum[index]);
}
break;
}
}
} else {
System.out.println("抱歉,你的输入有错误!");
}//
} else {
System.out.println("抱歉,没有该商品!");
}
break;
case 2:
purchasesn(); // 查询购物车内商品
break;
case 3: // 确定购买商品结账退出
getAllMoney(); // 打折前的总价格
getScore(); // 计算积分
getZheKou(); // 计算折扣
payPrice(); // 打折后的价格
saveMenu(); // 结账菜单
break;
case 4:
break;
default:
System.out.println("没有你所选的选项!!!");
}
System.out.println("是否继续:(y/n)"); // 是否回到购买菜单
answer = input.next();

} while (answer.equals("y"));

}

/**
* 查看商品 修改商品 删除商品
*
*/
String answer = "";
boolean big = true;

public void purchasesn() {
int index1 = -1;
int index2 = -1;
System.out.println("商品编号\t\t" + "商品名称\t\t" + "商品数量" + "\t\t商品信息"
+ "\t\t商品价格");
for (int a = 0; a < saveNo.length; a++) { // 循环输出购物车里的商品
if (saveNo[a] != 0) {
System.out.println(saveNo[a] + "\t\t" + saveName[a] + "\t\t"
+ saveAmount[a] + "\t\t" + saveInfo[a] + "\t\t"
+ savePrice[a]);
big = false;
}
}
System.out.println("\n1.修 改 商 品 2.删 除 商 品 3.返 回");
System.out.println("(如果你要查询库存请去购买商品)");
int num = input.nextInt();
switch (num) {
case 1:
System.out.print("请选择需修改的商品编号:");
int num1 = input.nextInt();
for (int i = 0; i < saveNo.length; i++) { // 循环查找购物车内相对应是商品
if (saveNo[i] == 0) {
break;
}
if (saveNo[i] == num1) {
index1 = i;
break;////
}
}
for (int i = 0;i< manager.quotID.length; i++) { // 循环查找货架上相对应是商品
if (manager.quotID[i] == 0) {
break;
}
if (manager.quotID[i] == num1) {
index2 = i;
}
}
if (index1 != -1) {
System.out.print("请输入要修改的商品数量:");
int xiugaiNum = input.nextInt();

if (xiugaiNum > 0&& xiugaiNum < (manager.quotNum[index2] + saveAmount[index1])) { // 修改的数量的小于总数量
manager.quotNum[index2] = manager.quotNum[index2]+ saveAmount[index1];
saveAmount[index1] = xiugaiNu
m;
manager.quotNum[index2] = manager.quotNum[index2]- saveAmount[index

1];
System.out.println("修改成功!!");
} else {
System.out.println("库存商品不足,您不能修改!!");
}
} else {
System.out.println("没有您要修改的商品!!!");
}
break;
case 2:
System.out.print("请输入要删除的商品编号:");
int num2 = input.nextInt();
for (int i = 0; i < saveNo.length; i++) { // 循环查找购物车内的相对应是商品
if (saveNo[i] == 0) {
break;
}
if (saveNo[i] == num2) {
index1 = i;
}
}
for (int i = 0; i < manager.quotID.length; i++) { // 循环查找货架上的相对应是商品
if (manager.quotID[i] == 0) {
break;
}
if (manager.quotID[i] == num2) {
index2 = i;
}
}
if ((index1 != -1) && (index2 != -1)) {//
if (num2 == saveNo[index1]) { // 删除
manager.quotNum[index2] = manager.quotNum[index2]
+ saveAmount[index1];
if (index1 == saveNo.length - 1) { // 删除最后一个商品
saveNo[index1] = 0;// 编号
saveName[index1] = null; // 名称
savePrice[index1] = 0; // 价格
saveInfo[index1] = null; // 信息
saveAmount[index1] = 0; // 数量
}

for (int i = index1; i < saveNo.length - 1; i++) {
saveNo[i] = saveNo[i + 1]; // 编号
saveName[i] = saveName[i + 1]; // 名称
savePrice[i] = savePrice[i + 1]; // 价格
saveInfo[i] = saveInfo[i + 1]; // 信息
saveAmount[i] = saveAmount[i + 1]; // 数量
}
System.out.println("删除成功!!");
}

} else {
System.out.println("没有您要删除的商品!!");//
}
break;
default:
showShop(manager.quotID, manager.quotName, manager.quotNum,
manager.quotInfo, manager.quotPrice);
goumai(manager.quotID, manager.quotName, manager.quotNum,
manager.quotInfo, manager.quotPrice);
}
if (big) {
System.out.println("购物车暂时没有商品,请先购买商品然后再查看!");
}
}

/***************************************************************************
* 用户在购买商品时,根据用户购买商品所花的钱来给用户加积分
*
*/
int allMoney = 0; // 购买商品,计算折扣前的总价
int score = 100; // 定义初始积分。这里大家可以有多个会员进行登录,每个会员设置不同的积分变量存放各自的积分
double zhekou = 0.0; // 具体的折扣数
double pay; // 实际应付款数

public double getAllMoney() { // 购买商品,计算折扣前的总价
for (int i = 0; i < saveNo.length; i++) {
if (saveNo[i] == 0) {
break;
}
allMoney = allMoney + savePrice[i] * saveAmount[i]; // 价格累加

}
return allMoney;
}

public int getScore() { // 购物所得的积分
score = score + (int) (allMoney * 3 / 100);
return score;
}

// 根据积分求折扣
public double
getZheKou() {
if (score < 200) {
zhekou = 0.9;
} else if (score >= 200 && score < 400) {
zhekou = 0.8;
} else if (score >= 400 && score < 800) {
zhekou = 0.7;
} else if (score >= 800) {
zh

ekou = 0.6;
}
return zhekou;
}

/***************************************************************************
* 用户在选择 “保存商品订单”的时候,用来下订单,根据用户积分来算折扣,然后求出实际应该付的钱数
*
* @param money:
* 用户输入的钱数
*
*/
public double payPrice() { // 打折后的价格
pay = zhekou * allMoney;
return pay;
}

// 用户窗体菜单类,调用总金额,积分,折扣等方法
public void saveMenu() {
String answer = "";
System.out.println("您购买商品的总价为:\t" + allMoney + "元");
System.out.println("您目前的积分是:\t" + score + "分");
System.out.println("根据您购物的金额,您享有:\t" + zhekou + "的折扣优惠!");
System.out.println("您最后应付款为:\t" + pay);
do {
System.out.println("请输入您要付款的金额:\t");
double money = input.nextDouble();
if (money < pay) { // 输入的金额不足时
System.out.println("不好意思您输入的金额不足!不能完成交易!" + "\n是否继续交易(y/n):");
answer = input.next();
} else {
System.out.println("您好!交易成功!!" + "\n找回您:" + (money - pay));
System.out.println("感谢您的购物,你可以享受本超市的抽奖活动!");
System.out.println("规则:输入1~3"); // 结账完了进行抽奖活动
int shuzi = input.nextInt();
int random = (int) ((Math.random() * 3) + 1);
if (random == shuzi) {
System.out.println("恭喜你,获得苹果电脑一台!!欢迎下次光临");
} else {
System.out.println("谢谢参与,欢迎下次光临!!");
}
break;
}
} while (answer.equals("y"));

}
}
import java.util.Scanner;

public class ShopCar_Manager {

String userName = "manager"; // 定义管理员的用户名和密码
String password = "0000";
String user = ""; // 定义输入的管理员用户名和密码
String pwd = "";

public int i = 0; // 用来记录输入的管理员用户名或密码错误的次数.定义成全局的是为了在测试类中能用到
public int[] quotID = new int[10]; // 商品编号
public String[] quotName = new String[10]; // 商品名称
public int[] quotNum = new int[10]; // 商品数量
public String[] quotInfo = new String[10]; // 商品介绍
public int[] quotPrice = new int[10]; // 商品价格
Scanner input = new Scanner(System.in);

/**
* 初始化库存的商品信息
*/
public void initial() {
quotID[0] = 1;
quotName[0] = "java";
quotNum[0] = 20;
quotInfo[0] = "真是太牛了!!!";
quotPrice[0] = 87;

quotID[1] = 2;
quotName[1] = "net";
quotNum[1] = 28;
quotInfo[1] = "太好
用了!!!";
quotPrice[1] = 69;

quotID[2]

= 3;
quotName[2] = "c#";
quotNum[2] = 34;
quotInfo[2] = "太方便了!!!";
quotPrice[2] = 56;

quotID[3] = 4;
quotName[3] = "sql";
quotNum[3] = 42;
quotInfo[3] = "太实用啦!!!";
quotPrice[3] = 59;
}


//编写管理员用户登录的方法

public void login() {
boolean big = true;
for (int i = 3; i > 0; i--) {
System.out.print("请输入管理员用户名:");
user = input.next();
System.out.print("请输入管理员密码:");
pwd = input.next();
if (user.equals(userName) && pwd.equals(password)) {
menu();
big = false;
return;
} else {
System.out.println("用户名或密码有误,请重新输入,你还有" + (i - 1) + "次机会:");
}
}
if (big) {
System.out.println("很抱歉,你输入的次数有限不能在输入!!");
}
}


// 商品主菜单

public void menu(){
// 输出管理员界面的主菜单
System.out.println("商品管理系统 > 商品主菜单");
System.out.println("************************************************************************");
System.out.println("\t\t1、添加商品信息");
System.out.println("\t\t2、修改商品信息");
System.out.println("\t\t3、删除商品信息");
System.out.println("\t\t4、查询商品信息");
System.out.println("\t\t0、退出");
System.out.println("************************************************************************");
System.out.println("请输入你要执行的功能编号:");
int num = input.nextInt();
switch (num) {// 判断如果num是上面的商品主菜单的功能
case 1:
addShop(); //添加商品
break;
case 2:
rework(); //修改商品
break;
case 3:
delete(); //删除商品
break;
case 4:
showAllShop(); //显示商品
break;
case 0:
break;
default :// 如果输入的是商品菜单里没有的功能后执行下面
System.out.println("没有您输入的选项,返回登陆菜单!!");
menu();
break;// returnMenu();
}

}




// 显示所有商品信息的方法
public String answer = "";
public void showAllShop(){
System.out.println("是否显示所有商品?(y/n)");
answer =input.next();
if(answer.equals("y")){
System.out.println("商品编号\t\t"+"商品名称\t\t"+"商品数量"+"\t\t产品信息"+"\t\t产品价格");

for (int i = 0; i < quotID.length; i++) { // 循环数组编号里的元素
if (quotID[i] != 0){ // 判断如果货架上的商品编号不为空的情况下显示所有商品信息
System.out.println(quotID[i] + "\t\t" + quotName[i]
+ "\t\t" + quotNum[i]+"\t\t"+quotInfo[i]+"\t\t"+quotPrice[i]);
}else{ // 如果没有,则结束本次循环
break;
}
}
}else if(answer.equals("n")){
lookUp();
}else {
Syste

m.out.println("您是
输入有误!返回主菜单!");
menu(); // 显示返回菜单
}
lookUp(); //按名称查询商品

}


//查询单个商品菜单

public void lookUp(){
System.out.println("是否按照商品编号查询显示商品?(y/n)");
answer =input.next();
if(answer.equals("y")){
System.out.print("请输入商品编号");
int shopid = input.nextInt();
for (int i = 0; i < quotID.length; i++){
if( quotID[i]==shopid ){
System.out.println("商品编号\t\t"+"商品名称\t\t"+"商品数量"+"\t\t产品信息"+"\t\t产品价格");
System.out.println(quotID[i] + "\t\t" + quotName[i]+ "\t\t" + quotNum[i]+"\t\t"+quotInfo[i]+"\t\t"+quotPrice[i]);
break;
}
if(quotID[i]==0){
System.out.print("抱歉!没有您要选购的商品!");
break;
}
}

System.out.print("\n按'n'返回主菜单:");
answer=input.next();
if(answer.equals("n")){
menu(); // 显示返回菜单
}else {
System.out.println("您的输入有误!返回主菜单!");
menu();
}
}else if(answer.equals("n")){
menu(); // 显示返回菜单
}else {
System.out.println("您的输入有误!返回主菜单!");
menu(); // 显示返回菜单
}
}


// 返回主菜单的方法
public void returnMenu(){
menu();
}


// 添加商品菜单
boolean big=true;
public void addShop() { // 添加货架上的商品编号进行判断,判断货架上是否有该商品编号
System.out.print("请输入商品编号:"); //添加的编号
int bianhao = input.nextInt();
int index = -1;
for (int q = 0; q < quotID.length; q++) { // 循环数组编号里的元素
if(bianhao==quotID[q]){ // 如果输入的是编号重复执行下面操作
System.out.println("输入的编号重复,返回主菜单查看后再添加!!");
menu(); // 显示返回主菜单功能
return;
}
if (quotID[q] == 0) { // 如果输入的编号循环去和货架上的编号数组进行判断,如果相等就执行下面语句
index=q;
break;
}
}
do{
System.out.print("请输入商品名称:");
String shopName = input.next(); // 添加货架上的商品名称

for(int i=0;i<quotID.length;i++){ //循环判断添加的名称
if(shopName.equals(quotName[i])){
System.out.println("输入的名称重复,返回主菜单查看后再添加!!");
menu(); // 显示返回主菜单功能
return;
}
}

System.out.print("请输入商品数量:");
int shopNum = input.nextInt(); // 添加货架上的商品数量
if(shopNum<=0){
System.out.println("您输入的数量不合理,返回主菜单!!!");
menu();
return;//
}

System.out.print("请输入商品信息:");
String shopInfo = input.next(); // 添加货架上的商

品信息
System.out.print("请输入商品价格:");
int shopPric
e = input.nextInt(); // 添加货架上的商品价格
if(shopPrice<=0){
System.out.print("您输入的价格不合理,返回菜单!!");
menu();
break;
}

quotID[index] = bianhao;
quotName[index] = shopName;
quotNum[index] = shopNum;
quotInfo[index] = shopInfo;
quotPrice[index] = shopPrice;

System.out.println("商品添加成功,返回主菜单查看!!");
answer="n";
menu();
break;
}while(answer.equals("y"));


}

// 修改菜单

public void rework(){ // 从键盘输入接收一个编号
boolean big=true;
System.out.print("请输入你要修改的商品编号:");
int shopid=input.nextInt();
for(int i=0;i<quotID.length;i++){
if(quotID[i]==0){
break;
}
if(quotID[i]==shopid){
big=false;
System.out.println("1.修改商品名称"+"\n"+"2.修改商品数量"+"\n"+"3.修改商品价格"+"\n"+"4.修改商品信息");
System.out.println("请选择:");
int num1=input.nextInt();
switch(num1){
case 1:
System.out.print("请输入修改后的商品名称:");
quotName[i]=input.next();
System.out.print("修改成功!!");
break;
case 2:
System.out.print("请输入修改后的商品数量:");
quotNum[i]=input.nextInt();
System.out.println("修改成功!!");
break;
case 3:
System.out.print("请输入修改后的商品价格:");
quotPrice[i]=input.nextInt();
System.out.println("修改成功!!");
break;
case 4:
System.out.print("请输入修改后的商品信息:");
quotInfo[i]=input.next();
System.out.println("修改成功!!");
default:
System.out.println("没有你要修改的选项!");
}
}else{
continue;
}

if(big){
System.out.println("您输入的商品编号为无效的编号!");
}
System.out.print("\n按'n'返回主菜单,按'q'退出:");
String fanhui=input.next();
if(fanhui.equals("q")){
System.out.println("退出系统!欢迎下次光临!");
break;
}else if(fanhui.equals("n")){
menu(); // 显示返回菜单
}else {
System.out.println("您是输入有误!返回主菜单!");
menu();
}
}
}


// 删除商品

public void delete() {
boolean big=true;
System.out.print("请输入要删除的商品编号:"); //删除的编号
int delShopid = input.nextInt();
// 循环数组编号里的元素
for (int i = 0; i < quotID.length; i++){ // 循环数组编号里的元素下标
if(quotID[i] == 0){
break;
}
if (quotID[i] == delShopid){ // 判断编号等于数组里的编号的时候执行下面语句

for(int x=i;x<quotID.length-1;x++){
quotID[x]=quotID[x+1];
quotName[x] = quotName[x+1];
quotNum[x] = quotNum[x+1];
quo

tInfo[x] = quotInfo[x+1];
quotPrice[x] = quotPrice[x+1];
continue;
}
b
ig=false;
}else{
continue;
}
}
if(big){ // 如果编号不等于数组里的编号的时候执行下面语句
System.out.println("您输入的商品编号为无效的编号!");

}
System.out.println("删除成功!!");
System.out.print("\n按'n'返回主菜单");
String fanhui=input.next();
if(fanhui.equals("n")){
menu(); // 显示返回菜单
}else {
System.out.println("您是输入有误!返回主菜单!");
menu();
}
}
}

import java.util.Scanner;

public class ShopCar_Test {

// 显示系统主菜单的方法
public static void mainMenu() {
System.out.println("***************************************************************************");
System.out.println("\t\t\t\t欢迎你进入商品购物管理系统\n\n");
System.out.println("\t\t\t\t1.商品购物系统\n");
System.out.println("\t\t\t\t2.商品管理系统\n");
System.out.println("如果你是客户请选择(<1>),如果你是管理员请选择(<2>)");
System.out.println("***************************************************************************");
}

public static void main(String[] args) {
ShopCar_Client client = new ShopCar_Client(); // 定义前台用户类的对象
client.manager.initial();
Scanner input = new Scanner(System.in);
String con = "";
con = "no";
do {
if (con == "no") {
client.manager.initial();
} // 先初始化库存中的商品信息
mainMenu(); // 显示系统主菜单
int num = input.nextInt();
if (num == 1) { // 显示执行前台用户
client.showShop(client.manager.quotID, client.manager.quotName, client.manager.quotNum, client.manager.quotInfo,client.manager. quotPrice);
client.goumai(client.manager.quotID, client.manager.quotName,
client.manager.quotNum, client.manager.quotInfo, client.manager.quotPrice);
} else if (num == 2) {
client.manager.login();
} else {
System.out.println("没有你所选的选项!!!");
}
System.out.println("是否重新选择进入购物管理系统(yes/no)");
con = input.next();
} while (con.equals("yes"));
System.out.println("谢谢您的使用,欢迎下次光临!!");
}
}



相关文档
最新文档