java实现水果超市管理系统

作者:scropio0zry 时间:2022-02-03 12:52:32 

本文为大家分享了java实现水果超市管理系统的具体代码,供大家参考,具体内容如下

首先建立水果类的界面


public class Fruit {
//定义ID
private String id;
//定义名称
private String name;
//定义价格
private int price;
//定义单位
private String unit;

//定义数量
private int number;
public Fruit(String id, String name, int price, String unit) {
super();
this.id = id;
this.name = name;
this.price = price;
this.unit = unit;
}
public Fruit() {
super();
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}

//获取价格
public int getMoney(){
return price * number;
}

}

水果超市的界面


import java.io.IOException;
import java.util.Scanner;

public class FruitTest {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
Shopper shopper = new Shopper();
Manager manager = new Manager();

while(true){
 System.out.println( "    欢迎光临水果系统");
 System.out.println("请输入你的角色:(1.顾客 2.管理员 3.退出)");
 int choice = sc.nextInt();
 switch(choice){
 case 1:
 //顾客
 shopper.shop();
 break;
 case 2:
 //管理员
 manager.manager();
 break;
 case 3:
 System.exit(0);
 default:
 System.out.println("你的输入有误!");
 }
}

}
}

顾客类


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Shopper {
public void shop() throws IOException {
Scanner sc = new Scanner(System.in);
ArrayList<Fruit> list = new ArrayList<Fruit>();
check(list);
while (true) {
 System.out
  .println("     欢迎光临水果系统");
 System.out
  .println("请输入你的操作:(1.查看水果 2.购买水果 3.结账  4.退出)");
 int choice = sc.nextInt();
 switch (choice) {
 case 1:
 // 查看水果
 print(list);
 break;
 case 2:
 // 购买水果
 buy(list);
 break;
 case 3:
 // 结账
 checkOut(list);
 break;
 case 4:
 // 退出
 return;
 default:
 System.out.println("你输入的操作有误!");
 }

}

}

//结账
private void checkOut(ArrayList<Fruit> list) {
int sum = 0;
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 sum += f.getMoney();
}

if(sum>200){
 int newSum = (int) (sum * 0.9);
 System.out.println("金额:" + sum+ "元, 优惠价格:"+ newSum+"元");
}else{
 System.out.println("金额:" + sum+"元");
}

//结完账后,将数量清0
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 f.setNumber(0);
}
}

// 购买水果
public void buy(ArrayList<Fruit> list) throws IOException {
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
print(list);
while (true) {
 System.out.println("购买超过200元,享受九折优惠!");
 System.out.println("请输入想要购买的水果的ID:(如果不想购买,请输入-1退出)");
 String id = sc1.nextLine();
 if ("-1".equals(id)) {
 System.out.println("购买已结束,请去结账 ");
 return;
 } else {
 boolean flag = false;
 for (int i = 0; i < list.size(); i++) {
  Fruit f = list.get(i);
  if(f.getId().equals(id)) {
  System.out.println("请输入购买" + f.getName() + "数量: ");
  int num = sc2.nextInt();
  f.setNumber(num);
  flag = true;
  }
 }
 if(!flag){
  System.out.println("你输入的水果ID不正确,请重新输入");
 }
 }

}

}

// 查看水果
public void check(ArrayList<Fruit> list) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("fruit.txt"));
String line;
while ((line = br.readLine()) != null) {
 String[] str = line.split(" ");
 Fruit f = new Fruit(str[0], str[1], Integer.parseInt(str[2]),
  str[3]);
 list.add(f);
}
br.close();
}

public void print(ArrayList<Fruit> list) {
System.out.println("ID\t水果\t价格\t单位");
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 System.out.println(f.getId() + "\t" + f.getName() + "\t"
  + f.getPrice() + "\t" + f.getUnit());
}
}
}

管理员类


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Manager {

public void manager() throws IOException {
if (load()) {  
 Scanner sc = new Scanner(System.in);
 while (true) {
 ArrayList<Fruit> list = new ArrayList<Fruit>();
 check(list);
 System.out
  .println("请输入您的操作: (1.查看水果种类  2.增加水果种类 3.修改水果种类 4.删除水果种类  5退出)");
 int choice = sc.nextInt();
 switch (choice) {
 case 1:
  // 查看水果种类
  print(list);
  break;
 case 2:
  // 增加水果种类
  addFruit(list);
  break;
 case 3:
  // 修改水果种类
  reverse(list);
  break;
 case 4:
  // 删除水果种类
  remove(list);
  break;
 case 5:
  // 退出
  return;
 default:
  System.out.println("你输入的操作有误!");
  break;
 }
 }

} else {
 return;
}
}

public void remove(ArrayList<Fruit> list) throws IOException {
Scanner sc = new Scanner(System.in);
print(list);
System.out.println("请输入要删除的水果ID: ");
String id = sc.nextLine();
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 if(f.getId().equals(id)){
 list.remove(i);
 write(list);
 System.out.println("删除成功");
 return;
 }
}
System.out.println("找不到要删除的水果ID!");
}

