导语:面向对象的笔记还在整理,之前的笔记也有点凌乱,先发一个国庆期间做的一个超市购物的案例。主要的功能是顾客查看,购买,结算商品、管理员添加,删除,查看商品。
1.实体包
//管理员账号实体类
public class Admin {
private String id;//管理员帐号
private String passWord;//管理员密码
public Admin() {
super();
}
public Admin(String id, String passWord) {
super();
this.id = id;
this.passWord = passWord;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
//商品实体类
public class Product {
private int id;//商品序号
private String name;//商品名称
private float price;//商品价格
private int nums;//商品数量
public static Product[] goodList = new Product[200];//存储商品的数组
public Product() {
super();
}
public Product(int num, String name, float price,int nums) {
super();
this.id = num;
this.name = name;
this.price = price;
this.nums = nums;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public int getId() {
return id;
}
public void setId(int num) {
this.id = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
//购物车实体类
public class ShopCar {
public static Product[] shopCars = new Product[200];//存储商品的数组
public ShopCar() {
super();
}
}
2.界面包
import java.util.Scanner;
import util.AdminUtil;
import util.GoodsUtil;
public class AdminUI {
private static Scanner adminInput = new Scanner(System.in);
//管理员操作界面
public static void adminUI() {
int inputNums = 0;
//管理员账号登陆
while(true) {
System.out.println("请输入管理员账号");
String adminId = adminInput.next();
System.out.println("请输入管理员密码");
String passWord = adminInput.next();
if(adminId.equals( AdminUtil.getAdmin().getId()) && passWord.equals(AdminUtil.getAdmin().getPassWord())){
break;
}else {
if(inputNums == 2) {
System.out.println("输入次数已上线,退出管理员登陆!");
return;
}else {
System.out.println("账号密码错误");
}
}
inputNums++;
}
//管理员操作
while(true) {
System.out.println("1.管理员添加商品2.管理员删除商品3.显示商品列表4.退出管理员操作");
int choice = adminInput.nextInt();
if(choice == 1) {
System.out.println("请输入商品的编号");
int id = adminInput.nextInt();
System.out.println("请输入商品的名字");
String name = adminInput.next();
System.out.println("请输入商品的价格");
float price = adminInput.nextFloat();
System.out.println("请输入商品的数量");
int nums = adminInput.nextInt();
GoodsUtil.addGoods(id, name, price, nums);
}else if(choice == 2) {
System.out.println("请输入你要删除的商品的序号");
int deletenum = adminInput.nextInt();
GoodsUtil.deleteGoods(deletenum);
}else if(choice == 3) {
GoodsUtil.showGoods();
}else if(choice == 4) {
break;
}else {
System.out.println("请输入正确的数字!");
}
}
}
}
import java.util.Scanner;
import entity.ShopCar;
import util.ShopCarUtil;
public class ShoppingCarUI {
private static Scanner input = new Scanner(System.in);
//购物车界面
public static void shoppingCarUI(){
while(true) {
ShopCarUtil.showShopCar();
if(ShopCar.shopCars[0] == null) {
return;
}
System.out.println("1.删除商品2.清空购物车3.返回上一层");
int choice = input.nextInt();
if(choice == 1) {
System.out.println("请输入你要删除商品的序号");
int deleteNum = input.nextInt();
ShopCarUtil.deleteAccounts(deleteNum);
}else if(choice == 2) {
ShopCarUtil.clearShopCar();
}else if(choice ==3) {
break;
}else {
System.out.println("输入错误,请输入正确的数字!");
}
}
}
}
import java.util.Scanner;
import entity.Product;
import util.ShopCarUtil;
import util.GoodsUtil;
//购物车界面
public class ShoppingUI {
Scanner input = new Scanner(System.in);
public void showShopping() {
/*
* 超市的界面方法
*/
while(true) {
System.out.println("--------------欢迎来到Java超市--------------");
System.out.println("1.显示商品列表2.购买商品3.查看购物车列表4.结算5.管理员登陆6.退出");
int choice = input.nextInt();
if(choice == 1) {
GoodsUtil.showGoods();
}else if(choice == 2){
int list;
int nums;
while(true) {
System.out.println("请输入你要添加商品的序号");
list = input.nextInt();
if(list < GoodsUtil.addProduct) {
break;
}else {
System.out.println("请输入正确的商品序号!");
}
}
while(true) {
System.out.println("请输入你要添加商品的数量");
nums = input.nextInt();
if(nums < Product.goodList[nums - 1].getNums()) {
break;
}else {
System.out.println("商品库存不足,请重新输入!");
}
}
ShopCarUtil.addGoodsCar(list,nums);
}else if(choice ==3) {
ShoppingCarUI.shoppingCarUI();
}else if(choice ==4) {
int allPrice = ShopCarUtil.settleAccounts();
if(allPrice == 0) {
System.out.println("请先购买商品!");
return;
}
System.out.println("总价为:" + allPrice);
}else if(choice ==5) {
AdminUI.adminUI();
}else if(choice ==6) {
System.out.println("期待您的下次光临!");
break;
}else {
System.out.println("请输入正确的数字");
}
}
}
}
3.工具包
import entity.Admin;
public class AdminUtil {
//创建管理员帐号
static Admin admin = new Admin("admin","123456");
public static Admin getAdmin() {
return admin;
}
public static void setAdmin(Admin admin) {
AdminUtil.admin = admin;
}
}
import entity.Product;
//商品工具类
public class GoodsUtil {
private static int numsProduct = 0;//打印商品列表商品的序号
public static int addProduct = 0;//添加商品的数组角标
//初始化商品
static {
Product p1 = new Product(1001,"茄子",1.5f,100);
Product p2 = new Product(1002,"萝卜",1.7f,100);
Product p3 = new Product(1003,"黄瓜",2.3f,100);
Product.goodList[addProduct++] = p1;
Product.goodList[addProduct++] = p2;
Product.goodList[addProduct++] = p3;//addProduct = 3
}
//显示商品列表
public static void showGoods() {
numsProduct = 1;
System.out.println("序号\t" + "商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t");
for(Product p :Product.goodList) {
if(p != null) {
System.out.print(numsProduct++ + "\t" + p.getId() + "\t" + p.getName() + "\t" + p.getPrice() + "\t" + p.getNums() + "\t" );
System.out.println();
}else {
break;
}
}
}
//管理员添加商品
public static void addGoods(int id,String name,float price,int nums) {
Product newProduct = new Product(id,name,price,nums);
Product.goodList[addProduct++] = newProduct;
}
//管理员删除商品
public static void deleteGoods(int deletenum) {
//将商品的后一个商品覆盖前一个商品,来删除商品
for(int i = deletenum - 1;i < Product.goodList.length;i++ ) {
if(Product.goodList[i + 1] == null) {
Product.goodList[i] = Product.goodList[i + 1];
break;
}else {
Product.goodList[i] = Product.goodList[i + 1];
}
}
addProduct--;
}
}
import entity.Product;
import entity.ShopCar;
//购物车工具类
public class ShopCarUtil {
private static int nums = 0;
public static int numsShopCarProduct = 0;
//添加商品,将商品写入购物车
public static void addGoodsCar(int id,int goodsNum) {
Product addGoods = Product.goodList[id - 1];
Product shopCar = new Product();
shopCar.setId(addGoods.getId());
shopCar.setName(addGoods.getName());
shopCar.setPrice(addGoods.getPrice());
shopCar.setNums(goodsNum);
int goodsnums = addGoods.getNums();
if((goodsnums - goodsNum) >= 0) {
addGoods.setNums(goodsnums - goodsNum);
}else {
System.out.println("库存不足,请重新选择!");
return;
}
ShopCar.shopCars[nums++] = shopCar;
}
//显示购物车列表
public static void showShopCar() {
if(ShopCar.shopCars[0] == null) {
System.out.println("您的购物车是空的,请先购买商品在查看!");
return;
}
numsShopCarProduct = 1;
System.out.println("------------------你的购物车-------------------");
System.out.println("序号\t" + "商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t");
for(Product p :ShopCar.shopCars) {
if(p != null) {
System.out.print(numsShopCarProduct++ + "\t" + p.getId() + "\t" + p.getName() + "\t" + p.getPrice() + "\t" + p.getNums() + "\t" );
System.out.println();
}else {
break;
}
}
}
//结算
public static int settleAccounts() {
int allPrice = 0;
for(Product p :ShopCar.shopCars) {
if(p != null) {
allPrice += p.getPrice()*p.getNums();
}else {
break;
}
}
return allPrice;
}
//删除购物车商品
public static void deleteAccounts(int deletenum) {
ShopCarUtil.returnNums(deletenum - 1);
//将商品的后一个商品覆盖前一个商品,来删除商品
for(int i = deletenum - 1;i < ShopCar.shopCars.length;i++ ) {
if(ShopCar.shopCars[i + 1] == null) {
ShopCar.shopCars[i] = ShopCar.shopCars[i + 1];
break;
}else {
ShopCar.shopCars[i] = ShopCar.shopCars[i + 1];
}
}
ShopCarUtil.nums--;
}
//清空购物车
public static void clearShopCar() {
for(int i = 0;i < ShopCar.shopCars.length;i++) {
if(ShopCar.shopCars[i] != null) {
ShopCarUtil.returnNums(i);
}else {
break;
}
}
for(int i = 0;i < ShopCar.shopCars.length;i++) {
if(ShopCar.shopCars[i] != null) {
ShopCar.shopCars[i] = null;
}else {
break;
}
}
}
//将删除商品的数量返回至商品列表
public static void returnNums(int deletenum) {
Product delAccount = ShopCar.shopCars[deletenum];
for(Product p :Product.goodList) {
if(p.getId() == delAccount.getId()) {
int nums = p.getNums() + delAccount.getNums();
p.setNums(nums);
break;
}
}
}
}