欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现水果超市管理系统

程序员文章站 2023-12-16 17:32:16
本文为大家分享了java实现水果超市管理系统的具体代码,供大家参考,具体内容如下 首先建立水果类的界面 public class fruit { //...

本文为大家分享了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; 
 } 
 } 
} 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: