java实现水果超市管理系统
作者:scropio0zry 发布时间:2022-02-03 12:52:32
标签:java,管理系统
本文为大家分享了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


猜你喜欢
- 目前为止,许多编程语言和工具都包含对正则表达式的支持,C#也不例外,C#基础类库中包含有一个命名空间(System.Text.Regular
- 本文实例为大家分享了Android使用AIDL方式实现播放音乐的具体代码,供大家参考,具体内容如下思路:① 新建两个APP项目或者Modul
- 1.Maven依赖<!-- Kettle --><dependency> <grou
- 前言前段时间因为工作的需要用到Spring事件,翻翻文档将功能实现了,但是存在少许理解不畅的地方,今天有空来梳理梳理。需求背景叶子同学在新入
- 苹果的iphone有语音识别用的是Google的技术,做为Google力推的Android 自然会将其核心技术往Android 系统里面植入
- spring boot版本和spring cloud版本框架版本SpringBoot2.3.12.RELEASESpringCloudHox
- 0. 困扰很久的问题Android控件的宽和高保持比例,这是从我接触Android以来,一直不断会遇到的需求。以前,要么就是在代码里直接设置
- 在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个spring Boot是怎
- 介绍1.BLE 是 Bluetooth Low Energy 的缩写,意思为低功耗蓝牙。由蓝牙技术联盟(Bluetooth SI
- 一般情况下在Word中输入的文字都是横向的,今天给大家分享两种方法来设置/更改一个section内的所有文本的方向及部分文本的方向,有兴趣的
- FileInputStream流的三种read()函数对比首先我们先创建FileIputStream流对文件进行读取public stati
- 前言我们常说的字符串为空,其实就是一个没有字符的空数组。比如:String a = "";a 就可以称为是一个空字符串。
- java并发之ArrayBlockingQueue详细介绍 ArrayBlockingQueue是常用的线程集合,在线程池中也常常
- 介绍该系统有三个角色,分别是:普通用户、房屋中介、管理员。普通用户的功能:浏览房屋信息、预约看房、和中介聊天、申请成为中介等等。房屋中介的功
- 一个好的app 异常处理机制 我认为应该至少包含以下几个功能:1.能把错误信息上传到服务器 让开发者可以持续改进app2.错误信
- android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 Translat
- 返回集合为null还是空集合及空集合的三种写法个人认为在自己写接口时,需要返回集合时返回一个空集合,比如mybatis查询如果返回一个集合,
- 1、什么是值传递,什么是引用传递?值传递(pass by value)是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数
- 前言对于Java程序员,可以说对于ArrayList和LinkedList可谓是十分熟悉了对于ArrayList和LinkedList,他们
- 主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法,具体内容详情如下所示:在Android Studio中