//修改水果
public void reverse(ArrayList<Fruit> list) throws IOException {
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
print(list);
System.out.println("请输入要修改的水果ID: ");
String id = sc1.nextLine();
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 if(f.getId().equals(id)){
 System.out.println("请输入水果的名称: ");
 String name = sc1.nextLine();
 System.out.println("请输入水果的价格: ");
 int price = sc2.nextInt();
 System.out.println("请输入水果的单位: ");
 String unit = sc1.nextLine();

f.setName(name);
 f.setPrice(price);
 f.setUnit(unit);

write(list);
 System.out.println("修改成功");
 return;
 }
}
System.out.println("找不到要修改的水果ID!");

}

//增加水果
public void addFruit(ArrayList<Fruit> list) throws IOException {
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
print(list);
System.out.println("请输入要增加水果的ID: ");
String id = sc1.nextLine();
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 if(f.getId().equals(id)){
 System.out.println("水果ID名重复!");
 return;
 }
}
System.out.println("请输入水果的名字: ");
String name = sc1.nextLine();
System.out.println("请输入水果的价格: ");
int price = sc2.nextInt();
System.out.println("请输入水果的单位: ");
String unit = sc1.nextLine();

Fruit f = new Fruit(id, name, price, unit);
list.add(f);

write(list);
System.out.println("增加成功");

}
//写入新加的种类
private void write(ArrayList<Fruit> list) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt"));
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 bw.write(f.getId()+" " + f.getName() + " " + f.getPrice() + " " + f.getUnit());
 bw.newLine();
}
bw.close();
}

public void print(ArrayList<Fruit> list) {
System.out.println("ID\t水果\t价格\t单位");
for (int i = 0; i < list.size(); i++) {
 Fruit f = list.get(i);
 System.out.println(f.getId() + "\t" + f.getName() + "\t"
  + f.getPrice() + "\t" + f.getUnit());
}
}

// 查看水果
public void check(ArrayList<Fruit> list) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("fruit.txt"));
String line;
while ((line = br.readLine()) != null) {
 String[] str = line.split(" ");
 Fruit f = new Fruit(str[0], str[1], Integer.parseInt(str[2]),
  str[3]);
 list.add(f);
}
br.close();
}

// 登陆系统
public boolean load() throws FileNotFoundException, IOException {
Scanner sc = new Scanner(System.in);

System.out.println("请输入用户名: ");
String username = sc.nextLine();
System.out.println("请输入密码: ");
String password = sc.nextLine();
BufferedReader br = new BufferedReader(new FileReader("admin.txt"));
String line = br.readLine();
String[] str = line.split(",");
if (str[0].equals(username) && str[1].equals(password)) {
 System.out.println("欢迎您进入水果管理系统: " + username);
 return true;
} else {
 System.out.println("你的用户名或密码输入不正确,无法进入管理系统");
 return false;
}
}
}

更多学习资料请关注专题《管理系统开发》。

来源:http://blog.csdn.net/scropio0zry/article/details/78440253

标签:java,管理系统
0
投稿

猜你喜欢

  • SpringMVC实现注解式权限验证的实例

    2021-11-19 11:52:44
  • Kotlin封装RecyclerView Adapter实例教程

    2023-11-06 01:54:23
  • javaweb购物车案列学习开发

    2021-10-28 13:30:26
  • c#获取相同概率随机数的算法代码

    2022-09-07 21:18:13
  • 详解Android Studio无法检测新版本问题解决

    2021-08-01 08:45:05
  • springboot如何开启一个监听线程执行任务

    2022-01-09 08:44:48
  • Java实现AWT四大事件的详细过程

    2023-11-28 18:39:52
  • Idea2020.2创建JavaWeb项目(部署Tomcat)方法详解

    2023-11-02 13:29:52
  • C#微信开发之接收 / 返回文本消息

    2021-08-21 14:37:11
  • 一文搞懂C#实现读写文本文件中的数据

    2021-11-07 00:21:07
  • 在Java代码中解析html,获取其中的值方法

    2023-12-04 20:13:41
  • 利用spring aop实现动态代理

    2022-02-02 16:50:25
  • Android RecyclerView实现水平、垂直方向分割线

    2023-07-24 15:40:35
  • 关于C#转换二进制所引起的一些思考

    2021-06-02 18:31:22
  • java中Executor,ExecutorService,ThreadPoolExecutor详解

    2023-10-31 10:50:56
  • java多线程抓取铃声多多官网的铃声数据

    2023-12-18 23:20:05
  • 详解Android的内存优化--LruCache

    2022-07-18 14:28:41
  • Android下拉列表spinner的实例代码

    2023-07-31 20:39:47
  • C#微信公众号开发之自定义菜单

    2023-01-23 02:07:08
  • springboot自动配置原理以及spring.factories文件的作用详解

    2021-12-20 20:19:27
  • asp之家 软件编程 m.aspxhome.com