概述
package com.itheima;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import static java.lang.System.exit;
public class Main{
static Scanner input = new Scanner(System.in);
public static ArrayList<Goods> goodsList = new ArrayList<>();
public static void main(String[] args)throws IOException {
int choice;
Init();//初始化
while(true) {
menu();
choice = input.nextInt();
switch (choice) {
case 1:
query();
break;
case 2:
addStock();
break;
case 3:
addGoods();
break;
case 4:
printAllGoods();
break;
case 5:
exit(0);
break;
default:
System.out.println("选择有误!请再次选择。");
}
}
}
//显示所有商品的相关信息
public static void printAllGoods(){
for(Goods goods: goodsList)
System.out.println(goods);
}
//查询商品信息(连下)
public static void query(){
System.out.print("请输入商品编号:");
String id = input.next();
try{
System.out.println(((Goods)queryID(id)).currentRecord());
}catch(NullPointerException ex){
System.out.println("无此编号");
exit(1);
}
}
//看看有没有
public static Object queryID(String id){
for(Goods goods : goodsList)
{
if(goods.getId().equals(id))
return goods;
}
return null;
}
//增加库存
public static void addStock()throws IOException{
System.out.print("请输入进货数量:");
int number = input.nextInt();
System.out.print("请输入商品编号:");
String id = input.next();
try{
Goods good = (Goods)queryID(id);
good.setStock(number + good.getStock());
GoodsRecorder.save(good);//保存到进货记录
GoodsRecorder.saveRealRecord(goodsList);//商品信息保存到进货
System.out.println(good.currentRecord());
}catch(NullPointerException ex){
System.out.println("无此编号");
exit(2);
}
}
//初始化
public static void Init()throws IOException{
goodsList.add(new Goods("1001", 4.5,"百事可乐", 100, "张三"));
goodsList.add(new Goods("1002", 4.0, "可口可乐",100, "李四"));
goodsList.add(new Goods("1003", 3.8, "百事雪碧", 100,"王五"));
for (Goods goods : goodsList) { //将商品信息添加到进货记录
GoodsRecorder.save(goods);
}
GoodsRecorder.saveRealRecord(goodsList); //将商品信息添加到进货实时记录
}
//添加新商品
public static void addGoods()throws IOException{
System.out.print("请输入商品编号:");
String id = input.next();
if(((Goods)queryID(id)) != null){
System.out.println("已有此编号,不可重复");
exit(3);
}
System.out.print("请输入商品名称:");
String goodsname = input.next();
System.out.print("请输入购买数量:");
int amount = input.nextInt();
System.out.print("请输入单价:");
double unitPrice = input.nextDouble();
System.out.print("请输入联系人:");
String custumer = input.next();
goodsList.add(new Goods(id, unitPrice, goodsname, amount, custumer));
GoodsRecorder.save(goodsList.get(goodsList.size()-1));
GoodsRecorder.saveRealRecord(goodsList);
System.out.println("商品进货成功.n");
}
public static void menu(){
System.out.println("-----------商品进货信息及操作------------");
System.out.println("1.查询 2.进货 ");
System.out.println("3.增加商品 4.显示所有商品信息");
System.out.println("5.退出" );
System.out.print("请选择需要进行的操作:");
}
}
package com.itheima;
public class Goods{
private String id;//编号
private double unitPrice;//单价
private String goodsName;//商品名称
private int stock;//库存
private String custumerName;//联系人
public Goods(String id, double unitPrice, String goodsName, int stock, String custumerName)
{
this.id = id;
this.unitPrice = unitPrice;
this.goodsName = goodsName;
this.stock = stock;
this.custumerName = custumerName;
}
//返回总价(库存*单价)
public double getTotalPrice(){
return stock * unitPrice;
}
//重写toString
public String toString(){//返回商品的相关信息
return "进货记录编号:"+id+"n商品名称:"+goodsName+"n联系人:"+custumerName+"n单价:"+unitPrice+"n库存数量:"+stock+"n";
}
public String currentRecord(){//当前商品
return "当前商品库存信息" + toString();
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public double getUnitPrice(){
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getCustumerName() {
return custumerName;
}
public void setCustumerName(String custumerName) {
this.custumerName = custumerName;
}
}
package com.itheima;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class GoodsRecorder {//整理好文件名准备进行写入
public static void save(Goods good)throws IOException {
DateFormat date = new SimpleDateFormat("yyyyMMdd");
String fileName = "进货记录"+date.format(new Date()) +".cvs";
File file = new File("D:\java商品进货记录", fileName);
if(file.exists())
save(good,file,true);
else
save(good, file, false);
}
//一个文件的写入
public static void save(Goods good, File file, boolean flag)throws IOException{//将商品写入文件
if(flag)//在原有的基础上进行追加
{
byte[] b = ToStringBuffer(good).getBytes();//返回字符串的字节数
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(b, 0, b.length);//输出
bufferedOutputStream.close();//关闭
}
else
{
StringBuffer sb = new StringBuffer();//字符串缓冲器对象
String string = "商品编号 "+"商品名称 "+"购买数量 "+"单价 "+"总价 "+"联系人n"+ToStringBuffer(good);
sb.append(string);
byte[] b = sb.toString().getBytes();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(b, 0, b.length);
bufferedOutputStream.close();
}
}
public static String ToStringBuffer(Goods good)//将商品信息进行整理
{
StringBuffer sb = new StringBuffer();
sb.append(good.getId()).append(" ");
sb.append(good.getGoodsName()).append(" "); //缓冲器添加商品名称
sb.append(good.getStock()).append(" "); //缓冲器添加商品库存
sb.append(good.getUnitPrice()).append(" "); //缓冲器添加商品单价
sb.append(good.getTotalPrice()).append(" "); //缓冲器添加商品总价
sb.append(good.getCustumerName()).append("n");//缓冲器添加商品联系人名称
return sb.toString();
}
//一个链表的写入
public static void saveRealRecord(ArrayList<Goods> list)throws IOException {
DateFormat date = new SimpleDateFormat("yyyyMMdd");
String fileName = "商品进货实时记录" + date.format(new Date()) + ".cvs";
File file = new File("D:\java商品进货记录", fileName);
if (file.exists()) {
String string = "商品编号 " + "商品名称 " + "购买数量 " + "单价 " + "总价 " + "联系人n";
for (Goods goods : list) { //字符串string依次加入各商品的相关信息
string += ToStringBuffer(goods);
}
byte[] b = string.getBytes();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(b, 0, b.length);
bufferedOutputStream.close();
} else { //如果文件不存在,创造文件并再次调用本方法
file.createNewFile();
saveRealRecord(list);
}
}
}
最后
以上就是无私凉面为你收集整理的java基础案例7-2商城进货交易记录的全部内容,希望文章能够帮你解决java基础案例7-2商城进货交易记录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复