Java implements the shopping management system

  • 2020-12-22 17:40:37
  • OfStack

The example of this paper shares the Java implementation of shopping management system specific code for your reference, the specific content is as follows

The key content

JAVA branch structure exercise
The dynamic array
Registry read and write delete

Code:


package Endless;

import java.util.ArrayList;
import java.util.Random;
/**
* @JAVA01.person1.java
* @author  Current user: Endless The author  :Endless
* @version  Creation time: 2017 years 7 month 26 day   In the afternoon 6:29:16
* // Realize commodity and member information management ( Add, delete, display )
* // Realize supermarket Settlement 
*/
import java.util.Scanner;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class persion {
 static int s;
 static boolean huiyuan,xhuiyuan;
 static String bke=" Pepsi ",ke="500ml coke 1 bottle ",m="5 Kg of flour ",ss="1 A Supor wok ",o=" L 'Oreal Toner 1 bottle ";
 static ArrayList<String>array=new ArrayList<String>();
 static Preferences goodsnodes = Preferences.systemRoot().node("goods"); // Create a node 
 static Preferences goodschildnode = goodsnodes.node( "goods");  // Create child nodes  

 public static void main(String []args) {


  outline();
  System.out.println("\t Welcome to the shopping management system ");
  System.out.println("\t1. Log in the system \n\t2. Exit the program \n ");
  outline();
  int n =scanint(); 
  if(n == 1){ 
   login();  
  }else if( n== 2){ 
  System.out.println(" Have withdrawn from the ");
  System.exit(0);
  }else {
  System.out.println(" Please re-select: ");
  main(null);
 }

 }
 public static void outline(){
  System.out.println("* * * * * * * * * * * * * * * * * * * * * * *\n");
 }

 public static int scanint(){
 int x = 0;
 try {
  Scanner scan =new Scanner(System.in);
  x = scan.nextInt();
 } catch (Exception e) {
  System.out.println(" Input is wrong , Please enter the number: ");
 }
 return x; 
 }

 public static String scanStr(){
 String x = null;
 try {

  Scanner scan =new Scanner(System.in);
  x = scan.next();
 } catch (Exception e) {
  System.out.println(" Input is wrong , Please enter the string: ");
 }
 return x; 
 }
 public static void login(){
  System.out.println(" * * * * * * * * *  landing  * * * * * * * * * * * *\n Please enter your password. \n : ");
  int n1 =scanint();
  if(n1==(123123)){
  System.out.println(" Log in successfully \n");
  showMain();
  }else{ 
  System.out.println(" Password mistake ");
  System.out.println(" Please re-enter your password \n : ");
  login();
  } 
 }
 private static void showMain() {
 System.out.println("\n\n\t Welcome to the shopping management system \n"); 
 outline(); 
 System.out.println("\t\t 1.  will   member   The letter   Interest rates   tube   Richard \n"); 
 System.out.println("\t\t 2.  cargo   things   The letter   Interest rates   tube   Richard \n"); 
 System.out.println("\t\t 3.  purchase   things   "   calculate \n"); 
 System.out.println("\t\t 4.  note   pin \n"); 
 outline(); 
 System.out.print(" Please select a , Enter the Numbers :"); 
 int n2 =scanint();
 switch (n2) {
 case 1:
  new user();
  break;
 case 2:
  goods();
  break;
 case 3:
  jiesuan();
  break;
 case 4:
  System.out.println(" Cancellation of success ");
  main(null);
 default:
  System.out.println(" There is no option you want, please reenter ");
  showMain();
  break;
 }
 }
 private static void goods() {
 // Commodity management   : Add product:   Serial number   Of the goods   The price 
 //  Delete: Select label to delete 

 outline();
 System.out.println("\t\t Commodity information management \n");
 System.out.println("\t\t1, Show product \n");
 System.out.println("\t\t2, Add item \n");
 System.out.println("\t\t3, Delete the goods \n");
 System.out.println("\t\t4, Return to the superior \n");
 outline();
 int n = scanint();
 if (n==1){
  showgoods();
 }else if(n==2){
  addgoods();
 }else if(n ==3){
  delgoods();
 }else if(n==4) {
  showMain();
 }else {
  goods();
 }
 }
 private static void delgoods() {
   System.out.println(" Please select the item to delete ID : \n According to the 0 Return to the superior ");
   try {
   Scanner scan =new Scanner(System.in);
   int x = scan.nextInt();
   boolean remove=false;
   for (int i=0;i<array.size();i++){
    if(x==Integer.parseInt(array.get(i))){
    remove=true;
    }
   }
   if (remove){
    goodschildnode.remove(Integer.toString(x));
    System.out.println(" Delete the success ");
    goods();
   }else if (x==0) {
    goods();
   }else {
   System.out.println(" There is no such product! "); 
    delgoods();
   }
   new user();
   } catch (Exception e) {
   System.out.println(" Please enter the number ");
    delgoods();
   }
 }
 private static void showgoods() {
  try {
   int leng =goodschildnode.keys().length;
   syncread();
   if(leng==0){
   System.out.println(" There is no goods ");
   }else{
   try {
    System.out.println(" The current is "+goodschildnode.keys().length+" Kinds of goods ");
    for(int i = 0;i<=goodschildnode.keys().length;i++){
    String shows =goodschildnode.get(array.get(i), "");
     System.out.println(shows);
     System.out.println(" ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ "); 
    }
    System.out.println("00000000");
    } catch (Exception e) {
    }
   }

  } catch (BackingStoreException e) {

  }
  System.out.println("111111111");
   goods(); 
 }

 private static void syncread() {
  try {
  String[] arr= goodschildnode.keys();
  goodschildnode.sync();
  array.clear();
  for(int i=0;i<arr.length;i++){
  array.add(arr[i]);
  }
  } catch (Exception e) {
  }
 }

 private static int check(int x) {
  Random n=new Random();
  syncread();
  for(int i=0;i<array.size();i++){
   if(x==Integer.parseInt(array.get(i))){
   check(n.nextInt(999)+1);
   } 
  } 
  return x;

 }
 private static void addgoods(){
  outline();
  System.out.println("\t\t Please enter the product information ");
  System.out.println("\t\t Of the goods , The price ( yuan ), unit ");
  System.out.println("\t\t Press enter to differentiate ");
  outline();
  syncread();
  String name =scanStr();
  String nuit =scanStr();
  int price = scanint(); 
  Random n=new Random();
  String id ;
   id=Integer.toString(check(n.nextInt(999)+1));
   try{
   goodschildnode.put(id, "ID:"+id+"  Item description: "+name+"  Price: "+price+"  Units: "+nuit);
   syncread();
   System.out.println(" The first "+(goodschildnode.keys().length)+" Product added successfully ");
   System.out.println(" The current commodity "+goodschildnode.keys().length+" Kind of \n"+array+"\n"+array.size());
   goods(); 
  } catch (Exception e) {
   goods(); 
  }
 }
 static void jiesuan(){
 try{
  System.out.println(" Are you a member? Is to choose 1  Whether to choose 0");
  int x= scanint();
  if (x==1){
   huiyuan = true;
   Scanner sc =new Scanner(System.in); 
   System.out.println(" Please enter your membership number: ");
   int hui = sc.nextInt()/100%10;
   if(hui==9){
   System.out.println(" Congratulations on being a lucky member , You can enjoy the whole thing. 8 A discount ");
   xhuiyuan = true;
   }else{
   xhuiyuan = false;
   }
  }else if (x==0) {
   huiyuan = false;
  }
  System.out.println(" Please enter the amount spent :");
  Scanner scan =new Scanner(System.in); 
  if(scan.hasNext()){
  s=scan.nextInt();
  outline();
  System.out.println(" Whether to participate in discount purchase activities: " );
  System.out.println("1: full 50 Yuan, add 2 Yuan to buy "+bke );
  System.out.println("2: full 100 Yuan, add 3 Yuan to buy " +ke);
  System.out.println("3: full 100 Yuan, add 10 Yuan to buy " +m );
  System.out.println("4: full 200 Yuan, add 10 Yuan to buy " +ss );
  System.out.println("5: full 200 Yuan, add 20 Yuan to buy  " +o );
  System.out.println("0 : I don't buy \n Please select: \n" );
  outline();
  os();
  } 
 } catch (Exception e) {
  System.out.println(" Please enter the number ");
  jiesuan();

 }
 } 
 static void out(String n , double s){
 if(!(n==null)){
  System.out.println(" Success for "+n);
 }
 System.out.println(" Total amount of consumption: "+s+" yuan ");
 if (huiyuan && xhuiyuan){
  System.out.println(" Dear lucky member, enjoy this consumption 8 A discount ");
  double x =s*0.8;
  System.out.println(" Discounts save you: "+(s-x)+" yuan ");
 }else if (huiyuan) {
  System.out.println(" Dear members, enjoy this consumption 9 A discount ");
  double x =s*0.9;
  System.out.println(" Discounts save you: "+(s-x)+" yuan ");
 }
 System.out.println(" Total amount of actual consumption: "+s+" yuan ");
 showMain();
 }
 static void os(){
  switch (scanint()) {
  case 0:
  out(null,s);
  case 1:
  if(s>=52){
   out(bke,s+2);
  }else{
   Err();
  }
  case 2:
  if(s>=103){
   out(ke,s+3);
  }else{
   Err();
  }
  break;
  case 3:
  if(s>=110){
   out(m,s+10);
  }else{
   Err();
  }
  break;
  case 4:
  if(s>=210){
   out(ss,s+10);
  }else{
   Err();
  }
  break;
  case 5:
  if(s>=220){
   out(o,s+20);
  }else{
   Err();
  }
  break;
  default:
  System.out.println(" Please reselect ");
  os();
  }
 }
 static void Err(){

  System.out.println(" Amount is less than \n");
  main(null);
 } 
 static class user{
  static ArrayList<String>array=new ArrayList<>();
  static Preferences nodes = Preferences.systemRoot().node("user"); // Create a node 
  static Preferences childnode = nodes.node( "user");  // Create child nodes  


  public user() {
  outline();
  System.out.println("\t\t1, Add user ");
  System.out.println("\t\t2, Delete user ");
  System.out.println("\t\t3, According to the user ");
  System.out.println("\t\t4, Return to the superior ");
  outline();
  try {
   int x=scanint();
   switch (x) {
   case 1:
   add();
   break;
   case 2:
   deluser();
   break;
   case 3:
   show();
   break;
   case 4:
   showMain();
   break;
   default:
   System.out.println(" There is no option you want! Please reselect ");
    new user();
   break;
   }
  } catch (Exception e) {
  new user();
  } 
 }
 private void deluser() throws BackingStoreException {
   System.out.println(" Please select the user to delete ID : \n According to the 0 Return to the superior ");
   try {
   Scanner scan =new Scanner(System.in);
   int x = scan.nextInt();
   boolean remove=false;
   for (int i=0;i<array.size();i++){
    if(x==Integer.parseInt(array.get(i))){
    remove=true;
    }
   }
   if (remove){
    childnode.remove(Integer.toString(x));
    System.out.println(" Delete the success ");
   }else if (x==0) {
   new user();
   }else {
   System.out.println(" No such user! "); 
    deluser();
   }
   new user();
   } catch (Exception e) {
   System.out.println(" Please enter the number ");
    deluser();
   }
 }
 private static void show() {
  try {
   int leng =childnode.keys().length;
   syncread();
   if(leng==0){
   System.out.println(" No user ");
   }else{
   System.out.println(" The current is "+childnode.keys().length+" A user ");
   for(int i = 0;i<=childnode.keys().length;i++){
    String shows =childnode.get(childnode.keys()[i], "");
    System.out.println(shows);
    System.out.println(" ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ "); 
   }
   }
  } catch (BackingStoreException e) {
  }
  new user();  
 }

 private static void syncread() {
  try {
  String[] arr= childnode.keys();
  childnode.sync();
  array.clear();
  for(int i=0;i<arr.length;i++){
  array.add(arr[i]);
  }
  } catch (Exception e) {
  }
 }


 private static void add(){
  Preferences nodes = Preferences.systemRoot().node("user"); // Create a node 
  Preferences childnode = nodes.node( "user");  // Create child nodes  
  outline();
  System.out.println("\t\t Please enter user information ");
  System.out.println("\t\t The name ,6 A card number , age , The phone ");
  System.out.println("\t\t Press enter to differentiate ");
  outline();
  syncread();
  String name =scanStr();
  int card = scanint();
  int age =scanint();
  String tel =scanStr();
  Random n=new Random();

  String id ;
   id = Integer.toString(n.nextInt(999)+1);
   try{
   childnode.put(id, "ID:"+id+"  Name: "+name+"  Card number: "+card+"  Age: "+age+"  Telephone: "+tel);
   syncread();
   System.out.println(" The first "+(childnode.keys().length)+" Bit member added successfully ");
   System.out.println(" The current user "+childnode.keys().length+" position \n"+array+"\n"+array.size());
   new user(); 
  } catch (Exception e) {
   new user(); 
  }
 }
 } 
}

For more information, please pay attention to the topic management System Development.


Related articles: