Own first Little program
PS: The book is in the collection behind the file processing a little bit of a problem, I hope someone will give some advice
Customer class
Import java.io.Serializable;
public class Customer implements serializable{
Properties of the customer
private String name;//Customer Name
Private String password;//Customer password
Get,set method for customer attributes
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetPassword () {
return PassWord;
}
public void SetPassword (String passWord) {
This.password = PassWord;
}
Construction method with parameters
Public Customer (string name, String PassWord) {
Super ();
THIS.name = name;
This.password = PassWord;
}
Public Customer () {
Super ();
}
}
Customer Login Class
public class Signin {
Arraylist<customer> arraylist=new arraylist<customer> ();//Storing a collection of customer information
Customer Customer=new customers ();//Structuring Client Objects
Adding a collection to customer information
public void SetInformation () {
Arraylist.add (New Customer ("3", "123"));
Arraylist.add (New Customer ("Li 4", "1234"));
Arraylist.add (New Customer ("King 5", "12345"));
Arraylist.add (New Customer ("Zhao 6", "123456"));
}
Traverse a Customer collection
public int Bianli (String name,string passWord) {
for (int j=0;j<arraylist.size (); j + +) {//password, name to find customer
Customer=arraylist.get (j);
Customer=it.next ();
Returns 1 if same as incoming information
if (Customer.getname (). Equals (name) &&customer.getpassword (). Equals (PassWord)) {
Break
}else if (j!=arraylist.size ()-1) {//control for loop loops to each of the collections
Continue
}else{
Return 0;//not found returns 0
}
}
Return 1;//Find returned 1
}
Write a customer collection to a file
public void Writeinformation () {
ObjectOutputStream Oos=null;
try {
Oos=new ObjectOutputStream (New FileOutputStream ("D:\\pro2.docx"));//Append information to the file without overwriting it
Oos.writeobject (arrayList);
} catch (IOException e) {
E.printstacktrace ();
}finally{
try {
Oos.flush ();
Oos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Read the information from the file
@SuppressWarnings ("Unchecked")
public void Readinformation () {
ObjectInputStream Ois=null;
try {
Ois=new ObjectInputStream (New FileInputStream ("D:\\pro2.docx"));
Arraylist= (arraylist<customer>) ois.readobject ();//read-in Object
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}finally{
try {
Ois.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
The class of the book
public class Booktwo {
Properties of the book
Private String bname;//Book name
Private String btype;//Book category
Private Double bprice;//book price
Private String bnum;//Book number
private int bleft;//Book Inventory
The Get,set method of the property of the book
Public String Getbname () {
return bname;
}
public void Setbname (String bname) {
This.bname = bname;
}
Public String Getbtype () {
return btype;
}
public void Setbtype (String btype) {
This.btype = btype;
}
Public double Getbprice () {
return bprice;
}
public void Setbprice (double bprice) {
This.bprice = Bprice;
}
Public String Getbnum () {
return bnum;
}
public void Setbnum (String bnum) {
This.bnum = Bnum;
}
public int Getbleft () {
return bleft;
}
public void Setbleft (int bleft) {
This.bleft = Bleft;
}
Constructor with parameters
Public Booktwo (String bnum,string bname, String btype, double Bprice,
int bleft) {
Super ();
This.bname = bname;
This.btype = btype;
This.bprice = Bprice;
This.bnum = Bnum;
This.bcount = Bcount;
This.bleft = Bleft;
}
Constructors without parameters
Public Booktwo () {
Super ();
}
}
Method Class of the book
public class Booktwomenthod {
int count;
Booktwo book=new booktwo ();
Client's interface after successful landing
public void Show () {
SYSTEM.OUT.PRINTLN ("********* Welcome to use the bookstore system **********");
System.out.println ("1. View");
System.out.println ("2. return book");
System.out.println ("3. Exit");
System.out.print ("Please enter the number to select:");
}
Add the incoming list to the book content
public void Show1 (arraylist<booktwo> list) {
List.add (New Booktwo ("1001", "Naughty pony Jumping", "funny", 23,100));
List.add (New Booktwo ("1002", "Boiled great God", "funny", 23,100));
List.add (New Booktwo ("1003", "Comedy Little White", "funny", 23,100));
List.add (New Booktwo ("1004", "then Love is the same", "warm", 23,100));
List.add (New Booktwo ("1005", "Heart Is Unwilling", "masochism", 23,100));
List.add (New Booktwo ("1006", "Immortal Meat", "Funny + sadistic Heart", 23,100));
}
Traverse incoming list and output all information
public void Show2 (arraylist<booktwo> list) {
Iterator<booktwo> ti=list.iterator ();//iterator Iteration output
System.out.println ("number \ t name \t\t type \ t price \ t stock");
while (Ti.hasnext ()) {
Booktwo e= (Booktwo) ti.next ();
System.out.println (E.getbnum () + "\ T"
+e.getbname () + "\ T" +e.getbtype () + "\t\t"
+e.getbprice () + "\ T" +e.getbleft ());
}
}
How to buy
public void Buybook (arraylist<booktwo> list,string bnum,int bcount) {
for (int i=0;i<list.size (); i++) {
Book=list.get (i);
if (Bcount>=book.getbleft ()) {//Compare inventory with the number of books purchased
SYSTEM.OUT.PRINTLN ("The library does not have enough stock! ");
Break
}else if (Book.getbnum () equals (Bnum)) {//through Bnum to find the book
System.out.println ("The book you want to Buy is:" +book.getbname ());
SYSTEM.OUT.PRINTLN ("You need to pay the amount is:" +bcount*book.getbprice ());
Pass modification data, modify the information in the collection by the Set method
Book=new booktwo (Bnum, Book.getbname (), Book.getbtype (), Book.getbprice (), Book.getbleft ()-bcount);
List.set (i, book);
Break
}else if (i==list.size ()-1) {//control for loop loops to each of the collections
System.out.println ("No book you want to buy");
}else{
Continue
}
}
}
How to rent a book
public void Rentbook (arraylist<booktwo> list,string bnum,int bcount,int date) {
for (int i=0;i<list.size (); i++) {
Book=list.get (i);
if (Bcount>=book.getbleft ()) {//Compare inventory with the number of books purchased
SYSTEM.OUT.PRINTLN ("The library does not have enough stock! ");
Break
}else if (Book.getbnum (). Equals (Bnum)) {
Switch (date) {
Case 1://The price of a book a week
System.out.println ("The book you want to rent is:" +book.getbname ());
System.out.println ("The price you are going to pay is:" +7*bcount*5);
Break
Case 2://Rent January price required
System.out.println ("The book you want is:" +book.getbname ());
System.out.println ("The price you are going to pay is:" +15*bcount*2);
Break
Case 3://Rent Book half-year price required
System.out.println ("The book you want to Buy is:" +book.getbname ());
System.out.println ("The price you are going to pay is:" +183*bcount*1);
Break
Default
System.out.println ("Please enter the number (1-3)!" ");
Break
}
}else if (i!=list.size ()-1) {//control for loop loops to each of the collections
Continue
}else{
System.out.println ("No this book! ");
Break
}
Break
}
Data Update
for (int i=0;i<list.size (); i++) {
Book=list.get (i);
if (Book.getbnum (). Equals (Bnum)) {
Book=new booktwo (Bnum, Book.getbname (), Book.getbtype (), Book.getbprice (), Book.getbleft ()-bcount);
List.set (i, book);
}else{
Continue
}
}
}
The method of returning books
public int Returnbook (arraylist<booktwo> list,string bnum,int bcount) {
for (int i=0;i<list.size (); i++) {
Book=list.get (i);
if (Book.getbnum (). Equals (Bnum)) {
if (Book.getbleft () <100) {
Update data
Book=new booktwo (Bnum, Book.getbname (), Book.getbtype (), Book.getbprice (), Book.getbleft () +bcount);
List.set (i, book);
System.out.println ("Book a Success!") ");
Return 1;//returns 1 when the book is available
}else{
SYSTEM.OUT.PRINTLN ("You have not borrowed this book please make sure the book number after the return");
return 1;
}
}else if (i!=list.size ()-1) {//control for loop loops to each of the collections
Continue
}else{
System.out.println ("No such book, please confirm the number of the book, re-Login");
Break
}
}
Return 0;//returns 0 without the book
}
}
Test class
public class Booktwotest {
public static void Main (string[] args) {
Signin signin=new Signin ();//Landing Object
Customer Customer=new customer ();
Arraylist<booktwo> list=new arraylist<booktwo> ();//Book Collection
Arraylist<customer> arraylist2=new arraylist<customer> ();//Newly Added customer information collection
Booktwomenthod fiction=new Booktwomenthod ();//Book Method object
Scanner input=new Scanner (system.in);
Signin.setinformation ();//Call the Signin class to write the client information to the collection
Signin.writeinformation ();//Call the method that writes the file inside the Signin class
Fiction.show1 (list);//Call the Booktwomenthod class to write book information to the collection
bo=
Customer Login Interface
SYSTEM.OUT.PRINTLN ("********** Welcome to use the Landing interface ***********");
System.out.println ("Please enter your name:");
String name =input.next ();
System.out.println ("Please enter your password:");
String Password=input.next ();
if (Signin.bianli (name, PassWord) ==1) {//Call Signin traversal method returns 1 if the login succeeds
System.out.println ("Landing success!") ");
for (int j=0;; J + +) {
Fiction.show ()//display the user interface after successful login
String I=input.next ();//Features selected by the customer
switch (i) {
Case "1"://Display Book
Fiction.show2 (list);
System.out.println ("Do you want to buy books (1) or rent books (2) or exit (3)?" ");
String K=input.next ();
if (K.equals ("1")) {//buy Book
System.out.println ("Please enter the number of the book you want to purchase, quantity:");
String Bnum=input.next ();
int Bcount =input.nextint ();
Fiction.buybook (List,bnum, Bcount);
continue;//skip to select Main interface
}else if (K.equals ("2")) {//Rent books
System.out.println ("Please enter the number, quantity, and date of the book you wish to Purchase");
String Bnum=input.next ();
int Bcount =input.nextint ();
SYSTEM.OUT.PRINTLN ("You want to rent: 1. One week; 2. One months; 3. Half year");
try{
int Date=input.nextint ();
Fiction.rentbook (List,bnum, bcount, date);
}catch (Inputmismatchexception e) {
System.out.println ("Please enter a number");
}
continue;//skip to select Main interface
}else if (K.equals ("3")) {//exit to login interface
break;//Jump to landing screen
}else{
System.out.println ("Please enter the required numbers!") ");
continue;//skip to select Main interface
}
Case "2"://book Book
do{
System.out.println ("Please enter the number to return the book, Quantity");
String Bnum=input.next ();
int Bcount=input.nextint ();
if (Fiction.returnbook (List,bnum, Bcount) ==0) {//Call book method
continue;//jump to select the return page
}else{
break;//jump to select the Big interface
}
}while (TRUE);
Continue
Case "3"://Return to login interface
Break
default://Input has errors
System.out.println ("Please enter the number (1-3)!" ");
continue;//Return to landing screen
}
Break
}
}else{
Call the Signin traversal method to register if the return is not 1
SYSTEM.OUT.PRINTLN ("Password or username error! ");
System.out.println ("Do you want to register?") Y or n ");
String S=input.next ();
if (s.equals ("Y")) {
When you need to register
System.out.println ("Please enter your name:");
String name1 =input.next ();//Get Name
System.out.println ("Please enter your password:");
String Password1=input.next ();//Get password
Traverse the client collection to determine the name, whether the password exists in the file
for (int j=0;j<signin.arraylist.size (); j + +) {
Customer=signin.arraylist.get (j);
already exists
if (Customer.getname (). Equals (name) &&customer.getpassword (). Equals (PassWord)) {
System.out.println ("User already exists! ");
break;//Jump to landing screen
does not exist
}else{
Signin.arrayList.add (New Customer (name1, passWord1));//Add a member to a customer collection
Signin.writeinformation ();//Call the Write method to write the new member
SYSTEM.OUT.PRINTLN ("Registered successfully! ");
Signin.readinformation ();//Call Read method read into memory
Signin.bianli (name1, passWord1);//dispensable
break;//Jump to landing screen
}
}
continue;//Return to landing screen
}else{
System.out.println ("User name or password is wrong! Otherwise you will not be able to access the department ");
continue;//Return to landing screen
}
}
}while (TRUE);//loop to login interface
}
}
A bookstore management system Java