超市管理系统
- 登录系统
- 新增图书
- 修改图书
- 查询图书
- 删除图书
- 退出系统
封装的优点
- 良好的封装能够减少耦合。
- 类内部的结构可以自由修改。
- 可以对成员变量进行更精确的控制。
- 隐藏信息,实现细节。
封装的实现
修改属性的可见性来限制对属性的访问(一般限制为private),例如:
- 新建一个图书类
package com.book.test;
public class Book {
private int id; //编号
private double price; //价格
private String name; //书名
//无参构造器
public Book() {
}
//有参构造器
public Book(int id, double price, String name) {
this.id = id;
this.price = price;
this.name = name;
}
//get和set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//toString方法
public String toString() {
return "\t" + id + "\t" + price + "\t" + name;
}
}
工具类
- 将用户输入变成工具类 相当于是一个api 调用比较方便 代码可复用
package com.book.test;
import java.util.Scanner;
//工具类
public class Mytools {
//键盘录入
private static Scanner sc = new Scanner(System.in);
//用户提示 字符串
public static String getString(String scan) {
System.out.print(scan);
return sc.next();
}
//用户提示 整数
public static int getInput(String scan) {
System.out.print(scan);
return sc.nextInt();
}
//用户提示 小数
public static double getDouble(String scan) {
System.out.print(scan);
return sc.nextDouble();
}
}
图书管理系统的实现
登录系统
public class Books {
public static Book[] book = new Book[4]; //定义一个公开的数组
public static int index = 0; //修改图书用到的下标
public static boolean True = true;
public static void main(String[] args) {
book[0] = new Book(1, 20.5, "三国演义");
book[1] = new Book(2, 22.4, "水浒传");
book[2] = new Book(3, 25.5, "红楼梦");
book[3] = new Book(4, 32.5, "西游记");
//登录系统
String username = Mytools.getString("请输入用户名:");
String passwd = Mytools.getString("请输入密码:");
if ("admin".equals(username) && "666".equals(passwd)) {
while (True) {
System.out.println("=====================欢迎来到图书管理系统=====================");
System.out.println("1:新增图书" + "\t" + "2:删除图书" + "\t" + "3:修改图书" + "\t" + "4:查询图书" + "\t" + "5:退出系统");
int input = Mytools.getInput("请输入编号:");
//增
if (input == 1) {
add();
//删
} else if (input == 2) {
remove();
//改
} else if (input == 3) {
change();
//查
} else if (input == 4) {
query();
//退出系统
} else if (input == 5) {
System.out.println("退出成功");
System.exit(0);
} else {
System.out.println("请输入正确的编号");
}
}
} else {
System.out.println("用户名或密码不正确,请重新输入");
System.exit(0);
}
}
新增图书
public static void add(){
book = Arrays.copyOf(book, book.length + 1);
book[4] = new Book();
int id = Mytools.getInput("请输入新增编号:");
for (int i = 0; i < book.length; i++) {
if (book[i].getId() == id) {
System.out.println("ID" + id + "已存在");
System.exit(0);
}
}
book[book.length - 1].setId(id);
double Bookprice = Mytools.getDouble("请输入图书单价:");
String BookName = Mytools.getString("请输入图书名称:");
book[book.length - 1].setPrice(Bookprice);
book[book.length - 1].setName(BookName);
System.out.println("图书新增成功");
}
删除图书
public static void remove(){
int id1 = Mytools.getInput("请输入要删除的商品ID:");
for (int i = 0; i < book.length; i++) {
if (id1 == book[i].getId()) {
book[i] = null;
}
}
System.out.println("删除成功");
}
修改图书
public static void change(){
int change = Mytools.getInput("请输入要修改的商品ID:");
for (int i = 0; i < book.length; i++) {
if (index == book[i].getId()){
index = i;
}
}
book[index].setId(Mytools.getInput("请输入图书编号:"));
book[index].setPrice(Mytools.getDouble("请输入图书单价:"));
book[index].setName(Mytools.getString("请书图书名称:"));
System.out.println("修改成功");
}
查询图书
public static void query(){
System.out.println("图书编号" + "\t" + "图书单价" + "\t" + "图书名称");
for (int i = 0; i < book.length; i++) {
if (book[i] != null) {
System.out.println(book[i]);
}
}
}