十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
由于ArrayList是按照保存的顺序排序的 所以删除就是删除最后一个
创新互联建站专注于张湾企业网站建设,成都响应式网站建设,商城网站建设。张湾网站建设公司,为张湾等地区提供建站服务。全流程按需定制,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
代码如下:
private final static String[] chineseNumber = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
private final static String[] chineseBits = {"十", "百", "千", "万"};
public static addBook(String bookName, int cost, String id, String date) {
books.add(new Book("第" + toChineseNumber(books.length) + "本书", "第" + toChineseNumber(books.length) + "号出版社" ), cost, id, date);
}
public static deleteBook() {
books.remove(books.length - 1);
}
private static toChineseNumber(int source) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i source.length(); i++) {
int n = Integer.valueOf(source.charAt(i));
String chinese = chineseNumber[n];
String bits = chineseBits[source.length() - i - 1];
if(0 == n) {
sb.append(chinese);
} else {
sb.append(chinese + bits);
}
}
}
程序如下:
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LibrarySystem
{
static ListString list = new ArrayListString();
static double money = 0;
static boolean borrow = true;
public static void doBorrow()
{
System.out.print("请输入图书名称计算机书,新书:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.next();
System.out.print("请输入图书类型1:计算机书,2:新书:");
int type = 0;
int day = 0;
try
{
type = scanner.nextInt();
if((bookName.equals("计算机书") type != 1) ||
(bookName.equals("新书") type != 2))
{
System.out.println("类型输入有误,系统退出。");
System.exit(0);
}
} catch (RuntimeException e)
{
System.out.println("类型输入有误,系统退出。");
System.exit(0);
}
System.out.print("请输入借阅天数(必须是整数):");
try
{
day = scanner.nextInt();
} catch (RuntimeException e)
{
System.out.println("天数输入有误,系统退出。");
System.exit(0);
}
double tempMoney = 0;
//计算租金
if(type == 1)
{
tempMoney = day*1.5;
money += tempMoney;
}
else if(type == 2)
{
tempMoney = day 3?((day-3)*2+4.5):day*1.5;
money += tempMoney;
}
list.add(MessageFormat.format("书名:{0},借阅天数:{1},租金:{2}", bookName,day,tempMoney));
System.out.print("是否继续借书(1-是,2-不是):");
try
{
type = scanner.nextInt();
while(type != 1 type != 2)
{
System.out.print("输入有误,请重新输入:");
type = scanner.nextInt();
}
borrow = type == 1? true:false;
} catch (RuntimeException e)
{
System.out.println("天数输入有误,系统退出。");
}
}
public static void main(String[] args)
{
System.out.println("============欢迎使用图书租赁管理系统===========");
while(borrow)
{
doBorrow();
}
System.out.println("你的借书记录:");
System.out.println("-----------------------------------");
for(String s : list)
{
System.out.println(s);
}
System.out.println("-----------------------------------");
System.out.println("总金额:" + money);
}
}
有问题欢迎提问,满意请采纳,谢谢!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookWork {
static ListBook data;
private static Scanner input;
public static void main(String[] args) {
if (!initBook("d:/book.txt")) {
System.out.println("初始图书列表失败 ..");
return;
}
input = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入操作:");
System.out.println("1.找书 2.删除图书 3.退出");
int number = Integer.parseInt(input.next());
if (number == 1) {
findBook();
} else if (number == 2) {
delBook();
} else if (number == 3) {
System.out.println("退出");
break;
} else {
System.out.println("这个不是我要的...重来...");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("这个不是我要的...重来...");
System.out.println();
}
}
}
private static void delBook() {
System.out.println("请输入要删除的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
data.remove(book);
System.out.println(" 图书 " + book.toString() + " 已删除");
return;
}
}
}
System.out.println("没有您要删除的");
}
private static void findBook() {
System.out.println("请输入要查找的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
System.out.println("找到了 图书 " + book.toString());
return;
}
}
}
System.out.println("没有您要找的");
}
private static boolean initBook(String string) {
try {
System.out.println("图书导入中...");
System.out.println("列表文件 -- " + string);
File file = new File(string);
if (!file.exists()) {
return false;
}
data = new ArrayListBook();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
Book b = new Book(strings[0], strings[1]);
data.add(b);
System.out.println("导入" + b.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static class Book {
String number;
String name;
public Book(String number, String name) {
super();
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Book [编码:" + number + ", 名称:" + name + "]";
}
}
}
001,金瓶梅
002,杂事秘辛
003,飞燕外传
004,控鹤监秘记
005,汉宫春色
连接数据库代码:
Connection getConnect(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("创建驱动成功!\n");
} catch (ClassNotFoundException e) {
System.out.println("驱动创建失败!\n");
return null;
}
try {
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/test?
user=rootpassword=rootuseUnicode=truecharacterEncoding=GB2312");
System.out.println("数据库连接成功\n");
} catch (SQLException e) {
System.out.println("数据库连接失败\n");
e.printStackTrace();
return null;
}
return conn;
}