1.设计学生类并测试
请按照以下要求设计一个学生类Student,并进行测试。要求如下: (1)Student类中包含name、grade两个属性。 (2)给每个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值。 (3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为name、grade属性赋值。 (4)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用设置值的方法给name、grade属性赋值,另一个使用有参的构造方法,在构造方法中给name、grade属性赋值。
输入样例: 结尾无空行 输出样例: 在这里给出相应的输出。例如: name=Tom,grade=92.0 name=Jack,grade=86.0 结尾无空行
实现代码
class Main {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("Tom");
s1.setGrade(92.0);
Student s2=new Student("Jack",86.0);
System.out.println("name="+s1.getName()+",grade="+s1.getGrade());
System.out.println("name="+s2.getName()+",grade="+s2.getGrade());
}
}
class Student{
private String name;
private double grade;
public Student() {
}
public Student(String name, double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
2. 设计学生类及子类并测试
设计一个学生类Student和它的一个子类Undergraduate,要求如下: (1)Student类有name和age属性,一个包含两个参数的构造方法,用于给name和age属性赋值,一个show()方法输出Student的属性信息。 (2)本科生类Undergraduate增加一个degree(学位)属性。有一个包含三个参数的构造方法,前两个参数用于给继承的name和age属性赋值,第三个参数给degree赋值,一个show()方法用于输出Undergraduate的属性信息。 (3)在测试类中分别创建Student对象和Undergraduate对象,调用它们的show()。 输出样例:例如:
name: Tom age: 16 name: Jack age: 20 degree: bechalor (结尾无空行)
实现代码
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void show(){
System.out.println("name: "+getName() +" age: "+getAge());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Undergraduate extends Student{
private String degree;
public Undergraduate(String degree) {
this.degree = degree;
}
public Undergraduate(String name, int age, String degree) {
super(name, age);
this.degree = degree;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
@Override
public void show(){
System.out.println("name: "+getName() +" age: "+getAge()+" degree: "+getDegree());
}
}
public class Text {
public static void main(String[] args) {
Student s=new Student("Tom",16);
Undergraduate un=new Undergraduate("Jack",20,"bechalor");
s.show();
un.show();
}
}
3. 设计Shape接口
设计一个Shape接口和它的两个实现类Square和Circle。要求如下: (1)Shape接口中有一个抽象方法area(),方法接收一个double类型的参数,返回一个double类型的结果。 (2)Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形的面积并返回。 在测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的圆形面积。 输出样例: square area:4.0 circle area:28.274333882308138 结尾无空行
实现代码
public interface Shape {
double area(double l);
}
public class Square implements Shape{
@Override
public double area(double l) {
double s;
s=l*l;
return s;
}
}
public class Circle implements Shape{
@Override
public double area(double l) {
double s;
s=Math.PI*l*l;
return s;
}
}
public class Test {
public static void main(String[] args) {
Shape s1= new Square();
System.out.println("square area:"+s1.area(2));
Shape circle=new Circle();
System.out.println("circle area:"+circle.area(3));
}
}
4.接口练习题
设计一个PCI接口,一个实现类声卡(SoundCard),和一个实现类网卡(NetWorkCard),设计一个主板类(MainBoard),一个装配工(Assembler),要求如下 1.PCI接口有两个抽象方法start()和stop()。 2.两个实现类实现接口 3.主板类接受PCI类型的参数。调用传入对象的方法。 4.装配工创建声卡,网卡,主板对象。
代码如下
public interface PCI {
void start();
void stop();
}
public class SoundCard implements PCI{
@Override
public void start() {
System.out.println("du du du du");
}
@Override
public void stop() {
System.out.println("Sound Stop");
}
}
public class NetWorkCard implements PCI{
@Override
public void start() {
System.out.println("Send");
}
@Override
public void stop() {
System.out.println("NetWork Stop");
}
}
public class MainBoard {
public void usePCICard(PCI pci){
pci.start();
pci.stop();
}
}
public class Assembler {
public static void main(String[] args){
MainBoard mb=new MainBoard();
NetWorkCard nw=new NetWorkCard();
SoundCard sc=new SoundCard();
mb.usePCICard(nw);
mb.usePCICard(sc);
}
}
5.超市购物程序
编写一个超市购物程序,实现超市购物功能。购物时,如果购物者所要购买的商品在超市中有,则提示购物者买到了某商品;如果超市中没有购物者所需的商品,则提示购物者白跑了一趟,在超市中什么都没有买到要求如下 1.定义商品类Product,封装姓名属性proName。 2.定义超市类,声明超市名称marketName和存放商品的仓库proArr,定义一个卖出指定商品的方法sell(),该方法遍历仓库,如果有该商品,返回商品名,否则返回null。 3.定义购物者类Person,声明姓名name属性,编写购物shopping()方法。 4.定义测试类Shopping,创建商品和超市对象,存入货物。创建购物者,进行购物。
在这里插入代码片public class Product {
public Product() {
}
public Product(String proName) {
this.proName = proName;
}
private String proName;
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
}
public class Market {
public Market(String marketName) {
this.marketName = marketName;
}
private String marketName;
private Product[] proArr;
public String getMarketName() {
return marketName;
}
public void setMarketName(String marketName) {
this.marketName = marketName;
}
public Product[] getProArr() {
return proArr;
}
public void setProArr(Product[] proArr) {
this.proArr = proArr;
}
public Product sell(String name){
for (int i = 0; i < proArr.length; i++) {
if(proArr[i].getProName()==name){
return proArr[i];
}
}
return null;
}
}
public class Person {
public Person() {
}
public Person(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Product shopping(Market market,String name){
return market.sell(name);
}
}
6.银行新用户现金业务办理
编写一个银行新用户现金业务办理程序,使其模拟新用户到银行办理现金存取业务时的场景。要求此场景中,要模拟出银行对用户到来的欢迎动作、对用户离开的提醒动作,以及用户的开户、存款和取款动作,在完成开户、存款和取款操作后,要提示用户的账户余额。具体要求如下 1.定义一个银行类Bank,①定义银行的名称bankName、用户的名称 name、密码password、账户余额balance和交易金额tuenover。②定义欢迎方法welcome()和欢迎下次再来welcomeNext()方法。③用构造方法进行开户操作,开户时需扣除10元卡费④定义存款deposit()和取款withdrawal()方法,取款时要验证密码和取款金额是否大于账户余额。 2.编写交易类Trade,在此类中模拟新用户去银行办理现金业务的场景。
public class Bank {
public static String bankName;
private String name ;
private String password;
private double balance;
private double tuenover;
public Bank(String name, String password, double tuenover) {
this.name = name;
this.password = password;
this.balance = tuenover-10;
this.tuenover = tuenover;
}
static void welcome(){
System.out.println("欢迎来到"+bankName);
}
public void deposit(double tuenover){
balance+=tuenover;
System.out.println("您此次存款"+balance+"元,您的余额为"+tuenover+"元。");
}
public void withdrawal(String password,double tuenover){
if(this.password.equals(password)){
if(balance>=tuenover){
balance-=tuenover;
System.out.println("您此次取款"+balance+"元,您的余额为"+tuenover+"元。");
}else {
System.out.println("对不起,账户余额不足");
}
}else{
System.out.println("对不起,密码错误");
}
}
static void welcomeNext(){
System.out.println("欢迎再来"+bankName);
}
}
public class Trade {
public static void main(String[] args) {
Bank.bankName="崽崽银行";
Bank.welcome();
Bank bank=new Bank("小崽","123456",300);
bank.withdrawal("12345",500);
bank.withdrawal("123456",500);
bank.deposit(500);
bank.withdrawal("123456",500);
Bank.welcomeNext();
}
}
7.USB接口程序设计
此任务中涉及到的对象有USB接口、鼠标、键盘、麦克风以及计算机。要实现此程序,就需要对这些对象进行相应的编写。要求如下 1.设计一个USB接口,定义启动turnOn()和停止turnOff()的方法。 2.写接口的实现类鼠标Mouse、键盘KeyBoard和麦克风Mic,在实现类中要实现这些设备的启动和关闭方法。 3.由于这些设备是在计算机中使用的,所以接下来编写一个计算机类。类中编写一个USB插槽usbArr和安装USB设备add()的方法。同时定义开机powerOn()和关机powerOff()的方法。 4.编写测试类,实例化计算机对象,并向计算机对象中添加三个USB设备,运行查看结果。
public interface USB {
void turnOn();
void turnOff();
}
public class Mouse implements USB{
@Override
public void turnOn() {
System.out.println("鼠标启动了");
}
@Override
public void turnOff() {
System.out.println("鼠标关闭了");
}
}
public class KeyBoard implements USB{
@Override
public void turnOn() {
System.out.println("键盘开启了");
}
@Override
public void turnOff() {
System.out.println("键盘关闭了");
}
}
public class Mic implements USB{
@Override
public void turnOn() {
System.out.println("麦克风开启了");
}
@Override
public void turnOff() {
System.out.println("麦克风关闭了");
}
}
public class Computer {
private USB[]usbArr=new USB[4];
public void add(USB usb){
for (int i = 0; i < usbArr.length; i++) {
if(usbArr[i]==null){
usbArr[i]=usb;
break;
}
}
}
public void powerOn(){
System.out.println("电脑开机la");
for (int i = 0; i < usbArr.length; i++) {
if(usbArr[i]!=null)
usbArr[i].turnOn();
}
}
public void powerOff(){
for (int i = 0; i < usbArr.length; i++) {
if(usbArr[i]!=null)
usbArr[i].turnOff();
}
System.out.println("电脑关机la");
}
}
public class Text {
public static void main(String[] args) {
Computer c=new Computer();
Mouse mouse=new Mouse();
KeyBoard keyBoard=new KeyBoard();
Mic mic=new Mic();
c.add(mouse);
c.add(keyBoard);
c.add(mic);
c.powerOn();
c.powerOff();
}
}
8.模拟KTV点歌系统
分别使用Linkeduist和AravList集合,实现编写一个模拟KTV点歌系统的程序。在程序中,指令0代表添加歌曲,指令1代表将所选歌曲置顶,指令2代表将所选歌曲提前一位,指令3名退出该系统。要求根据用户输入的指令和歌曲名展现歌曲列表。
public class Text {
public static void main(String[] args) {
System.out.println("欢迎来到点歌系统");
System.out.println("0:添加歌曲");
System.out.println("1:将所选歌曲置顶");
System.out.println("2:将所选歌曲提前一位");
System.out.println("3:退出该系统");
LinkedList linkedList=new LinkedList();
addMusicList(linkedList);
while (true){
System.out.println("输入你想执行的序号");
int command=new Scanner(System.in).nextInt();
switch(command){
case 0:
addMusic(linkedList);
break;
case 1:
setTop(linkedList);
break;
case 2:
setbefore(linkedList);
break;
case 3:
exit();
break;
}
}
}
private static void addMusicList(LinkedList linkedList){
linkedList.add("夜的第七章");
linkedList.add("稻香");
linkedList.add("霍元甲");
linkedList.add("青花瓷");
linkedList.add("菊花台");
System.out.println("原始歌曲列表为"+linkedList);
}
private static void addMusic(LinkedList linkedList){
System.out.println("请您输入要添加的曲目");
String song=new Scanner(System.in).next();
linkedList.add(song);
System.out.println(song+"添加完成");
}
private static void setTop(LinkedList linkedList){
System.out.println("请您输入要置顶的曲目");
String song=new Scanner(System.in).next();
int index= linkedList.indexOf(song);
if(index<0){
System.out.println("该曲目不存在");
}else{
linkedList.remove(index);
linkedList.addFirst(song);
}
}
private static void setbefore(LinkedList linkedList){
System.out.println("请您输入要前置的曲目");
String song=new Scanner(System.in).next();
int index= linkedList.indexOf(song);
if(index<0){
System.out.println("该曲目不存在");
} else if(index==0){
System.out.println("该歌曲已经在第一个");
}else{
linkedList.remove(index);
linkedList.add(index-1,song);
}
}
private static void exit(){
System.out.println("即将退出系统");
System.exit(0);
}
}
public class KTVByArrayList {
public static void main(String[] args) {
System.out.println("欢迎来到点歌系统");
System.out.println("0:添加歌曲");
System.out.println("1:将所选歌曲置顶");
System.out.println("2:将所选歌曲提前一位");
System.out.println("3:退出该系统");
ArrayList arrayList=new ArrayList();
addMusicList(arrayList);
while(true){
System.out.println("输入你想执行的序号");
int command=new Scanner(System.in).nextInt();
switch(command){
case 0:
addMusic(arrayList);
break;
case 1:
setTop(arrayList);
break;
case 2:
setbefore(arrayList);
break;
case 3:
exit();
break;
}
}
}
private static void addMusicList(ArrayList arrayList){
arrayList.add("夜的第七章");
arrayList.add("稻香");
arrayList.add("霍元甲");
arrayList.add("青花瓷");
arrayList.add("菊花台");
System.out.println("原始歌曲列表为"+arrayList);
}
private static void addMusic(ArrayList arrayList){
System.out.println("请您输入要添加的曲目");
String song=new Scanner(System.in).next();
arrayList.add(song);
System.out.println(song+"添加完成");
}
private static void setTop(ArrayList arrayList){
System.out.println("请您输入要置顶的曲目");
String song=new Scanner(System.in).next();
int index= arrayList.indexOf(song);
if(index<0){
System.out.println("该曲目不存在");
}else{
arrayList.remove(index);
arrayList.add(0,song);
}
}
private static void setbefore(ArrayList arrayList){
System.out.println("请您输入要前置的曲目");
String song=new Scanner(System.in).next();
int index= arrayList.indexOf(song);
if(index<0){
System.out.println("该曲目不存在");
} else if(index==0){
System.out.println("该歌曲已经在第一个");
}else{
arrayList.remove(index);
arrayList.add(index-1,song);
}
}
private static void exit(){
System.out.println("即将退出系统");
System.exit(0);
}
}
9.模拟新用户注册
编写一个模拟新浪微博用户注册的程序,要求使用HashSet 集合实现。假设当用户输入用户名、密码、确认密码、生日(输入格式为yyy-mm-dd为正确)手机号码(手机号长度为11位,并且以13、15、17或18为开头的手机号位为正确人邮箱(包含符号“@”为正确)信息之后,判断信息输入是否正确,正确验证用户是否重复注册,如果不是重复注册则注册成功。要求如下 1.创建一个用户类包含用户名userName,密码passwor,手机号telNumber,邮箱email,生日birthday,在类中重写其中的HashCodel()equls0方法。 2.创建一个用户注册类来模拟注册信息,该类中可以用HashSet集合来创建一个数据列表, 然后向列表中添加两条初始用户信息。 3.创建一个校验信息类,在类中实现校验用户输入信息的方法。 注:校验信息类中的正则表达式为本人所写,非原题标准答案,如有错误,望指正。
public class User {
private String userName;
private String password ;
private String telNumber;
private String email;
private Date birthday;
public User(String userName, String password, String telNumber, String email, Date birthday) {
this.userName = userName;
this.password = password;
this.telNumber = telNumber;
this.email = email;
this.birthday = birthday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return userName.equals(user.userName);
}
@Override
public int hashCode() {
return Objects.hash(userName);
}
}
public class CheckInfo {
public static HashSet<User> USER_DATA =new HashSet<>();
public CheckInfo(HashSet<User> USER_DATA) {
this.USER_DATA=USER_DATA;
}
public String checkAction(String userName, String password,String repassword , String telNumber,String email,String birthday){
StringBuffer result=new StringBuffer();
int state=1;
if (!password.equals(repassword)){
result.append("两次密码不一致,");
state=2;
}
if(birthday.matches("\\d{4}-\\d{2}-\\d{2}")){
}else{
result.append("生日格式不正确,");
state=2;
}
if(telNumber.matches("1[3,5,7,8]\\d{9}")){
}else{
result.append("手机格式不正确,");
state=2;
}
if(email.matches("\\w{1,30}@[a-zA-Z0-9]{1,30}\\.([a-zA-Z0-9]{1,30}){1,2}")){
}else{
result.append("邮箱格式不正确,");
state=2;
}
if(state==1){
DateFormat foemat=new SimpleDateFormat("yyyy-mm-dd");
Date datebirthday=null;
try {
datebirthday=foemat.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
User newUser=new User(userName,repassword , telNumber,email,datebirthday);
if(!USER_DATA.add(newUser)){
result.append("用户重复");
state=2;
}
if(state==1){
result.append("注册成功!");
}
}
return result.toString();
}
}
public class UserRegister {
public static HashSet<User> USER_DATA =new HashSet<>();
public static void main(String[] args) {
initDate();
Scanner sc =new Scanner(System.in);
System.out.println("请输入用户名");
String userName=sc.nextLine();
System.out.println("请输入密码");
String password=sc.nextLine();
System.out.println("请再次输入密码");
String repassword=sc.nextLine();
System.out.println("请输入电话号码");
String telNumber=sc.nextLine();
System.out.println("请输入电子邮件");
String email=sc.nextLine();
System.out.println("请输入生日");
String birthday=sc.nextLine();
CheckInfo checkInfo=new CheckInfo(USER_DATA);
String result=checkInfo.checkAction(userName,password,repassword,telNumber,email,birthday);
System.out.println("注册结果"+result);
}
public static void initDate(){
User user=new User("林黛玉","ldy,123","18123457697","lindaiyu@itcase",new Date());
User user2=new User("薛宝钗","xbc,123","18123856797","xuebaochai@itcase",new Date());
USER_DATA.add(user);
USER_DATA.add(user2);
}
}
10.模拟斗地主游戏
编写一个斗地主洗牌发牌的程序,要求按照斗地主的规则完成洗牌发牌的过程。总共有54张牌,牌面由花色和数字(包括J、Q、K、 A字母)组成,花色有?、?、?和?这4种,小🃏表示小王,大🃏表示大王。将这54张牌打乱顺序,共有3位玩家参与游戏,每人轮流一次摸一-张牌,剩余3张留为底牌。程序结束,打印每人手中的纸牌和底牌。
public class Text {
public static void main(String[] args) {
ArrayList<String> color = new ArrayList<>();
color.add("?");
color.add("?");
color.add("?");
color.add("?");
ArrayList<String> number = new ArrayList<>();
for (int i = 2; i <= 10; i++) {
number.add(i + "");
}
number.add("J");
number.add("Q");
number.add("K");
number.add("A");
HashMap<Integer, String> map = new HashMap<>();
int index = 0;
for (String thiscolor : color) {
for (String thisnumber : number) {
map.put(index++, thiscolor + thisnumber);
}
}
map.put(index++, "小🃏");
map.put(index++, "大🃏");
ArrayList<Integer> cards = new ArrayList<>();
for (int i = 0; i < 54; i++) {
cards.add(i);
}
Collections.shuffle(cards);
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
ArrayList<Integer> iSceretCards = new ArrayList<>();
for (int i = 0; i < cards.size(); i++) {
if (i >= 51) {
iSceretCards.add(i);
} else if (i % 3 == 0) {
player1.add(i);
} else if (i % 3 == 1) {
player2.add(i);
} else {
player3.add(i);
}
}
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
ArrayList<String> sPlayer1 = new ArrayList<>();
ArrayList<String> sPlayer2 = new ArrayList<>();
ArrayList<String> sPlayer3 = new ArrayList<>();
ArrayList<String> sSeretCards = new ArrayList<>();
for (Integer key : player1) {
sPlayer1.add(map.get(key));
}
for (Integer key : player2) {
sPlayer2.add(map.get(key));
}
for (Integer key : player3) {
sPlayer3.add(map.get(key));
}
for (Integer key : iSceretCards) {
sSeretCards.add(map.get(key));
}
System.out.println("玩家1"+sPlayer1);
System.out.println("玩家2"+sPlayer2);
System.out.println("玩家3"+sPlayer3);
System.out.println("底牌"+sSeretCards);
}
}
11.保存书店每日交易记录程序设计
编写一个保存书店每日交易记录的程序,使用字节流将书店的交易信息记录在本地的CSV文件中。当用户输入图书编号时,后台会根据图书编号查询到相应图书信息,并返回打印出来。用户紧接着输入购买数量,系统会判断库存是否充足,如果充足则将信息保存至本地的CSV文件中,其中,每条信息包含了“图书编号”“图书名称”“购买数量”“单价”“总价”“出版社”等数据,每个数据之间用英文逗号或空格分割,每条数据之间由换行符分割。保存的时候需要判断本地是否存在当天的数据,如果存在则追加,不存在则新建。(本题有一定的难度,慎重书写)
public class Books {
int id;
String name;
double price;
int number;
double money;
String Publish;
public Books(int id,String name, double peice, int number, double money, String publish) {
this.id=id;
this.name = name;
this.price = peice;
this.number = number;
this.money = money;
Publish = publish;
}
@Override
public String toString() {
return "Books{" +
"图书编号:" + id + '\'' +
",图书名称:" + name + '\'' +
",图书价格 :" + price +
", 图书数量:" + number +
", 单价" + money +
", 库存数量" + Publish + '\'' +
'}';
}
public void setNumber(int number) {
this.number = number;
}
}
public class RecordBooksOrder {
static ArrayList<Books> bookslist=new ArrayList<>();
public static void main(String[] args) {
init();
for (Books books : bookslist) {
System.out.println(books);
}
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("请输入图书编号");
int bookId=sc.nextInt();
Books stockBooks=getbooksById(bookId);
if(stockBooks!=null){
System.out.println("当前图书信息"+stockBooks);
System.out.println("请输入购买数量");
int bookNumber=sc.nextInt();
if(bookNumber<=stockBooks.number){
Books books=new Books(stockBooks.id,stockBooks.name,stockBooks.price, (stockBooks.number-bookNumber),stockBooks.money,stockBooks.Publish);
FileUtil.saveBooks(books);
}else{
System.out.println("库存不足");
}
}else{
System.out.println("图书编号输入错误");
}
}
}
public static void init(){
Books goods1=new Books(101,"Java基础入门",44.50,100,4450.00,"清华大学出版社");
Books goods2=new Books(102 ,"Java编程思想",108.00,50,5400.00,"机械工业出版社");
Books goods3=new Books(102,"疯狂Java讲义",99.00,100,9900.00,"电子工业出版社");
bookslist.add(goods1);
bookslist.add(goods2);
bookslist.add(goods3);
}
public static Books getbooksById(int bookId){
for (int i = 0; i < bookslist.size(); i++) {
Books thisBooks=bookslist.get(i);
if(bookId==thisBooks.id)
return thisBooks;
}
return null;
}
}
public class FileUtil {
public static final String SEPARATE_FIELD=",";
public static final String SEPARATE_LINE="\r\n";
public static void saveBooks(Books books){
Date date=new Date();
DateFormat format =new SimpleDateFormat("yyyyMMdd");
String name="销售记录"+format.format(date)+".csv";
InputStream in=null;
try {
in=new FileInputStream(name);
if(in!=null){
in.close();
createFile(name,true,books);
}
} catch (FileNotFoundException e) {
createFile(name,false,books);
}catch (IOException e) {
e.printStackTrace();
}
}
private static void createFile(String name, boolean label, Books books) {
BufferedOutputStream out=null;
StringBuffer sbf=new StringBuffer();
try {
if(label) {
out = new BufferedOutputStream(new FileOutputStream(name, true));
}else{
out = new BufferedOutputStream(new FileOutputStream(name));
String[] fieldSort=new String[]{"图书编号","图书名称","购买数量","单价","总价","出版社"};
for(String filedKey:fieldSort){
sbf.append(filedKey).append(SEPARATE_FIELD);
}
}
sbf.append(SEPARATE_FIELD);
sbf.append(books.id).append(SEPARATE_FIELD);
sbf.append(books.name).append(SEPARATE_FIELD);
sbf.append(books.number).append(SEPARATE_FIELD);
sbf.append((double)books.price).append(SEPARATE_FIELD);
sbf.append((double)books.money).append(SEPARATE_FIELD);
sbf.append(books.Publish).append(SEPARATE_FIELD);
String str=sbf.toString();
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|